2 * Copyright (C) 1997 Massachusetts Institute of Technology
4 * This software is being provided by the copyright holders under the
5 * following license. By obtaining, using and/or copying this software,
6 * you agree that you have read, understood, and will comply with the
7 * following terms and conditions:
9 * Permission to use, copy, modify, distribute, and sell this software
10 * and its documentation for any purpose and without fee or royalty is
11 * hereby granted, provided that the full text of this NOTICE appears on
12 * ALL copies of the software and documentation or portions thereof,
13 * including modifications, that you make.
15 * THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
16 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
17 * BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
18 * WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
19 * THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
20 * THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
21 * HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
24 * The name and trademarks of copyright holders may NOT be used in
25 * advertising or publicity pertaining to the software without specific,
26 * written prior permission. Title to copyright in this software and any
27 * associated documentation will at all times remain with copyright
28 * holders. See the file AUTHORS which should have accompanied this software
29 * for a list of all copyright holders.
31 * This file may be derived from previously copyrighted software. This
32 * copyright applies only to those changes made by the copyright
33 * holders listed in the AUTHORS file. The rest of this file is covered by
34 * the copyright notices, if any, listed below.
48 #define MASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT))
50 /* Page offset (bits 0:11). */
51 #define PGSHIFT 0 /* First offset bit. */
52 #define PGBITS 12 /* Number of offset bits. */
53 #define PGMASK MASK(PGSHIFT, PGBITS)
54 #define PGSIZE (1 << PGBITS)
56 /* Page table (bits 12:21). */
57 #define PTSHIFT PGBITS /* First page table bit. */
58 #define PTBITS 10 /* Number of page table bits. */
59 #define PTMASK MASK(PTSHIFT, PTBITS)
61 /* Page directory (bits 22:31). */
62 #define PDSHIFT (PTSHIFT + PTBITS) /* First page dir bit. */
63 #define PDBITS 10 /* Number of page dir bits. */
64 #define PDMASK MASK(PDSHIFT, PDBITS)
67 /* Offset within a page. */
68 static inline unsigned pg_ofs (void *va) { return (uintptr_t) va & PGMASK; }
71 static inline uintptr_t pg_no (void *va) { return (uintptr_t) va >> PTSHIFT; }
73 /* Page table number. */
74 static inline unsigned pt_no (void *va) {
75 return ((uintptr_t) va & PTMASK) >> PTSHIFT;
78 /* Page directory number. */
79 static inline uintptr_t pd_no (void *va) { return (uintptr_t) va >> PDSHIFT; }
81 /* Round up to nearest page boundary. */
82 static inline void *pg_round_up (void *va) {
83 return (void *) (((uintptr_t) va + PGSIZE - 1) & ~PGMASK);
86 /* Round down to nearest page boundary. */
87 static inline void *pg_round_down (void *va) {
88 return (void *) ((uintptr_t) va & ~PGMASK);
91 #define PHYS_BASE ((void *) LOADER_PHYS_BASE)
93 /* Returns kernel virtual address at which physical address PADDR
96 ptov (uintptr_t paddr)
98 ASSERT ((void *) paddr < PHYS_BASE);
100 return (void *) (paddr + PHYS_BASE);
103 /* Returns physical address at which kernel virtual address VADDR
105 static inline uintptr_t
108 ASSERT (vaddr >= PHYS_BASE);
110 return (uintptr_t) vaddr - (uintptr_t) PHYS_BASE;
114 /* Page Table/Directory Entry flags
115 * these are defined by the hardware
117 #define PG_P 0x1 /* Present */
118 #define PG_W 0x2 /* Writeable */
119 #define PG_U 0x4 /* User */
120 #define PG_A 0x20 /* Accessed */
121 #define PG_D 0x40 /* Dirty */
123 * The PG_USER bits are not used by the kernel and they are
124 * not interpreted by the hardware. The kernel allows
125 * user processes to set them arbitrarily.
128 /* EFLAGS Register. */
129 #define FLAG_MBS 0x00000002 /* Must be set. */
130 #define FLAG_IF 0x00000200 /* Interrupt Flag. */
132 /* Page fault error codes */
133 #define FEC_PR 0x1 /* Page fault caused by protection violation */
134 #define FEC_WR 0x2 /* Page fault caused by a write */
135 #define FEC_U 0x4 /* Page fault occured while in user mode */
138 /* Application segment type bits */
139 #define STA_X 0x8 /* Executable segment */
140 #define STA_A 0x1 /* Accessed */
142 #define STA_C 0x4 /* Conforming code segment (executable only) */
143 #define STA_R 0x2 /* Readable (executable segments) */
145 #define STA_E 0x4 /* Expand down (non-executable segments) */
146 #define STA_W 0x2 /* Writeable (non-executable segments) */
149 /* Segment selectors. */
150 #define SEL_NULL 0x00 /* Null selector. */
151 #define SEL_KCSEG 0x08 /* Kernel code selector. */
152 #define SEL_KDSEG 0x10 /* Kernel data selector. */
153 #define SEL_UCSEG 0x1B /* User code selector. */
154 #define SEL_UDSEG 0x23 /* User data selector. */
155 #define SEL_TSS 0x28 /* Task-state segment. */
156 #define SEL_CNT 6 /* Number of segments. */
158 #ifndef __ASSEMBLER__
161 uint16_t back_link, :16;
171 uint32_t eax, ecx, edx, ebx;
172 uint32_t esp, ebp, esi, edi;
180 uint16_t trace, bitmap;
185 SYS_SYSTEM = 0, /* System segment. */
186 SYS_CODE_DATA = 1 /* Code or data segment. */
191 GRAN_BYTE = 0, /* Limit has 1-byte granularity. */
192 GRAN_PAGE = 1 /* Limit has 4 kB granularity. */
197 /* System segment types. */
198 TYPE_TSS_16_A = 1, /* 16-bit TSS (available). */
199 TYPE_LDT = 2, /* LDT. */
200 TYPE_TSS_16_B = 3, /* 16-bit TSS (busy). */
201 TYPE_CALL_16 = 4, /* 16-bit call gate. */
202 TYPE_TASK = 5, /* Task gate. */
203 TYPE_INT_16 = 6, /* 16-bit interrupt gate. */
204 TYPE_TRAP_16 = 7, /* 16-bit trap gate. */
205 TYPE_TSS_32_A = 9, /* 32-bit TSS (available). */
206 TYPE_TSS_32_B = 11, /* 32-bit TSS (busy). */
207 TYPE_CALL_32 = 12, /* 32-bit call gate. */
208 TYPE_INT_32 = 14, /* 32-bit interrupt gate. */
209 TYPE_TRAP_32 = 15, /* 32-bit trap gate. */
211 /* Code/data segment types. */
212 TYPE_CODE = 8, /* 1=Code segment, 0=data segment. */
213 TYPE_ACCESSED = 1, /* Set if accessed. */
215 /* Data segment types. */
216 TYPE_EXPAND_DOWN = 4, /* 1=Expands up, 0=expands down. */
217 TYPE_WRITABLE = 2, /* 1=Read/write, 0=read-only. */
219 /* Code segment types. */
220 TYPE_CONFORMING = 4, /* 1=Conforming, 0=nonconforming. */
221 TYPE_READABLE = 2 /* 1=Exec/read, 0=exec-only. */
224 static inline uint64_t
225 make_dtr_operand (uint16_t limit, void *base)
227 return limit | ((uint64_t) (uint32_t) base << 16);
231 #endif /* !_MMU_H_ */