2 * Memory and machine-specific definitions. Used in C and assembler.
4 #define KiB 1024u /* Kibi 0x0000000000000400 */
5 #define MiB 1048576u /* Mebi 0x0000000000100000 */
6 #define GiB 1073741824u /* Gibi 000000000040000000 */
7 #define TiB 1099511627776ull /* Tebi 0x0000010000000000 */
8 #define PiB 1125899906842624ull /* Pebi 0x0004000000000000 */
9 #define EiB 1152921504606846976ull /* Exbi 0x1000000000000000 */
11 #define MIN(a, b) ((a) < (b)? (a): (b))
12 #define MAX(a, b) ((a) > (b)? (a): (b))
14 #define ALIGNED(p, a) (!(((uintptr)(p)) & ((a)-1)))
19 #define BI2BY 8 /* bits per byte */
20 #define BI2WD 32 /* bits per word */
21 #define BY2WD 8 /* bytes per word */
22 #define BY2V 8 /* bytes per double word */
23 #define BY2PG (0x1000ull) /* bytes per page */
24 #define WD2PG (BY2PG/BY2WD) /* words per page */
25 #define PGSHIFT 12 /* log(BY2PG) */
26 #define ROUND(s, sz) (((s)+((sz)-1))&~((sz)-1))
27 #define PGROUND(s) ROUND(s, BY2PG)
31 #define MAXMACH 128 /* max # cpus system can run */
33 #define KSTACK (16*KiB) /* Size of Proc kernel stack */
38 #define HZ (100) /* clock frequency */
39 #define MS2HZ (1000/HZ) /* millisec per clock tick */
40 #define TK2SEC(t) ((t)/HZ) /* ticks to seconds */
43 * Address spaces. User:
45 #define UTZERO (0x0000000000200000ull) /* first address in user text */
46 #define UADDRMASK (0x00007fffffffffffull) /* canonical address mask */
47 #define USTKTOP (0x00007ffffffff000ull)
48 #define USTKSIZE (16*MiB) /* size of user stack */
51 * Address spaces. Kernel, sorted by address.
53 #define KZERO (0xffffffff80000000ull)
54 #define KTZERO (KZERO+1*MiB+64*KiB)
56 #define VMAP (0xffffff0000000000ull)
57 #define VMAPSIZE (512ull*GiB)
59 #define KMAP (0xfffffe8000000000ull)
60 #define KMAPSIZE (2*MiB)
63 * Fundamental addresses - bottom 64kB saved for return to real mode
65 #define CONFADDR (KZERO+0x1200ull) /* info passed from boot loader */
66 #define APBOOTSTRAP (KZERO+0x7000ull) /* AP bootstrap code */
67 #define IDTADDR (KZERO+0x10000ull) /* idt */
68 #define REBOOTADDR (0x11000) /* reboot code - physical address */
70 #define CPU0PML4 (KZERO+0x13000ull)
71 #define CPU0PDP (KZERO+0x14000ull)
72 #define CPU0PD0 (KZERO+0x15000ull) /* KZERO */
73 #define CPU0PD1 (KZERO+0x16000ull) /* KZERO+1GB */
75 #define CPU0GDT (KZERO+0x17000ull) /* bootstrap processor GDT */
77 #define CPU0MACH (KZERO+0x18000ull) /* Mach for bootstrap processor */
78 #define CPU0END (CPU0MACH+MACHSIZE)
80 #define MACHSIZE (2*KSTACK)
83 * Where configuration info is left for the loaded programme.
84 * There are 24064 bytes available at CONFADDR.
86 #define BOOTLINE ((char*)CONFADDR)
87 #define BOOTLINELEN 64
88 #define BOOTARGS ((char*)(CONFADDR+BOOTLINELEN))
89 #define BOOTARGSLEN (0x6000-0x200-BOOTLINELEN)
92 * known x86 segments (in GDT) and their selectors
94 #define NULLSEG 0 /* null segment */
95 #define KESEG 1 /* kernel executable */
96 #define KDSEG 2 /* kernel data */
97 #define UE32SEG 3 /* user executable 32bit */
98 #define UDSEG 4 /* user data/stack */
99 #define UESEG 5 /* user executable 64bit */
100 #define TSSSEG 8 /* task segment (two descriptors) */
102 #define NGDT 10 /* number of GDT entries required */
104 #define SELGDT (0<<2) /* selector is in gdt */
105 #define SELLDT (1<<2) /* selector is in ldt */
107 #define SELECTOR(i, t, p) (((i)<<3) | (t) | (p))
109 #define NULLSEL SELECTOR(NULLSEG, SELGDT, 0)
110 #define KDSEL NULLSEL
111 #define KESEL SELECTOR(KESEG, SELGDT, 0)
112 #define UE32SEL SELECTOR(UE32SEG, SELGDT, 3)
113 #define UDSEL SELECTOR(UDSEG, SELGDT, 3)
114 #define UESEL SELECTOR(UESEG, SELGDT, 3)
115 #define TSSSEL SELECTOR(TSSSEG, SELGDT, 0)
118 * fields in segment descriptors
120 #define SEGDATA (0x10<<8) /* data/stack segment */
121 #define SEGEXEC (0x18<<8) /* executable segment */
122 #define SEGTSS (0x9<<8) /* TSS segment */
123 #define SEGCG (0x0C<<8) /* call gate */
124 #define SEGIG (0x0E<<8) /* interrupt gate */
125 #define SEGTG (0x0F<<8) /* trap gate */
126 #define SEGLDT (0x02<<8) /* local descriptor table */
127 #define SEGTYPE (0x1F<<8)
129 #define SEGP (1<<15) /* segment present */
130 #define SEGPL(x) ((x)<<13) /* priority level */
131 #define SEGB (1<<22) /* granularity 1==4k (for expand-down) */
132 #define SEGD (1<<22) /* default 1==32bit (for code) */
133 #define SEGE (1<<10) /* expand down */
134 #define SEGW (1<<9) /* writable (for data/stack) */
135 #define SEGR (1<<9) /* readable (for code) */
136 #define SEGL (1<<21) /* 64 bit */
137 #define SEGG (1<<23) /* granularity 1==4k (for other) */
142 #define PTEMAPMEM (1ull*MiB)
143 #define PTEPERTAB (PTEMAPMEM/BY2PG)
144 #define SEGMAPSIZE 65536
145 #define SSEGMAPSIZE 16
146 #define PPN(x) ((x)&~(BY2PG-1))
151 #define PTEVALID (1ull<<0)
152 #define PTEWT (1ull<<3)
153 #define PTEUNCACHED (1ull<<4)
154 #define PTEWRITE (1ull<<1)
155 #define PTERONLY (0ull<<1)
156 #define PTEKERNEL (0ull<<2)
157 #define PTEUSER (1ull<<2)
158 #define PTESIZE (1ull<<7)
159 #define PTEGLOBAL (1ull<<8)
162 * Hierarchical Page Tables.
163 * For example, traditional IA-32 paging structures have 2 levels,
164 * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
165 * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
166 * and level 0 the PT pages. The PTLX macro gives an index into the
167 * page-table page at level 'l' for the virtual address 'v'.
169 #define PTSZ (4*KiB) /* page table page size */
170 #define PTSHIFT 9 /* */
172 #define PTLX(v, l) (((v)>>(((l)*PTSHIFT)+PGSHIFT)) & ((1<<PTSHIFT)-1))
173 #define PGLSZ(l) (1ull<<(((l)*PTSHIFT)+PGSHIFT))
175 #define getpgcolor(a) 0
177 #define RMACH R15 /* m-> */
178 #define RUSER R14 /* up-> */