Working i386 paging and liballoc

This commit is contained in:
2025-12-07 00:53:33 +01:00
commit de5350010b
75 changed files with 6716 additions and 0 deletions

View File

@@ -0,0 +1 @@
*.o

View File

@@ -0,0 +1,198 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libk/types.h>
#include <libk/util.h>
#include <libk/string.h>
#include <libk/stdatomic.h>
#include <sys/cpu.h>
#include <sys/gdt_flush.h>
#include <sys/tss_flush.h>
#include <sys/idt_flush.h>
#include <sys/isr.h>
#include <sys/pic.h>
static volatile struct gdt_entry gdt[6];
static volatile struct gdt_ptr gdtptr;
static volatile struct tss tss;
static volatile struct idt_entry idt[0x100];
static volatile struct idt_ptr idtptr;
static struct { volatile uint32_t eflags; atomic_int nesting; } intr_state =
{ .eflags = 0, .nesting = 0 };
static void gdt_make_entry(int index, uint32_t base, uint32_t limit,
uint8_t access, uint8_t gran) {
gdt[index].base_low = (base & 0xFFFF);
gdt[index].base_mid = (base >> 16) & 0xFF;
gdt[index].base_up = (base >> 24) & 0xFF;
gdt[index].limit_low = (limit & 0xFFFF);
gdt[index].gran = ((limit >> 16) & 0x0F);
gdt[index].gran |= (gran & 0xF0);
gdt[index].access = access;
}
static void gdt_init(void) {
gdtptr.limit = ELEM_SIZE(gdt) * LEN(gdt) - 1;
gdtptr.base = (uint32_t)&gdt;
gdt_make_entry(0, 0, 0, 0, 0);
gdt_make_entry(1, 0, 0xFFFFF, 0x9A, 0xCF);
gdt_make_entry(2, 0, 0xFFFFF, 0x92, 0xCF);
gdt_make_entry(3, 0, 0xFFFFF, 0xFA, 0xCF);
gdt_make_entry(4, 0, 0xFFFFF, 0xF2, 0xCF);
gdt_flush((ptr_t)&gdtptr);
}
static void tss_init(void) {
uint32_t base = (uint32_t)&tss;
gdt_make_entry(5, base, base + sizeof(struct tss), 0xE9, 0);
memset((void *)&tss, 0, sizeof(tss));
uptr_t sp;
__asm__ volatile("mov %%esp, %0" : "=r"(sp));
tss.ss0 = 0x10;
tss.esp0 = (uint32_t)sp;
tss.cs = 0x08 | 0x3;
tss.ds = 0x10 | 0x3;
tss.es = 0x10 | 0x3;
tss.fs = 0x10 | 0x3;
tss.fs = 0x10 | 0x3;
tss.ss = 0x10 | 0x3;
tss_flush();
}
static void idt_make_entry(int index, uint32_t base, uint32_t sel, uint8_t flags) {
volatile struct idt_entry *ent = &idt[index];
ent->base_low = (base & 0xFFFF);
ent->base_up = ((base >> 16) & 0xFFFF);
ent->always0 = 0;
ent->sel = sel;
ent->flags = flags | 0x60;
}
void idt_init(void) {
memset((void *)idt, 0, sizeof(idt));
idtptr.base = (uint32_t)idt;
idtptr.limit = sizeof(idt) - 1;
idt_make_entry(0, (uint32_t)&except0, 0x08, 0x8E);
idt_make_entry(1, (uint32_t)&except1, 0x08, 0x8E);
idt_make_entry(2, (uint32_t)&except2, 0x08, 0x8E);
idt_make_entry(3, (uint32_t)&except3, 0x08, 0x8E);
idt_make_entry(4, (uint32_t)&except4, 0x08, 0x8E);
idt_make_entry(5, (uint32_t)&except5, 0x08, 0x8E);
idt_make_entry(6, (uint32_t)&except6, 0x08, 0x8E);
idt_make_entry(7, (uint32_t)&except7, 0x08, 0x8E);
idt_make_entry(8, (uint32_t)&except8, 0x08, 0x8E);
idt_make_entry(9, (uint32_t)&except9, 0x08, 0x8E);
idt_make_entry(10, (uint32_t)&except10, 0x08, 0x8E);
idt_make_entry(11, (uint32_t)&except11, 0x08, 0x8E);
idt_make_entry(12, (uint32_t)&except12, 0x08, 0x8E);
idt_make_entry(13, (uint32_t)&except13, 0x08, 0x8E);
idt_make_entry(14, (uint32_t)&except14, 0x08, 0x8E);
idt_make_entry(15, (uint32_t)&except15, 0x08, 0x8E);
idt_make_entry(16, (uint32_t)&except16, 0x08, 0x8E);
idt_make_entry(17, (uint32_t)&except17, 0x08, 0x8E);
idt_make_entry(18, (uint32_t)&except18, 0x08, 0x8E);
idt_make_entry(19, (uint32_t)&except19, 0x08, 0x8E);
idt_make_entry(20, (uint32_t)&except20, 0x08, 0x8E);
idt_make_entry(21, (uint32_t)&except21, 0x08, 0x8E);
idt_make_entry(22, (uint32_t)&except22, 0x08, 0x8E);
idt_make_entry(23, (uint32_t)&except23, 0x08, 0x8E);
idt_make_entry(24, (uint32_t)&except24, 0x08, 0x8E);
idt_make_entry(25, (uint32_t)&except25, 0x08, 0x8E);
idt_make_entry(26, (uint32_t)&except26, 0x08, 0x8E);
idt_make_entry(27, (uint32_t)&except27, 0x08, 0x8E);
idt_make_entry(28, (uint32_t)&except28, 0x08, 0x8E);
idt_make_entry(29, (uint32_t)&except29, 0x08, 0x8E);
idt_make_entry(30, (uint32_t)&except30, 0x08, 0x8E);
idt_make_entry(31, (uint32_t)&except31, 0x08, 0x8E);
idt_make_entry(32, (uint32_t)&irq0, 0x08, 0x8E);
idt_make_entry(33, (uint32_t)&irq1, 0x08, 0x8E);
idt_make_entry(34, (uint32_t)&irq2, 0x08, 0x8E);
idt_make_entry(35, (uint32_t)&irq3, 0x08, 0x8E);
idt_make_entry(36, (uint32_t)&irq4, 0x08, 0x8E);
idt_make_entry(37, (uint32_t)&irq5, 0x08, 0x8E);
idt_make_entry(38, (uint32_t)&irq6, 0x08, 0x8E);
idt_make_entry(39, (uint32_t)&irq7, 0x08, 0x8E);
idt_make_entry(40, (uint32_t)&irq8, 0x08, 0x8E);
idt_make_entry(41, (uint32_t)&irq9, 0x08, 0x8E);
idt_make_entry(42, (uint32_t)&irq10, 0x08, 0x8E);
idt_make_entry(43, (uint32_t)&irq11, 0x08, 0x8E);
idt_make_entry(44, (uint32_t)&irq12, 0x08, 0x8E);
idt_make_entry(45, (uint32_t)&irq13, 0x08, 0x8E);
idt_make_entry(46, (uint32_t)&irq14, 0x08, 0x8E);
idt_make_entry(47, (uint32_t)&irq15, 0x08, 0x8E);
idt_make_entry(128, (uint32_t)&except128, 0x08, 0xEE);
idt_flush((ptr_t)&idtptr);
}
void cpu_init(void) {
gdt_init();
tss_init();
pic_init();
idt_init();
}
static uint32_t intr_save1(void) {
uint32_t eflags;
__asm__ volatile ("pushfl; cli; popl %0;" : "=r"(eflags) :: "memory", "cc");
return eflags;
}
static void intr_restore1(uint32_t eflags) {
if (eflags & (1 << 9)) {
__asm__ volatile("sti" ::: "memory", "cc");
}
}
void intr_save(void) {
int prev = atomic_fetch_add_explicit(&intr_state.nesting, 1, memory_order_acq_rel);
if (prev == 0) {
intr_state.eflags = intr_save1();
}
}
void intr_restore(void) {
int prev = atomic_fetch_sub_explicit(&intr_state.nesting, 1, memory_order_acq_rel);
if (prev == 1) {
intr_restore1(intr_state.eflags);
}
}
void cpu_relax(void) {
__asm__ volatile("pause");
}

