Redo makefiles.
[pintos-anon] / src / threads / mmu.h
1 #ifndef HEADER_MMU_H
2 #define HEADER_MMU_H 1
3
4 #ifndef __ASSEMBLER__
5 #include <stdint.h>
6 #include "lib/debug.h"
7 #endif
8
9 #include "loader.h"
10
11 #define MASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT))
12
13 /* Page offset (bits 0:11). */
14 #define PGSHIFT         0                  /* First offset bit. */
15 #define PGBITS          12                 /* Number of offset bits. */
16 #define PGMASK          MASK(PGSHIFT, PGBITS)
17 #define PGSIZE          (1 << PGBITS)
18
19 /* Page table (bits 12:21). */
20 #define PTSHIFT         PGBITS             /* First page table bit. */
21 #define PTBITS          10                 /* Number of page table bits. */
22 #define PTMASK          MASK(PTSHIFT, PTBITS)
23
24 /* Page directory (bits 22:31). */
25 #define PDSHIFT         (PTSHIFT + PTBITS) /* First page dir bit. */
26 #define PDBITS          10                 /* Number of page dir bits. */
27 #define PDMASK          MASK(PDSHIFT, PDBITS)
28
29 #ifndef __ASSEMBLER__
30 /* Offset within a page. */
31 static inline unsigned pg_ofs (void *va) { return (uintptr_t) va & PGMASK; }
32
33 /* Page number. */
34 static inline uintptr_t pg_no (void *va) { return (uintptr_t) va >> PTSHIFT; }
35
36 /* Page table number. */
37 static inline unsigned pt_no (void *va) {
38   return ((uintptr_t) va & PTMASK) >> PTSHIFT;
39 }
40
41 /* Page directory number. */
42 static inline uintptr_t pd_no (void *va) { return (uintptr_t) va >> PDSHIFT; }
43
44 /* Round up to nearest page boundary. */
45 static inline void *pg_round_up (void *va) {
46   return (void *) (((uintptr_t) va + PGSIZE - 1) & ~PGMASK);
47 }
48
49 /* Round down to nearest page boundary. */
50 static inline void *pg_round_down (void *va) {
51   return (void *) ((uintptr_t) va & ~PGMASK);
52 }
53
54 #define PHYS_BASE ((void *) LOADER_PHYS_BASE)
55
56 /* Returns kernel virtual address at which physical address PADDR
57    is mapped. */
58 static inline void *
59 ptov (uintptr_t paddr)
60 {
61   ASSERT ((void *) paddr < PHYS_BASE);
62
63   return (void *) (paddr + PHYS_BASE);
64 }
65
66 /* Returns physical address at which kernel virtual address VADDR
67    is mapped. */
68 static inline uintptr_t
69 vtop (void *vaddr)
70 {
71   ASSERT (vaddr >= PHYS_BASE);
72
73   return (uintptr_t) vaddr - (uintptr_t) PHYS_BASE;
74 }
75 #endif
76
77 /* Page Directory Entry (PDE) and Page Table Entry (PTE) flags. */
78 #define PG_P 0x1               /* 1=present, 0=not present. */
79 #define PG_W 0x2               /* 1=read/write, 0=read-only. */
80 #define PG_U 0x4               /* 1=user/kernel, 0=kernel only. */
81 #define PG_A 0x20              /* 1=accessed, 0=not acccessed. */
82 #define PG_D 0x40              /* 1=dirty, 0=not dirty. */
83
84 /* EFLAGS Register. */
85 #define FLAG_MBS  0x00000002    /* Must be set. */
86 #define FLAG_IF   0x00000200    /* Interrupt Flag. */
87
88 #endif /* mmu.h */