]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc64/mem.h
kernel: make addbroken() static, remove misleading Proc* argument
[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         64
30
31 #define MAXMACH         128                     /* 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 USTKTOP         (0x00007ffffffff000ull)
48 #define USTKSIZE        (16*MiB)                        /* size of user stack */
49
50 /*
51  *  Address spaces. Kernel, sorted by address.
52  */
53 #define KZERO           (0xffffffff80000000ull)
54 #define KTZERO          (KZERO+1*MiB+64*KiB)
55
56 #define VMAP            (0xffffff0000000000ull)
57 #define VMAPSIZE        (512ull*GiB)
58
59 #define KMAP            (0xfffffe8000000000ull)
60 #define KMAPSIZE        (2*MiB)
61
62 /*
63  * Fundamental addresses
64  */
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 */
69
70 #define CPU0PML4        (KZERO+0x13000ull)
71 #define CPU0PDP         (KZERO+0x14000ull)
72 #define CPU0PD0         (KZERO+0x15000ull)              /* KZERO */
73 #define CPU0PD1         (KZERO+0x16000ull)              /* KZERO+1GB */
74
75 #define CPU0GDT         (KZERO+0x17000ull)              /* bootstrap processor GDT */
76
77 #define CPU0MACH        (KZERO+0x18000ull)              /* Mach for bootstrap processor */
78 #define CPU0END         (CPU0MACH+MACHSIZE)
79
80 #define MACHSIZE        (2*KSTACK)
81
82 /*
83  * Where configuration info is left for the loaded programme.
84  * There are 24064 bytes available at CONFADDR.
85  */
86 #define BOOTLINE        ((char*)CONFADDR)
87 #define BOOTLINELEN     64
88 #define BOOTARGS        ((char*)(CONFADDR+BOOTLINELEN))
89 #define BOOTARGSLEN     (0x6000-0x200-BOOTLINELEN)
90
91 /*
92  *  known x86 segments (in GDT) and their selectors
93  */
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) */
101
102 #define NGDT    10      /* number of GDT entries required */
103
104 #define SELGDT  (0<<2)  /* selector is in gdt */
105 #define SELLDT  (1<<2)  /* selector is in ldt */
106
107 #define SELECTOR(i, t, p)       (((i)<<3) | (t) | (p))
108
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)
116
117 /*
118  *  fields in segment descriptors
119  */
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)
128
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) */
138
139 /*
140  *  virtual MMU
141  */
142 #define PTEMAPMEM       (1ull*MiB)      
143 #define PTEPERTAB       (PTEMAPMEM/BY2PG)
144 #define SEGMAPSIZE      65536
145 #define SSEGMAPSIZE     16
146 #define PPN(x)          ((x)&~(1ull<<63 | BY2PG-1))
147
148 /*
149  *  physical MMU
150  */
151 #define PTEVALID        (1ull<<0)
152 #define PTEWT           (1ull<<3)
153 #define PTEUNCACHED     (1ull<<4)
154 #define PTECACHED       (0ull<<4)
155 #define PTEWRITE        (1ull<<1)
156 #define PTERONLY        (0ull<<1)
157 #define PTEKERNEL       (0ull<<2)
158 #define PTEUSER         (1ull<<2)
159 #define PTESIZE         (1ull<<7)
160 #define PTEGLOBAL       (1ull<<8)
161 #define PTENOEXEC       ((uvlong)m->havenx<<63)
162
163 /*
164  * Hierarchical Page Tables.
165  * For example, traditional IA-32 paging structures have 2 levels,
166  * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
167  * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
168  * and level 0 the PT pages. The PTLX macro gives an index into the
169  * page-table page at level 'l' for the virtual address 'v'.
170  */
171 #define PTSZ            (4*KiB)                 /* page table page size */
172 #define PTSHIFT         9                       /*  */
173
174 #define PTLX(v, l)      (((v)>>(((l)*PTSHIFT)+PGSHIFT)) & ((1<<PTSHIFT)-1))
175 #define PGLSZ(l)        (1ull<<(((l)*PTSHIFT)+PGSHIFT))
176
177 #define getpgcolor(a)   0
178
179 /* PAT entry used for write combining */
180 #define PATWC   7
181
182 #define RMACH           R15                     /* m-> */
183 #define RUSER           R14                     /* up-> */