View File

@@ -0,0 +1,93 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_CPU_H
#define _SYS_CPU_H
#include <libk/types.h>
#include <libk/compiler.h>
struct gdt_entry {
uint16_t limit_low;
uint16_t base_low;
uint8_t base_mid;
uint8_t access;
uint8_t gran;
uint8_t base_up;
} packed;
struct gdt_ptr {
uint16_t limit;
uint32_t base;
} packed;
struct tss {
uint32_t link;
uint32_t esp0, ss0;
uint32_t esp1, ss1;
uint32_t esp2, ss2;
uint32_t cr3;
uint32_t eip, eflags, eax, ecx, edx, ebx, esp, ebp, esi, edi;
uint32_t es, cs, ss, ds, fs, gs;
uint32_t ldt;
uint16_t trap;
uint16_t iomap;
} packed;
struct idt_entry {
uint16_t base_low;
uint16_t sel;
uint8_t always0;
uint8_t flags;
uint16_t base_up;
} packed;
struct idt_ptr {
uint16_t limit;
uint32_t base;
} packed;
struct trapframe {
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
uint32_t ds;
uint32_t cr3;
uint32_t trapno;
uint32_t ec;
uint32_t eip, cs, eflags, uesp, uss;
} packed;
void cpu_init(void);
void intr_save(void);
void intr_restore(void);
void cpu_relax(void);
#endif // _SYS_CPU_H

