xref: /haiku/src/system/kernel/arch/riscv64/arch_vm_translation_map.cpp (revision 372b901dfeada686207d00bbcce456f748bbda12)
1 /*
2  * Copyright 2007-2010, François Revol, revol@free.fr.
3  * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
4  * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. All rights
5  *   reserved.
6  * Copyright 2019, Adrien Destugues, pulkomandy@pulkomandy.tk.
7  * Distributed under the terms of the MIT License.
8  *
9  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
10  * Distributed under the terms of the NewOS License.
11  */
12 
13 
14 #include <arch_cpu_defs.h>
15 #include <boot/kernel_args.h>
16 #include <KernelExport.h>
17 #include <kernel.h>
18 #include <vm/vm.h>
19 #include <vm/vm_priv.h>
20 #include <vm/VMAddressSpace.h>
21 #include <Clint.h>
22 #include <Htif.h>
23 #include <Plic.h>
24 
25 #include "RISCV64VMTranslationMap.h"
26 
27 
28 #define TRACE_VM_TMAP
29 #ifdef TRACE_VM_TMAP
30 #	define TRACE(x...) dprintf(x)
31 #else
32 #	define TRACE(x...) ;
33 #endif
34 
35 
36 ssize_t gVirtFromPhysOffset = 0;
37 
38 phys_addr_t sPageTable = 0;
39 char sPhysicalPageMapperData[sizeof(RISCV64VMPhysicalPageMapper)];
40 
41 
42 // TODO: Consolidate function with RISCV64VMTranslationMap
43 
44 static Pte*
45 LookupPte(addr_t virtAdr, bool alloc, kernel_args* args,
46 	phys_addr_t (*get_free_page)(kernel_args *))
47 {
48 	Pte *pte = (Pte*)VirtFromPhys(sPageTable);
49 	for (int level = 2; level > 0; level --) {
50 		pte += VirtAdrPte(virtAdr, level);
51 		if (!((1 << pteValid) & pte->flags)) {
52 			if (!alloc)
53 				return NULL;
54 			pte->ppn = get_free_page(args);
55 			if (pte->ppn == 0)
56 				return NULL;
57 			memset((Pte*)VirtFromPhys(B_PAGE_SIZE * pte->ppn), 0, B_PAGE_SIZE);
58 			pte->flags |= (1 << pteValid);
59 		}
60 		pte = (Pte*)VirtFromPhys(B_PAGE_SIZE * pte->ppn);
61 	}
62 	pte += VirtAdrPte(virtAdr, 0);
63 	return pte;
64 }
65 
66 
67 static void
68 Map(addr_t virtAdr, phys_addr_t physAdr, uint64 flags, kernel_args* args,
69 	phys_addr_t (*get_free_page)(kernel_args *))
70 {
71 	// dprintf("Map(0x%" B_PRIxADDR ", 0x%" B_PRIxADDR ")\n", virtAdr, physAdr);
72 	Pte* pte = LookupPte(virtAdr, true, args, get_free_page);
73 	if (pte == NULL) panic("can't allocate page table");
74 
75 	pte->ppn = physAdr / B_PAGE_SIZE;
76 	pte->flags = (1 << pteValid) | (1 << pteAccessed) | (1 << pteDirty) | flags;
77 
78 	FlushTlbPage(virtAdr);
79 }
80 
81 
82 //#pragma mark -
83 
84 status_t
85 arch_vm_translation_map_init(kernel_args *args,
86 	VMPhysicalPageMapper** _physicalPageMapper)
87 {
88 	TRACE("vm_translation_map_init: entry\n");
89 
90 #ifdef TRACE_VM_TMAP
91 	TRACE("physical memory ranges:\n");
92 	for (uint32 i = 0; i < args->num_physical_memory_ranges; i++) {
93 		phys_addr_t start = args->physical_memory_range[i].start;
94 		phys_addr_t end = start + args->physical_memory_range[i].size;
95 		TRACE("  %" B_PRIxPHYSADDR " - %" B_PRIxPHYSADDR "\n", start, end);
96 	}
97 
98 	TRACE("allocated physical ranges:\n");
99 	for (uint32 i = 0; i < args->num_physical_allocated_ranges; i++) {
100 		phys_addr_t start = args->physical_allocated_range[i].start;
101 		phys_addr_t end = start + args->physical_allocated_range[i].size;
102 		TRACE("  %" B_PRIxPHYSADDR " - %" B_PRIxPHYSADDR "\n", start, end);
103 	}
104 
105 	TRACE("allocated virtual ranges:\n");
106 	for (uint32 i = 0; i < args->num_virtual_allocated_ranges; i++) {
107 		addr_t start = args->virtual_allocated_range[i].start;
108 		addr_t end = start + args->virtual_allocated_range[i].size;
109 		TRACE("  %" B_PRIxADDR " - %" B_PRIxADDR "\n", start, end);
110 	}
111 
112 	TRACE("kernel args ranges:\n");
113 	for (uint32 i = 0; i < args->num_kernel_args_ranges; i++) {
114 		phys_addr_t start = args->kernel_args_range[i].start;
115 		phys_addr_t end = start + args->kernel_args_range[i].size;
116 		TRACE("  %" B_PRIxPHYSADDR " - %" B_PRIxPHYSADDR "\n", start, end);
117 	}
118 #endif
119 
120 	{
121 		SatpReg satp(Satp());
122 		sPageTable = satp.ppn * B_PAGE_SIZE;
123 	}
124 
125 	dprintf("physMapBase: %#" B_PRIxADDR "\n", args->arch_args.physMap.start);
126 	dprintf("physMemBase: %#" B_PRIxADDR "\n", args->physical_memory_range[0].start);
127 	gVirtFromPhysOffset = args->arch_args.physMap.start - args->physical_memory_range[0].start;
128 
129 	clear_ac();
130 
131 	*_physicalPageMapper = new(&sPhysicalPageMapperData)
132 		RISCV64VMPhysicalPageMapper();
133 
134 	return B_OK;
135 }
136 
137 
138 status_t
139 arch_vm_translation_map_init_post_sem(kernel_args *args)
140 {
141 	return B_OK;
142 }
143 
144 
145 status_t
146 arch_vm_translation_map_init_post_area(kernel_args *args)
147 {
148 	TRACE("vm_translation_map_init_post_area: entry\n");
149 	return B_OK;
150 }
151 
152 
153 status_t
154 arch_vm_translation_map_early_map(kernel_args *args,
155 	addr_t virtAdr, phys_addr_t physAdr, uint8 attributes,
156 	phys_addr_t (*get_free_page)(kernel_args *))
157 {
158 	//dprintf("early_map(%#" B_PRIxADDR ", %#" B_PRIxADDR ")\n", virtAdr, physAdr);
159 	uint64 flags = 0;
160 	if ((attributes & B_KERNEL_READ_AREA) != 0)
161 		flags |= (1 << pteRead);
162 	if ((attributes & B_KERNEL_WRITE_AREA) != 0)
163 		flags |= (1 << pteWrite);
164 	if ((attributes & B_KERNEL_EXECUTE_AREA) != 0)
165 		flags |= (1 << pteExec);
166 	Map(virtAdr, physAdr, flags, args, get_free_page);
167 	return B_OK;
168 }
169 
170 
171 status_t
172 arch_vm_translation_map_create_map(bool kernel, VMTranslationMap** _map)
173 {
174 	*_map = new(std::nothrow) RISCV64VMTranslationMap(kernel,
175 		(kernel) ? sPageTable : 0);
176 
177 	if (*_map == NULL)
178 		return B_NO_MEMORY;
179 
180 	return B_OK;
181 }
182 
183 
184 bool
185 arch_vm_translation_map_is_kernel_page_accessible(addr_t virtualAddress,
186 	uint32 protection)
187 {
188 	return virtualAddress != 0;
189 }
190