1 | /* |
---|
2 | * Copyright (c) 2018. |
---|
3 | * Amaan Cheval <amaan.cheval@gmail.com> |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * |
---|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
24 | * SUCH DAMAGE. |
---|
25 | */ |
---|
26 | |
---|
27 | #ifndef _LIBCPU_AMD64_PAGE_H |
---|
28 | #define _LIBCPU_AMD64_PAGE_H |
---|
29 | |
---|
30 | #ifndef ASM |
---|
31 | |
---|
32 | #define NUM_PAGE_TABLE_ENTRIES 512 |
---|
33 | |
---|
34 | extern uint64_t amd64_pml4[NUM_PAGE_TABLE_ENTRIES]; |
---|
35 | extern uint64_t amd64_pdpt[NUM_PAGE_TABLE_ENTRIES]; |
---|
36 | |
---|
37 | bool paging_1gib_pages_supported(void); |
---|
38 | uint8_t get_maxphysaddr(void); |
---|
39 | uint64_t get_mask_for_bits(uint8_t start, uint8_t end); |
---|
40 | uint64_t create_cr3_entry( |
---|
41 | uint64_t phys_addr, uint8_t maxphysaddr, uint64_t flags |
---|
42 | ); |
---|
43 | uint64_t create_pml4_entry( |
---|
44 | uint64_t phys_addr, uint8_t maxphysaddr, uint64_t flags |
---|
45 | ); |
---|
46 | uint64_t create_pdpt_entry( |
---|
47 | uint64_t phys_addr, uint8_t maxphysaddr, uint64_t flags |
---|
48 | ); |
---|
49 | |
---|
50 | void paging_init(void); |
---|
51 | |
---|
52 | #define PAGE_FLAGS_PRESENT (1 << 0) |
---|
53 | #define PAGE_FLAGS_WRITABLE (1 << 1) |
---|
54 | #define PAGE_FLAGS_USER_ACCESSIBLE (1 << 2) |
---|
55 | #define PAGE_FLAGS_WRITE_THROUGH (1 << 3) |
---|
56 | #define PAGE_FLAGS_NO_CACHE (1 << 4) |
---|
57 | #define PAGE_FLAGS_ACCESSED (1 << 5) |
---|
58 | #define PAGE_FLAGS_DIRTY (1 << 6) |
---|
59 | #define PAGE_FLAGS_HUGE_PAGE (1 << 7) |
---|
60 | #define PAGE_FLAGS_GLOBAL (1 << 8) |
---|
61 | #define PAGE_FLAGS_NO_EXECUTE (1 << 63) |
---|
62 | |
---|
63 | #define PAGE_FLAGS_DEFAULTS \ |
---|
64 | (PAGE_FLAGS_PRESENT | PAGE_FLAGS_WRITABLE | PAGE_FLAGS_USER_ACCESSIBLE \ |
---|
65 | | PAGE_FLAGS_WRITE_THROUGH | PAGE_FLAGS_NO_CACHE | PAGE_FLAGS_GLOBAL) |
---|
66 | |
---|
67 | #endif /* !ASM */ |
---|
68 | #endif |
---|