View File

@@ -0,0 +1,46 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
.section .text
.global gdt_flush
.type gdt_flush, @function
gdt_flush:
mov 4(%esp), %eax
lgdt (%eax)
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
ljmp $0x08, $1f
1:
ret

View File

@@ -0,0 +1,39 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_GDT_FLUSH_H
#define _SYS_GDT_FLUSH_H
#include <libk/types.h>
void gdt_flush(ptr_t gdtptr);
#endif // _SYS_GDT_FLUSH_H

View File

@@ -0,0 +1,38 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
.section .text
.global halt
.type halt, @function
halt:
cli
hlt
jmp halt

View File

@@ -0,0 +1,37 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_HALT_H
#define _SYS_HALT_H
void halt(void);
#endif // _SYS_HALT_H

View File

@@ -0,0 +1,39 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
.section .text
.global idt_flush
.type idt_flush, @function
idt_flush:
mov 4(%esp), %eax
lidt (%eax)
sti
ret

View File

@@ -0,0 +1,39 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_IDT_FLUSH_H
#define _SYS_IDT_FLUSH_H
#include <libk/types.h>
void idt_flush(ptr_t idtptr);
#endif // _SYS_IDT_FLUSH_H

View File

@@ -0,0 +1,85 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libk/types.h>
#include <sys/ioport.h>
uint8_t ioport_in8(uint16_t port) {
uint8_t r;
__asm__ volatile("inb %1, %0" : "=a"(r) : "dN"(port));
return r;
}
void ioport_out8(uint16_t port, uint8_t value) {
__asm__ volatile("outb %1, %0" :: "dN"(port), "a"(value));
}
uint16_t ioport_in16(uint16_t port) {
uint16_t r;
__asm__ volatile("in %%dx, %%ax" : "=a"(r) : "d"(port));
return r;
}
void ioport_out16(uint16_t port, uint16_t value) {
__asm__ volatile("out %%ax, %%dx" :: "a"(value), "d"(port));
}
uint32_t ioport_in32(uint16_t port) {
uint32_t r;
__asm__ volatile("inl %%dx, %%eax" : "=a"(r) : "d"(port));
return r;
}
void ioport_out32(uint16_t port, uint32_t value) {
__asm__ volatile("outl %%eax, %%dx" :: "d"(port), "a"(value));
}
void ioport_ins16(uint16_t port, void *addr, int cnt) {
__asm__ volatile(
"cld; rep insw"
: "+D"(addr), "+c"(cnt)
: "d"(port)
: "memory", "cc"
);
}
void ioport_outs16(uint16_t port, const void *addr, int cnt) {
__asm__ volatile(
"cld; rep outsw"
: "+S"(addr), "+c"(cnt)
: "d"(port)
: "memory", "cc"
);
}
void ioport_wait(void) {
ioport_out8(0x80, 0);
}

View File

@@ -0,0 +1,48 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_IOPORT_H
#define _SYS_IOPORT_H
#include <libk/types.h>
uint8_t ioport_in8(uint16_t port);
void ioport_out8(uint16_t port, uint8_t value);
uint16_t ioport_in16(uint16_t port);
void ioport_out16(uint16_t port, uint16_t value);
uint32_t ioport_in32(uint16_t port);
void ioport_out32(uint16_t port, uint32_t value);
void ioport_ins16(uint16_t port, void *addr, int cnt);
void ioport_outs16(uint16_t port, const void *addr, int cnt);
void ioport_wait(void);
#endif // _SYS_IOPORT_H

