[kernel] Move more from kutil to kernel

The moving of kernel-only code out of kutil continues. (See 042f061)
This commit moves the following:

- The heap allocator code
- memory.cpp/h which means:
  - letting string.h be the right header for memset and memcpy, still
    including an implementation of it for the kernel though, since
    we're not linking libc to the kernel
  - Changing calls to kalloc/kfree to new/delete in kutil containers
    that aren't going to be merged into the kernel
- Fixing a problem with stdalign.h from libc, which was causing issues
  for type_traits.
This commit is contained in:
Justin C. Miller
2022-01-01 23:23:51 -08:00
parent 4d5ed8157c
commit a6ec294f63
29 changed files with 116 additions and 123 deletions

View File

@@ -1,6 +1,6 @@
#include "kutil/memory.h"
#include "kutil/no_construct.h" #include "kutil/no_construct.h"
#include "printf/printf.h" #include "printf/printf.h"
#include "console.h" #include "console.h"
#include "serial.h" #include "serial.h"

View File

@@ -1,6 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "cpu.h" #include "cpu.h"
#include "cpu/cpu_id.h" #include "cpu/cpu_id.h"
#include "device_manager.h" #include "device_manager.h"

View File

@@ -2,7 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "acpi_tables.h" #include "acpi_tables.h"
#include "apic.h" #include "apic.h"
#include "clock.h" #include "clock.h"
@@ -11,6 +11,7 @@
#include "interrupts.h" #include "interrupts.h"
#include "kernel_memory.h" #include "kernel_memory.h"
#include "log.h" #include "log.h"
#include "memory.h"
#include "objects/endpoint.h" #include "objects/endpoint.h"
#include "serial.h" #include "serial.h"
@@ -47,7 +48,7 @@ struct acpi2_rsdp
bool bool
acpi_table_header::validate(uint32_t expected_type) const 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); 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], kassert(acpi1->signature[i] == expected_signature[i],
"ACPI RSDP table signature mismatch"); "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(sum == 0, "ACPI 1.0 RSDP checksum mismatch.");
kassert(acpi1->revision > 1, "ACPI 1.0 not supported."); 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 = const acpi2_rsdp *acpi2 =
reinterpret_cast<const acpi2_rsdp *>(acpi1); reinterpret_cast<const acpi2_rsdp *>(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."); kassert(sum == 0, "ACPI 2.0 RSDP checksum mismatch.");
load_xsdt(memory::to_virtual(acpi2->xsdt_address)); load_xsdt(memory::to_virtual(acpi2->xsdt_address));
@@ -212,8 +213,8 @@ device_manager::load_apic(const acpi_table_header *header)
switch (type) { switch (type) {
case 0: { // Local APIC case 0: { // Local APIC
uint8_t uid = kutil::read_from<uint8_t>(p+2); uint8_t uid = read_from<uint8_t>(p+2);
uint8_t id = kutil::read_from<uint8_t>(p+3); uint8_t id = read_from<uint8_t>(p+3);
m_apic_ids.append(id); m_apic_ids.append(id);
log::debug(logs::device, " Local APIC uid %x id %x", uid, 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; break;
case 1: { // I/O APIC case 1: { // I/O APIC
uintptr_t base = kutil::read_from<uint32_t>(p+4); uintptr_t base = read_from<uint32_t>(p+4);
uint32_t base_gsi = kutil::read_from<uint32_t>(p+8); uint32_t base_gsi = read_from<uint32_t>(p+8);
m_ioapics.emplace(base, base_gsi); m_ioapics.emplace(base, base_gsi);
log::debug(logs::device, " IO APIC gsi %x base %x", base_gsi, base); 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 case 2: { // Interrupt source override
irq_override o; irq_override o;
o.source = kutil::read_from<uint8_t>(p+3); o.source = read_from<uint8_t>(p+3);
o.gsi = kutil::read_from<uint32_t>(p+4); o.gsi = read_from<uint32_t>(p+4);
o.flags = kutil::read_from<uint16_t>(p+8); o.flags = read_from<uint16_t>(p+8);
m_overrides.append(o); m_overrides.append(o);
log::debug(logs::device, " Intr source override IRQ %d -> %d Pol %d Tri %d", 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 case 4: {// LAPIC NMI
apic_nmi nmi; apic_nmi nmi;
nmi.cpu = kutil::read_from<uint8_t>(p + 2); nmi.cpu = read_from<uint8_t>(p + 2);
nmi.lint = kutil::read_from<uint8_t>(p + 5); nmi.lint = read_from<uint8_t>(p + 5);
nmi.flags = kutil::read_from<uint16_t>(p + 3); nmi.flags = read_from<uint16_t>(p + 3);
m_nmis.append(nmi); m_nmis.append(nmi);
log::debug(logs::device, " LAPIC NMI Proc %02x LINT%d Pol %d Tri %d", log::debug(logs::device, " LAPIC NMI Proc %02x LINT%d Pol %d Tri %d",

View File

@@ -1,5 +1,4 @@
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "frame_allocator.h" #include "frame_allocator.h"
#include "kernel_args.h" #include "kernel_args.h"

View File

@@ -1,8 +1,9 @@
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "kutil/no_construct.h" #include "kutil/no_construct.h"
#include "console.h" #include "console.h"
#include "cpu.h" #include "cpu.h"
#include "gdt.h" #include "gdt.h"
@@ -28,7 +29,7 @@ GDT &g_bsp_gdt = __g_bsp_gdt_storage.value;
GDT::GDT(TSS *tss) : GDT::GDT(TSS *tss) :
m_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.limit = sizeof(m_entries) - 1;
m_ptr.base = &m_entries[0]; m_ptr.base = &m_entries[0];
@@ -110,7 +111,7 @@ GDT::set_tss(TSS *tss)
type::ring3 | type::ring3 |
type::present; type::present;
kutil::memcpy(&m_entries[tss_index], &tssd, sizeof(tss_descriptor)); memcpy(&m_entries[tss_index], &tssd, sizeof(tss_descriptor));
} }
void void

View File

@@ -1,10 +1,10 @@
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/heap_allocator.h"
#include "kutil/memory.h"
#include "kutil/util.h" #include "kutil/util.h"
namespace kutil { #include "heap_allocator.h"
#include "memory.h"
struct heap_allocator::mem_header struct heap_allocator::mem_header
{ {
@@ -43,8 +43,8 @@ struct heap_allocator::mem_header
set_next(nullptr); set_next(nullptr);
} }
inline mem_header * next() { return kutil::mask_pointer(m_next, 0x3f); } inline mem_header * next() { return mask_pointer(m_next, 0x3f); }
inline mem_header * prev() { return kutil::mask_pointer(m_prev, 0x3f); } inline mem_header * prev() { return mask_pointer(m_prev, 0x3f); }
inline mem_header * buddy() const { inline mem_header * buddy() const {
return reinterpret_cast<mem_header *>( return reinterpret_cast<mem_header *>(
@@ -70,7 +70,7 @@ heap_allocator::heap_allocator(uintptr_t start, size_t size) :
m_blocks {0}, m_blocks {0},
m_allocated_size {0} m_allocated_size {0}
{ {
kutil::memset(m_free, 0, sizeof(m_free)); memset(m_free, 0, sizeof(m_free));
} }
void * void *
@@ -81,7 +81,7 @@ heap_allocator::allocate(size_t length)
if (length == 0) if (length == 0)
return nullptr; return nullptr;
unsigned order = log2(total); unsigned order = kutil::log2(total);
if (order < min_order) if (order < min_order)
order = min_order; order = min_order;
@@ -89,7 +89,7 @@ heap_allocator::allocate(size_t length)
if (order > max_order) if (order > max_order)
return nullptr; return nullptr;
scoped_lock lock {m_lock}; kutil::scoped_lock lock {m_lock};
mem_header *header = pop_free(order); mem_header *header = pop_free(order);
header->set_used(true); header->set_used(true);
@@ -106,7 +106,7 @@ heap_allocator::free(void *p)
kassert(addr >= m_start && addr < m_end, kassert(addr >= m_start && addr < m_end,
"Attempt to free non-heap pointer"); "Attempt to free non-heap pointer");
scoped_lock lock {m_lock}; kutil::scoped_lock lock {m_lock};
mem_header *header = reinterpret_cast<mem_header *>(p); mem_header *header = reinterpret_cast<mem_header *>(p);
header -= 1; // p points after the header header -= 1; // p points after the header
@@ -154,7 +154,7 @@ heap_allocator::ensure_block(unsigned order)
} else { } else {
mem_header *orig = pop_free(order + 1); mem_header *orig = pop_free(order + 1);
if (orig) { 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); new (next) mem_header(orig, nullptr, order);
orig->set_next(next); orig->set_next(next);
@@ -175,5 +175,3 @@ heap_allocator::pop_free(unsigned order)
} }
return block; return block;
} }
} // namespace kutil

View File

@@ -5,8 +5,6 @@
#include <stddef.h> #include <stddef.h>
#include "kutil/spinlock.h" #include "kutil/spinlock.h"
namespace kutil {
/// Allocator for a given heap range /// Allocator for a given heap range
class heap_allocator class heap_allocator
@@ -58,9 +56,7 @@ protected:
mem_header *m_free[max_order - min_order + 1]; mem_header *m_free[max_order - min_order + 1];
size_t m_allocated_size; size_t m_allocated_size;
spinlock m_lock; kutil::spinlock m_lock;
heap_allocator(const heap_allocator &) = delete; heap_allocator(const heap_allocator &) = delete;
}; };
} // namespace kutil

View File

@@ -1,5 +1,7 @@
#include "kutil/memory.h" #include <string.h>
#include "kutil/no_construct.h" #include "kutil/no_construct.h"
#include "cpu.h" #include "cpu.h"
#include "idt.h" #include "idt.h"
#include "log.h" #include "log.h"
@@ -33,7 +35,7 @@ IDT::set_nmi_handler(uintptr_t address)
IDT::IDT() IDT::IDT()
{ {
kutil::memset(this, 0, sizeof(IDT)); memset(this, 0, sizeof(IDT));
m_ptr.limit = sizeof(m_entries) - 1; m_ptr.limit = sizeof(m_entries) - 1;
m_ptr.base = &m_entries[0]; m_ptr.base = &m_entries[0];

View File

@@ -20,6 +20,7 @@ kernel = module("kernel",
"frame_allocator.cpp", "frame_allocator.cpp",
"gdt.cpp", "gdt.cpp",
"gdtidt.s", "gdtidt.s",
"heap_allocator.cpp",
"hpet.cpp", "hpet.cpp",
"idt.cpp", "idt.cpp",
"interrupts.cpp", "interrupts.cpp",
@@ -27,6 +28,7 @@ kernel = module("kernel",
"io.cpp", "io.cpp",
"log.cpp", "log.cpp",
"logger.cpp", "logger.cpp",
"memory.cpp",
"memory_bootstrap.cpp", "memory_bootstrap.cpp",
"msr.cpp", "msr.cpp",
"objects/channel.cpp", "objects/channel.cpp",

View File

@@ -1,8 +1,9 @@
#include "j6/signals.h" #include "j6/signals.h"
#include "kutil/memory.h"
#include "kutil/no_construct.h" #include "kutil/no_construct.h"
#include "console.h" #include "console.h"
#include "log.h" #include "log.h"
#include "memory.h"
#include "objects/system.h" #include "objects/system.h"
#include "objects/thread.h" #include "objects/thread.h"
@@ -57,8 +58,8 @@ logger_task()
size_t size = g_logger.get_entry(buffer, buffer_size); size_t size = g_logger.get_entry(buffer, buffer_size);
if (size > buffer_size) { if (size > buffer_size) {
while (size > buffer_size) buffer_size *= 2; while (size > buffer_size) buffer_size *= 2;
kutil::kfree(buffer); kfree(buffer);
buffer = reinterpret_cast<uint8_t*>(kutil::kalloc(buffer_size)); buffer = reinterpret_cast<uint8_t*>(kalloc(buffer_size));
kassert(buffer, "Could not allocate logger task buffer"); kassert(buffer, "Could not allocate logger task buffer");
continue; continue;
} }

View File

@@ -1,6 +1,7 @@
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/constexpr_hash.h" #include "kutil/constexpr_hash.h"
#include "kutil/memory.h"
#include "printf/printf.h" #include "printf/printf.h"
#include "logger.h" #include "logger.h"
@@ -15,9 +16,6 @@ namespace logs {
namespace log { namespace log {
using kutil::memset;
using kutil::memcpy;
logger *logger::s_log = nullptr; logger *logger::s_log = nullptr;
const char *logger::s_level_names[] = {"", "debug", "info", "warn", "error", "fatal"}; const char *logger::s_level_names[] = {"", "debug", "info", "warn", "error", "fatal"};

View File

@@ -1,9 +1,12 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "j6/signals.h" #include "j6/signals.h"
#include "kernel_args.h"
#include "kernel_memory.h"
#include "kutil/assert.h" #include "kutil/assert.h"
#include "apic.h" #include "apic.h"
#include "block_device.h" #include "block_device.h"
#include "clock.h" #include "clock.h"
@@ -14,8 +17,6 @@
#include "idt.h" #include "idt.h"
#include "interrupts.h" #include "interrupts.h"
#include "io.h" #include "io.h"
#include "kernel_args.h"
#include "kernel_memory.h"
#include "log.h" #include "log.h"
#include "msr.h" #include "msr.h"
#include "objects/channel.h" #include "objects/channel.h"
@@ -28,6 +29,7 @@
#include "tss.h" #include "tss.h"
#include "vm_space.h" #include "vm_space.h"
#ifndef GIT_VERSION #ifndef GIT_VERSION
#define GIT_VERSION #define GIT_VERSION
#endif #endif
@@ -96,7 +98,7 @@ kernel_main(init::args *args)
extern uintptr_t idle_stack_end; extern uintptr_t idle_stack_end;
cpu_data *cpu = &g_bsp_cpu_data; cpu_data *cpu = &g_bsp_cpu_data;
kutil::memset(cpu, 0, sizeof(cpu_data)); memset(cpu, 0, sizeof(cpu_data));
cpu->self = cpu; cpu->self = cpu;
cpu->idt = new (&g_bsp_idt) IDT; cpu->idt = new (&g_bsp_idt) IDT;
@@ -158,7 +160,7 @@ kernel_main(init::args *args)
if (disk) { if (disk) {
for (int i=0; i<1; ++i) { for (int i=0; i<1; ++i) {
uint8_t buf[512]; uint8_t buf[512];
kutil::memset(buf, 0, 512); memset(buf, 0, 512);
kassert(disk->read(0x200, sizeof(buf), buf), kassert(disk->read(0x200, sizeof(buf), buf),
"Disk read returned 0"); "Disk read returned 0");
@@ -214,7 +216,7 @@ start_aps(lapic &apic, const kutil::vector<uint8_t> &ids, void *kpml4)
uint8_t vector = addr >> 12; uint8_t vector = addr >> 12;
vm_area *vma = new vm_area_fixed(addr, 0x1000, vm_flags::write); vm_area *vma = new vm_area_fixed(addr, 0x1000, vm_flags::write);
vm_space::kernel_space().add(addr, vma); vm_space::kernel_space().add(addr, vma);
kutil::memcpy( memcpy(
reinterpret_cast<void*>(addr), reinterpret_cast<void*>(addr),
reinterpret_cast<void*>(&ap_startup), reinterpret_cast<void*>(&ap_startup),
ap_startup_code_size); ap_startup_code_size);
@@ -241,7 +243,7 @@ start_aps(lapic &apic, const kutil::vector<uint8_t> &ids, void *kpml4)
TSS *tss = new TSS; TSS *tss = new TSS;
GDT *gdt = new GDT {tss}; GDT *gdt = new GDT {tss};
cpu_data *cpu = new cpu_data; cpu_data *cpu = new cpu_data;
kutil::memset(cpu, 0, sizeof(cpu_data)); memset(cpu, 0, sizeof(cpu_data));
cpu->self = cpu; cpu->self = cpu;
cpu->id = id; cpu->id = id;

View File

@@ -1,10 +1,12 @@
#include "kutil/memory.h" #include "memory.h"
namespace std { namespace std {
enum class __attribute__ ((__type_visibility("default"))) align_val_t : size_t { }; 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 * void *
memset(void *s, uint8_t v, size_t n) memset(void *s, uint8_t v, size_t n)
@@ -23,6 +25,8 @@ memcpy(void *dest, const void *src, size_t n)
return d; return d;
} }
}
uint8_t uint8_t
checksum(const void *p, size_t len, size_t off) 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]; for (int i = off; i < len; ++i) sum += c[i];
return sum; return sum;
} }
} // namespace kutil

View File

@@ -6,34 +6,16 @@
void * operator new (size_t, void *p) noexcept; void * operator new (size_t, void *p) noexcept;
namespace kutil { /// Allocate from the default allocator.
/// Allocate from the default allocator. Note this needs to be
/// implemented by users of the kutil library.
/// \arg size The size in bytes requested /// \arg size The size in bytes requested
/// \returns A pointer to the newly allocated memory, /// \returns A pointer to the newly allocated memory,
/// or nullptr on error /// or nullptr on error
void * kalloc(size_t size); void * kalloc(size_t size);
/// Free memory allocated by `kalloc`. Note this needs to be /// Free memory allocated by `kalloc`.
/// implemented by users of the kutil library.
/// \arg p Pointer that was returned from a `kalloc` call /// \arg p Pointer that was returned from a `kalloc` call
void kfree(void *p); 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 /// Read a value of type T from a location in memory
/// \arg p The location in memory to read /// \arg p The location in memory to read
/// \returns The value at the given location cast to T /// \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 len The number of bytes in the region
/// \arg off An optional offset into the region /// \arg off An optional offset into the region
uint8_t checksum(const void *p, size_t len, size_t off = 0); uint8_t checksum(const void *p, size_t len, size_t off = 0);
} // namespace kutil

View File

@@ -5,12 +5,12 @@
#include "enum_bitfields.h" #include "enum_bitfields.h"
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/heap_allocator.h"
#include "kutil/no_construct.h" #include "kutil/no_construct.h"
#include "device_manager.h" #include "device_manager.h"
#include "frame_allocator.h" #include "frame_allocator.h"
#include "gdt.h" #include "gdt.h"
#include "heap_allocator.h"
#include "io.h" #include "io.h"
#include "log.h" #include "log.h"
#include "msr.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, // These objects are initialized _before_ global constructors are called,
// so we don't want them to have global constructors at all, lest they // so we don't want them to have global constructors at all, lest they
// overwrite the previous initialization. // overwrite the previous initialization.
static kutil::no_construct<kutil::heap_allocator> __g_kernel_heap_storage; static kutil::no_construct<heap_allocator> __g_kernel_heap_storage;
kutil::heap_allocator &g_kernel_heap = __g_kernel_heap_storage.value; heap_allocator &g_kernel_heap = __g_kernel_heap_storage.value;
static kutil::no_construct<frame_allocator> __g_frame_allocator_storage; static kutil::no_construct<frame_allocator> __g_frame_allocator_storage;
frame_allocator &g_frame_allocator = __g_frame_allocator_storage.value; 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); }
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 * kalloc(size_t size) { return g_kernel_heap.allocate(size); }
void kfree(void *p) { return g_kernel_heap.free(p); } void kfree(void *p) { return g_kernel_heap.free(p); }
}
template <typename T> template <typename T>
uintptr_t uintptr_t
@@ -80,7 +78,7 @@ memory_initialize_pre_ctors(init::args &kargs)
page_table *kpml4 = static_cast<page_table*>(kargs.pml4); page_table *kpml4 = static_cast<page_table*>(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<frame_block*>(memory::bitmap_start); frame_block *blocks = reinterpret_cast<frame_block*>(memory::bitmap_start);
new (&g_frame_allocator) frame_allocator {blocks, kargs.frame_blocks.count}; new (&g_frame_allocator) frame_allocator {blocks, kargs.frame_blocks.count};

View File

@@ -1,3 +1,4 @@
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kernel_memory.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); size_t avail = m_buffer.reserve(*len, &buffer);
*len = *len > avail ? avail : *len; *len = *len > avail ? avail : *len;
kutil::memcpy(buffer, data, *len); memcpy(buffer, data, *len);
m_buffer.commit(*len); m_buffer.commit(*len);
assert_signal(j6_signal_channel_can_recv); 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); size_t avail = m_buffer.get_block(&buffer);
*len = *len > avail ? avail : *len; *len = *len > avail ? avail : *len;
kutil::memcpy(data, buffer, *len); memcpy(data, buffer, *len);
m_buffer.consume(*len); m_buffer.consume(*len);
assert_signal(j6_signal_channel_can_send); assert_signal(j6_signal_channel_can_send);

View File

@@ -1,6 +1,8 @@
#include "j6/signals.h" #include "j6/signals.h"
#include "cpu.h" #include "cpu.h"
#include "log.h" #include "log.h"
#include "memory.h"
#include "objects/thread.h" #include "objects/thread.h"
#include "objects/process.h" #include "objects/process.h"
#include "objects/vm_area.h" #include "objects/vm_area.h"
@@ -40,7 +42,7 @@ thread::from_tcb(TCB *tcb)
{ {
static ptrdiff_t offset = static ptrdiff_t offset =
-1 * static_cast<ptrdiff_t>(offsetof(thread, m_tcb)); -1 * static_cast<ptrdiff_t>(offsetof(thread, m_tcb));
return reinterpret_cast<thread*>(kutil::offset_pointer(tcb, offset)); return reinterpret_cast<thread*>(offset_pointer(tcb, offset));
} }
thread & thread::current() { return *current_cpu().thread; } thread & thread::current() { return *current_cpu().thread; }

View File

@@ -1,8 +1,11 @@
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "console.h" #include "console.h"
#include "frame_allocator.h" #include "frame_allocator.h"
#include "kernel_memory.h" #include "kernel_memory.h"
#include "memory.h"
#include "page_table.h" #include "page_table.h"
using memory::page_offset; 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) page_table::iterator::iterator(const page_table::iterator &o)
{ {
kutil::memcpy(&m_table, &o.m_table, sizeof(m_table)); memcpy(&m_table, &o.m_table, sizeof(m_table));
kutil::memcpy(&m_index, &o.m_index, sizeof(m_index)); memcpy(&m_index, &o.m_index, sizeof(m_index));
} }
inline static level to_lv(unsigned i) { return static_cast<level>(i); } inline static level to_lv(unsigned i) { return static_cast<level>(i); }
@@ -190,7 +193,7 @@ page_table::get_table_page()
--s_cache_count; --s_cache_count;
} }
kutil::memset(page, 0, memory::frame_size); memset(page, 0, memory::frame_size);
return reinterpret_cast<page_table*>(page); return reinterpret_cast<page_table*>(page);
} }
@@ -220,11 +223,11 @@ page_table::fill_table_page_cache()
memory::to_virtual<free_page_header>(phys); memory::to_virtual<free_page_header>(phys);
for (int i = 0; i < n - 1; ++i) for (int i = 0; i < n - 1; ++i)
kutil::offset_pointer(start, i * memory::frame_size) offset_pointer(start, i * memory::frame_size)
->next = kutil::offset_pointer(start, (i+1) * memory::frame_size); ->next = offset_pointer(start, (i+1) * memory::frame_size);
free_page_header *end = 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; end->next = s_page_cache;
s_page_cache = start; s_page_cache = start;

View File

@@ -1,5 +1,7 @@
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "frame_allocator.h" #include "frame_allocator.h"
#include "kernel_memory.h" #include "kernel_memory.h"
#include "page_tree.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_base {base & level_mask(level)},
m_level {level} m_level {level}
{ {
kutil::memset(m_entries, 0, sizeof(m_entries)); memset(m_entries, 0, sizeof(m_entries));
} }
page_tree::~page_tree() page_tree::~page_tree()

View File

@@ -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 // Walk the extended capabilities list
uint8_t next = m_base[13] & 0xff; uint8_t next = m_base[13] & 0xff;
while (next) { while (next) {
pci_cap *cap = reinterpret_cast<pci_cap *>(kutil::offset_pointer(m_base, next)); pci_cap *cap = reinterpret_cast<pci_cap *>(offset_pointer(m_base, next));
next = cap->next; next = cap->next;
log::debug(logs::device, " - found PCI cap type %02x", cap->id); log::debug(logs::device, " - found PCI cap type %02x", cap->id);

View File

@@ -3,7 +3,7 @@
/// PCI devices and groups /// PCI devices and groups
#include <stdint.h> #include <stdint.h>
#include "kutil/memory.h" #include "memory.h"
struct pci_group; struct pci_group;
enum class isr : uint8_t; enum class isr : uint8_t;
@@ -126,7 +126,7 @@ struct pci_group
/// \returns A pointer to the memory-mapped configuration registers /// \returns A pointer to the memory-mapped configuration registers
inline uint32_t * base_for(uint8_t bus, uint8_t device, uint8_t func) 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); pci_device::bus_addr(bus, device, func) << 12);
} }

View File

@@ -1,6 +1,7 @@
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "kutil/no_construct.h" #include "kutil/no_construct.h"
#include "interrupts.h" #include "interrupts.h"
#include "io.h" #include "io.h"
#include "serial.h" #include "serial.h"
@@ -121,7 +122,7 @@ serial_port::do_write()
if (n > fifo_size) if (n > fifo_size)
n = fifo_size; n = fifo_size;
kutil::memcpy(tmp, data, n); memcpy(tmp, data, n);
m_out_buffer.consume(n); m_out_buffer.consume(n);
for (size_t i = 0; i < n; ++i) for (size_t i = 0; i < n; ++i)

View File

@@ -1,7 +1,7 @@
// vim: ft=cpp // vim: ft=cpp
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include "kutil/memory.h"
#include "console.h" #include "console.h"
#include "debug.h" #include "debug.h"
@@ -38,7 +38,7 @@ syscall_invalid(uint64_t call)
void void
syscall_initialize() syscall_initialize()
{ {
kutil::memset(&syscall_registry, 0, sizeof(syscall_registry)); memset(&syscall_registry, 0, sizeof(syscall_registry));
/*[[[cog code generation /*[[[cog code generation
for id, scope, method in syscalls.methods: for id, scope, method in syscalls.methods:

View File

@@ -1,5 +1,5 @@
#include <string.h>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "kutil/no_construct.h" #include "kutil/no_construct.h"
#include "cpu.h" #include "cpu.h"
@@ -17,7 +17,7 @@ TSS &g_bsp_tss = __g_bsp_tss_storage.value;
TSS::TSS() TSS::TSS()
{ {
kutil::memset(this, 0, sizeof(TSS)); memset(this, 0, sizeof(TSS));
m_iomap_offset = sizeof(TSS); m_iomap_offset = sizeof(TSS);
} }

View File

@@ -1,3 +1,5 @@
#include <string.h>
#include "frame_allocator.h" #include "frame_allocator.h"
#include "kernel_memory.h" #include "kernel_memory.h"
#include "log.h" #include "log.h"
@@ -38,7 +40,7 @@ vm_space::vm_space() :
m_pml4 = page_table::get_table_page(); m_pml4 = page_table::get_table_page();
page_table *kpml4 = kernel_space().m_pml4; 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) for (unsigned i = memory::pml4e_kernel; i < memory::table_entries; ++i)
m_pml4->entries[i] = kpml4->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 // TODO: iterate page mappings and continue copying. For now i'm blindly
// assuming both buffers are fully contained within single pages // assuming both buffers are fully contained within single pages
kutil::memcpy( memcpy(
memory::to_virtual<void>((*dit & ~0xfffull) | (ito & 0xffful)), memory::to_virtual<void>((*dit & ~0xfffull) | (ito & 0xffful)),
memory::to_virtual<void>((*sit & ~0xfffull) | (ifrom & 0xffful)), memory::to_virtual<void>((*sit & ~0xfffull) | (ifrom & 0xffful)),
length); length);

View File

@@ -10,9 +10,10 @@
/// http://codecapsule.com/2013/11/11/robin-hood-hashing/ /// http://codecapsule.com/2013/11/11/robin-hood-hashing/
/// http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/ /// http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/
#include <new>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "kutil/hash.h" #include "kutil/hash.h"
#include "kutil/memory.h"
#include "kutil/vector.h" #include "kutil/vector.h"
#include "kutil/util.h" #include "kutil/util.h"
@@ -87,7 +88,7 @@ public:
virtual ~base_map() { virtual ~base_map() {
for (size_t i = 0; i < m_capacity; ++i) for (size_t i = 0; i < m_capacity; ++i)
m_nodes[i].~node(); m_nodes[i].~node();
kfree(m_nodes); delete [] reinterpret_cast<uint8_t*>(m_nodes);
} }
iterator begin() { iterator begin() {
@@ -149,8 +150,8 @@ protected:
m_capacity = capacity; m_capacity = capacity;
const size_t size = m_capacity * sizeof(node); const size_t size = m_capacity * sizeof(node);
m_nodes = reinterpret_cast<node*>(kalloc(size)); m_nodes = reinterpret_cast<node*>(new uint8_t [size]);
kutil::memset(m_nodes, 0, size); memset(m_nodes, 0, size);
} }
void grow() { void grow() {
@@ -169,7 +170,7 @@ protected:
n.~node(); n.~node();
} }
kfree(old); delete [] reinterpret_cast<uint8_t*>(old);
} }
inline node * construct(size_t i, uint64_t h, K &&k, V &&v) { inline node * construct(size_t i, uint64_t h, K &&k, V &&v) {

View File

@@ -2,9 +2,9 @@
/// \file vector.h /// \file vector.h
/// Definition of a simple dynamic vector collection for use in kernel space /// Definition of a simple dynamic vector collection for use in kernel space
#include <string.h>
#include <utility> #include <utility>
#include "kutil/assert.h" #include "kutil/assert.h"
#include "kutil/memory.h"
#include "kutil/util.h" #include "kutil/util.h"
namespace kutil { namespace kutil {
@@ -42,7 +42,7 @@ public:
m_elements(nullptr) m_elements(nullptr)
{ {
set_capacity(other.m_capacity); 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; m_size = other.m_size;
} }
@@ -73,7 +73,7 @@ public:
bool was_static = m_capacity & ~cap_mask; bool was_static = m_capacity & ~cap_mask;
if (!was_static) if (!was_static)
kfree(m_elements); delete [] m_elements;
} }
/// Get the size of the array. /// Get the size of the array.
@@ -271,17 +271,18 @@ public:
void set_capacity(count_t capacity) void set_capacity(count_t capacity)
{ {
bool was_static = m_capacity & ~cap_mask; bool was_static = m_capacity & ~cap_mask;
T *new_array = reinterpret_cast<T*>(kalloc(capacity * sizeof(T))); T *new_array = reinterpret_cast<T*>(new uint8_t [capacity * sizeof(T)]);
count_t size = capacity > m_size ? m_size : capacity; 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(); while (size < m_size) remove();
m_size = size; m_size = size;
m_capacity = capacity; m_capacity = capacity;
if (!was_static) if (!was_static)
kfree(m_elements); delete [] m_elements;
m_elements = new_array; m_elements = new_array;
} }

View File

@@ -6,7 +6,5 @@ module("kutil",
sources = [ sources = [
"assert.cpp", "assert.cpp",
"bip_buffer.cpp", "bip_buffer.cpp",
"heap_allocator.cpp",
"memory.cpp",
"spinlock.cpp", "spinlock.cpp",
]) ])

View File

@@ -5,8 +5,10 @@
Permission is granted to use, modify, and / or redistribute at will. Permission is granted to use, modify, and / or redistribute at will.
*/ */
#ifndef __cplusplus
#define alignas _Alignas #define alignas _Alignas
#define alignof _Alignof #define alignof _Alignof
#endif
#define __alignas_is_defined 1 #define __alignas_is_defined 1
#define __alignof_is_defined 1 #define __alignof_is_defined 1