Skip to content

Commit 8ca5b18

Browse files
committed
migrate os::mem::Access to os::mem::Permission
1 parent fa6308e commit 8ca5b18

File tree

14 files changed

+142
-129
lines changed

14 files changed

+142
-129
lines changed

api/arch/x86/paging.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ namespace paging {
127127
using namespace util::literals;
128128
using namespace util::bitops;
129129

130-
/** Conversion from x86 paging flags to mem::Accessflags **/
131-
os::mem::Access to_memflags(Flags f);
130+
/** Conversion from x86 paging flags to mem::Permission flags **/
131+
os::mem::Permission to_memflags(Flags f);
132132

133-
/** Conversion from mem::Access flags to x86 paging flags **/
134-
Flags to_x86(os::mem::Access prot);
133+
/** Conversion from mem::Permission flags to x86 paging flags **/
134+
Flags to_x86(os::mem::Permission prot);
135135

136136
/** Summary of currently mapped page- and page directories **/
137137
struct Summary {

api/kernel/memory.hpp

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,10 @@
2626
#include <sstream>
2727
#include <expects>
2828
#include <kernel/memmap.hpp>
29+
#include <sys/mman.hpp>
2930

3031
namespace os::mem {
3132

32-
/** POSIX mprotect compliant access bits **/
33-
enum class Access : uint8_t {
34-
none = 0,
35-
read = 1,
36-
write = 2,
37-
execute = 4
38-
};
39-
4033
using Raw_allocator = buddy::Alloc<false>;
4134

4235
/** Get default allocator for untyped allocations */
@@ -68,7 +61,7 @@ namespace os::mem {
6861
* Virtual to physical memory mapping.
6962
* For interfacing with the virtual memory API, e.g. mem::map / mem::protect.
7063
**/
71-
template <typename Fl = Access>
64+
template <typename Fl = os::mem::Permission>
7265
struct Mapping
7366
{
7467
static const size_t any_size;
@@ -126,7 +119,7 @@ namespace os::mem {
126119
Map unmap(uintptr_t addr);
127120

128121
/** Get protection flags for page enclosing a given address */
129-
Access flags(uintptr_t addr);
122+
Permission flags(uintptr_t addr);
130123

131124
/** Determine active page size of a given linear address **/
132125
uintptr_t active_page_size(uintptr_t addr);
@@ -142,20 +135,20 @@ namespace os::mem {
142135
* might result in 513 4KiB pages or 1 2MiB page and 1 4KiB page getting
143136
* protected.
144137
**/
145-
Map protect(uintptr_t linear, size_t len, Access flags = Access::read);
138+
Map protect(uintptr_t linear, size_t len, Permission flags = Permission::Read); // TODO(mazunki): consider whether we should default to Read here
146139

147140
/**
148141
* Set and return access flags for a given linear address range
149142
* The range is expected to be mapped by a previous call to map.
150143
**/
151-
Access protect_range(uintptr_t linear, Access flags = Access::read);
144+
Permission protect_range(uintptr_t linear, Permission flags = Permission::Read); // TODO(mazunki): consider whether we should default to Read here
152145

153146
/**
154147
* Set and return access flags for a page starting at linear.
155148
* @note : the page size can be any of the supported sizes and
156149
* protection will apply for that whole page.
157150
**/
158-
Access protect_page(uintptr_t linear, Access flags = Access::read);
151+
Permission protect_page(uintptr_t linear, Permission flags = Permission::Read); // TODO(mazunki): consider whether we should default to Read here
159152

160153

161154
/** Get the physical address to which linear address is mapped **/
@@ -176,20 +169,6 @@ namespace os::mem {
176169

177170

178171

179-
180-
181-
// Enable bitwise ops on access flags
182-
namespace util {
183-
inline namespace bitops {
184-
template<>
185-
struct enable_bitmask_ops<os::mem::Access> {
186-
using type = typename std::underlying_type<os::mem::Access>::type;
187-
static constexpr bool enable = true;
188-
};
189-
}
190-
}
191-
192-
193172
namespace os::mem {
194173

195174
//
@@ -333,11 +312,12 @@ namespace os::mem {
333312
virtual_move(uintptr_t src, size_t size, uintptr_t dst, const char* label)
334313
{
335314
using namespace util::bitops;
336-
const auto flags = os::mem::Access::read | os::mem::Access::write;
315+
const auto flags = os::mem::Permission::Data; // TODO(mazunki): shouldn't this inherit flags from @src?
337316
// setup @dst as new virt area for @src
338317
os::mem::map({dst, src, flags, size}, label);
318+
339319
// unpresent @src
340-
os::mem::protect(src, size, os::mem::Access::none);
320+
os::mem::protect(src, size, os::mem::Permission::Any); // TODO(mazunki): change to Permission::None when introduced
341321
}
342322
}
343323

api/sys/mman.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ namespace os::mem {
1717
Fixed = MAP_FIXED,
1818
Anonymous = MAP_ANONYMOUS,
1919
};
20+
21+
enum class Permission : uint8_t { // TODO(mazunki): consider making Permission::{Read,Write,Execute} private or standalone class
22+
Read = PROT_READ,
23+
Write = PROT_WRITE,
24+
Execute = PROT_EXEC,
25+
26+
Data = Read | Write,
27+
Code = Read | Execute,
28+
29+
Any = 0, // TODO(mazunki): this should really be R|W|X; but requires some refactoring
30+
RWX = Read|Write|Execute, // TODO(mazunki): temporary, remove me. references should use Permission::Any
31+
32+
// None = 0, // TODO(mazunki): implement this after Any is properly implemented (to avoid confusion with old Access::none which had a different meaning). should block all access (best used for unmapped stuff, potentially tests)
33+
};
2034
} // os::mmap
2135

2236

@@ -28,3 +42,12 @@ namespace util {
2842
};
2943
}
3044

45+
inline namespace bitops {
46+
template<>
47+
struct enable_bitmask_ops<os::mem::Permission> {
48+
using type = typename std::underlying_type<os::mem::Permission>::type;
49+
static constexpr bool enable = true;
50+
};
51+
}
52+
}
53+
#endif // _SYS_MMAN_HPP

src/arch/i686/paging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ namespace mem {
3333
}
3434

3535
template <>
36-
const size_t Mapping<os::mem::Access>::any_size = 4096;
36+
const size_t Mapping<os::mem::Permission>::any_size = 4096;
3737
}
3838
}

src/arch/x86_64/ist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static stack create_stack_virt(size_t size, const char* name)
3838
// TODO randomize location / ask virtual memory allocator
3939
const uintptr_t stack_area = 1ull << 46;
4040

41-
const mem::Access flags = mem::Access::read | mem::Access::write;
41+
const mem::Permission flags = mem::Permission::Data;
4242

4343
// Virtual area
4444
// Adds a guard page between each new stack
@@ -53,7 +53,7 @@ static stack create_stack_virt(size_t size, const char* name)
5353

5454
Expects(map);
5555
Expects(mem::active_page_size(map.lin) == 4096);
56-
Expects(mem::flags(map.lin - 1) == mem::Access::none
56+
Expects(mem::flags(map.lin - 1) == mem::Permission::Any // TODO(mazunki): should this be Permission::None?
5757
&& "Guard page should not present");
5858

5959
// Next stack starts after next page

src/arch/x86_64/paging.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,42 +134,44 @@ void __arch_init_paging() {
134134
namespace x86 {
135135
namespace paging {
136136

137-
os::mem::Access to_memflags(Flags f)
137+
os::mem::Permission to_memflags(Flags f)
138138
{
139-
os::mem::Access prot = os::mem::Access::none;
139+
using Permission = os::mem::Permission;
140+
Permission prot = Permission::Any; // TODO(mazunki): should probably be 0 (or introduce Permission::Empty)
140141

141142
if (! has_flag(f, Flags::present)) {
142-
prot |= os::mem::Access::none;
143+
prot |= Permission::Any; // TODO(mazunki): should probably be Permission::None
143144
return prot;
144145
}
145146

146-
prot |= os::mem::Access::read;
147+
prot |= Permission::Read;
147148

148149
if (has_flag(f, Flags::writable)) {
149-
prot |= os::mem::Access::write;
150+
prot |= Permission::Write;
150151
}
151152

152153
if (! has_flag(f, Flags::no_exec)) {
153-
prot |= os::mem::Access::execute;
154+
prot |= Permission::Execute;
154155
}
155156

156157
return prot;
157158
}
158159

159-
Flags to_x86(os::mem::Access prot)
160+
Flags to_x86(os::mem::Permission prot) // TODO(mazunki): probably implement Any, RWX, None here
160161
{
162+
using Permission = os::mem::Permission;
161163
Flags flags = Flags::none;
162-
if (prot != os::mem::Access::none) {
164+
if (prot != Permission::Any) {
163165
flags |= Flags::present;
164166
} else {
165167
return Flags::none;
166168
}
167169

168-
if (has_flag(prot, os::mem::Access::write)) {
170+
if (has_flag(prot, Permission::Write)) {
169171
flags |= Flags::writable;
170172
}
171173

172-
if (not has_flag(prot, os::mem::Access::execute)) {
174+
if (not has_flag(prot, Permission::Execute)) {
173175
flags |= Flags::no_exec;
174176
}
175177

@@ -183,7 +185,7 @@ void invalidate(void *pageaddr){
183185

184186
}} // x86::paging
185187

186-
namespace os {
188+
namespace os { // TODO(mazunki): could it be worth moving this into `x86::paging::` instead?
187189
namespace mem {
188190

189191
using Map_x86 = Mapping<x86::paging::Flags>;
@@ -237,7 +239,15 @@ uintptr_t mem::virt_to_phys(uintptr_t linear)
237239
return __pml4->addr_of(*ent);
238240
}
239241

240-
os::mem::Access mem::protect_page(uintptr_t linear, Access flags)
242+
/*
243+
* TODO(mazunki):
244+
* might be better to rename this to set_protection(linear, flags),
245+
* and introduce permit_page() and prohibit_page() to add/remove permissions
246+
*
247+
* mprotect/protect_page() are misleading as we can use it to remove
248+
* protections of pages too
249+
*/
250+
os::mem::Permission mem::protect_page(uintptr_t linear, Permission flags)
241251
{
242252
MEM_PRINT("::protect_page 0x%lx\n", linear);
243253
x86::paging::Flags xflags = x86::paging::to_x86(flags);
@@ -246,7 +256,7 @@ os::mem::Access mem::protect_page(uintptr_t linear, Access flags)
246256
return to_memflags(f);
247257
};
248258

249-
os::mem::Access mem::protect_range(uintptr_t linear, Access flags)
259+
os::mem::Permission mem::protect_range(uintptr_t linear, Permission flags)
250260
{
251261
MEM_PRINT("::protect 0x%lx \n", linear);
252262
x86::paging::Flags xflags = x86::paging::to_x86(flags);
@@ -273,7 +283,7 @@ os::mem::Access mem::protect_range(uintptr_t linear, Access flags)
273283
return to_memflags(fl);
274284
};
275285

276-
os::mem::Map mem::protect(uintptr_t linear, size_t len, Access flags)
286+
os::mem::Map mem::protect(uintptr_t linear, size_t len, Permission flags)
277287
{
278288
if (UNLIKELY(len < min_psize()))
279289
mem_fail_fast("Can't map less than a page\n");
@@ -297,7 +307,7 @@ os::mem::Map mem::protect(uintptr_t linear, size_t len, Access flags)
297307
return to_mmap(res);
298308
}
299309

300-
os::mem::Access mem::flags(uintptr_t addr)
310+
os::mem::Permission mem::flags(uintptr_t addr)
301311
{
302312
return to_memflags(__pml4->flags_r(addr));
303313
}
@@ -356,7 +366,7 @@ os::mem::Map mem::unmap(uintptr_t lin){
356366
m.phys = 0;
357367
m.size = map_ent.size();
358368

359-
m = __pml4->map_r({key, 0, x86::paging::to_x86(Access::none), (size_t)map_ent.size()});
369+
m = __pml4->map_r({key, 0, x86::paging::to_x86(Permission::Any), (size_t)map_ent.size()}); // TODO(mazunki): this should maybe be Permission::None
360370

361371
Ensures(m.size == util::bits::roundto<4_KiB>(map_ent.size()));
362372
os::mem::vmmap().erase(key);
@@ -386,7 +396,7 @@ void allow_executable()
386396
m.phys = __exec_begin;
387397
m.size = exec_size;
388398
m.page_sizes = os::mem::Map::any_size;
389-
m.flags = os::mem::Access::execute | os::mem::Access::read;
399+
m.flags = os::mem::Permission::Code;
390400

391401
os::mem::map(m, "ELF .text");
392402
}

src/kernel/elf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,6 @@ void elf_protect_symbol_areas()
502502
{(uintptr_t) src, (uintptr_t) src + size-1, "Symbols & strings"});
503503

504504
INFO2("* Protecting syms %p to %p (size %#zx)", src, &src[size], size);
505-
os::mem::protect((uintptr_t) src, size, os::mem::Access::read);
505+
os::mem::protect((uintptr_t) src, size, os::mem::Permission::Read);
506506
}
507507
#endif

src/kernel/multiboot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void kernel::multiboot(uint32_t boot_addr)
164164
if (not (map.type & MULTIBOOT_MEMORY_AVAILABLE)) {
165165

166166
if (util::bits::is_aligned<4_KiB>(map.addr)) {
167-
os::mem::map({addr, addr, os::mem::Access::read | os::mem::Access::write, size},
167+
os::mem::map({addr, addr, os::mem::Permission::Data, size},
168168
"Reserved (Multiboot)");
169169
continue;
170170
}
@@ -175,7 +175,7 @@ void kernel::multiboot(uint32_t boot_addr)
175175
else
176176
{
177177
// Map as free memory
178-
//os::mem::map_avail({map.addr, map.addr, {os::mem::Access::read | os::mem::Access::write}, map.len}, "Reserved (Multiboot)");
178+
//os::mem::map_avail({map.addr, map.addr, {os::mem::Permission::Data}, map.len}, "Reserved (Multiboot)");
179179
}
180180
}
181181
INFO2("");

src/musl/mmap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ uintptr_t mmap_allocation_end() {
5757
static void* sys_mmap(void * addr, size_t length, int /*prot*/, int _flags,
5858
int fd, off_t /*offset*/)
5959
{
60-
using os::mmap::Flags;
60+
using os::mem::Flags;
6161
const Flags flags = static_cast<Flags>(_flags);
6262

6363
// TODO: Implement minimal functionality to be POSIX compliant

src/platform/x86_pc/os.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void kernel::start(uint32_t boot_magic, uint32_t boot_addr)
139139
#if defined(ARCH_x86_64)
140140
// protect the basic pagetable used by LiveUpdate and any other
141141
// systems that need to exit long/protected mode
142-
os::mem::map({0x1000, 0x1000, os::mem::Access::read, 0x7000}, "Page tables");
142+
os::mem::map({0x1000, 0x1000, os::mem::Permission::Read, 0x7000}, "Page tables");
143143
memmap.assign_range({0x10000, 0x9d3ff, "Stack"});
144144
#elif defined(ARCH_i686)
145145
memmap.assign_range({0x10000, 0x9d3ff, "Stack"});

0 commit comments

Comments
 (0)