View File

@@ -0,0 +1,182 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
.extern except_fini
.extern irq_fini
.section .text
#define EXCEPT_NOERR(exc) \
.global except ## exc; \
.type except ## exc, @function; \
except ## exc:; \
cli; \
push $0; \
push $exc; \
jmp temp_except_hndlr;
#define EXCEPT_ERR(exc) \
.global except ## exc; \
.type except ## exc, @function; \
except ## exc:; \
cli; \
push $exc; \
jmp temp_except_hndlr;
EXCEPT_NOERR(0)
EXCEPT_NOERR(1)
EXCEPT_NOERR(2)
EXCEPT_NOERR(3)
EXCEPT_NOERR(4)
EXCEPT_NOERR(5)
EXCEPT_NOERR(6)
EXCEPT_NOERR(7)
EXCEPT_ERR(8)
EXCEPT_NOERR(9)
EXCEPT_ERR(10)
EXCEPT_ERR(11)
EXCEPT_ERR(12)
EXCEPT_ERR(13)
EXCEPT_ERR(14)
EXCEPT_NOERR(15)
EXCEPT_NOERR(16)
EXCEPT_NOERR(17)
EXCEPT_NOERR(18)
EXCEPT_NOERR(19)
EXCEPT_NOERR(20)
EXCEPT_NOERR(21)
EXCEPT_NOERR(22)
EXCEPT_NOERR(23)
EXCEPT_NOERR(24)
EXCEPT_NOERR(25)
EXCEPT_NOERR(26)
EXCEPT_NOERR(27)
EXCEPT_NOERR(28)
EXCEPT_NOERR(29)
EXCEPT_NOERR(30)
EXCEPT_NOERR(31)
EXCEPT_NOERR(128)
temp_except_hndlr:
pushal /* push eax, ecx, edx, ebx, esp, ebp, esi, edi (8*4) */
xorl %eax, %eax
mov %ds, %ax
push %eax /* save ds (4) */
/* load kernel DS */
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %cr3, %ecx
push %ecx
/* save frame (4) */
push %esp
call except_fini
add $0x04, %esp
/* restore DS */
pop %ebx
mov %bx, %ds
mov %bx, %es
mov %bx, %fs
mov %bx, %gs
/* rebalance */
popal
add $0x8, %esp
sti
iret
#define IRQ(irq1) \
.global irq ## irq1; \
.type irq ## irq1, @function; \
irq ## irq1:; \
cli; \
push $0; \
push $irq1; \
jmp temp_irq_hndlr;
IRQ(0)
IRQ(1)
IRQ(2)
IRQ(3)
IRQ(4)
IRQ(5)
IRQ(6)
IRQ(7)
IRQ(8)
IRQ(9)
IRQ(10)
IRQ(11)
IRQ(12)
IRQ(13)
IRQ(14)
IRQ(15)
temp_irq_hndlr:
pushal /* push eax, ecx, edx, ebx, esp, ebp, esi, edi (8*4) */
xorl %eax, %eax
mov %ds, %ax
push %eax /* save ds (4) */
/* load kernel DS */
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %cr3, %ecx
push %ecx
/* save frame (4) */
push %esp
call irq_fini
add $0x04, %esp
/* restore DS */
pop %ebx
mov %bx, %ds
mov %bx, %es
mov %bx, %fs
mov %bx, %gs
/* rebalance */
popal
add $0x8, %esp
sti
iret

View File

@@ -0,0 +1,86 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_ISR_H
#define _SYS_ISR_H
void except0(void);
void except1(void);
void except2(void);
void except3(void);
void except4(void);
void except5(void);
void except6(void);
void except7(void);
void except8(void);
void except9(void);
void except10(void);
void except11(void);
void except12(void);
void except13(void);
void except14(void);
void except15(void);
void except16(void);
void except17(void);
void except18(void);
void except19(void);
void except20(void);
void except21(void);
void except22(void);
void except23(void);
void except24(void);
void except25(void);
void except26(void);
void except27(void);
void except28(void);
void except29(void);
void except30(void);
void except31(void);
void except128(void);
void irq0(void);
void irq1(void);
void irq2(void);
void irq3(void);
void irq4(void);
void irq5(void);
void irq6(void);
void irq7(void);
void irq8(void);
void irq9(void);
void irq10(void);
void irq11(void);
void irq12(void);
void irq13(void);
void irq14(void);
void irq15(void);
#endif // _SYS_ISR_H

