]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/pc64/mem.h
pc, pc64: support for multiboot framebuffer, common bootargs and multiboot code
[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         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 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  * Where configuration info is left for the loaded programme.
85  * This will turn into a structure as more is done by the boot loader
86  * (e.g. why parse the .ini file twice?).
87  * There are 3584 bytes available at CONFADDR.
88  */
89 #define BOOTLINE        ((char*)CONFADDR)
90 #define BOOTLINELEN     64
91 #define BOOTARGS        ((char*)(CONFADDR+BOOTLINELEN))
92 #define BOOTARGSLEN     (4096-0x200-BOOTLINELEN)
93
94 /*
95  *  known x86 segments (in GDT) and their selectors
96  */
97 #define NULLSEG 0       /* null segment */
98 #define KESEG   1       /* kernel executable */
99 #define KDSEG   2       /* kernel data */
100 #define UE32SEG 3       /* user executable 32bit */
101 #define UDSEG   4       /* user data/stack */
102 #define UESEG   5       /* user executable 64bit */
103 #define TSSSEG  8       /* task segment (two descriptors) */
104
105 #define NGDT    10      /* number of GDT entries required */
106
107 #define SELGDT  (0<<2)  /* selector is in gdt */
108 #define SELLDT  (1<<2)  /* selector is in ldt */
109
110 #define SELECTOR(i, t, p)       (((i)<<3) | (t) | (p))
111
112 #define NULLSEL SELECTOR(NULLSEG, SELGDT, 0)
113 #define KDSEL   NULLSEL
114 #define KESEL   SELECTOR(KESEG, SELGDT, 0)
115 #define UE32SEL SELECTOR(UE32SEG, SELGDT, 3)
116 #define UDSEL   SELECTOR(UDSEG, SELGDT, 3)
117 #define UESEL   SELECTOR(UESEG, SELGDT, 3)
118 #define TSSSEL  SELECTOR(TSSSEG, SELGDT, 0)
119
120 /*
121  *  fields in segment descriptors
122  */
123 #define SEGDATA (0x10<<8)       /* data/stack segment */
124 #define SEGEXEC (0x18<<8)       /* executable segment */
125 #define SEGTSS  (0x9<<8)        /* TSS segment */
126 #define SEGCG   (0x0C<<8)       /* call gate */
127 #define SEGIG   (0x0E<<8)       /* interrupt gate */
128 #define SEGTG   (0x0F<<8)       /* trap gate */
129 #define SEGLDT  (0x02<<8)       /* local descriptor table */
130 #define SEGTYPE (0x1F<<8)
131
132 #define SEGP    (1<<15)         /* segment present */
133 #define SEGPL(x) ((x)<<13)      /* priority level */
134 #define SEGB    (1<<22)         /* granularity 1==4k (for expand-down) */
135 #define SEGD    (1<<22)         /* default 1==32bit (for code) */
136 #define SEGE    (1<<10)         /* expand down */
137 #define SEGW    (1<<9)          /* writable (for data/stack) */
138 #define SEGR    (1<<9)          /* readable (for code) */
139 #define SEGL    (1<<21)         /* 64 bit */
140 #define SEGG    (1<<23)         /* granularity 1==4k (for other) */
141
142 /*
143  *  virtual MMU
144  */
145 #define PTEMAPMEM       (1ull*MiB)      
146 #define PTEPERTAB       (PTEMAPMEM/BY2PG)
147 #define SEGMAPSIZE      65536
148 #define SSEGMAPSIZE     16
149 #define PPN(x)          ((x)&~(BY2PG-1))
150
151 /*
152  *  physical MMU
153  */
154 #define PTEVALID        (1ull<<0)
155 #define PTEWT           (1ull<<3)
156 #define PTEUNCACHED     (1ull<<4)
157 #define PTEWRITE        (1ull<<1)
158 #define PTERONLY        (0ull<<1)
159 #define PTEKERNEL       (0ull<<2)
160 #define PTEUSER         (1ull<<2)
161 #define PTESIZE         (1ull<<7)
162 #define PTEGLOBAL       (1ull<<8)
163
164 /*
165  * Hierarchical Page Tables.
166  * For example, traditional IA-32 paging structures have 2 levels,
167  * level 1 is the PD, and level 0 the PT pages; with IA-32e paging,
168  * level 3 is the PML4(!), level 2 the PDP, level 1 the PD,
169  * and level 0 the PT pages. The PTLX macro gives an index into the
170  * page-table page at level 'l' for the virtual address 'v'.
171  */
172 #define PTSZ            (4*KiB)                 /* page table page size */
173 #define PTSHIFT         9                       /*  */
174
175 #define PTLX(v, l)      (((v)>>(((l)*PTSHIFT)+PGSHIFT)) & ((1<<PTSHIFT)-1))
176 #define PGLSZ(l)        (1ull<<(((l)*PTSHIFT)+PGSHIFT))
177
178 #define getpgcolor(a)   0
179
180 #define RMACH           R15                     /* m-> */
181 #define RUSER           R14                     /* up-> */