]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc64/mem.h
etherwpi: Intel PRO Wireless 3945abg driver based on openbsd's if_wpi (thanks aap)
[plan9front.git] / sys / src / 9 / pc64 / mem.h
1 /*
2  * Memory and machine-specific definitions.  Used in C and assembler.
3  */
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 */
10
11 #define MIN(a, b)       ((a) < (b)? (a): (b))
12 #define MAX(a, b)       ((a) > (b)? (a): (b))
13
14 #define ALIGNED(p, a)   (!(((uintptr)(p)) & ((a)-1)))
15
16 /*
17  * Sizes
18  */
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)
28 #define BLOCKALIGN      8
29 #define FPalign         16
30
31 #define MAXMACH         32                      /* max # cpus system can run */
32
33 #define KSTACK          (16*KiB)                /* Size of Proc kernel stack */
34
35 /*
36  * Time
37  */
38 #define HZ              (100)                   /* clock frequency */
39 #define MS2HZ           (1000/HZ)               /* millisec per clock tick */
40 #define TK2SEC(t)       ((t)/HZ)                /* ticks to seconds */
41
42 /*
43  *  Address spaces. User:
44  */
45 #define UTZERO          (0x0000000000200000ull)         /* first address in user text */
46 #define UADDRMASK       (0x00007fffffffffffull)         /* canonical address mask */
47 #define TSTKTOP         (0x00007ffffffff000ull)
48 #define USTKSIZE        (16*MiB)                        /* size of user stack */
49 #define USTKTOP         (TSTKTOP-USTKSIZE)              /* end of new stack in sysexec */
50
51 /*
52  *  Address spaces. Kernel, sorted by address.
53  */
54 #define KZERO           (0xffffffff80000000ull)
55 #define KTZERO          (KZERO+1*MiB+64*KiB)
56
57 #define VMAP            (0xffffff0000000000ull)
58 #define VMAPSIZE        (512ull*GiB)
59
60 #define KMAP            (0xfffffe8000000000ull)
61 #define KMAPSIZE        (2*MiB)
62
63 /*
64  * Fundamental addresses - bottom 64kB saved for return to real mode
65  */
66 #define CONFADDR        (KZERO+0x1200ull)               /* info passed from boot loader */
67 #define APBOOTSTRAP     (KZERO+0x3000ull)               /* AP bootstrap code */
68 #define IDTADDR         (KZERO+0x10000ull)              /* idt */
69 #define REBOOTADDR      (0x11000)                       /* reboot code - physical address */
70
71 #define CPU0PML4        (KZERO+0x13000ull)
72 #define CPU0PDP         (KZERO+0x14000ull)
73 #define CPU0PD0         (KZERO+0x15000ull)              /* KZERO */
74 #define CPU0PD1         (KZERO+0x16000ull)              /* KZERO+1GB */
75
76 #define CPU0GDT         (KZERO+0x17000ull)              /* bootstrap processor GDT */
77
78 #define CPU0MACH        (KZERO+0x18000ull)              /* Mach for bootstrap processor */
79 #define CPU0END         (CPU0MACH+MACHSIZE)
80
81 #define MACHSIZE        (2*KSTACK)
82
83 /*
84  *  known x86 segments (in GDT) and their selectors
85  */
86 #define NULLSEG 0       /* null segment */
87 #define KESEG   1       /* kernel executable */
88 #define KDSEG   2       /* kernel data */
89 #define UE32SEG 3       /* user executable 32bit */
90 #define UDSEG   4       /* user data/stack */
91 #define UESEG   5       /* user executable 64bit */
92 #define TSSSEG  8       /* task segment (two descriptors) */
93
94 #define NGDT    10      /* number of GDT entries required */
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 KESEL   SELECTOR(KESEG, SELGDT, 0)
103 #define UE32SEL SELECTOR(UE32SEG, SELGDT, 3)
104 #define UDSEL   SELECTOR(UDSEG, SELGDT, 3)
105 #define UESEL   SELECTOR(UESEG, SELGDT, 3)
106 #define TSSSEL  SELECTOR(TSSSEG, SELGDT, 0)
107
108 /*
109  *  fields in segment descriptors
110  */
111 #define SEGDATA (0x10<<8)       /* data/stack segment */
112 #define SEGEXEC (0x18<<8)       /* executable segment */
113 #define SEGTSS  (0x9<<8)        /* TSS segment */
114 #define SEGCG   (0x0C<<8)       /* call gate */
115 #define SEGIG   (0x0E<<8)       /* interrupt gate */
116 #define SEGTG   (0x0F<<8)       /* trap gate */
117 #define SEGLDT  (0x02<<8)       /* local descriptor table */
118 #define SEGTYPE (0x1F<<8)
119
120 #define SEGP    (1<<15)         /* segment present */
121 #define SEGPL(x) ((x)<<13)      /* priority level */
122 #define SEGB    (1<<22)         /* granularity 1==4k (for expand-down) */
123 #define SEGD    (1<<22)         /* default 1==32bit (for code) */
124 #define SEGE    (1<<10)         /* expand down */
125 #define SEGW    (1<<9)          /* writable (for data/stack) */
126 #define SEGR    (1<<9)          /* readable (for code) */
127 #define SEGL    (1<<21)         /* 64 bit */
128 #define SEGG    (1<<23)         /* granularity 1==4k (for other) */
129
130 /*
131  *  virtual MMU
132  */
133 #define PTEMAPMEM       (1ull*MiB)      
134 #define PTEPERTAB       (PTEMAPMEM/BY2PG)
135 #define SEGMAPSIZE      65536
136 #define SSEGMAPSIZE     16
137 #define PPN(x)          ((x)&~(BY2PG-1))
138
139 /*
140  *  physical MMU
141  */
142 #define PTEVALID        (1ull<<0)
143 #define PTEWT           (1ull<<3)
144 #define PTEUNCACHED     (1ull<<4)
145 #define PTEWRITE        (1ull<<1)
146 #define PTERONLY        (0ull<<1)
147 #define PTEKERNEL       (0ull<<2)
148 #define PTEUSER         (1ull<<2)
149 #define PTESIZE         (1ull<<7)
150 #define PTEGLOBAL       (1ull<<8)
151
152 /*
153  * Hierarchical Page Tables.
154  * For example, traditional IA-32 paging structures have 2 levels,
155  * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
156  * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
157  * and level 0 the PT pages. The PTLX macro gives an index into the
158  * page-table page at level 'l' for the virtual address 'v'.
159  */
160 #define PTSZ            (4*KiB)                 /* page table page size */
161 #define PTSHIFT         9                       /*  */
162
163 #define PTLX(v, l)      (((v)>>(((l)*PTSHIFT)+PGSHIFT)) & ((1<<PTSHIFT)-1))
164 #define PGLSZ(l)        (1ull<<(((l)*PTSHIFT)+PGSHIFT))
165
166 #define getpgcolor(a)   0
167
168 #define RMACH           R15                     /* m-> */
169 #define RUSER           R14                     /* up-> */