};
// Copy kernel mappings to the new virtual address space.
- for (size_t i = 0; i < 256; i++) {
- result.PML4T->contents[256 + i] = original_PML4T.contents[256 + i];
- }
+ memcpy(result.PML4T->contents + 256, original_PML4T.contents + 256, 256 * sizeof(os::paging::PML4E));
for (std::size_t i = 0; i < elf_header.entry_count_program_header_table; i++) {
const os::elf::program_header program_header = *(os::elf::program_header*)(
for (std::size_t i = 0; i < nb_pages; i++) {
std::byte* const page =
os::paging::setup_page(*result.PML4T, program_header.p_vaddr + i * 0x1000, (program_header.flags & 2) >> 1, 1);
- for (std::size_t j = 0; j < 0x1000 && j < program_header.p_filesz - i * 0x1000; j++) {
- page[j] = ((std::byte*)start)[program_header.p_offset + j];
- }
+ memcpy(page, (std::byte*)start + program_header.p_offset, clamp(0ul, program_header.p_filesz - i * 0x1000, 0x1000ul));
}
}
T* operator->() const {
return get_virt_addr();
}
+ explicit operator T*() const {
+ return get_virt_addr();
+ }
+ explicit operator void*() const {
+ return get_virt_addr();
+ }
constexpr phys_ptr<T>& operator++() {
return *this += 1;
for (auto it = begin(); it != end(); it = it->next) {
if (count == it->size) {
erase(it);
- for (std::size_t i = 0; i < count * 0x1000; i++) {
- phys_ptr<char>(it.get_phys_addr())[i] = 0;
- }
+ memset((void*)it, 0, count * 0x1000);
return {
.ptr = phys_ptr<paging::page>(it.get_phys_addr()),
.size = count,
};
} else if (count < it->size) {
erase(split_at_offset(it, count));
- for (std::size_t i = 0; i < count * 0x1000; i++) {
- phys_ptr<char>(it.get_phys_addr())[i] = 0;
- }
+ memset((void*)it, 0, count * 0x1000);
return {
.ptr = phys_ptr<paging::page>(it.get_phys_addr()),
.size = count,
#pragma once
#include <cstdint>
+#include <cstddef>
namespace os {
bool cpu_has_msr();
std::uint64_t get_msr(std::uint32_t msr);
void set_msr(std::uint32_t msr, std::uint64_t v);
+template <typename T> T clamp(T min, T v, T max) {
+ return v < min ? min : max < v ? max : v;
+}
+inline void* memset(void* dest, int c, size_t n) {
+ while (n-- > 0) {
+ reinterpret_cast<std::byte*>(dest)[n] = std::byte(c);
+ }
+ return dest;
+}
+inline void* memcpy(void* dest, const void* src, size_t n) {
+ while (n-- > 0) {
+ reinterpret_cast<std::byte*>(dest)[n] = reinterpret_cast<const std::byte*>(src)[n];
+ }
+ return dest;
+}
} // namespace os