summaryrefslogtreecommitdiffstats
path: root/threads/vaddr.h
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-03-27 11:51:08 +0200
committermanuel <manuel@mausz.at>2012-03-27 11:51:08 +0200
commit4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b (patch)
tree868c52e06f207b5ec8a3cc141f4b8b2bdfcc165c /threads/vaddr.h
parenteae0bd57f0a26314a94785061888d193d186944a (diff)
downloadprogos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.gz
progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.bz2
progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.zip
reorganize file structure to match the upstream requirements
Diffstat (limited to 'threads/vaddr.h')
-rw-r--r--threads/vaddr.h89
1 files changed, 89 insertions, 0 deletions
diff --git a/threads/vaddr.h b/threads/vaddr.h
new file mode 100644
index 0000000..184c824
--- /dev/null
+++ b/threads/vaddr.h
@@ -0,0 +1,89 @@
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. */
24static inline unsigned pg_ofs (const void *va) {
25 return (uintptr_t) va & PGMASK;
26}
27
28/* Virtual page number. */
29static inline uintptr_t pg_no (const void *va) {
30 return (uintptr_t) va >> PGBITS;
31}
32
33/* Round up to nearest page boundary. */
34static 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. */
39static 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. */
56static inline bool
57is_user_vaddr (const void *vaddr)
58{
59 return vaddr < PHYS_BASE;
60}
61
62/* Returns true if VADDR is a kernel virtual address. */
63static inline bool
64is_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. */
71static inline void *
72ptov (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. */
81static inline uintptr_t
82vtop (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 */