1 #ifndef THREADS_VADDR_H
2 #define THREADS_VADDR_H
8 #include "threads/loader.h"
10 /* Functions and macros for working with virtual addresses.
12 See pte.h for functions and macros specifically for x86
13 hardware page tables. */
15 #define BITMASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT))
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). */
23 /* Offset within a page. */
24 static inline unsigned pg_ofs (const void *va
) {
25 return (uintptr_t) va
& PGMASK
;
28 /* Virtual page number. */
29 static inline uintptr_t pg_no (const void *va
) {
30 return (uintptr_t) va
>> PGBITS
;
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
);
38 /* Round down to nearest page boundary. */
39 static inline void *pg_round_down (const void *va
) {
40 return (void *) ((uintptr_t) va
& ~PGMASK
);
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
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)
55 /* Returns true if VADDR is a user virtual address. */
57 is_user_vaddr (const void *vaddr
)
59 return vaddr
< PHYS_BASE
;
62 /* Returns true if VADDR is a kernel virtual address. */
64 is_kernel_vaddr (const void *vaddr
)
66 return vaddr
>= PHYS_BASE
;
69 /* Returns kernel virtual address at which physical address PADDR
72 ptov (uintptr_t paddr
)
74 ASSERT ((void *) paddr
< PHYS_BASE
);
76 return (void *) (paddr
+ PHYS_BASE
);
79 /* Returns physical address at which kernel virtual address VADDR
81 static inline uintptr_t
82 vtop (const void *vaddr
)
84 ASSERT (is_kernel_vaddr (vaddr
));
86 return (uintptr_t) vaddr
- (uintptr_t) PHYS_BASE
;
89 #endif /* threads/vaddr.h */