View File

@@ -0,0 +1,54 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libk/types.h>
#include <sys/cpu.h>
#include <sys/serialdbg.h>
#include <sys/halt.h>
#include <config.h>
void except_fini(struct trapframe *tf) {
dbgf("EXCEPTION %u\n", tf->trapno);
/* dbgf("trapframe:\n"); */
/* dbgf("ds =%08x, edi=%08x, esi=%08x, ebp=%08x\n", */
/* tf->ds, tf->edi, tf->esi, tf->ebp); */
/* dbgf("esp=%08x, ebx=%08x, edx=%08x, ecx=%08x\n", */
/* tf->esp, tf->ebx, tf->edx, tf->ecx); */
/* dbgf("trp=%08x, erc=%08x, eip=%08x, cs =%08x\n", */
/* tf->trapno, tf->ec, tf->eip, tf->cs); */
/* dbgf("efl=%08x, usp=%08x, uss=%08x, cr3=%08x\n", */
/* tf->eflags, tf->uesp, tf->uss, tf->cr3); */
halt();
}
void irq_fini(struct trapframe *tf) {
dbgf("IRQ %u\n", tf->trapno);
}

View File

@@ -0,0 +1,28 @@
dir_sys := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
c_files += $(dir_sys)/cpu.c \
$(dir_sys)/multiboot.c \
$(dir_sys)/serialdbg.c \
$(dir_sys)/ioport.c \
$(dir_sys)/isr1.c \
$(dir_sys)/pic.c \
$(dir_sys)/mm.c
S_files += $(dir_sys)/gdt_flush.S \
$(dir_sys)/tss_flush.S \
$(dir_sys)/idt_flush.S \
$(dir_sys)/isr.S \
$(dir_sys)/halt.S
o_files += $(dir_sys)/cpu.o \
$(dir_sys)/multiboot.o \
$(dir_sys)/gdt_flush.o \
$(dir_sys)/tss_flush.o \
$(dir_sys)/idt_flush.o \
$(dir_sys)/isr.o \
$(dir_sys)/serialdbg.o \
$(dir_sys)/ioport.o \
$(dir_sys)/isr1.o \
$(dir_sys)/halt.o \
$(dir_sys)/pic.o \
$(dir_sys)/mm.o

View File

