1b3215a62SHenry Harrington/*
2b3215a62SHenry Harrington * Copyright 2014, Henry Harrington, henry.harrington@gmail.com.
3b3215a62SHenry Harrington * Distributed under the terms of the MIT License.
4b3215a62SHenry Harrington */
5b3215a62SHenry Harrington
6b3215a62SHenry Harrington#ifndef MMU_H
7b3215a62SHenry Harrington#define MMU_H
8b3215a62SHenry Harrington
9b3215a62SHenry Harrington#include <arch/x86/descriptors.h>
10b3215a62SHenry Harrington
11b3215a62SHenry Harrington#undef BOOT_GDT_SEGMENT_COUNT
12b3215a62SHenry Harrington#define BOOT_GDT_SEGMENT_COUNT	(USER_DATA_SEGMENT + 1)
13b3215a62SHenry Harrington
14b3215a62SHenry Harrington#ifndef _ASSEMBLER
15b3215a62SHenry Harrington
16b3215a62SHenry Harrington#include "efi_platform.h"
17b3215a62SHenry Harrington
18b3215a62SHenry Harrington#include <util/FixedWidthPointer.h>
19b3215a62SHenry Harrington
20b3215a62SHenry Harrington
21b3215a62SHenry Harringtonextern segment_descriptor gBootGDT[BOOT_GDT_SEGMENT_COUNT];
22b3215a62SHenry Harrington
23b3215a62SHenry Harringtonstatic const uint32 kDefaultPageFlags = 0x3;	    // present, R/W
24b3215a62SHenry Harringtonstatic const uint64 kTableMappingFlags = 0x7;       // present, R/W, user
25b3215a62SHenry Harringtonstatic const uint64 kLargePageMappingFlags = 0x183; // present, R/W, user, global, large
26b3215a62SHenry Harringtonstatic const uint64 kPageMappingFlags = 0x103;      // present, R/W, user, global
27b3215a62SHenry Harrington
28b3215a62SHenry Harrington
29b3215a62SHenry Harrington#ifdef __cplusplus
30b3215a62SHenry Harringtonextern "C" {
31b3215a62SHenry Harrington#endif
32b3215a62SHenry Harrington
33ec239abcSJessica Hamiltonextern addr_t mmu_map_physical_memory(addr_t physicalAddress, size_t size, uint32 flags);
34ec239abcSJessica Hamiltonextern void mmu_free(void *virtualAddress, size_t size);
35ec239abcSJessica Hamilton
36b3215a62SHenry Harringtonextern void
37b3215a62SHenry Harringtonmmu_post_efi_setup(UINTN memory_map_size, EFI_MEMORY_DESCRIPTOR *memory_map, UINTN descriptor_size, UINTN descriptor_version);
38b3215a62SHenry Harringtonextern uint64_t
39b3215a62SHenry Harringtonmmu_generate_post_efi_page_tables(UINTN memory_map_size, EFI_MEMORY_DESCRIPTOR *memory_map, UINTN descriptor_size, UINTN descriptor_version);
40b3215a62SHenry Harringtonextern status_t
41b3215a62SHenry Harringtonplatform_kernel_address_to_bootloader_address(uint64_t address, void **_result);
42b3215a62SHenry Harringtonextern status_t
43b3215a62SHenry Harringtonplatform_bootloader_address_to_kernel_address(void *address, uint64_t *_result);
44b3215a62SHenry Harrington
45b3215a62SHenry Harrington#ifdef __cplusplus
46b3215a62SHenry Harrington}
47b3215a62SHenry Harrington#endif
48b3215a62SHenry Harrington
49b3215a62SHenry Harrington
50b3215a62SHenry Harrington/*! Convert a 32-bit address to a 64-bit address. */
51b3215a62SHenry Harringtoninline uint64
52b3215a62SHenry Harringtonfix_address(uint64 address)
53b3215a62SHenry Harrington{
54b3215a62SHenry Harrington	uint64 result;
55b3215a62SHenry Harrington	if (platform_bootloader_address_to_kernel_address((void *)address, &result) != B_OK)
56b3215a62SHenry Harrington		return address;
57b3215a62SHenry Harrington	else
58b3215a62SHenry Harrington		return result;
59b3215a62SHenry Harrington}
60b3215a62SHenry Harrington
61b3215a62SHenry Harrington
62b3215a62SHenry Harringtontemplate<typename Type>
63b3215a62SHenry Harringtoninline void
64b3215a62SHenry Harringtonfix_address(FixedWidthPointer<Type>& p)
65b3215a62SHenry Harrington{
66b3215a62SHenry Harrington	if (p != NULL)
67b3215a62SHenry Harrington		p.SetTo(fix_address(p.Get()));
68b3215a62SHenry Harrington}
69b3215a62SHenry Harrington
70b3215a62SHenry Harrington
71b3215a62SHenry Harrington#endif	// !_ASSEMBLER
72b3215a62SHenry Harrington
73b3215a62SHenry Harrington#endif	/* MMU_H */
74