]>
Commit | Line | Data |
---|---|---|
3c4e084d JP |
1 | /* packet.c |
2 | * OS-to-NetWatch communication packet interface | |
3 | * NetWatch system management mode administration console | |
4 | * | |
5 | * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved. | |
6 | * This program is free software; you can redistribute and/or modify it under | |
7 | * the terms found in the file LICENSE in the root of this source tree. | |
8 | * | |
9 | */ | |
10 | ||
05c5b136 JP |
11 | #include <stdint.h> |
12 | #include "packet.h" | |
13 | ||
14 | #define PTE_FOR(x) (((unsigned int)(x) >> 12) & 0x3FF) | |
15 | #define PDE_FOR(x) ((unsigned int)(x) >> 22) | |
16 | #define ADDR_12_MASK(x) ((unsigned int)(x) & ~((1 << 12) - 1)) | |
17 | #define ADDR_22_MASK(x) ((unsigned int)(x) & ~((1 << 22) - 1)) | |
18 | #define LOWER_12(x) ((unsigned int)(x) & ((1 << 12) - 1)) | |
19 | #define LOWER_22(x) ((unsigned int)(x) & ((1 << 22) - 1)) | |
20 | ||
21 | #define PDE_4M_ADDR_SHIFT 22 | |
22 | #define PTE_4K_ADDR_SHIFT 12 | |
23 | #define PDE_TABLE_ADDR_SHIFT 12 | |
24 | #define PTE_FRAME_ADDR_MASK (~((1 << PTE_4K_ADDR_SHIFT) - 1)) | |
25 | #define PDE_TABLE_ADDR_MASK (~((1 << PDE_TABLE_ADDR_SHIFT) - 1)) | |
26 | ||
27 | #define PDE_ATTRIB_INDEX (1 << 12) | |
28 | #define PDE_GLOBAL (1 << 8) | |
29 | #define PDE_PAGE_SIZE (1 << 7) | |
30 | #define PDE_DIRTY (1 << 6) | |
31 | #define PDE_ACCESSED (1 << 5) | |
32 | #define PDE_NO_CACHE (1 << 4) | |
33 | #define PDE_WRITE_THROUGH (1 << 3) | |
34 | #define PDE_USER (1 << 2) | |
35 | #define PDE_READ_WRITE (1 << 1) | |
36 | #define PDE_PRESENT (1 << 0) | |
37 | ||
38 | #define PTE_GLOBAL (1 << 8) | |
39 | #define PTE_ATTRIB_INDEX (1 << 7) | |
40 | #define PTE_DIRTY (1 << 6) | |
41 | #define PTE_ACCESSED (1 << 5) | |
42 | #define PTE_NO_CACHE (1 << 4) | |
43 | #define PTE_WRITE_THROUGH (1 << 3) | |
44 | #define PTE_USER (1 << 2) | |
45 | #define PTE_READ_WRITE (1 << 1) | |
46 | #define PTE_PRESENT (1 << 0) | |
47 | ||
48 | asm ("get_cr3: mov %cr3, %eax \n ret"); | |
49 | uint32_t * get_cr3(); | |
50 | ||
51 | void * l2p (void * addr) { | |
52 | ||
53 | uint32_t * cr3 = get_cr3(); | |
54 | uint32_t * page_table; | |
55 | uint32_t pde, pte; | |
56 | ||
57 | pde = cr3[PDE_FOR(addr)]; | |
58 | ||
59 | if (!(pde & PDE_PRESENT)) { | |
60 | return 0; | |
61 | } | |
62 | ||
63 | if (pde & PDE_PAGE_SIZE) { | |
64 | return (void *)(ADDR_22_MASK(pde) | LOWER_22(addr)); | |
65 | } else { | |
66 | page_table = (uint32_t *)(ADDR_12_MASK(pde)); | |
67 | pte = page_table[PTE_FOR(addr)]; | |
68 | ||
69 | if (!pte & PTE_PRESENT) { | |
70 | return 0; | |
71 | } | |
72 | return (void *)(ADDR_12_MASK(pte) | LOWER_12(addr)); | |
73 | } | |
74 | } | |
75 | ||
76 | packet_t * check_packet (uint32_t logical_base) { | |
77 | packet_t * physical_base; | |
78 | ||
79 | if (LOWER_12(logical_base) != 0) return 0; | |
80 | ||
81 | physical_base = l2p((void *)logical_base); | |
82 | ||
83 | if (!physical_base) return 0; | |
84 | if (physical_base->signature != 0x1BADD00D) return 0; | |
85 | ||
86 | return physical_base; | |
87 | } |