xref: /haiku/headers/private/kernel/vm/VMTranslationMap.h (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
1 /*
2  * Copyright 2002-2010, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 #ifndef KERNEL_VM_VM_TRANSLATION_MAP_H
9 #define KERNEL_VM_VM_TRANSLATION_MAP_H
10 
11 
12 #include <kernel.h>
13 #include <lock.h>
14 
15 #include <vm/VMArea.h>
16 
17 
18 struct kernel_args;
19 struct vm_page_reservation;
20 
21 
22 struct VMTranslationMap {
23 								VMTranslationMap();
24 	virtual						~VMTranslationMap();
25 
26 	virtual	bool	 			Lock() = 0;
27 	virtual	void				Unlock() = 0;
28 
29 	virtual	addr_t				MappedSize() const = 0;
30 	virtual	size_t				MaxPagesNeededToMap(addr_t start,
31 									addr_t end) const = 0;
32 
33 	virtual	status_t			Map(addr_t virtualAddress,
34 									phys_addr_t physicalAddress,
35 									uint32 attributes, uint32 memoryType,
36 									vm_page_reservation* reservation) = 0;
37 	virtual	status_t			Unmap(addr_t start, addr_t end) = 0;
38 
39 	// map not locked
40 	virtual	status_t			UnmapPage(VMArea* area, addr_t address,
41 									bool updatePageQueue) = 0;
42 	virtual	void				UnmapPages(VMArea* area, addr_t base,
43 									size_t size, bool updatePageQueue);
44 	virtual	void				UnmapArea(VMArea* area,
45 									bool deletingAddressSpace,
46 									bool ignoreTopCachePageFlags);
47 
48 	virtual	status_t			Query(addr_t virtualAddress,
49 									phys_addr_t* _physicalAddress,
50 									uint32* _flags) = 0;
51 	virtual	status_t			QueryInterrupt(addr_t virtualAddress,
52 									phys_addr_t* _physicalAddress,
53 									uint32* _flags) = 0;
54 
55 	virtual	status_t			Protect(addr_t base, addr_t top,
56 									uint32 attributes, uint32 memoryType) = 0;
57 			status_t			ProtectPage(VMArea* area, addr_t address,
58 									uint32 attributes);
59 			status_t			ProtectArea(VMArea* area,
60 									uint32 attributes);
61 
62 	virtual	status_t			ClearFlags(addr_t virtualAddress,
63 									uint32 flags) = 0;
64 
65 	virtual	bool				ClearAccessedAndModified(
66 									VMArea* area, addr_t address,
67 									bool unmapIfUnaccessed,
68 									bool& _modified) = 0;
69 
70 	virtual	void				Flush() = 0;
71 
72 protected:
73 			void				PageUnmapped(VMArea* area,
74 									page_num_t pageNumber, bool accessed,
75 									bool modified, bool updatePageQueue);
76 			void				UnaccessedPageUnmapped(VMArea* area,
77 									page_num_t pageNumber);
78 
79 protected:
80 			recursive_lock		fLock;
81 			int32				fMapCount;
82 };
83 
84 
85 struct VMPhysicalPageMapper {
86 								VMPhysicalPageMapper();
87 	virtual						~VMPhysicalPageMapper();
88 
89 	// get/put virtual address for physical page -- will be usuable on all CPUs
90 	// (usually more expensive than the *_current_cpu() versions)
91 	virtual	status_t			GetPage(phys_addr_t physicalAddress,
92 									addr_t* _virtualAddress,
93 									void** _handle) = 0;
94 	virtual	status_t			PutPage(addr_t virtualAddress,
95 									void* handle) = 0;
96 
97 	// get/put virtual address for physical page -- thread must be pinned the
98 	// whole time
99 	virtual	status_t			GetPageCurrentCPU(
100 									phys_addr_t physicalAddress,
101 									addr_t* _virtualAddress,
102 									void** _handle) = 0;
103 	virtual	status_t			PutPageCurrentCPU(addr_t virtualAddress,
104 									void* _handle) = 0;
105 
106 	// get/put virtual address for physical in KDL
107 	virtual	status_t			GetPageDebug(phys_addr_t physicalAddress,
108 									addr_t* _virtualAddress,
109 									void** _handle) = 0;
110 	virtual	status_t			PutPageDebug(addr_t virtualAddress,
111 									void* handle) = 0;
112 
113 	// memory operations on pages
114 	virtual	status_t			MemsetPhysical(phys_addr_t address, int value,
115 									phys_size_t length) = 0;
116 	virtual	status_t			MemcpyFromPhysical(void* to, phys_addr_t from,
117 									size_t length, bool user) = 0;
118 	virtual	status_t			MemcpyToPhysical(phys_addr_t to,
119 									const void* from, size_t length,
120 									bool user) = 0;
121 	virtual	void				MemcpyPhysicalPage(phys_addr_t to,
122 									phys_addr_t from) = 0;
123 };
124 
125 
126 
127 inline status_t
128 VMTranslationMap::ProtectPage(VMArea* area, addr_t address, uint32 attributes)
129 {
130 	return Protect(address, address + B_PAGE_SIZE - 1, attributes,
131 		area->MemoryType());
132 }
133 
134 
135 #include <vm/VMArea.h>
136 inline status_t
137 VMTranslationMap::ProtectArea(VMArea* area, uint32 attributes)
138 {
139 	return Protect(area->Base(), area->Base() + area->Size() - 1, attributes,
140 		area->MemoryType());
141 }
142 
143 
144 #include <arch/vm_translation_map.h>
145 
146 #endif	/* KERNEL_VM_VM_TRANSLATION_MAP_H */
147