@@ -0,0 +1,158 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libk/util.h>
#include <libk/types.h>
#include <libk/string.h>
#include <sys/mm.h>
#include <sync/spinlock.h>
#include <mm/bba.h>
#include <mm/pmm.h>
#include <config.h>
#define PD_INDEX(va) (((va) >> 22) & 0x3FF)
#define PT_INDEX(va) (((va) >> 12) & 0x3FF)
extern char __init_pd;
static struct page_dir *init_pd;
struct page_dir *mm_get_kernel_pd(void) { return init_pd; }
static struct page_dir *mm_alloc_pd(void) {
struct page_dir *pd = (struct page_dir *)bba_alloc();
memset(pd, 0, sizeof(*pd));
sl_init(&pd->sl, "pd");
pd->pd = (volatile uint32_t *)bba_alloc();
return pd;
}
static struct page_dir *mm_alloc_existing_pd(uptr_t ptr) {
struct page_dir *pd = (struct page_dir *)bba_alloc();
memset(pd, 0, sizeof(*pd));
sl_init(&pd->sl, "pd");
pd->pd = (volatile uint32_t *)ptr;
return pd;
}
void mm_map_page(struct page_dir *pd, uptr_t vaddr, uptr_t paddr, uint32_t flags) {
if (!(flags & PF_PRESENT))
return;
if (flags & PF_LOCK)
sl_lock(&pd->sl);
uint32_t pd_idx = PD_INDEX(vaddr);
uint32_t pt_idx = PT_INDEX(vaddr);
volatile uint32_t *pt;
if (!(pd->pd[pd_idx] & PF_PRESENT)) {
pt = (volatile uint32_t *)bba_alloc();
if (pt == 0) {
if (flags & PF_LOCK)
sl_unlock(&pd->sl);
dbgf("mm_map_page(): run out of BBA memory to alloc a PD\n");
return;
}
for (usize_t i = 0; i < PAGE_SIZE/sizeof(uint32_t); i++)
pt[i] = 0;
uptr_t phys_pt = (uptr_t)pt - VIRT_BASE;
pd->pd[pd_idx] = phys_pt | PF_PRESENT | PF_WRITABLE;
} else {
uptr_t pt_phys = pd->pd[pd_idx] & ~(PAGE_SIZE - 1);
pt = (volatile uint32_t *)(pt_phys + VIRT_BASE);
}
pt[pt_idx] = (paddr & ~(PAGE_SIZE - 1)) | (flags & ~PF_LOCK);
if (flags & PF_LOCK)
sl_unlock(&pd->sl);
}
void mm_unmap_page(struct page_dir *pd, uptr_t vaddr, uint32_t flags) {
if (flags & PF_LOCK)
sl_lock(&pd->sl);
uint32_t pd_idx = PD_INDEX(vaddr);
uint32_t pt_idx = PT_INDEX(vaddr);
volatile uint32_t *pt;
if (!(pd->pd[pd_idx] & PF_PRESENT)) {
if (flags & PF_LOCK)
sl_unlock(&pd->sl);
return;
} else {
uptr_t pt_phys = pd->pd[pd_idx] & ~(PAGE_SIZE - 1);
pt = (volatile uint32_t *)(pt_phys + VIRT_BASE);
}
pt[pt_idx] &= ~PF_PRESENT;
__asm__ volatile ("invlpg (%0)" :: "r"(vaddr) : "memory");
if (flags & PF_LOCK)
sl_unlock(&pd->sl);
}
uptr_t mm_translate(struct page_dir *pd, uptr_t vaddr, uint32_t flags) {
if (flags & PF_LOCK)
sl_lock(&pd->sl);
uint32_t pd_idx = PD_INDEX(vaddr);
uint32_t pt_idx = PT_INDEX(vaddr);
volatile uint32_t *pt;
uptr_t paddr = 0;
if (!(pd->pd[pd_idx] & PF_PRESENT))
goto done;
uptr_t pt_phys = pd->pd[pd_idx] & ~(PAGE_SIZE - 1);
pt = (volatile uint32_t *)(pt_phys + VIRT_BASE);
if (!(pt[pt_idx] & PF_PRESENT))
goto done;
paddr = (pt[pt_idx] & ~(PAGE_SIZE - 1)) | (vaddr & (PAGE_SIZE - 1));
done:
if (flags & PF_LOCK)
sl_unlock(&pd->sl);
return paddr;
}
void mm_load_pd(struct page_dir *pd) {
uptr_t phys = (uptr_t)pd->pd - VIRT_BASE;
__asm__ volatile ("movl %0, %%cr3" :: "r"(phys));
}
void mm_init(void) {
init_pd = mm_alloc_existing_pd((uptr_t)&__init_pd);
}

View File

@@ -0,0 +1,67 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_MM_H
#define _SYS_MM_H
#include <libk/util.h>
#include <libk/types.h>
#include <libk/compiler.h>
#include <sync/spinlock.h>
#define VIRT_BASE 0xC0000000
#define VIRT(x) (((uptr_t)(x)) + VIRT_BASE)
#define PAGE_SIZE 0x1000
#define PF_PRESENT (1<<0)
#define PF_WRITABLE (1<<1)
#define PF_USER (1<<2)
#define PF_LOCK (1<<31) /* Special flag for internal usage that doesn't exist in x86.
I use this here, because when (un)mapping an entire page range
it would be inefficient to constantly (un)lock. */
#define KERNEL_HEAP_START 0xF0000000
struct page_dir {
volatile uint32_t *pd;
struct spinlock sl;
};
void mm_init(void);
void mm_map_page(struct page_dir *pd, uptr_t vaddr, uptr_t paddr, uint32_t flags);
void mm_unmap_page(struct page_dir *pd, uptr_t vaddr, uint32_t flags);
uptr_t mm_translate(struct page_dir *pd, uptr_t vaddr, uint32_t flags);
void mm_load_pd(struct page_dir *pd);
struct page_dir *mm_get_kernel_pd(void);
#endif // _SYS_MM_H

