diff --git a/src/kernel/console.cpp b/src/kernel/console.cpp index 0b3c89a..4353700 100644 --- a/src/kernel/console.cpp +++ b/src/kernel/console.cpp @@ -1,6 +1,6 @@ -#include "kutil/memory.h" #include "kutil/no_construct.h" #include "printf/printf.h" + #include "console.h" #include "serial.h" diff --git a/src/kernel/cpu.cpp b/src/kernel/cpu.cpp index ae8ed55..24d3c84 100644 --- a/src/kernel/cpu.cpp +++ b/src/kernel/cpu.cpp @@ -1,6 +1,6 @@ #include #include "kutil/assert.h" -#include "kutil/memory.h" + #include "cpu.h" #include "cpu/cpu_id.h" #include "device_manager.h" diff --git a/src/kernel/device_manager.cpp b/src/kernel/device_manager.cpp index 48099a3..96cfdb5 100644 --- a/src/kernel/device_manager.cpp +++ b/src/kernel/device_manager.cpp @@ -2,7 +2,7 @@ #include #include "kutil/assert.h" -#include "kutil/memory.h" + #include "acpi_tables.h" #include "apic.h" #include "clock.h" @@ -11,6 +11,7 @@ #include "interrupts.h" #include "kernel_memory.h" #include "log.h" +#include "memory.h" #include "objects/endpoint.h" #include "serial.h" @@ -47,7 +48,7 @@ struct acpi2_rsdp bool acpi_table_header::validate(uint32_t expected_type) const { - if (kutil::checksum(this, length) != 0) return false; + if (::checksum(this, length) != 0) return false; return !expected_type || (expected_type == type); } @@ -82,7 +83,7 @@ device_manager::parse_acpi(const void *root_table) kassert(acpi1->signature[i] == expected_signature[i], "ACPI RSDP table signature mismatch"); - uint8_t sum = kutil::checksum(acpi1, sizeof(acpi1_rsdp), 0); + uint8_t sum = checksum(acpi1, sizeof(acpi1_rsdp), 0); kassert(sum == 0, "ACPI 1.0 RSDP checksum mismatch."); kassert(acpi1->revision > 1, "ACPI 1.0 not supported."); @@ -90,7 +91,7 @@ device_manager::parse_acpi(const void *root_table) const acpi2_rsdp *acpi2 = reinterpret_cast(acpi1); - sum = kutil::checksum(acpi2, sizeof(acpi2_rsdp), sizeof(acpi1_rsdp)); + sum = checksum(acpi2, sizeof(acpi2_rsdp), sizeof(acpi1_rsdp)); kassert(sum == 0, "ACPI 2.0 RSDP checksum mismatch."); load_xsdt(memory::to_virtual(acpi2->xsdt_address)); @@ -212,8 +213,8 @@ device_manager::load_apic(const acpi_table_header *header) switch (type) { case 0: { // Local APIC - uint8_t uid = kutil::read_from(p+2); - uint8_t id = kutil::read_from(p+3); + uint8_t uid = read_from(p+2); + uint8_t id = read_from(p+3); m_apic_ids.append(id); log::debug(logs::device, " Local APIC uid %x id %x", uid, id); @@ -221,8 +222,8 @@ device_manager::load_apic(const acpi_table_header *header) break; case 1: { // I/O APIC - uintptr_t base = kutil::read_from(p+4); - uint32_t base_gsi = kutil::read_from(p+8); + uintptr_t base = read_from(p+4); + uint32_t base_gsi = read_from(p+8); m_ioapics.emplace(base, base_gsi); log::debug(logs::device, " IO APIC gsi %x base %x", base_gsi, base); @@ -231,9 +232,9 @@ device_manager::load_apic(const acpi_table_header *header) case 2: { // Interrupt source override irq_override o; - o.source = kutil::read_from(p+3); - o.gsi = kutil::read_from(p+4); - o.flags = kutil::read_from(p+8); + o.source = read_from(p+3); + o.gsi = read_from(p+4); + o.flags = read_from(p+8); m_overrides.append(o); log::debug(logs::device, " Intr source override IRQ %d -> %d Pol %d Tri %d", @@ -243,9 +244,9 @@ device_manager::load_apic(const acpi_table_header *header) case 4: {// LAPIC NMI apic_nmi nmi; - nmi.cpu = kutil::read_from(p + 2); - nmi.lint = kutil::read_from(p + 5); - nmi.flags = kutil::read_from(p + 3); + nmi.cpu = read_from(p + 2); + nmi.lint = read_from(p + 5); + nmi.flags = read_from(p + 3); m_nmis.append(nmi); log::debug(logs::device, " LAPIC NMI Proc %02x LINT%d Pol %d Tri %d", diff --git a/src/kernel/frame_allocator.cpp b/src/kernel/frame_allocator.cpp index 217a0ca..c4698ea 100644 --- a/src/kernel/frame_allocator.cpp +++ b/src/kernel/frame_allocator.cpp @@ -1,5 +1,4 @@ #include "kutil/assert.h" -#include "kutil/memory.h" #include "frame_allocator.h" #include "kernel_args.h" diff --git a/src/kernel/gdt.cpp b/src/kernel/gdt.cpp index aff2b9a..9ec7e6a 100644 --- a/src/kernel/gdt.cpp +++ b/src/kernel/gdt.cpp @@ -1,8 +1,9 @@ #include +#include #include "kutil/assert.h" -#include "kutil/memory.h" #include "kutil/no_construct.h" + #include "console.h" #include "cpu.h" #include "gdt.h" @@ -28,7 +29,7 @@ GDT &g_bsp_gdt = __g_bsp_gdt_storage.value; GDT::GDT(TSS *tss) : m_tss(tss) { - kutil::memset(this, 0, sizeof(GDT)); + memset(this, 0, sizeof(GDT)); m_ptr.limit = sizeof(m_entries) - 1; m_ptr.base = &m_entries[0]; @@ -110,7 +111,7 @@ GDT::set_tss(TSS *tss) type::ring3 | type::present; - kutil::memcpy(&m_entries[tss_index], &tssd, sizeof(tss_descriptor)); + memcpy(&m_entries[tss_index], &tssd, sizeof(tss_descriptor)); } void diff --git a/src/libraries/kutil/heap_allocator.cpp b/src/kernel/heap_allocator.cpp similarity index 89% rename from src/libraries/kutil/heap_allocator.cpp rename to src/kernel/heap_allocator.cpp index a7aef70..4ffa717 100644 --- a/src/libraries/kutil/heap_allocator.cpp +++ b/src/kernel/heap_allocator.cpp @@ -1,10 +1,10 @@ #include +#include #include "kutil/assert.h" -#include "kutil/heap_allocator.h" -#include "kutil/memory.h" #include "kutil/util.h" -namespace kutil { +#include "heap_allocator.h" +#include "memory.h" struct heap_allocator::mem_header { @@ -43,8 +43,8 @@ struct heap_allocator::mem_header set_next(nullptr); } - inline mem_header * next() { return kutil::mask_pointer(m_next, 0x3f); } - inline mem_header * prev() { return kutil::mask_pointer(m_prev, 0x3f); } + inline mem_header * next() { return mask_pointer(m_next, 0x3f); } + inline mem_header * prev() { return mask_pointer(m_prev, 0x3f); } inline mem_header * buddy() const { return reinterpret_cast( @@ -70,7 +70,7 @@ heap_allocator::heap_allocator(uintptr_t start, size_t size) : m_blocks {0}, m_allocated_size {0} { - kutil::memset(m_free, 0, sizeof(m_free)); + memset(m_free, 0, sizeof(m_free)); } void * @@ -81,7 +81,7 @@ heap_allocator::allocate(size_t length) if (length == 0) return nullptr; - unsigned order = log2(total); + unsigned order = kutil::log2(total); if (order < min_order) order = min_order; @@ -89,7 +89,7 @@ heap_allocator::allocate(size_t length) if (order > max_order) return nullptr; - scoped_lock lock {m_lock}; + kutil::scoped_lock lock {m_lock}; mem_header *header = pop_free(order); header->set_used(true); @@ -106,7 +106,7 @@ heap_allocator::free(void *p) kassert(addr >= m_start && addr < m_end, "Attempt to free non-heap pointer"); - scoped_lock lock {m_lock}; + kutil::scoped_lock lock {m_lock}; mem_header *header = reinterpret_cast(p); header -= 1; // p points after the header @@ -154,7 +154,7 @@ heap_allocator::ensure_block(unsigned order) } else { mem_header *orig = pop_free(order + 1); if (orig) { - mem_header *next = kutil::offset_pointer(orig, 1 << order); + mem_header *next = offset_pointer(orig, 1 << order); new (next) mem_header(orig, nullptr, order); orig->set_next(next); @@ -175,5 +175,3 @@ heap_allocator::pop_free(unsigned order) } return block; } - -} // namespace kutil diff --git a/src/libraries/kutil/include/kutil/heap_allocator.h b/src/kernel/heap_allocator.h similarity index 96% rename from src/libraries/kutil/include/kutil/heap_allocator.h rename to src/kernel/heap_allocator.h index 76ecacf..457aada 100644 --- a/src/libraries/kutil/include/kutil/heap_allocator.h +++ b/src/kernel/heap_allocator.h @@ -5,8 +5,6 @@ #include #include "kutil/spinlock.h" -namespace kutil { - /// Allocator for a given heap range class heap_allocator @@ -58,9 +56,7 @@ protected: mem_header *m_free[max_order - min_order + 1]; size_t m_allocated_size; - spinlock m_lock; + kutil::spinlock m_lock; heap_allocator(const heap_allocator &) = delete; }; - -} // namespace kutil diff --git a/src/kernel/idt.cpp b/src/kernel/idt.cpp index 53c571a..96e33ff 100644 --- a/src/kernel/idt.cpp +++ b/src/kernel/idt.cpp @@ -1,5 +1,7 @@ -#include "kutil/memory.h" +#include + #include "kutil/no_construct.h" + #include "cpu.h" #include "idt.h" #include "log.h" @@ -33,7 +35,7 @@ IDT::set_nmi_handler(uintptr_t address) IDT::IDT() { - kutil::memset(this, 0, sizeof(IDT)); + memset(this, 0, sizeof(IDT)); m_ptr.limit = sizeof(m_entries) - 1; m_ptr.base = &m_entries[0]; diff --git a/src/kernel/kernel.module b/src/kernel/kernel.module index 6f4375a..e34dee7 100644 --- a/src/kernel/kernel.module +++ b/src/kernel/kernel.module @@ -20,6 +20,7 @@ kernel = module("kernel", "frame_allocator.cpp", "gdt.cpp", "gdtidt.s", + "heap_allocator.cpp", "hpet.cpp", "idt.cpp", "interrupts.cpp", @@ -27,6 +28,7 @@ kernel = module("kernel", "io.cpp", "log.cpp", "logger.cpp", + "memory.cpp", "memory_bootstrap.cpp", "msr.cpp", "objects/channel.cpp", diff --git a/src/kernel/log.cpp b/src/kernel/log.cpp index ddeaa45..3c1347c 100644 --- a/src/kernel/log.cpp +++ b/src/kernel/log.cpp @@ -1,8 +1,9 @@ #include "j6/signals.h" -#include "kutil/memory.h" #include "kutil/no_construct.h" + #include "console.h" #include "log.h" +#include "memory.h" #include "objects/system.h" #include "objects/thread.h" @@ -57,8 +58,8 @@ logger_task() size_t size = g_logger.get_entry(buffer, buffer_size); if (size > buffer_size) { while (size > buffer_size) buffer_size *= 2; - kutil::kfree(buffer); - buffer = reinterpret_cast(kutil::kalloc(buffer_size)); + kfree(buffer); + buffer = reinterpret_cast(kalloc(buffer_size)); kassert(buffer, "Could not allocate logger task buffer"); continue; } diff --git a/src/kernel/logger.cpp b/src/kernel/logger.cpp index a99aee6..23ac5ac 100644 --- a/src/kernel/logger.cpp +++ b/src/kernel/logger.cpp @@ -1,6 +1,7 @@ +#include + #include "kutil/assert.h" #include "kutil/constexpr_hash.h" -#include "kutil/memory.h" #include "printf/printf.h" #include "logger.h" @@ -15,9 +16,6 @@ namespace logs { namespace log { -using kutil::memset; -using kutil::memcpy; - logger *logger::s_log = nullptr; const char *logger::s_level_names[] = {"", "debug", "info", "warn", "error", "fatal"}; diff --git a/src/kernel/main.cpp b/src/kernel/main.cpp index 3961110..12a36ab 100644 --- a/src/kernel/main.cpp +++ b/src/kernel/main.cpp @@ -1,9 +1,12 @@ #include #include +#include #include "j6/signals.h" - +#include "kernel_args.h" +#include "kernel_memory.h" #include "kutil/assert.h" + #include "apic.h" #include "block_device.h" #include "clock.h" @@ -14,8 +17,6 @@ #include "idt.h" #include "interrupts.h" #include "io.h" -#include "kernel_args.h" -#include "kernel_memory.h" #include "log.h" #include "msr.h" #include "objects/channel.h" @@ -28,6 +29,7 @@ #include "tss.h" #include "vm_space.h" + #ifndef GIT_VERSION #define GIT_VERSION #endif @@ -96,7 +98,7 @@ kernel_main(init::args *args) extern uintptr_t idle_stack_end; cpu_data *cpu = &g_bsp_cpu_data; - kutil::memset(cpu, 0, sizeof(cpu_data)); + memset(cpu, 0, sizeof(cpu_data)); cpu->self = cpu; cpu->idt = new (&g_bsp_idt) IDT; @@ -158,7 +160,7 @@ kernel_main(init::args *args) if (disk) { for (int i=0; i<1; ++i) { uint8_t buf[512]; - kutil::memset(buf, 0, 512); + memset(buf, 0, 512); kassert(disk->read(0x200, sizeof(buf), buf), "Disk read returned 0"); @@ -214,7 +216,7 @@ start_aps(lapic &apic, const kutil::vector &ids, void *kpml4) uint8_t vector = addr >> 12; vm_area *vma = new vm_area_fixed(addr, 0x1000, vm_flags::write); vm_space::kernel_space().add(addr, vma); - kutil::memcpy( + memcpy( reinterpret_cast(addr), reinterpret_cast(&ap_startup), ap_startup_code_size); @@ -241,7 +243,7 @@ start_aps(lapic &apic, const kutil::vector &ids, void *kpml4) TSS *tss = new TSS; GDT *gdt = new GDT {tss}; cpu_data *cpu = new cpu_data; - kutil::memset(cpu, 0, sizeof(cpu_data)); + memset(cpu, 0, sizeof(cpu_data)); cpu->self = cpu; cpu->id = id; diff --git a/src/libraries/kutil/memory.cpp b/src/kernel/memory.cpp similarity index 85% rename from src/libraries/kutil/memory.cpp rename to src/kernel/memory.cpp index 149435e..6a8fd49 100644 --- a/src/libraries/kutil/memory.cpp +++ b/src/kernel/memory.cpp @@ -1,10 +1,12 @@ -#include "kutil/memory.h" +#include "memory.h" namespace std { enum class __attribute__ ((__type_visibility("default"))) align_val_t : size_t { }; } -namespace kutil { +// Implementation of memset and memcpy because we're not +// linking libc into the kernel +extern "C" { void * memset(void *s, uint8_t v, size_t n) @@ -23,6 +25,8 @@ memcpy(void *dest, const void *src, size_t n) return d; } +} + uint8_t checksum(const void *p, size_t len, size_t off) { @@ -31,5 +35,3 @@ checksum(const void *p, size_t len, size_t off) for (int i = off; i < len; ++i) sum += c[i]; return sum; } - -} // namespace kutil diff --git a/src/libraries/kutil/include/kutil/memory.h b/src/kernel/memory.h similarity index 64% rename from src/libraries/kutil/include/kutil/memory.h rename to src/kernel/memory.h index c5d8905..7faf59d 100644 --- a/src/libraries/kutil/include/kutil/memory.h +++ b/src/kernel/memory.h @@ -6,34 +6,16 @@ void * operator new (size_t, void *p) noexcept; -namespace kutil { - -/// Allocate from the default allocator. Note this needs to be -/// implemented by users of the kutil library. +/// Allocate from the default allocator. /// \arg size The size in bytes requested /// \returns A pointer to the newly allocated memory, /// or nullptr on error void * kalloc(size_t size); -/// Free memory allocated by `kalloc`. Note this needs to be -/// implemented by users of the kutil library. +/// Free memory allocated by `kalloc`. /// \arg p Pointer that was returned from a `kalloc` call void kfree(void *p); -/// Fill memory with the given value. -/// \arg p The beginning of the memory area to fill -/// \arg v The byte value to fill memory with -/// \arg n The size in bytes of the memory area -/// \returns A pointer to the filled memory -void * memset(void *p, uint8_t v, size_t n); - -/// Copy an area of memory to another -/// \dest The memory to copy to -/// \src The memory to copy from -/// \n The number of bytes to copy -/// \returns A pointer to the destination memory -void * memcpy(void *dest, const void *src, size_t n); - /// Read a value of type T from a location in memory /// \arg p The location in memory to read /// \returns The value at the given location cast to T @@ -68,5 +50,3 @@ inline T* mask_pointer(T *p, uintptr_t mask) /// \arg len The number of bytes in the region /// \arg off An optional offset into the region uint8_t checksum(const void *p, size_t len, size_t off = 0); - -} // namespace kutil diff --git a/src/kernel/memory_bootstrap.cpp b/src/kernel/memory_bootstrap.cpp index 078910f..99e2aeb 100644 --- a/src/kernel/memory_bootstrap.cpp +++ b/src/kernel/memory_bootstrap.cpp @@ -5,12 +5,12 @@ #include "enum_bitfields.h" #include "kutil/assert.h" -#include "kutil/heap_allocator.h" #include "kutil/no_construct.h" #include "device_manager.h" #include "frame_allocator.h" #include "gdt.h" +#include "heap_allocator.h" #include "io.h" #include "log.h" #include "msr.h" @@ -39,8 +39,8 @@ extern "C" uintptr_t initialize_main_user_stack(); // These objects are initialized _before_ global constructors are called, // so we don't want them to have global constructors at all, lest they // overwrite the previous initialization. -static kutil::no_construct __g_kernel_heap_storage; -kutil::heap_allocator &g_kernel_heap = __g_kernel_heap_storage.value; +static kutil::no_construct __g_kernel_heap_storage; +heap_allocator &g_kernel_heap = __g_kernel_heap_storage.value; static kutil::no_construct __g_frame_allocator_storage; frame_allocator &g_frame_allocator = __g_frame_allocator_storage.value; @@ -62,10 +62,8 @@ void * operator new [] (size_t size) { return g_kernel_heap.allocate(size) void operator delete (void *p) noexcept { return g_kernel_heap.free(p); } void operator delete [] (void *p) noexcept { return g_kernel_heap.free(p); } -namespace kutil { - void * kalloc(size_t size) { return g_kernel_heap.allocate(size); } - void kfree(void *p) { return g_kernel_heap.free(p); } -} +void * kalloc(size_t size) { return g_kernel_heap.allocate(size); } +void kfree(void *p) { return g_kernel_heap.free(p); } template uintptr_t @@ -80,7 +78,7 @@ memory_initialize_pre_ctors(init::args &kargs) page_table *kpml4 = static_cast(kargs.pml4); - new (&g_kernel_heap) kutil::heap_allocator {heap_start, kernel_max_heap}; + new (&g_kernel_heap) heap_allocator {heap_start, kernel_max_heap}; frame_block *blocks = reinterpret_cast(memory::bitmap_start); new (&g_frame_allocator) frame_allocator {blocks, kargs.frame_blocks.count}; diff --git a/src/kernel/objects/channel.cpp b/src/kernel/objects/channel.cpp index c1db0d4..e67b696 100644 --- a/src/kernel/objects/channel.cpp +++ b/src/kernel/objects/channel.cpp @@ -1,3 +1,4 @@ +#include #include "kutil/assert.h" #include "kernel_memory.h" @@ -38,7 +39,7 @@ channel::enqueue(size_t *len, const void *data) size_t avail = m_buffer.reserve(*len, &buffer); *len = *len > avail ? avail : *len; - kutil::memcpy(buffer, data, *len); + memcpy(buffer, data, *len); m_buffer.commit(*len); assert_signal(j6_signal_channel_can_recv); @@ -65,7 +66,7 @@ channel::dequeue(size_t *len, void *data) size_t avail = m_buffer.get_block(&buffer); *len = *len > avail ? avail : *len; - kutil::memcpy(data, buffer, *len); + memcpy(data, buffer, *len); m_buffer.consume(*len); assert_signal(j6_signal_channel_can_send); diff --git a/src/kernel/objects/thread.cpp b/src/kernel/objects/thread.cpp index d363272..7c5dac0 100644 --- a/src/kernel/objects/thread.cpp +++ b/src/kernel/objects/thread.cpp @@ -1,6 +1,8 @@ #include "j6/signals.h" + #include "cpu.h" #include "log.h" +#include "memory.h" #include "objects/thread.h" #include "objects/process.h" #include "objects/vm_area.h" @@ -40,7 +42,7 @@ thread::from_tcb(TCB *tcb) { static ptrdiff_t offset = -1 * static_cast(offsetof(thread, m_tcb)); - return reinterpret_cast(kutil::offset_pointer(tcb, offset)); + return reinterpret_cast(offset_pointer(tcb, offset)); } thread & thread::current() { return *current_cpu().thread; } diff --git a/src/kernel/page_table.cpp b/src/kernel/page_table.cpp index e14eaef..0050843 100644 --- a/src/kernel/page_table.cpp +++ b/src/kernel/page_table.cpp @@ -1,8 +1,11 @@ +#include + #include "kutil/assert.h" -#include "kutil/memory.h" + #include "console.h" #include "frame_allocator.h" #include "kernel_memory.h" +#include "memory.h" #include "page_table.h" using memory::page_offset; @@ -28,8 +31,8 @@ page_table::iterator::iterator(uintptr_t virt, page_table *pml4) : page_table::iterator::iterator(const page_table::iterator &o) { - kutil::memcpy(&m_table, &o.m_table, sizeof(m_table)); - kutil::memcpy(&m_index, &o.m_index, sizeof(m_index)); + memcpy(&m_table, &o.m_table, sizeof(m_table)); + memcpy(&m_index, &o.m_index, sizeof(m_index)); } inline static level to_lv(unsigned i) { return static_cast(i); } @@ -190,7 +193,7 @@ page_table::get_table_page() --s_cache_count; } - kutil::memset(page, 0, memory::frame_size); + memset(page, 0, memory::frame_size); return reinterpret_cast(page); } @@ -220,11 +223,11 @@ page_table::fill_table_page_cache() memory::to_virtual(phys); for (int i = 0; i < n - 1; ++i) - kutil::offset_pointer(start, i * memory::frame_size) - ->next = kutil::offset_pointer(start, (i+1) * memory::frame_size); + offset_pointer(start, i * memory::frame_size) + ->next = offset_pointer(start, (i+1) * memory::frame_size); free_page_header *end = - kutil::offset_pointer(start, (n-1) * memory::frame_size); + offset_pointer(start, (n-1) * memory::frame_size); end->next = s_page_cache; s_page_cache = start; diff --git a/src/kernel/page_tree.cpp b/src/kernel/page_tree.cpp index 7188e88..84d1fa9 100644 --- a/src/kernel/page_tree.cpp +++ b/src/kernel/page_tree.cpp @@ -1,5 +1,7 @@ +#include + #include "kutil/assert.h" -#include "kutil/memory.h" + #include "frame_allocator.h" #include "kernel_memory.h" #include "page_tree.h" @@ -33,7 +35,7 @@ page_tree::page_tree(uint64_t base, uint8_t level) : m_base {base & level_mask(level)}, m_level {level} { - kutil::memset(m_entries, 0, sizeof(m_entries)); + memset(m_entries, 0, sizeof(m_entries)); } page_tree::~page_tree() diff --git a/src/kernel/pci.cpp b/src/kernel/pci.cpp index a5676f1..21aacdc 100644 --- a/src/kernel/pci.cpp +++ b/src/kernel/pci.cpp @@ -106,7 +106,7 @@ pci_device::pci_device(pci_group &group, uint8_t bus, uint8_t device, uint8_t fu // Walk the extended capabilities list uint8_t next = m_base[13] & 0xff; while (next) { - pci_cap *cap = reinterpret_cast(kutil::offset_pointer(m_base, next)); + pci_cap *cap = reinterpret_cast(offset_pointer(m_base, next)); next = cap->next; log::debug(logs::device, " - found PCI cap type %02x", cap->id); diff --git a/src/kernel/pci.h b/src/kernel/pci.h index 06b8228..1e446e3 100644 --- a/src/kernel/pci.h +++ b/src/kernel/pci.h @@ -3,7 +3,7 @@ /// PCI devices and groups #include -#include "kutil/memory.h" +#include "memory.h" struct pci_group; enum class isr : uint8_t; @@ -126,7 +126,7 @@ struct pci_group /// \returns A pointer to the memory-mapped configuration registers inline uint32_t * base_for(uint8_t bus, uint8_t device, uint8_t func) { - return kutil::offset_pointer(base, + return offset_pointer(base, pci_device::bus_addr(bus, device, func) << 12); } diff --git a/src/kernel/serial.cpp b/src/kernel/serial.cpp index 9d247bc..cdd50a2 100644 --- a/src/kernel/serial.cpp +++ b/src/kernel/serial.cpp @@ -1,6 +1,7 @@ +#include #include "kutil/assert.h" -#include "kutil/memory.h" #include "kutil/no_construct.h" + #include "interrupts.h" #include "io.h" #include "serial.h" @@ -121,7 +122,7 @@ serial_port::do_write() if (n > fifo_size) n = fifo_size; - kutil::memcpy(tmp, data, n); + memcpy(tmp, data, n); m_out_buffer.consume(n); for (size_t i = 0; i < n; ++i) diff --git a/src/kernel/syscall.cpp.cog b/src/kernel/syscall.cpp.cog index 74e0807..a061897 100644 --- a/src/kernel/syscall.cpp.cog +++ b/src/kernel/syscall.cpp.cog @@ -1,7 +1,7 @@ // vim: ft=cpp #include +#include -#include "kutil/memory.h" #include "console.h" #include "debug.h" @@ -38,7 +38,7 @@ syscall_invalid(uint64_t call) void syscall_initialize() { - kutil::memset(&syscall_registry, 0, sizeof(syscall_registry)); + memset(&syscall_registry, 0, sizeof(syscall_registry)); /*[[[cog code generation for id, scope, method in syscalls.methods: diff --git a/src/kernel/tss.cpp b/src/kernel/tss.cpp index a894c30..9fc82bc 100644 --- a/src/kernel/tss.cpp +++ b/src/kernel/tss.cpp @@ -1,5 +1,5 @@ +#include #include "kutil/assert.h" -#include "kutil/memory.h" #include "kutil/no_construct.h" #include "cpu.h" @@ -17,7 +17,7 @@ TSS &g_bsp_tss = __g_bsp_tss_storage.value; TSS::TSS() { - kutil::memset(this, 0, sizeof(TSS)); + memset(this, 0, sizeof(TSS)); m_iomap_offset = sizeof(TSS); } diff --git a/src/kernel/vm_space.cpp b/src/kernel/vm_space.cpp index 84b9988..9621d28 100644 --- a/src/kernel/vm_space.cpp +++ b/src/kernel/vm_space.cpp @@ -1,3 +1,5 @@ +#include + #include "frame_allocator.h" #include "kernel_memory.h" #include "log.h" @@ -38,7 +40,7 @@ vm_space::vm_space() : m_pml4 = page_table::get_table_page(); page_table *kpml4 = kernel_space().m_pml4; - kutil::memset(m_pml4, 0, memory::frame_size/2); + memset(m_pml4, 0, memory::frame_size/2); for (unsigned i = memory::pml4e_kernel; i < memory::table_entries; ++i) m_pml4->entries[i] = kpml4->entries[i]; } @@ -295,7 +297,7 @@ vm_space::copy(vm_space &source, vm_space &dest, const void *from, void *to, siz // TODO: iterate page mappings and continue copying. For now i'm blindly // assuming both buffers are fully contained within single pages - kutil::memcpy( + memcpy( memory::to_virtual((*dit & ~0xfffull) | (ito & 0xffful)), memory::to_virtual((*sit & ~0xfffull) | (ifrom & 0xffful)), length); diff --git a/src/libraries/kutil/include/kutil/map.h b/src/libraries/kutil/include/kutil/map.h index e46321c..b290881 100644 --- a/src/libraries/kutil/include/kutil/map.h +++ b/src/libraries/kutil/include/kutil/map.h @@ -10,9 +10,10 @@ /// http://codecapsule.com/2013/11/11/robin-hood-hashing/ /// http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/ +#include #include +#include #include "kutil/hash.h" -#include "kutil/memory.h" #include "kutil/vector.h" #include "kutil/util.h" @@ -87,7 +88,7 @@ public: virtual ~base_map() { for (size_t i = 0; i < m_capacity; ++i) m_nodes[i].~node(); - kfree(m_nodes); + delete [] reinterpret_cast(m_nodes); } iterator begin() { @@ -149,8 +150,8 @@ protected: m_capacity = capacity; const size_t size = m_capacity * sizeof(node); - m_nodes = reinterpret_cast(kalloc(size)); - kutil::memset(m_nodes, 0, size); + m_nodes = reinterpret_cast(new uint8_t [size]); + memset(m_nodes, 0, size); } void grow() { @@ -169,7 +170,7 @@ protected: n.~node(); } - kfree(old); + delete [] reinterpret_cast(old); } inline node * construct(size_t i, uint64_t h, K &&k, V &&v) { diff --git a/src/libraries/kutil/include/kutil/vector.h b/src/libraries/kutil/include/kutil/vector.h index 825f356..ebd67b5 100644 --- a/src/libraries/kutil/include/kutil/vector.h +++ b/src/libraries/kutil/include/kutil/vector.h @@ -2,9 +2,9 @@ /// \file vector.h /// Definition of a simple dynamic vector collection for use in kernel space +#include #include #include "kutil/assert.h" -#include "kutil/memory.h" #include "kutil/util.h" namespace kutil { @@ -42,7 +42,7 @@ public: m_elements(nullptr) { set_capacity(other.m_capacity); - kutil::memcpy(m_elements, other.m_elements, other.m_size * sizeof(T)); + memcpy(m_elements, other.m_elements, other.m_size * sizeof(T)); m_size = other.m_size; } @@ -73,7 +73,7 @@ public: bool was_static = m_capacity & ~cap_mask; if (!was_static) - kfree(m_elements); + delete [] m_elements; } /// Get the size of the array. @@ -271,17 +271,18 @@ public: void set_capacity(count_t capacity) { bool was_static = m_capacity & ~cap_mask; - T *new_array = reinterpret_cast(kalloc(capacity * sizeof(T))); + T *new_array = reinterpret_cast(new uint8_t [capacity * sizeof(T)]); count_t size = capacity > m_size ? m_size : capacity; - kutil::memcpy(new_array, m_elements, size * sizeof(T)); + memcpy(new_array, m_elements, size * sizeof(T)); while (size < m_size) remove(); m_size = size; m_capacity = capacity; if (!was_static) - kfree(m_elements); + delete [] m_elements; + m_elements = new_array; } diff --git a/src/libraries/kutil/kutil.module b/src/libraries/kutil/kutil.module index 7120d01..7ed24c3 100644 --- a/src/libraries/kutil/kutil.module +++ b/src/libraries/kutil/kutil.module @@ -6,7 +6,5 @@ module("kutil", sources = [ "assert.cpp", "bip_buffer.cpp", - "heap_allocator.cpp", - "memory.cpp", "spinlock.cpp", ]) diff --git a/src/libraries/libc/include/stdalign.h b/src/libraries/libc/include/stdalign.h index 4b30dbc..cd33a9e 100644 --- a/src/libraries/libc/include/stdalign.h +++ b/src/libraries/libc/include/stdalign.h @@ -5,8 +5,10 @@ Permission is granted to use, modify, and / or redistribute at will. */ +#ifndef __cplusplus #define alignas _Alignas #define alignof _Alignof +#endif #define __alignas_is_defined 1 #define __alignof_is_defined 1