xref: /plan9/sys/src/9/pc/mem.h (revision 12009bff671a91993ae58f16dab833e809f4a6f3)
1 /*
2  * Memory and machine-specific definitions.  Used in C and assembler.
3  */
4 
5 /*
6  * Sizes
7  */
8 #define	BI2BY		8			/* bits per byte */
9 #define	BI2WD		32			/* bits per word */
10 #define	BY2WD		4			/* bytes per word */
11 #define	BY2V		8			/* bytes per double word */
12 #define	BY2PG		4096			/* bytes per page */
13 #define	WD2PG		(BY2PG/BY2WD)		/* words per page */
14 #define	BY2XPG		(4096*1024)		/* bytes per big page */
15 #define	PGSHIFT		12			/* log(BY2PG) */
16 #define CACHELINESZ	32			/* pentium & later */
17 #define	BLOCKALIGN	8
18 #define FPalign		16			/* required for FXSAVE */
19 
20 /*
21  * In 32-bit mode, the MAXMACH limit is 32 without
22  * changing the way active.machs is defined and used
23  * (unfortunately, it is also used in the port code).
24  */
25 #define	MAXMACH		32			/* max # cpus system can run */
26 #define	KSTACK		4096			/* Size of kernel stack */
27 
28 /*
29  * Time
30  */
31 #define	HZ		(100)			/* clock frequency */
32 #define	MS2HZ		(1000/HZ)		/* millisec per clock tick */
33 #define	TK2SEC(t)	((t)/HZ)		/* ticks to seconds */
34 
35 /*
36  *  Address spaces
37  */
38 #define	KZERO		0xF0000000		/* base of kernel address space */
39 #define	KTZERO		(KZERO+0x100000)	/* first address in kernel text - 9load sits below */
40 #define	VPT		(KZERO-VPTSIZE)
41 #define	VPTSIZE		BY2XPG
42 #define	NVPT		(VPTSIZE/BY2WD)
43 #define	KMAP		(VPT-KMAPSIZE)
44 #define	KMAPSIZE	BY2XPG
45 #define	VMAP		(KMAP-VMAPSIZE)
46 #define	VMAPSIZE	(0x10000000-VPTSIZE-KMAPSIZE)
47 #define	UZERO		0			/* base of user address space */
48 #define	UTZERO		(UZERO+BY2PG)		/* first address in user text */
49 #define UTROUND(t)	ROUNDUP((t), BY2PG)
50 #define	USTKTOP		(VMAP-BY2PG)		/* byte just beyond user stack */
51 #define	USTKSIZE	(16*1024*1024)		/* size of user stack */
52 #define	TSTKTOP		(USTKTOP-USTKSIZE)	/* end of new stack in sysexec */
53 #define	TSTKSIZ 	100			/* pages in new stack; limits exec args */
54 
55 /*
56  * Fundamental addresses - bottom 64kB saved for return to real mode
57  */
58 #define	CONFADDR	(KZERO+0x1200)		/* info passed from boot loader */
59 #define	TMPADDR		(KZERO+0x2000)		/* used for temporary mappings */
60 #define	APBOOTSTRAP	(KZERO+0x3000)		/* AP bootstrap code */
61 #define	RMUADDR		(KZERO+0x7C00)		/* real mode Ureg */
62 #define	RMCODE		(KZERO+0x8000)		/* copy of first page of KTEXT */
63 #define	RMBUF		(KZERO+0x9000)		/* buffer for user space - known to vga */
64 #define	IDTADDR		(KZERO+0x10800)		/* idt */
65 #define	REBOOTADDR	(0x11000)		/* reboot code - physical address */
66 #define	CPU0PDB		(KZERO+0x12000)		/* bootstrap processor PDB */
67 #define	CPU0PTE		(KZERO+0x13000)		/* bootstrap processor PTE's for 0-4MB */
68 #define	CPU0GDT		(KZERO+0x14000)		/* bootstrap processor GDT */
69 #define	MACHADDR	(KZERO+0x15000)		/* as seen by current processor */
70 #define	CPU0MACH	(KZERO+0x16000)		/* Mach for bootstrap processor */
71 #define	MACHSIZE	BY2PG
72 #define CPU0PTE1	(KZERO+0x17000)		/* bootstrap processor PTE's for 4MB-8MB */
73 #define CPU0END		(CPU0PTE1+BY2PG)
74 /*
75  * N.B.  ramscan knows that CPU0END is the end of reserved data
76  * N.B.  _startPADDR knows that CPU0PDB is the first reserved page
77  * and that there are 6 of them.
78  */
79 
80 /*
81  *  known x86 segments (in GDT) and their selectors
82  */
83 #define	NULLSEG	0	/* null segment */
84 #define	KDSEG	1	/* kernel data/stack */
85 #define	KESEG	2	/* kernel executable */
86 #define	UDSEG	3	/* user data/stack */
87 #define	UESEG	4	/* user executable */
88 #define	TSSSEG	5	/* task segment */
89 #define	APMCSEG		6	/* APM code segment */
90 #define	APMCSEG16	7	/* APM 16-bit code segment */
91 #define	APMDSEG		8	/* APM data segment */
92 #define	KESEG16		9	/* kernel executable 16-bit */
93 #define	NGDT		10	/* number of GDT entries required */
94 /* #define	APM40SEG	8	/* APM segment 0x40 */
95 
96 #define	SELGDT	(0<<2)	/* selector is in gdt */
97 #define	SELLDT	(1<<2)	/* selector is in ldt */
98 
99 #define	SELECTOR(i, t, p)	(((i)<<3) | (t) | (p))
100 
101 #define	NULLSEL	SELECTOR(NULLSEG, SELGDT, 0)
102 #define	KDSEL	SELECTOR(KDSEG, SELGDT, 0)
103 #define	KESEL	SELECTOR(KESEG, SELGDT, 0)
104 #define	UESEL	SELECTOR(UESEG, SELGDT, 3)
105 #define	UDSEL	SELECTOR(UDSEG, SELGDT, 3)
106 #define	TSSSEL	SELECTOR(TSSSEG, SELGDT, 0)
107 #define	APMCSEL 	SELECTOR(APMCSEG, SELGDT, 0)
108 #define	APMCSEL16	SELECTOR(APMCSEG16, SELGDT, 0)
109 #define	APMDSEL		SELECTOR(APMDSEG, SELGDT, 0)
110 /* #define	APM40SEL	SELECTOR(APM40SEG, SELGDT, 0) */
111 
112 /*
113  *  fields in segment descriptors
114  */
115 #define	SEGDATA	(0x10<<8)	/* data/stack segment */
116 #define	SEGEXEC	(0x18<<8)	/* executable segment */
117 #define	SEGTSS	(0x9<<8)	/* TSS segment */
118 #define	SEGCG	(0x0C<<8)	/* call gate */
119 #define	SEGIG	(0x0E<<8)	/* interrupt gate */
120 #define	SEGTG	(0x0F<<8)	/* trap gate */
121 #define	SEGTYPE	(0x1F<<8)
122 
123 #define	SEGP	(1<<15)		/* segment present */
124 #define	SEGPL(x) ((x)<<13)	/* priority level */
125 #define	SEGB	(1<<22)		/* granularity 1==4k (for expand-down) */
126 #define	SEGG	(1<<23)		/* granularity 1==4k (for other) */
127 #define	SEGE	(1<<10)		/* expand down */
128 #define	SEGW	(1<<9)		/* writable (for data/stack) */
129 #define	SEGR	(1<<9)		/* readable (for code) */
130 #define	SEGD	(1<<22)		/* default 1==32bit (for code) */
131 
132 /*
133  *  virtual MMU
134  */
135 #define	PTEMAPMEM	(1024*1024)
136 #define	PTEPERTAB	(PTEMAPMEM/BY2PG)
137 #define	SEGMAPSIZE	1984
138 #define	SSEGMAPSIZE	16
139 #define	PPN(x)		((x)&~(BY2PG-1))
140 
141 /*
142  *  physical MMU
143  */
144 #define	PTEVALID	(1<<0)
145 #define	PTEWT		(1<<3)
146 #define	PTEUNCACHED	(1<<4)
147 #define	PTEWRITE	(1<<1)
148 #define	PTERONLY	(0<<1)
149 #define	PTEKERNEL	(0<<2)
150 #define	PTEUSER		(1<<2)
151 #define	PTESIZE		(1<<7)
152 #define	PTEGLOBAL	(1<<8)
153 
154 /*
155  * Macros for calculating offsets within the page directory base
156  * and page tables.
157  */
158 #define	PDX(va)		((((ulong)(va))>>22) & 0x03FF)
159 #define	PTX(va)		((((ulong)(va))>>12) & 0x03FF)
160 
161 #define	getpgcolor(a)	0
162 
163