CS318 - Pintos
Pintos source browser for JHU CS318 course
vaddr.h
Go to the documentation of this file.
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 
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 */
PHYS_BASE
#define PHYS_BASE
Base address of the 1:1 physical-to-virtual mapping.
Definition: vaddr.h:53
vtop
static uintptr_t vtop(const void *vaddr)
Returns physical address at which kernel virtual address VADDR is mapped.
Definition: vaddr.h:82
pg_round_up
static void * pg_round_up(const void *va)
Round up to nearest page boundary.
Definition: vaddr.h:34
pg_round_down
static void * pg_round_down(const void *va)
Round down to nearest page boundary.
Definition: vaddr.h:39
ptov
static void * ptov(uintptr_t paddr)
Returns kernel virtual address at which physical address PADDR is mapped.
Definition: vaddr.h:72
stdbool.h
PGMASK
#define PGMASK
Page offset bits (0:12).
Definition: vaddr.h:21
PGSIZE
#define PGSIZE
Bytes in a page.
Definition: vaddr.h:20
PGBITS
#define PGBITS
Number of offset bits.
Definition: vaddr.h:19
stdint.h
ASSERT
#define ASSERT(CONDITION)
This is outside the header guard so that debug.h may be included multiple times with different settin...
Definition: debug.h:31
is_kernel_vaddr
static bool is_kernel_vaddr(const void *vaddr)
Returns true if VADDR is a kernel virtual address.
Definition: vaddr.h:64
is_user_vaddr
static bool is_user_vaddr(const void *vaddr)
Returns true if VADDR is a user virtual address.
Definition: vaddr.h:57
loader.h
uintptr_t
uint32_t uintptr_t
Definition: stdint.h:36
pg_ofs
static unsigned pg_ofs(const void *va)
Offset within a page.
Definition: vaddr.h:24
debug.h
pg_no
static uintptr_t pg_no(const void *va)
Virtual page number.
Definition: vaddr.h:29