#include "elf64.hpp"
void os::elf::load_elf(os::process& result, amy::byte* start, amy::size length, const paging::PML4T& original_PML4T) {
- os::assert(length >= 0 && (unsigned long long)(length) >= sizeof(os::elf::header), "Elf file isn't big enough to contain a header: there is an error.");
+ os::assert(length >= amy::byte_size<os::elf::header>(), "Elf file isn't big enough to contain a header: there is an error.");
// TODO: Check that the elf file sections are all fully inside the file.
// Load test-module elf file:
result.r15 = 0;
// Copy kernel mappings to the new virtual address space.
- memset(result.PML4T->contents, 0, 256 * sizeof(os::paging::PML4E));
- memcpy(result.PML4T->contents + 256, original_PML4T.contents + 256, 256 * sizeof(os::paging::PML4E));
+ memset(result.PML4T->contents, 0, 256 * amy::byte_size<os::paging::PML4E>());
+ memcpy(result.PML4T->contents + 256, original_PML4T.contents + 256, 256 * amy::byte_size<os::paging::PML4E>());
for (amy::size i = 0; i < elf_header.entry_count_program_header_table; i++) {
const os::elf::program_header program_header = *(os::elf::program_header*)(
amy::u16 entry_count_section_header_table;
amy::u16 index_section_names_section_header_table;
};
- static_assert(sizeof(header) == 64);
+ static_assert(amy::byte_size<header>() == 64);
struct program_header {
amy::u32 type;
amy::u64 p_memsz;
amy::u64 align;
};
- static_assert(sizeof(program_header) == 56);
+ static_assert(amy::byte_size<program_header>() == 56);
void load_elf(os::process& result, amy::byte* start, amy::size length, const paging::PML4T& original_PML4T);
} } // namespace os::elf
outb(0x21, 0xFF);
}
void os::set_APIC_base(os::phys_ptr<volatile amy::u32> ptr) {
- os::set_msr(0x1B, (ptr.get_phys_addr() & 0xFFFFFFFFFFFFF000) | 0x800);
+ os::set_msr(0x1B, (ptr.get_phys_addr() & 0xFFFF'FFFF'FFFF'F000) | 0x800);
}
os::phys_ptr<volatile amy::u32> os::get_APIC_base() {
- return os::phys_ptr<volatile amy::u32>{os::get_msr(0x1B) & 0xFFFFFFFFFFFFF000};
+ return os::phys_ptr<volatile amy::u32>{amy::ptr(os::get_msr(0x1B)) & amy::ptr(0xFFFF'FFFF'FFFF'F000)};
}
-amy::u32 os::get_APIC_reg(amy::dptr offset) {
+amy::u32 os::get_APIC_reg(amy::diff offset) {
return get_APIC_base()[offset / 4];
}
-void os::set_APIC_reg(amy::dptr offset, amy::u32 v) {
+void os::set_APIC_reg(amy::diff offset, amy::u32 v) {
get_APIC_base()[offset / 4] = v;
}
-extern "C" void set_APIC_reg_asm(amy::dptr offset, amy::u32 v) {
+extern "C" void set_APIC_reg_asm(amy::diff offset, amy::u32 v) {
os::set_APIC_reg(offset, v);
}
amy::u32 offset_high;
amy::u32 reserved_2 = 0;
};
-static_assert(sizeof(ide) == 16);
+static_assert(amy::byte_size<ide>() == 16);
template <amy::size interrupt_nb>
using idt __attribute__((aligned(0x1000))) = ide[interrupt_nb];
struct __attribute__((packed)) idtr {
amy::u16 size;
- amy::u64 offset;
+ amy::ptr offset;
};
void lidt(idtr idtr);
void disable_PIC();
void set_APIC_base(phys_ptr<volatile amy::u32> ptr);
phys_ptr<volatile amy::u32> get_APIC_base();
-amy::u32 get_APIC_reg(amy::dptr offset);
-void set_APIC_reg(amy::dptr offset, amy::u32 v);
+amy::u32 get_APIC_reg(amy::diff offset);
+void set_APIC_reg(amy::diff offset, amy::u32 v);
struct isr_info {
- amy::uptr addr;
+ amy::ptr addr;
enum class type_t { trap_gate = 0xF, interrupt_gate = 0xE };
type_t type : 4;
amy::u8 dpl: 2 = 0b00;
}
os::idtr idtr {
- .size = sizeof(idt) - 1,
- .offset = reinterpret_cast<amy::uptr>(&idt[0]),
+ .size = amy::byte_size<decltype(idt)>() - 1,
+ .offset = reinterpret_cast<amy::ptr>(&idt[0]),
};
lidt(idtr);
sti();
os::assert(magic == 0x36D76289, "Incorrect magic number: wasn't booted with multiboot2.");
os::paging::page_allocator.deallocate({
- .ptr = os::phys_ptr<os::paging::page<0>>(reinterpret_cast<uintptr_t>(bootstrap_pages_for_memory) - 0xFFFF'FFFF'8000'0000),
- .size = sizeof(bootstrap_pages_for_memory) / sizeof(bootstrap_pages_for_memory[0])
+ .ptr = os::phys_ptr<os::paging::page<0>>(amy::ptr(&bootstrap_pages_for_memory) & amy::ptr(~0xFFFF'FFFF'8000'0000)),
+ .size = amy::byte_size<decltype(bootstrap_pages_for_memory)>() / amy::byte_size<decltype(bootstrap_pages_for_memory[0])>()
});
os::assert(os::cpu_has_msr(), "MSRs aren't supported.");
// Initialise kernel map.
- os::assert(amy::iptr(&_kernel_phys_start) % 4096 == 0, "Kernel isn't page aligned !");
- os::assert(amy::iptr(&_kernel_phys_rw_start) % 4096 == 0, "Kernel isn't page aligned !");
- os::assert(amy::iptr(&_kernel_phys_end) % 4096 == 0, "Kernel isn't page aligned !");
- for (amy::size i = amy::iptr(&_kernel_phys_start); i < amy::iptr(&_kernel_phys_rw_start); ) {
- if (i % (1024 * 1024 * 1024) == 0 && i + 1024 * 1024 * 1024 < amy::iptr(&_kernel_phys_rw_start)) {
+ os::assert(amy::ptr(&_kernel_phys_start) % 4096 == 0, "Kernel isn't page aligned !");
+ os::assert(amy::ptr(&_kernel_phys_rw_start) % 4096 == 0, "Kernel isn't page aligned !");
+ os::assert(amy::ptr(&_kernel_phys_end) % 4096 == 0, "Kernel isn't page aligned !");
+ for (amy::size i = amy::ptr(&_kernel_phys_start); i < amy::ptr(&_kernel_phys_rw_start); ) {
+ if (i % (1024 * 1024 * 1024) == 0 && i + 1024 * 1024 * 1024 < amy::ptr(&_kernel_phys_rw_start)) {
os::paging::map_page<2, 3>(os::paging::global_PML4T, (os::paging::page<2>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<2>>(i),
{ .RW = false, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = false });
i += 1024 * 1024 * 1024;
- } else if (i % (1024 * 1024 * 2) == 0 && i + 1024 * 1024 * 2 < amy::iptr(&_kernel_phys_rw_start)) {
+ } else if (i % (1024 * 1024 * 2) == 0 && i + 1024 * 1024 * 2 < amy::ptr(&_kernel_phys_rw_start)) {
os::paging::map_page<1, 3>(os::paging::global_PML4T, (os::paging::page<1>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<1>>(i),
{ .RW = false, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = false });
i += 1024 * 1024 * 2;
i += 1024 * 4;
}
}
- for (amy::size i = amy::iptr(&_kernel_phys_rw_start); i < amy::iptr(&_kernel_phys_end); ) {
- if (i % (1024 * 1024 * 1024) == 0 && i + 1024 * 1024 * 1024 < amy::iptr(&_kernel_phys_rw_start)) {
+ for (amy::size i = amy::ptr(&_kernel_phys_rw_start); i < amy::ptr(&_kernel_phys_end); ) {
+ if (i % (1024 * 1024 * 1024) == 0 && i + 1024 * 1024 * 1024 < amy::ptr(&_kernel_phys_rw_start)) {
os::paging::map_page<2, 3>(os::paging::global_PML4T, (os::paging::page<2>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<2>>(i),
{ .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = true });
i += 1024 * 1024 * 1024;
- } else if (i % (1024 * 1024 * 2) == 0 && i + 1024 * 1024 * 2 < amy::iptr(&_kernel_phys_rw_start)) {
+ } else if (i % (1024 * 1024 * 2) == 0 && i + 1024 * 1024 * 2 < amy::ptr(&_kernel_phys_rw_start)) {
os::paging::map_page<1, 3>(os::paging::global_PML4T, (os::paging::page<1>*)(0xFFFF'FFFF'8000'0000 + i), os::phys_ptr<os::paging::page<1>>(i),
{ .RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .D = false, .PAT = false, .G = true, .AVL = 0, .NX = true });
i += 1024 * 1024 * 2;
case multiboot2::info::type_t::memory_map:
os::print("RAM:\n");
for (amy::size i = 0; i < multiboot2::memory_map_number_of_entries(it); i++) {
- amy::iptr const s = multiboot2::memory_map_base_addr(it, i);
- amy::iptr const e = s + multiboot2::memory_map_length(it, i);
+ amy::ptr const s = multiboot2::memory_map_base_addr(it, i);
+ amy::ptr const e = s + multiboot2::memory_map_length(it, i);
os::print("{} -> {} : {}\n", (void*)s, (void*)e, multiboot2::memory_map_type(it, i));
if (multiboot2::memory_map_type(it, i) != 1) {
continue;
}
os::assert(s % 0x1000 == 0, "Memory map unaligned on page boundaries.");
os::assert(e % 0x1000 == 0, "Memory map unaligned on page boundaries.");
- for (amy::iptr j = s; j < e; ) {
+ for (amy::ptr j = s; j < e; ) {
if (j % (1024 * 1024 * 1024) == 0 && j + 1024 * 1024 * 1024 < e) {
os::paging::map_page<2, 3>(
os::paging::global_PML4T,
}
}
// /TODO
- os::paging::load_pml4t(os::phys_ptr<os::paging::PML4T>{amy::uptr(&os::paging::global_PML4T) - 0xFFFF'FFFF'8000'0000});
+ os::paging::load_pml4t(os::phys_ptr<os::paging::PML4T>{amy::ptr(&os::paging::global_PML4T) & amy::ptr(~0xFFFF'FFFF'8000'0000)});
{ // Enable interrupts really early so I don't have to manually manage memory... Will make better later, when I make utils.hpp/incrementing_int64_map better.
os::isr_info isr_info[32];
- for (amy::size i = 0; (unsigned long long)(i) < sizeof(isr_info) / sizeof(isr_info[0]); i++) {
+ for (amy::size i = 0; i < amy::byte_size<decltype(isr_info)>() / amy::byte_size<decltype(isr_info[0])>(); i++) {
isr_info[i].type = os::isr_info::type_t::trap_gate;
}
asm ("movq $handler_0x00,%0" : "=ri"(isr_info[0x00].addr) : );
// Allocate those pages so that I only ever need to update the mapping once when I create a new process or port.
// TODO: Not an emergency, but adapt in case we need multiple PML4Es. (*NOT* the case right now.)
- static_assert(sizeof(os::processes) <= 1024 * 1024 * 1024, "Error: processes array too big.");
+ static_assert(amy::byte_size<decltype(os::processes)>() <= 1024 * 1024 * 1024, "Error: processes array too big.");
{
amy::size const index = (amy::size(&os::processes) >> (12 + 9 * 3)) & 0x1FF;
os::paging::global_PML4T.contents[index].paging_table_info({.RW = true, .US = false, .PWT = false, .PCD = false, .A = false, .AVL = 0, .NX = false});
case multiboot2::info::type_t::modules:
os::print("{}->{}: {}\n", multiboot2::modules_mod_start(it), multiboot2::modules_mod_end(it), multiboot2::modules_string(it));
module_specified = true;
- os::elf::load_elf(os::get_process(os::processes.create()),
+ os::elf::load_elf(os::get_process(amy::pid(os::processes.create())),
(amy::byte*)multiboot2::modules_mod_start(it),
multiboot2::modules_mod_end(it) - multiboot2::modules_mod_start(it),
os::paging::global_PML4T);
os::assert(module_specified, "No modules specified in the multiboot. This is unsupported.");
// kernel_start and kernel_end are aligned to 4K by the linker script.
- const os::phys_ptr<os::paging::page<0>> kernel_s{amy::uptr(&_kernel_phys_start)};
- const os::phys_ptr<os::paging::page<0>> kernel_e{amy::uptr(&_kernel_phys_end)};
+ const os::phys_ptr<os::paging::page<0>> kernel_s{amy::ptr(&_kernel_phys_start)};
+ const os::phys_ptr<os::paging::page<0>> kernel_e{amy::ptr(&_kernel_phys_end)};
// Remove kernel from available RAM:
- for (amy::iptr i = 0; i < available_ram_length; i++) {
+ for (amy::ptr i = 0; i < available_ram_length; i++) {
if (kernel_e < available_ram[i].start_address || available_ram[i].end_address < kernel_s) {
continue;
}
}
// Add available RAM to the page allocator (warning: overrides the multiboot info structure and the multiboot modules):
- for (amy::iptr i = 0; i < available_ram_length; i++) {
+ for (amy::ptr i = 0; i < available_ram_length; i++) {
os::paging::page_allocator.deallocate({
.ptr = available_ram[i].start_address,
.size = amy::size(available_ram[i].end_address - available_ram[i].start_address + 1)
os::enable_syscalls();
os::print("Moving to ring 3.\n");
- os::run_first_process(0);
+ os::run_first_process(amy::pid(0));
}
inline amy::size memory_map_number_of_entries(os::phys_ptr<const info> ptr) {
return (ptr->size - 16) / memory_map_entry_size(ptr);
}
- inline amy::u64 memory_map_base_addr(os::phys_ptr<const info> ptr, amy::size index) {
- return *reinterpret_cast<const amy::u64*>(&ptr->rest[8 + memory_map_entry_size(ptr) * index + 0]);
+ inline amy::ptr memory_map_base_addr(os::phys_ptr<const info> ptr, amy::size index) {
+ return *reinterpret_cast<const amy::ptr*>(&ptr->rest[8 + memory_map_entry_size(ptr) * index + 0]);
}
inline amy::u64 memory_map_length(os::phys_ptr<const info> ptr, amy::size index) {
return *reinterpret_cast<const amy::u64*>(&ptr->rest[8 + memory_map_entry_size(ptr) * index + 8]);
#pragma once
#include <types.hpp>
+#include <memory.hpp>
namespace os {
template <>
class phys_ptr<void> {
public:
- constexpr explicit phys_ptr(amy::uptr phys_addr): phys_addr(phys_addr) {}
- constexpr phys_ptr(amy::nptr): phys_addr(~0ull) {}
+ constexpr explicit phys_ptr(amy::ptr phys_addr): phys_addr(phys_addr) {}
+ constexpr phys_ptr(amy::nil_t): phys_addr(0xF000'0000'0000'0000) {}
inline void* operator->() const {
return get_virt_addr();
return get_virt_addr();
}
- constexpr amy::uptr get_phys_addr() const {
+ constexpr amy::ptr get_phys_addr() const {
return phys_addr;
}
return reinterpret_cast<void*>(phys_addr + 0xFFFF800000000000);
}
- amy::uptr phys_addr;
+ amy::ptr phys_addr;
};
template <typename T>
class phys_ptr {
public:
- constexpr explicit phys_ptr(amy::uptr phys_addr): phys_addr(phys_addr) {}
- constexpr phys_ptr(amy::nptr): phys_addr(~0ull) {}
+ constexpr explicit phys_ptr(amy::ptr phys_addr): phys_addr(phys_addr) {}
+ constexpr phys_ptr(amy::nil_t): phys_addr(0xF000'0000'0000'0000) {}
- T& operator[](amy::size i) const {
+ T& operator[](amy::diff i) const {
return get_virt_addr()[i];
}
T& operator*() const {
operator--();
return old;
}
- constexpr phys_ptr<T>& operator+=(amy::dptr offset) {
+ constexpr phys_ptr<T>& operator+=(amy::diff offset) {
return *this = *this + offset;
}
- constexpr phys_ptr<T>& operator-=(amy::dptr offset) {
+ constexpr phys_ptr<T>& operator-=(amy::diff offset) {
return *this = *this - offset;
}
- friend constexpr phys_ptr<T> operator+(phys_ptr<T> ptr, amy::dptr offset) {
- return phys_ptr<T>{ptr.phys_addr + offset * sizeof(T)};
+ friend constexpr phys_ptr<T> operator+(phys_ptr<T> ptr, amy::diff offset) {
+ return phys_ptr<T>{ptr.phys_addr + offset * amy::byte_size<T>()};
}
- friend constexpr phys_ptr<T> operator+(amy::dptr offset, phys_ptr<T> ptr) {
+ friend constexpr phys_ptr<T> operator+(amy::diff offset, phys_ptr<T> ptr) {
return ptr + offset;
}
- friend constexpr phys_ptr<T> operator-(phys_ptr<T> ptr, amy::dptr offset) {
- return phys_ptr<T>{ptr.phys_addr - offset * sizeof(T)};
+ friend constexpr phys_ptr<T> operator-(phys_ptr<T> ptr, amy::diff offset) {
+ return phys_ptr<T>{ptr.phys_addr - offset * amy::byte_size<T>()};
}
- friend constexpr amy::dptr operator-(phys_ptr<T> a, phys_ptr<T> b) {
- return (a.phys_addr - b.phys_addr) / sizeof(T);
+ friend constexpr amy::diff operator-(phys_ptr<T> a, phys_ptr<T> b) {
+ return (a.phys_addr - b.phys_addr) / amy::byte_size<T>();
}
- constexpr amy::uptr get_phys_addr() const {
+ constexpr amy::ptr get_phys_addr() const {
return phys_addr;
}
return reinterpret_cast<T*>(phys_addr + 0xFFFF800000000000);
}
- amy::uptr phys_addr;
+ amy::ptr phys_addr;
};
} // namespace os
}
os::phys_ptr<os::paging::page<depth>> page_ptr() {
os::assert(is_page(), "Cannot access page physical pointer for a non-page.");
- amy::uptr addr = data & (depth == 0 ? 0x0000'FFFF'FFFF'F000 : 0x0000'FFFF'FFFF'E000);
+ amy::ptr addr = data & (depth == 0 ? 0x0000'FFFF'FFFF'F000 : 0x0000'FFFF'FFFF'E000);
return os::phys_ptr<os::paging::page<depth>>(addr | ((addr & 0x0000'8000'0000'0000) != 0 ? 0xFFFF'8000'0000'0000 : 0));
}
void page_ptr(os::phys_ptr<os::paging::page<depth>> page) {
}
os::phys_ptr<os::paging::paging_table<depth - 1>> paging_table_ptr() {
os::assert(!is_page(), "Cannot access paging table physical pointer for a page.");
- amy::uptr addr = data & 0x0000'FFFF'FFFF'F000;
+ amy::ptr addr = data & 0x0000'FFFF'FFFF'F000;
return os::phys_ptr<os::paging::paging_table<depth - 1>>(addr | ((addr & 0x0000'8000'0000'0000) != 0 ? 0xFFFF'8000'0000'0000 : 0));
}
void paging_table_ptr(os::phys_ptr<os::paging::paging_table<depth - 1>> table) {
amy::byte contents[0x1000ull << (9 * depth)];
};
-static_assert(sizeof(page<0>) == 0x1000);
-static_assert(sizeof(page<1>) == 0x1000 * 512);
-static_assert(sizeof(page<2>) == 0x1000ull * 512 * 512);
-static_assert(sizeof(paging_table<3>) == 0x1000);
+static_assert(amy::byte_size<page<0>>() == 0x1000);
+static_assert(amy::byte_size<page<1>>() == 0x1000 * 512);
+static_assert(amy::byte_size<page<2>>() == 0x1000ull * 512 * 512);
+static_assert(amy::byte_size<paging_table<3>>() == 0x1000);
static_assert(alignof(paging_table<3>) == 0x1000);
static_assert(alignof(paging_entry<3>) == 8);
-static_assert(sizeof(paging_entry<3>) == 8);
-static_assert(sizeof(paging_table<2>) == 0x1000);
+static_assert(amy::byte_size<paging_entry<3>>() == 8);
+static_assert(amy::byte_size<paging_table<2>>() == 0x1000);
static_assert(alignof(paging_table<2>) == 0x1000);
static_assert(alignof(paging_entry<2>) == 8);
-static_assert(sizeof(paging_entry<2>) == 8);
-static_assert(sizeof(paging_table<1>) == 0x1000);
+static_assert(amy::byte_size<paging_entry<2>>() == 8);
+static_assert(amy::byte_size<paging_table<1>>() == 0x1000);
static_assert(alignof(paging_table<1>) == 0x1000);
static_assert(alignof(paging_entry<1>) == 8);
-static_assert(sizeof(paging_entry<1>) == 8);
-static_assert(sizeof(paging_table<0>) == 0x1000);
+static_assert(amy::byte_size<paging_entry<1>>() == 8);
+static_assert(amy::byte_size<paging_table<0>>() == 0x1000);
static_assert(alignof(paging_table<0>) == 0x1000);
static_assert(alignof(paging_entry<0>) == 8);
-static_assert(sizeof(paging_entry<0>) == 8);
+static_assert(amy::byte_size<paging_entry<0>>() == 8);
using PML4T = paging_table<3>;
using PML4E = paging_entry<3>;
template <amy::size depth, amy::size paging_depth = 3>
void map_page(paging_table<paging_depth>& paging_table, page<depth> const* vaddr, phys_ptr<page<depth>> paddr, os::paging::page_info page_info);
template <amy::size depth>
-void on_all_pages(const paging_table<depth>& paging_table, auto f, amy::uptr vaddr_offset = 0);
+void on_all_pages(const paging_table<depth>& paging_table, auto f, amy::diff vaddr_offset = 0);
void load_pml4t(phys_ptr<PML4T> PML4T);
phys_ptr<page> next;
amy::size size;
};
- static_assert(sizeof(page) == 0x1000);
+ static_assert(amy::byte_size<page>() == 0x1000);
static_assert(alignof(page) == 0x1000);
phys_ptr<page> begin;
template <amy::size depth, amy::size paging_depth>
void map_page(paging_table<paging_depth>& paging_table, page<depth> const* vaddr, phys_ptr<page<depth>> paddr, os::paging::page_info page_info) {
- amy::size const index = (amy::uptr(vaddr) >> (12 + 9 * paging_depth)) & 0x1FF;
+ amy::size const index = (amy::ptr(vaddr) >> (12 + 9 * paging_depth)) & 0x1FF;
if constexpr (depth == paging_depth) {
os::assert(!paging_table.contents[index].is_present(), "Virtual address 0x{} is already mapped.", vaddr);
paging_table.contents[index].page_info(page_info);
}
template <amy::size depth>
-void on_all_pages(const paging_table<depth>& paging_table, auto f, amy::uptr vaddr_offset) {
+void on_all_pages(const paging_table<depth>& paging_table, auto f, amy::diff vaddr_offset) {
for (amy::size i = 0; i < 512; i++) {
if (!is_present(paging_table.contents[i])) {
continue;
}
- amy::uptr const new_offset = ([&]() {
- amy::uptr const new_offset = vaddr_offset + (i << (12 + 9 * depth));
+ amy::diff const new_offset = ([&]() {
+ amy::diff const new_offset = vaddr_offset + (i << (12 + 9 * depth));
return new_offset | (new_offset >> 47 ? 0xFFFF'8000'0000'0000 : 0);
})();
if constexpr (depth == 3) {
#include "serial.hpp"
#include "paging.hpp"
-void os::run_first_process(amy::i64 pid) {
+void os::run_first_process(amy::pid pid) {
current_pid = pid;
os::paging::load_pml4t(os::get_process(current_pid).PML4T);
os::ftl_to_userspace((void*)os::get_process(current_pid).rip, (amy::byte*)os::get_process(current_pid).rsp);
}
void schedule_next_process() {
- if (os::processes.present(os::current_pid + 1)) {
- os::current_pid += 1;
+ if (os::processes.present(amy::i64(os::current_pid) + 1)) {
+ os::current_pid = amy::pid(amy::i64(os::current_pid) + 1);
} else {
- os::current_pid = 0;
+ os::current_pid = amy::pid(0);
}
- os::paging::load_pml4t(os::processes.get(os::current_pid).PML4T);
+ os::paging::load_pml4t(os::processes.get(amy::i64(os::current_pid)).PML4T);
}
// extern "C" int os::syscall_mount_kernel_device(amy::fd wd, char device, char const* path, amy::size path_len, int mount_type);
}
extern "C" amy::size os::syscall_read(amy::fd file, char* data, amy::size len) {
- os::assert(file == 0, "Read isn't really implemented for now.");
+ os::assert(file == amy::fd(0), "Read isn't really implemented for now.");
os::assert(len >= 0, "Read expects a positive size.");
if (len == 0) {
return 0;
}
extern "C" amy::size os::syscall_write(amy::fd file, char const* data, amy::size len) {
- os::assert(file == 0, "Write isn't really implemented for now.");
+ os::assert(file == amy::fd(0), "Write isn't really implemented for now.");
os::assert(len >= 0, "Write expects a positive size.");
if (len == 0) {
return 0;
os::incrementing_int64_map<os::process>& os::processes = *reinterpret_cast<os::incrementing_int64_map<os::process>*>(0xFFFF'C000'0000'0000);
amy::pid os::current_pid;
os::process& os::get_process(amy::pid pid) {
- return processes.get(pid);
+ return processes.get(amy::i64(pid));
}
struct process;
extern "C" void ftl_to_userspace(void* program, amy::byte* stack);
-void run_first_process(amy::i64 pid);
+void run_first_process(amy::pid pid);
extern "C" void load_tss();
extern "C" void syscall_64bit_handler();
incrementing_int64_map<port> ports;
};
-static_assert(0xFFFF'C000'0000'0000 + sizeof(incrementing_int64_map<process>) < 0xFFFF'FFFF'8000'0000);
+static_assert(0xFFFF'C000'0000'0000 + amy::byte_size<incrementing_int64_map<process>>() < 0xFFFF'FFFF'8000'0000);
extern incrementing_int64_map<process>& processes;
extern "C" amy::pid current_pid;
-extern "C" process& get_process(amy::i64 pid);
+extern "C" process& get_process(amy::pid pid);
} // namespace os
os::assert(format[0] == '}', "Format string unsupported. TODO.");
os::print("0x");
for (int i = 60; i >= 0; i -= 4) {
- const int v = (reinterpret_cast<amy::uptr>(val) >> i) & 0xF;
+ const int v = (reinterpret_cast<amy::ptr>(val) >> i) & 0xF;
os::printc(v < 10 ? v + '0' : v - 10 + 'a');
}
}
private:
struct elem_t {
bool present;
- alignas(T) amy::byte buffer[sizeof(T)];
+ alignas(T) amy::byte buffer[amy::byte_size<T>()];
};
amy::i64 n = 0;
static constexpr amy::i64 max_n = 4096;
namespace amy {
-// Taken from glibcxx, should be fine copyright-wise:
+// "Launder" taken from glibcxx, should be fine copyright-wise:
template <typename T>
[[nodiscard]] constexpr T* launder(T* p) noexcept {
return __builtin_launder(p);
}
+
+
+template <typename T> constexpr size byte_size() {
+ return sizeof(T);
+}
+template <typename T> constexpr size byte_align() {
+ return alignof(T);
+}
+
} // namespace amy
using i64 = int64_t;
using u64 = uint64_t;
-using iptr = intptr_t;
-using uptr = uintptr_t;
-using dptr = __PTRDIFF_TYPE__;
-using nptr = decltype(nullptr);
+using ptr = intptr_t;
+constexpr auto nil = nullptr;
+using nil_t = decltype(nil);
using size = i64;
+using diff = i64;
+static_assert(sizeof(diff) >= sizeof(__PTRDIFF_TYPE__));
-using pid = i64;
-using fd = i64;
+enum class pid : i64 {};
+enum class fd : i64 {};
} // namespace amy
extern "C" void loop_de_loop();
extern "C" void _start() {
- sys::mount_kernel_device(-1, 'I', "/", 1, MOUNT_REPLACE); // 'I' is the I/O device. /serial is the serial port /fb is the framebuffer
- const amy::fd serial = sys::open(-1, "/serial", 7, MODE_WRITE);
+ sys::mount_kernel_device(amy::fd(-1), 'I', "/", 1, MOUNT_REPLACE); // 'I' is the I/O device. /serial is the serial port /fb is the framebuffer
+ const amy::fd serial = sys::open(amy::fd(-1), "/serial", 7, MODE_WRITE);
sys::write(serial, "Entrez votre nom: ", 18);
char data[32];