Major revisions to documentation.
[pintos-anon] / src / threads / vaddr.h
1 #ifndef THREADS_VADDR_H
2 #define THREADS_VADDR_H
3
4 #include <debug.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7
8 #include "threads/loader.h"
9
10 /* Functions and macros for working with virtual addresses.
11
12    See pte.h for functions and macros specifically for x86
13    hardware page tables. */
14
15 #define BITMASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT))
16
17 /* Page offset (bits 0:12). */
18 #define PGSHIFT 0                          /* Index of first offset bit. */
19 #define PGBITS  12                         /* Number of offset bits. */
20 #define PGSIZE  (1 << PGBITS)              /* Bytes in a page. */
21 #define PGMASK  BITMASK(PGSHIFT, PGBITS)   /* Page offset bits (0:12). */
22
23 /* Offset within a page. */
24 static inline unsigned pg_ofs (const void *va) {
25   return (uintptr_t) va & PGMASK;
26 }
27
28 /* Virtual page number. */
29 static inline uintptr_t pg_no (const void *va) {
30   return (uintptr_t) va >> PGBITS;
31 }
32
33 /* Round up to nearest page boundary. */
34 static inline void *pg_round_up (const void *va) {
35   return (void *) (((uintptr_t) va + PGSIZE - 1) & ~PGMASK);
36 }
37
38 /* Round down to nearest page boundary. */
39 static inline void *pg_round_down (const void *va) {
40   return (void *) ((uintptr_t) va & ~PGMASK);
41 }
42 \f
43 /* Base address of the 1:1 physical-to-virtual mapping.  Physical
44    memory is mapped starting at this virtual address.  Thus,
45    physical address 0 is accessible at PHYS_BASE, physical
46    address address 0x1234 at (uint8_t *) PHYS_BASE + 0x1234, and
47    so on.
48
49    This address also marks the end of user programs' address
50    space.  Up to this point in memory, user programs are allowed
51    to map whatever they like.  At this point and above, the
52    virtual address space belongs to the kernel. */
53 #define PHYS_BASE ((void *) LOADER_PHYS_BASE)
54
55 /* Returns true if VADDR is a user virtual address. */
56 static inline bool
57 is_user_vaddr (const void *vaddr) 
58 {
59   return vaddr < PHYS_BASE;
60 }
61
62 /* Returns true if VADDR is a kernel virtual address. */
63 static inline bool
64 is_kernel_vaddr (const void *vaddr) 
65 {
66   return vaddr >= PHYS_BASE;
67 }
68
69 /* Returns kernel virtual address at which physical address PADDR
70    is mapped. */
71 static inline void *
72 ptov (uintptr_t paddr)
73 {
74   ASSERT ((void *) paddr < PHYS_BASE);
75
76   return (void *) (paddr + PHYS_BASE);
77 }
78
79 /* Returns physical address at which kernel virtual address VADDR
80    is mapped. */
81 static inline uintptr_t
82 vtop (const void *vaddr)
83 {
84   ASSERT (is_kernel_vaddr (vaddr));
85
86   return (uintptr_t) vaddr - (uintptr_t) PHYS_BASE;
87 }
88
89 #endif /* threads/vaddr.h */