View File

@@ -0,0 +1,42 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/multiboot.h>
struct multiboot *mb;
void multiboot_set(struct multiboot *mb1) {
mb = mb1;
}
struct multiboot *multiboot_get(void) {
return mb;
}

View File

@@ -0,0 +1,48 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_MULTIBOOT_H
#define _SYS_MULTIBOOT_H
#include <libk/types.h>
#include <libk/compiler.h>
struct multiboot {
uint32_t flags;
uint32_t mem_low;
uint32_t mem_up;
uint32_t boot_dev;
} packed;
void multiboot_set(struct multiboot *mb);
struct multiboot *multiboot_get(void);
#endif // _SYS_MULTIBOOT_H

View File

@@ -0,0 +1,84 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/pic.h>
#include <sys/ioport.h>
#define PIC1 0x20
#define PIC2 0xA0
#define PIC1_CMD PIC1
#define PIC1_DATA (PIC1 + 1)
#define PIC2_CMD PIC2
#define PIC2_DATA (PIC2 + 1)
#define ICW1_ICW4 0x01
#define ICW1_SINGLE 0x02
#define ICW1_INTVL4 0x04
#define ICW1_LEVEL 0x08
#define ICW1_INIT 0x10
#define ICW4_8086 0x01
#define ICW4_AUTO 0x02
#define ICW4_BUFSLAVE 0x08
#define ICW4_BUFMASER 0x0C
#define ICW4_SFNM 0x10
#define CASCADE_IRQ 2
void pic_init(void) {
// remap & disable
ioport_out8(PIC1_CMD, ICW1_INIT | ICW1_ICW4);
ioport_wait();
ioport_out8(PIC2_CMD, ICW1_INIT | ICW1_ICW4);
ioport_wait();
ioport_out8(PIC1_DATA, 4);
ioport_wait();
ioport_out8(PIC2_DATA, 2);
ioport_wait();
ioport_out8(PIC1_DATA, 1<<CASCADE_IRQ);
ioport_wait();
ioport_out8(PIC2_DATA, 2);
ioport_wait();
ioport_out8(PIC1_DATA, ICW4_8086);
ioport_wait();
ioport_out8(PIC2_DATA, ICW4_8086);
ioport_wait();
ioport_out8(PIC1_DATA, 0xFF);
ioport_out8(PIC2_DATA, 0xFF);
}

View File

@@ -0,0 +1,37 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_PIC_H
#define _SYS_PIC_H
void pic_init(void);
#endif // _SYS_PIC_H

View File

@@ -0,0 +1,76 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libk/types.h>
#include <libk/string.h>
#include <libk/stdarg.h>
#include <libk/printf.h>
#include <sys/serialdbg.h>
#include <sys/ioport.h>
#include <config.h>
static uint8_t serialdbg_tx_empty(void) {
return ioport_in8(CFG_I386_PC_DEBUG_SERIAL_PORT + 5) & 0x20;
}
static void serialdbg_writechr(char x) {
while (!serialdbg_tx_empty());
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT, (uint8_t)x);
}
void serialdbg_printf(const char *fmt, ...) {
char buffer[CFG_I386_PC_DEBUG_SERIAL_BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
buffer[sizeof(buffer) - 1] = '\0';
const char *p = buffer;
while (*p) {
serialdbg_writechr(*p);
p++;
}
}
void serialdbg_init(void) {
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT + 1, 0x00);
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT + 3, 0x80);
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT + 0, 0x03);
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT + 1, 0x00);
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT + 3, 0x03);
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT + 2, 0xC7);
ioport_out8(CFG_I386_PC_DEBUG_SERIAL_PORT + 4, 0x0B);
}

View File

@@ -0,0 +1,40 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_SERIALDBG_H
#define _SYS_SERIALDBG_H
#include <libk/types.h>
void serialdbg_init(void);
void serialdbg_printf(const char *fmt, ...);
#endif // _SYS_SERIALDBG_H

View File

@@ -0,0 +1,38 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
.section .text
.global tss_flush
.type tss_flush, @function
tss_flush:
mov $0x28, %ax
ltr %ax
ret

View File

@@ -0,0 +1,37 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_TSS_FLUSH_H
#define _SYS_TSS_FLUSH_H
void tss_flush(void);
#endif // _SYS_TSS_FLUSH_H