Break GDT, TSS out of init.c, mmu.h.
[pintos-anon] / src / threads / mmu.h
1 /*
2  * Copyright (C) 1997 Massachusetts Institute of Technology 
3  *
4  * This software is being provided by the copyright holders under the
5  * following license. By obtaining, using and/or copying this software,
6  * you agree that you have read, understood, and will comply with the
7  * following terms and conditions:
8  *
9  * Permission to use, copy, modify, distribute, and sell this software
10  * and its documentation for any purpose and without fee or royalty is
11  * hereby granted, provided that the full text of this NOTICE appears on
12  * ALL copies of the software and documentation or portions thereof,
13  * including modifications, that you make.
14  *
15  * THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
16  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
17  * BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
18  * WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
19  * THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
20  * THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
21  * HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
22  * DOCUMENTATION.
23  *
24  * The name and trademarks of copyright holders may NOT be used in
25  * advertising or publicity pertaining to the software without specific,
26  * written prior permission. Title to copyright in this software and any
27  * associated documentation will at all times remain with copyright
28  * holders. See the file AUTHORS which should have accompanied this software
29  * for a list of all copyright holders.
30  *
31  * This file may be derived from previously copyrighted software. This
32  * copyright applies only to those changes made by the copyright
33  * holders listed in the AUTHORS file. The rest of this file is covered by
34  * the copyright notices, if any, listed below.
35  */
36
37
38 #ifndef _MMU_H_
39 #define _MMU_H_
40
41 #ifndef __ASSEMBLER__
42 #include <stdint.h>
43 #include "debug.h"
44 #endif
45
46 #include "loader.h"
47
48 #define MASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT))
49
50 /* Page offset (bits 0:11). */
51 #define PGSHIFT         0                  /* First offset bit. */
52 #define PGBITS          12                 /* Number of offset bits. */
53 #define PGMASK          MASK(PGSHIFT, PGBITS)
54 #define PGSIZE          (1 << PGBITS)
55
56 /* Page table (bits 12:21). */
57 #define PTSHIFT         PGBITS             /* First page table bit. */
58 #define PTBITS          10                 /* Number of page table bits. */
59 #define PTMASK          MASK(PTSHIFT, PTBITS)
60
61 /* Page directory (bits 22:31). */
62 #define PDSHIFT         (PTSHIFT + PTBITS) /* First page dir bit. */
63 #define PDBITS          10                 /* Number of page dir bits. */
64 #define PDMASK          MASK(PDSHIFT, PDBITS)
65
66 #ifndef __ASSEMBLER__
67 /* Offset within a page. */
68 static inline unsigned pg_ofs (void *va) { return (uintptr_t) va & PGMASK; }
69
70 /* Page number. */
71 static inline uintptr_t pg_no (void *va) { return (uintptr_t) va >> PTSHIFT; }
72
73 /* Page table number. */
74 static inline unsigned pt_no (void *va) {
75   return ((uintptr_t) va & PTMASK) >> PTSHIFT;
76 }
77
78 /* Page directory number. */
79 static inline uintptr_t pd_no (void *va) { return (uintptr_t) va >> PDSHIFT; }
80
81 /* Round up to nearest page boundary. */
82 static inline void *pg_round_up (void *va) {
83   return (void *) (((uintptr_t) va + PGSIZE - 1) & ~PGMASK);
84 }
85
86 /* Round down to nearest page boundary. */
87 static inline void *pg_round_down (void *va) {
88   return (void *) ((uintptr_t) va & ~PGMASK);
89 }
90
91 #define PHYS_BASE ((void *) LOADER_PHYS_BASE)
92
93 /* Returns kernel virtual address at which physical address PADDR
94    is mapped. */
95 static inline void *
96 ptov (uintptr_t paddr) 
97 {
98   ASSERT ((void *) paddr < PHYS_BASE);
99
100   return (void *) (paddr + PHYS_BASE);
101 }
102
103 /* Returns physical address at which kernel virtual address VADDR
104    is mapped. */
105 static inline uintptr_t
106 vtop (void *vaddr) 
107 {
108   ASSERT (vaddr >= PHYS_BASE);
109
110   return (uintptr_t) vaddr - (uintptr_t) PHYS_BASE;
111 }
112 #endif
113
114 /* Page Table/Directory Entry flags
115  *   these are defined by the hardware
116  */
117 #define PG_P 0x1               /* Present */
118 #define PG_W 0x2               /* Writeable */
119 #define PG_U 0x4               /* User */
120 #define PG_A 0x20              /* Accessed */
121 #define PG_D 0x40              /* Dirty */
122
123 /* EFLAGS Register. */
124 #define FLAG_MBS  0x00000002    /* Must be set. */
125 #define FLAG_IF   0x00000200    /* Interrupt Flag. */
126
127 /* Page fault error codes */
128 #define FEC_PR 0x1             /* Page fault caused by protection violation */
129 #define FEC_WR 0x2             /* Page fault caused by a write */
130 #define FEC_U  0x4             /* Page fault occured while in user mode */
131
132
133 /* Application segment type bits */
134 #define STA_X 0x8              /* Executable segment */
135 #define STA_A 0x1              /* Accessed */
136
137 #define STA_C 0x4              /* Conforming code segment (executable only) */
138 #define STA_R 0x2              /* Readable (executable segments) */
139
140 #define STA_E 0x4              /* Expand down (non-executable segments) */
141 #define STA_W 0x2              /* Writeable (non-executable segments) */
142
143
144
145 #endif /* !_MMU_H_ */