1/*
2 * Copyright 2014, Henry Harrington, henry.harrington@gmail.com.
3 * Copyright 2019-2022, Haiku, Inc. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
6#ifndef MMU_H
7#define MMU_H
8
9
10#ifndef _ASSEMBLER
11
12
13#include "efi_platform.h"
14
15#include <util/FixedWidthPointer.h>
16
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22static const uint32 kDefaultPageFlags = 0x3;
23    // present, R/W
24static const uint64 kTableMappingFlags = 0x7;
25    // present, R/W, user
26static const uint64 kLargePageMappingFlags = 0x183;
27    // present, R/W, user, global, large
28static const uint64 kPageMappingFlags = 0x103;
29    // present, R/W, user, global
30
31
32extern addr_t get_next_virtual_address(size_t size);
33extern addr_t get_current_virtual_address();
34
35extern void mmu_init();
36
37extern phys_addr_t mmu_allocate_page();
38
39bool mmu_next_region(void** cookie, addr_t* vaddr, phys_addr_t* paddr, size_t* size);
40
41extern addr_t mmu_map_physical_memory(addr_t physicalAddress, size_t size,
42	uint32 flags);
43
44extern status_t platform_kernel_address_to_bootloader_address(addr_t address,
45	void **_result);
46
47extern status_t platform_bootloader_address_to_kernel_address(void *address,
48	addr_t *_result);
49
50extern status_t platform_allocate_lomem(void **_address, size_t size);
51
52#ifdef __cplusplus
53}
54#endif
55
56
57/*! Convert a 32-bit address to a 64-bit address. */
58inline addr_t
59fix_address(addr_t address)
60{
61	addr_t result;
62	if (platform_bootloader_address_to_kernel_address((void *)address, &result)
63		!= B_OK) {
64		return address;
65	} else
66		return result;
67}
68
69
70template<typename Type>
71inline void
72fix_address(FixedWidthPointer<Type>& p)
73{
74	if (p != NULL)
75		p.SetTo(fix_address(p.Get()));
76}
77
78
79#endif	// !_ASSEMBLER
80
81#endif	/* MMU_H */
82