Compare commits
56 Commits
e50f8940a9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 38e26a9c12 | |||
| 124aa12f5b | |||
| d2f5c032d9 | |||
| 73e42588fb | |||
| e78bfb9984 | |||
| d2a88b3641 | |||
| fdda2e2df8 | |||
| 388418a718 | |||
| 1c64d608bd | |||
| 3d23187acf | |||
| a3b62ebd3d | |||
| 8bda300f6a | |||
| cf51600c6a | |||
| b388b30b24 | |||
| 600886a7ee | |||
| 67b66f2b39 | |||
| 18f791222e | |||
| 5e16bb647c | |||
| a68373e4ee | |||
| 8650010992 | |||
| 95f590fb3b | |||
| 7bb3b77ede | |||
| c26fd3cb2b | |||
| fea0999726 | |||
| 7eceecf6e3 | |||
| fff51321bc | |||
| a29233f853 | |||
| 38a43b59b0 | |||
| ddafc4eb19 | |||
| 4f7077d458 | |||
| 9a7dbf0594 | |||
| ab8093cc6c | |||
| ddbb66b5e4 | |||
| 11a1eb52aa | |||
| a054257336 | |||
| 9fc8521e63 | |||
| 711da8aeab | |||
| ebd9f0cac6 | |||
| 7cd5623d36 | |||
| 270ff507d4 | |||
| 55166f9d5f | |||
| e5cc3a64d3 | |||
| 2ab308d678 | |||
| d1d772cb42 | |||
| 0d8f9e565f | |||
| f80a26e5eb | |||
| 5bf10c1218 | |||
| 41a458b925 | |||
| 6a474c21a0 | |||
| a5283283f6 | |||
| 79768d94e6 | |||
| 0555ddd041 | |||
| ebb026b807 | |||
| d7b734306f | |||
| 28aef30f77 | |||
| 9f107a1a5e |
@@ -25,7 +25,7 @@ jobs:
|
|||||||
- name: Install mkdocs
|
- name: Install mkdocs
|
||||||
run: |
|
run: |
|
||||||
pip install --upgrade pip
|
pip install --upgrade pip
|
||||||
pip install mkdocs mkdocs-material
|
pip install mkdocs mkdocs-material pymdown-extensions
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: make docs
|
run: make docs
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -4,4 +4,4 @@ include make/apps.mk
|
|||||||
include make/kernel.mk
|
include make/kernel.mk
|
||||||
include make/dist.mk
|
include make/dist.mk
|
||||||
include make/docs.mk
|
include make/docs.mk
|
||||||
include make/libc.mk
|
include make/libmsl.mk
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ cflags += --target=x86_64-pc-none-elf \
|
|||||||
-mno-avx \
|
-mno-avx \
|
||||||
-mno-mmx \
|
-mno-mmx \
|
||||||
-mno-80387 \
|
-mno-80387 \
|
||||||
-mno-red-zone
|
-mno-red-zone \
|
||||||
|
-mcmodel=large
|
||||||
|
|
||||||
ldflags += --target=x86_64-pc-none-elf \
|
ldflags += --target=x86_64-pc-none-elf \
|
||||||
-Wl,-zmax-page-size=0x1000
|
-Wl,-zmax-page-size=0x1000
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ PHDRS {
|
|||||||
text PT_LOAD;
|
text PT_LOAD;
|
||||||
rodata PT_LOAD;
|
rodata PT_LOAD;
|
||||||
data PT_LOAD;
|
data PT_LOAD;
|
||||||
|
bss PT_LOAD;
|
||||||
|
tls PT_TLS;
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTIONS {
|
SECTIONS {
|
||||||
@@ -13,32 +15,53 @@ SECTIONS {
|
|||||||
|
|
||||||
.text : {
|
.text : {
|
||||||
*(.text .text.*)
|
*(.text .text.*)
|
||||||
|
*(.ltext .ltext.*)
|
||||||
} :text
|
} :text
|
||||||
|
|
||||||
. = ALIGN(CONSTANT(MAXPAGESIZE));
|
. = ALIGN(0x1000);
|
||||||
|
|
||||||
.rodata : {
|
.rodata : {
|
||||||
*(.rodata .rodata.*)
|
*(.rodata .rodata.*)
|
||||||
} :rodata
|
} :rodata
|
||||||
|
|
||||||
.note.gnu.build-id : {
|
. = ALIGN(0x1000);
|
||||||
*(.note.gnu.build-id)
|
|
||||||
} :rodata
|
|
||||||
|
|
||||||
. = ALIGN(CONSTANT(MAXPAGESIZE));
|
|
||||||
|
|
||||||
.data : {
|
.data : {
|
||||||
*(.data .data.*)
|
*(.data .data.*)
|
||||||
|
*(.ldata .ldata.*)
|
||||||
} :data
|
} :data
|
||||||
|
|
||||||
|
. = ALIGN(0x1000);
|
||||||
|
|
||||||
__bss_start = .;
|
__bss_start = .;
|
||||||
|
|
||||||
.bss : {
|
.bss : {
|
||||||
*(.bss .bss.*)
|
*(.bss .bss.*)
|
||||||
} :data
|
*(.lbss .lbss.*)
|
||||||
|
} :bss
|
||||||
|
|
||||||
__bss_end = .;
|
__bss_end = .;
|
||||||
|
|
||||||
|
. = ALIGN(0x1000);
|
||||||
|
|
||||||
|
__tdata_start = .;
|
||||||
|
|
||||||
|
.tdata : {
|
||||||
|
*(.tdata .tdata.*)
|
||||||
|
} :tls
|
||||||
|
|
||||||
|
__tdata_end = .;
|
||||||
|
|
||||||
|
__tbss_start = .;
|
||||||
|
|
||||||
|
.tbss : {
|
||||||
|
*(.tbss .tbss.*)
|
||||||
|
} :tls
|
||||||
|
|
||||||
|
__tbss_end = .;
|
||||||
|
|
||||||
|
__tls_size = __tbss_end - __tdata_start;
|
||||||
|
|
||||||
/DISCARD/ : {
|
/DISCARD/ : {
|
||||||
*(.eh_frame*)
|
*(.eh_frame*)
|
||||||
*(.note .note.*)
|
*(.note .note.*)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
cpu: model=p4_prescott_celeron_336
|
cpu: model=p4_prescott_celeron_336, ips=200000000
|
||||||
|
|
||||||
memory: guest=4096 host=2048
|
memory: guest=4096 host=2048
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@ ata0: enabled=1
|
|||||||
ata0-master: type=cdrom, path=mop3.iso, status=inserted
|
ata0-master: type=cdrom, path=mop3.iso, status=inserted
|
||||||
com1: enabled=1, mode=file, dev=bochs-com1.txt
|
com1: enabled=1, mode=file, dev=bochs-com1.txt
|
||||||
pci: enabled=1, chipset=i440fx
|
pci: enabled=1, chipset=i440fx
|
||||||
|
clock: sync=realtime, time0=local
|
||||||
|
|
||||||
boot: cdrom
|
boot: cdrom
|
||||||
|
|
||||||
|
|||||||
BIN
docs/assets/images/only-processes.png
Normal file
BIN
docs/assets/images/only-processes.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 118 KiB |
BIN
docs/assets/images/processes-threads.png
Normal file
BIN
docs/assets/images/processes-threads.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 51 KiB |
30
docs/processes_overview.md
Normal file
30
docs/processes_overview.md
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# Overview of processes in MOP3
|
||||||
|
|
||||||
|
## What is a process?
|
||||||
|
|
||||||
|
A process is a structure defined to represent an internal state of a user application's environment. This includes
|
||||||
|
the necessary stacks, code, data and other resources. A process (usually) has it's own address, but in certain
|
||||||
|
circumstances may share it with another process.
|
||||||
|
|
||||||
|
## Only processes vs. processes-threads model
|
||||||
|
|
||||||
|
### Overview
|
||||||
|
|
||||||
|
MOP3 doesn't have a process-thread separation. Ususally in operating systems you'd have a "process", which consists
|
||||||
|
of multiple worker threads. For eg. a single-threaded application is a process, which consists of one worker. In MOP3
|
||||||
|
we do things a little differently. We only have processes, but some processes may work within the same pool of (generally speaking)
|
||||||
|
"resources", such as a shared address space, shared memory allocations, mutexes and so on. An application then consists of
|
||||||
|
not threads, but processes, which are loosely tied together via shared data.
|
||||||
|
|
||||||
|
#### Processes-threads model diagram
|
||||||
|

|
||||||
|
#### Only processes model diagram
|
||||||
|

|
||||||
|
|
||||||
|
## Scheduling
|
||||||
|
|
||||||
|
MOP3 uses a round-robin based scheduler. For now priorities are left unimplemented, ie. every processes has
|
||||||
|
equal priority, but this may change in the future.
|
||||||
|
|
||||||
|
A good explaination of round-robin scheduling can be found on the OSDev wiki: [the article](https://wiki.osdev.org/Scheduling_Algorithms#Round_Robin)
|
||||||
|
|
||||||
@@ -5,7 +5,9 @@ cflags += -nostdinc \
|
|||||||
-std=c11 \
|
-std=c11 \
|
||||||
-pedantic \
|
-pedantic \
|
||||||
-Wall \
|
-Wall \
|
||||||
-Wextra
|
-Wextra \
|
||||||
|
-ffunction-sections \
|
||||||
|
-fdata-sections
|
||||||
|
|
||||||
cflags += -isystem ../include
|
cflags += -isystem ../include
|
||||||
|
|
||||||
@@ -13,4 +15,7 @@ ldflags += -ffreestanding \
|
|||||||
-nostdlib \
|
-nostdlib \
|
||||||
-fno-builtin \
|
-fno-builtin \
|
||||||
-fuse-ld=lld \
|
-fuse-ld=lld \
|
||||||
-static
|
-static \
|
||||||
|
-Wl,--gc-sections \
|
||||||
|
-Wl,--strip-all \
|
||||||
|
-flto
|
||||||
|
|||||||
13
include/m/status.h
Normal file
13
include/m/status.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef _M_STATUS_H
|
||||||
|
#define _M_STATUS_H
|
||||||
|
|
||||||
|
#define ST_OK 0
|
||||||
|
#define ST_SYSCALL_NOT_FOUND 1
|
||||||
|
#define ST_UNALIGNED 2
|
||||||
|
#define ST_OOM_ERROR 3
|
||||||
|
#define ST_NOT_FOUND 4
|
||||||
|
#define ST_BAD_ADDRESS_SPACE 5
|
||||||
|
#define ST_PERMISSION_ERROR 6
|
||||||
|
#define ST_BAD_RESOURCE 7
|
||||||
|
|
||||||
|
#endif // _M_STATUS_H
|
||||||
@@ -1,10 +1,16 @@
|
|||||||
#ifndef _M_SYSCALL_DEFS_H
|
#ifndef _M_SYSCALL_DEFS_H
|
||||||
#define _M_SYSCALL_DEFS_H
|
#define _M_SYSCALL_DEFS_H
|
||||||
|
|
||||||
#define SYS_PROC_QUIT 1
|
#define SYS_QUIT 1
|
||||||
#define SYS_PROC_TEST 2
|
#define SYS_TEST 2
|
||||||
|
#define SYS_MAP 3
|
||||||
#define SR_OK 0
|
#define SYS_UNMAP 4
|
||||||
#define SR_SYSCALL_NOT_FOUND 1
|
#define SYS_CLONE 5
|
||||||
|
#define SYS_SCHED 6
|
||||||
|
#define SYS_MUTEX_CREATE 7
|
||||||
|
#define SYS_MUTEX_DELETE 8
|
||||||
|
#define SYS_MUTEX_LOCK 9
|
||||||
|
#define SYS_MUTEX_UNLOCK 10
|
||||||
|
#define SYS_ARGUMENT_PTR 11
|
||||||
|
|
||||||
#endif // _M_SYSCALL_DEFS_H
|
#endif // _M_SYSCALL_DEFS_H
|
||||||
|
|||||||
47
init/init.c
47
init/init.c
@@ -1,9 +1,46 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <m/proc.h>
|
#include <proc/local.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string/string.h>
|
||||||
|
|
||||||
|
#define MUTEX 2000
|
||||||
|
|
||||||
|
LOCAL volatile char letter = 'c';
|
||||||
|
|
||||||
|
void app_proc (void) {
|
||||||
|
char arg_letter = (char)(uintptr_t)argument_ptr ();
|
||||||
|
|
||||||
|
letter = arg_letter;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
mutex_lock (MUTEX);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
test (letter);
|
||||||
|
|
||||||
|
mutex_unlock (MUTEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
process_quit ();
|
||||||
|
}
|
||||||
|
|
||||||
void app_main (void) {
|
void app_main (void) {
|
||||||
m_proc_test ();
|
mutex_create (MUTEX);
|
||||||
m_proc_test ();
|
|
||||||
m_proc_test ();
|
letter = 'a';
|
||||||
m_proc_test ();
|
|
||||||
|
process_spawn (&app_proc, (void*)'a');
|
||||||
|
process_spawn (&app_proc, (void*)'b');
|
||||||
|
process_spawn (&app_proc, (void*)'c');
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
mutex_lock (MUTEX);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
test (letter);
|
||||||
|
|
||||||
|
mutex_unlock (MUTEX);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
S += init.S
|
c += init.c
|
||||||
|
|
||||||
o += init.o
|
o += init.o
|
||||||
|
|||||||
1
kernel/.gitignore
vendored
1
kernel/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
*.json
|
*.json
|
||||||
|
.cache
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <amd64/msr.h>
|
#include <amd64/msr.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <limine/requests.h>
|
#include <limine/requests.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
#include <sys/debug.h>
|
#include <sys/debug.h>
|
||||||
#include <sys/mm.h>
|
#include <sys/mm.h>
|
||||||
#include <sys/spin.h>
|
#include <sys/spin.h>
|
||||||
@@ -33,8 +34,16 @@
|
|||||||
/* Divide config register */
|
/* Divide config register */
|
||||||
#define LAPIC_DCR 0x3E0
|
#define LAPIC_DCR 0x3E0
|
||||||
|
|
||||||
|
#define DIVIDER_VALUE 0x0B
|
||||||
|
|
||||||
|
struct ioapic {
|
||||||
|
struct acpi_madt_ioapic table_data;
|
||||||
|
spin_lock_t lock;
|
||||||
|
uintptr_t mmio_base;
|
||||||
|
};
|
||||||
|
|
||||||
/* Table of IOAPICS */
|
/* Table of IOAPICS */
|
||||||
static struct acpi_madt_ioapic apics[IOAPICS_MAX];
|
static struct ioapic ioapics[IOAPICS_MAX];
|
||||||
/* Table of interrupt source overrides */
|
/* Table of interrupt source overrides */
|
||||||
/* clang-format off */
|
/* clang-format off */
|
||||||
static struct acpi_madt_interrupt_source_override intr_src_overrides[INTERRUPT_SRC_OVERRIDES_MAX];
|
static struct acpi_madt_interrupt_source_override intr_src_overrides[INTERRUPT_SRC_OVERRIDES_MAX];
|
||||||
@@ -43,33 +52,41 @@ static struct acpi_madt_interrupt_source_override intr_src_overrides[INTERRUPT_S
|
|||||||
static size_t ioapic_entries = 0;
|
static size_t ioapic_entries = 0;
|
||||||
/* Count of actual interrupt source overrides */
|
/* Count of actual interrupt source overrides */
|
||||||
static size_t intr_src_override_entries = 0;
|
static size_t intr_src_override_entries = 0;
|
||||||
/* Local APIC MMIO base address. It comes from MSR_APIC_BASE */
|
|
||||||
static uintptr_t lapic_mmio_base = 0;
|
static spin_lock_t lapic_calibration_lock = SPIN_LOCK_INIT;
|
||||||
|
|
||||||
/* Read IOAPIC */
|
/* Read IOAPIC */
|
||||||
static uint32_t amd64_ioapic_read (uintptr_t vaddr, uint32_t reg) {
|
static uint32_t amd64_ioapic_read (struct ioapic* ioapic, uint32_t reg) {
|
||||||
*(volatile uint32_t*)vaddr = reg;
|
spin_lock_ctx_t ctxioar;
|
||||||
return *(volatile uint32_t*)(vaddr + 0x10);
|
|
||||||
|
spin_lock (&ioapic->lock, &ctxioar);
|
||||||
|
*(volatile uint32_t*)ioapic->mmio_base = reg;
|
||||||
|
uint32_t ret = *(volatile uint32_t*)(ioapic->mmio_base + 0x10);
|
||||||
|
spin_unlock (&ioapic->lock, &ctxioar);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write IOAPIC */
|
/* Write IOAPIC */
|
||||||
static void amd64_ioapic_write (uintptr_t vaddr, uint32_t reg, uint32_t value) {
|
static void amd64_ioapic_write (struct ioapic* ioapic, uint32_t reg, uint32_t value) {
|
||||||
*(volatile uint32_t*)vaddr = reg;
|
spin_lock_ctx_t ctxioaw;
|
||||||
*(volatile uint32_t*)(vaddr + 0x10) = value;
|
|
||||||
|
spin_lock (&ioapic->lock, &ctxioaw);
|
||||||
|
*(volatile uint32_t*)ioapic->mmio_base = reg;
|
||||||
|
*(volatile uint32_t*)(ioapic->mmio_base + 0x10) = value;
|
||||||
|
spin_unlock (&ioapic->lock, &ctxioaw);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find an IOAPIC corresposting to provided IRQ */
|
/* Find an IOAPIC corresposting to provided IRQ */
|
||||||
static struct acpi_madt_ioapic* amd64_ioapic_find (uint8_t irq) {
|
static struct ioapic* amd64_ioapic_find (uint32_t irq) {
|
||||||
struct acpi_madt_ioapic* apic = NULL;
|
struct ioapic* ioapic = NULL;
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < ioapic_entries; i++) {
|
for (size_t i = 0; i < ioapic_entries; i++) {
|
||||||
apic = &apics[i];
|
ioapic = &ioapics[i];
|
||||||
uint32_t version = amd64_ioapic_read ((uintptr_t)hhdm->offset + (uintptr_t)apic->address, 1);
|
uint32_t version = amd64_ioapic_read (ioapic, 1);
|
||||||
uint32_t max = ((version >> 16) & 0xFF);
|
uint32_t max = ((version >> 16) & 0xFF);
|
||||||
|
|
||||||
if ((irq >= apic->gsi_base) && (irq <= (apic->gsi_base + max)))
|
if ((irq >= ioapic->table_data.gsi_base) && (irq <= (ioapic->table_data.gsi_base + max)))
|
||||||
return apic;
|
return ioapic;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -84,11 +101,10 @@ static struct acpi_madt_ioapic* amd64_ioapic_find (uint8_t irq) {
|
|||||||
* flags - IOAPIC redirection flags.
|
* flags - IOAPIC redirection flags.
|
||||||
* lapic_id - Local APIC that will receive the interrupt.
|
* lapic_id - Local APIC that will receive the interrupt.
|
||||||
*/
|
*/
|
||||||
void amd64_ioapic_route_irq (uint8_t vec, uint8_t irq, uint64_t flags, uint64_t lapic_id) {
|
void amd64_ioapic_route_irq (uint32_t vec, uint32_t irq, uint64_t flags, uint64_t lapic_id) {
|
||||||
struct acpi_madt_ioapic* apic = NULL;
|
struct ioapic* ioapic = NULL;
|
||||||
struct acpi_madt_interrupt_source_override* override;
|
struct acpi_madt_interrupt_source_override* override;
|
||||||
bool found_override = false;
|
bool found_override = false;
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < intr_src_override_entries; i++) {
|
for (size_t i = 0; i < intr_src_override_entries; i++) {
|
||||||
override = &intr_src_overrides[i];
|
override = &intr_src_overrides[i];
|
||||||
@@ -101,62 +117,23 @@ void amd64_ioapic_route_irq (uint8_t vec, uint8_t irq, uint64_t flags, uint64_t
|
|||||||
uint64_t calc_flags = (lapic_id << 56) | (flags) | (vec & 0xFF);
|
uint64_t calc_flags = (lapic_id << 56) | (flags) | (vec & 0xFF);
|
||||||
|
|
||||||
if (found_override) {
|
if (found_override) {
|
||||||
uint8_t polarity = ((override->flags & 0x03) == 0x03) ? 1 : 0;
|
uint32_t polarity = ((override->flags & 0x03) == 0x03) ? 1 : 0;
|
||||||
uint8_t mode = (((override->flags >> 2) & 0x03) == 0x03) ? 1 : 0;
|
uint32_t mode = (((override->flags >> 2) & 0x03) == 0x03) ? 1 : 0;
|
||||||
calc_flags |= (uint64_t)mode << 15;
|
calc_flags |= (uint64_t)mode << 15;
|
||||||
calc_flags |= (uint64_t)polarity << 13;
|
calc_flags |= (uint64_t)polarity << 13;
|
||||||
|
|
||||||
calc_flags |= flags;
|
|
||||||
} else {
|
|
||||||
calc_flags |= flags;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
apic = amd64_ioapic_find (irq);
|
uint32_t gsi = found_override ? override->gsi : irq;
|
||||||
|
|
||||||
if (apic == NULL)
|
ioapic = amd64_ioapic_find (gsi);
|
||||||
|
|
||||||
|
if (ioapic == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
uint32_t irq_reg = ((irq - apic->gsi_base) * 2) + 0x10;
|
uint32_t irq_reg = ((gsi - ioapic->table_data.gsi_base) * 2) + 0x10;
|
||||||
|
|
||||||
amd64_ioapic_write ((uintptr_t)hhdm->offset + (uintptr_t)apic->address, irq_reg,
|
amd64_ioapic_write (ioapic, irq_reg + 1, (uint32_t)(calc_flags >> 32));
|
||||||
(uint32_t)calc_flags);
|
amd64_ioapic_write (ioapic, irq_reg, (uint32_t)calc_flags);
|
||||||
|
|
||||||
amd64_ioapic_write ((uintptr_t)hhdm->offset + (uintptr_t)apic->address, irq_reg + 1,
|
|
||||||
(uint32_t)(calc_flags >> 32));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Mask a given IRQ */
|
|
||||||
void amd64_ioapic_mask (uint8_t irq) {
|
|
||||||
struct acpi_madt_ioapic* apic;
|
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
||||||
|
|
||||||
apic = amd64_ioapic_find (irq);
|
|
||||||
|
|
||||||
if (apic == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
uint32_t irq_reg = ((irq - apic->gsi_base) * 2) + 0x10;
|
|
||||||
|
|
||||||
uint32_t value = amd64_ioapic_read ((uintptr_t)hhdm->offset + (uintptr_t)apic->address, irq_reg);
|
|
||||||
amd64_ioapic_write ((uintptr_t)hhdm->offset + (uintptr_t)apic->address, irq_reg,
|
|
||||||
value | (1 << 16));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Unmask a given IRQ */
|
|
||||||
void amd64_ioapic_unmask (uint8_t irq) {
|
|
||||||
struct acpi_madt_ioapic* apic;
|
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
|
||||||
|
|
||||||
apic = amd64_ioapic_find (irq);
|
|
||||||
|
|
||||||
if (apic == NULL)
|
|
||||||
return;
|
|
||||||
|
|
||||||
uint32_t irq_reg = ((irq - apic->gsi_base) * 2) + 0x10;
|
|
||||||
|
|
||||||
uint32_t value = amd64_ioapic_read ((uintptr_t)hhdm->offset + (uintptr_t)apic->address, irq_reg);
|
|
||||||
amd64_ioapic_write ((uintptr_t)hhdm->offset + (uintptr_t)apic->address, irq_reg,
|
|
||||||
value & ~(1 << 16));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find and initialize the IOAPIC */
|
/* Find and initialize the IOAPIC */
|
||||||
@@ -180,11 +157,15 @@ void amd64_ioapic_init (void) {
|
|||||||
|
|
||||||
switch (current->type) {
|
switch (current->type) {
|
||||||
case ACPI_MADT_ENTRY_TYPE_IOAPIC: {
|
case ACPI_MADT_ENTRY_TYPE_IOAPIC: {
|
||||||
struct acpi_madt_ioapic* ioapic = (struct acpi_madt_ioapic*)current;
|
struct acpi_madt_ioapic* ioapic_table_data = (struct acpi_madt_ioapic*)current;
|
||||||
mm_map_kernel_page ((uintptr_t)ioapic->address,
|
mm_map_kernel_page ((uintptr_t)ioapic_table_data->address,
|
||||||
(uintptr_t)hhdm->offset + (uintptr_t)ioapic->address,
|
(uintptr_t)hhdm->offset + (uintptr_t)ioapic_table_data->address,
|
||||||
MM_PG_PRESENT | MM_PG_RW | MM_PD_RELOAD);
|
MM_PG_PRESENT | MM_PG_RW);
|
||||||
apics[ioapic_entries++] = *ioapic;
|
ioapics[ioapic_entries++] = (struct ioapic){
|
||||||
|
.lock = SPIN_LOCK_INIT,
|
||||||
|
.table_data = *ioapic_table_data,
|
||||||
|
.mmio_base = ((uintptr_t)hhdm->offset + (uintptr_t)ioapic_table_data->address),
|
||||||
|
};
|
||||||
} break;
|
} break;
|
||||||
case ACPI_MADT_ENTRY_TYPE_INTERRUPT_SOURCE_OVERRIDE: {
|
case ACPI_MADT_ENTRY_TYPE_INTERRUPT_SOURCE_OVERRIDE: {
|
||||||
struct acpi_madt_interrupt_source_override* override =
|
struct acpi_madt_interrupt_source_override* override =
|
||||||
@@ -198,7 +179,7 @@ void amd64_ioapic_init (void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Get MMIO base of Local APIC */
|
/* Get MMIO base of Local APIC */
|
||||||
static uintptr_t amd64_lapic_base (void) { return lapic_mmio_base; }
|
static uintptr_t amd64_lapic_base (void) { return thiscpu->lapic_mmio_base; }
|
||||||
|
|
||||||
/* Write Local APIC */
|
/* Write Local APIC */
|
||||||
static void amd64_lapic_write (uint32_t reg, uint32_t value) {
|
static void amd64_lapic_write (uint32_t reg, uint32_t value) {
|
||||||
@@ -216,24 +197,28 @@ uint32_t amd64_lapic_id (void) { return amd64_lapic_read (LAPIC_ID) >> 24; }
|
|||||||
/* Send End of interrupt command to Local APIC */
|
/* Send End of interrupt command to Local APIC */
|
||||||
void amd64_lapic_eoi (void) { amd64_lapic_write (LAPIC_EOI, 0); }
|
void amd64_lapic_eoi (void) { amd64_lapic_write (LAPIC_EOI, 0); }
|
||||||
|
|
||||||
/* Set initial counter value in Local APIC timer */
|
|
||||||
void amd64_lapic_tick (uint32_t tick) { amd64_lapic_write (LAPIC_TIMICT, tick); }
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Calibrate Local APIC to send interrupts in a set interval.
|
* Calibrate Local APIC to send interrupts in a set interval.
|
||||||
*
|
*
|
||||||
* us - Period length in microseconds
|
* us - Period length in microseconds
|
||||||
*/
|
*/
|
||||||
static uint32_t amd64_lapic_calibrate (uint32_t us) {
|
static uint32_t amd64_lapic_calibrate (uint32_t us) {
|
||||||
amd64_lapic_write (LAPIC_DCR, 0x0B);
|
spin_lock_ctx_t ctxlacb;
|
||||||
|
|
||||||
|
spin_lock (&lapic_calibration_lock, &ctxlacb);
|
||||||
|
|
||||||
|
amd64_lapic_write (LAPIC_DCR, DIVIDER_VALUE);
|
||||||
|
|
||||||
amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 16));
|
amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 16));
|
||||||
|
|
||||||
amd64_lapic_write (LAPIC_TIMICT, 0xFFFFFFFF);
|
amd64_lapic_write (LAPIC_TIMICT, 0xFFFFFFFF);
|
||||||
|
|
||||||
sleep_micro (us);
|
sleep_micro (us);
|
||||||
|
|
||||||
|
amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (0 << 16));
|
||||||
uint32_t ticks = 0xFFFFFFFF - amd64_lapic_read (LAPIC_TIMCCT);
|
uint32_t ticks = 0xFFFFFFFF - amd64_lapic_read (LAPIC_TIMCCT);
|
||||||
|
DEBUG ("timer ticks = %u\n", ticks);
|
||||||
|
|
||||||
|
spin_unlock (&lapic_calibration_lock, &ctxlacb);
|
||||||
|
|
||||||
return ticks;
|
return ticks;
|
||||||
}
|
}
|
||||||
@@ -244,35 +229,29 @@ static uint32_t amd64_lapic_calibrate (uint32_t us) {
|
|||||||
* ticks - Initial tick count
|
* ticks - Initial tick count
|
||||||
*/
|
*/
|
||||||
static void amd64_lapic_start (uint32_t ticks) {
|
static void amd64_lapic_start (uint32_t ticks) {
|
||||||
amd64_lapic_write (LAPIC_DCR, 0x0B);
|
amd64_lapic_write (LAPIC_DCR, DIVIDER_VALUE);
|
||||||
|
|
||||||
amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 17));
|
|
||||||
|
|
||||||
amd64_lapic_write (LAPIC_TIMICT, ticks);
|
amd64_lapic_write (LAPIC_TIMICT, ticks);
|
||||||
|
amd64_lapic_write (LAPIC_LVTTR, SCHED_PREEMPT_TIMER | (1 << 17));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize Local APIC, configure to send timer interrupts at a given period. See
|
* Initialize Local APIC, configure to send timer interrupts at a given period. See
|
||||||
* amd64_lapic_calibrate and amd64_lapic_start.
|
* amd64_lapic_calibrate and amd64_lapic_start.
|
||||||
*/
|
*/
|
||||||
uint64_t amd64_lapic_init (uint32_t us) {
|
void amd64_lapic_init (uint32_t us) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
|
|
||||||
amd64_wrmsr (MSR_APIC_BASE, amd64_rdmsr (MSR_APIC_BASE) | (1 << 11));
|
amd64_wrmsr (MSR_APIC_BASE, amd64_rdmsr (MSR_APIC_BASE) | (1 << 11));
|
||||||
|
|
||||||
uintptr_t lapic_paddr = amd64_rdmsr (MSR_APIC_BASE) & 0xFFFFF000;
|
uintptr_t lapic_paddr = amd64_rdmsr (MSR_APIC_BASE) & 0xFFFFF000;
|
||||||
lapic_mmio_base = lapic_paddr + (uintptr_t)hhdm->offset;
|
thiscpu->lapic_mmio_base = lapic_paddr + (uintptr_t)hhdm->offset;
|
||||||
|
|
||||||
mm_map_kernel_page (lapic_paddr, lapic_mmio_base,
|
mm_map_kernel_page (lapic_paddr, thiscpu->lapic_mmio_base, MM_PG_PRESENT | MM_PG_RW);
|
||||||
MM_PG_PRESENT | MM_PG_RW | MM_PD_LOCK | MM_PD_RELOAD);
|
|
||||||
|
|
||||||
amd64_lapic_write (LAPIC_SIVR, 0xFF | (1 << 8));
|
amd64_lapic_write (LAPIC_SIVR, 0xFF | (1 << 8));
|
||||||
|
|
||||||
uint32_t ticks = amd64_lapic_calibrate (us);
|
thiscpu->lapic_ticks = amd64_lapic_calibrate (us);
|
||||||
|
amd64_lapic_start (thiscpu->lapic_ticks);
|
||||||
amd64_lapic_start (ticks);
|
|
||||||
|
|
||||||
return ticks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -281,7 +260,12 @@ uint64_t amd64_lapic_init (uint32_t us) {
|
|||||||
* lapic_id - Target Local APIC
|
* lapic_id - Target Local APIC
|
||||||
* vec - Interrupt vector/IDT stub, which will be invoked by the IPI.
|
* vec - Interrupt vector/IDT stub, which will be invoked by the IPI.
|
||||||
*/
|
*/
|
||||||
void amd64_lapic_ipi (uint8_t lapic_id, uint8_t vec) {
|
void amd64_lapic_ipi (uint32_t lapic_id, uint32_t vec) {
|
||||||
|
/* wait for previous IPI to finish */
|
||||||
|
while (amd64_lapic_read (LAPIC_ICR) & (1 << 12)) {
|
||||||
|
__asm__ volatile ("pause");
|
||||||
|
}
|
||||||
|
|
||||||
amd64_lapic_write (LAPIC_ICR + 0x10, (lapic_id << 24));
|
amd64_lapic_write (LAPIC_ICR + 0x10, (lapic_id << 24));
|
||||||
amd64_lapic_write (LAPIC_ICR, vec);
|
amd64_lapic_write (LAPIC_ICR, vec | (1 << 14));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,12 @@
|
|||||||
|
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
|
||||||
void amd64_ioapic_route_irq (uint8_t vec, uint8_t irq, uint64_t flags, uint64_t lapic_id);
|
void amd64_ioapic_route_irq (uint32_t vec, uint32_t irq, uint64_t flags, uint64_t lapic_id);
|
||||||
void amd64_ioapic_mask (uint8_t irq);
|
|
||||||
void amd64_ioapic_unmask (uint8_t irq);
|
|
||||||
void amd64_ioapic_init (void);
|
void amd64_ioapic_init (void);
|
||||||
|
|
||||||
uint32_t amd64_lapic_id (void);
|
uint32_t amd64_lapic_id (void);
|
||||||
void amd64_lapic_tick (uint32_t tick);
|
|
||||||
void amd64_lapic_eoi (void);
|
void amd64_lapic_eoi (void);
|
||||||
void amd64_lapic_ipi (uint8_t lapic_id, uint8_t vec);
|
void amd64_lapic_ipi (uint32_t lapic_id, uint32_t vec);
|
||||||
uint64_t amd64_lapic_init (uint32_t us);
|
void amd64_lapic_init (uint32_t us);
|
||||||
|
|
||||||
#endif // _KERNEL_AMD64_APIC_H
|
#endif // _KERNEL_AMD64_APIC_H
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <irq/irq.h>
|
#include <irq/irq.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <limine/limine.h>
|
#include <limine/limine.h>
|
||||||
|
#include <limine/requests.h>
|
||||||
#include <mm/liballoc.h>
|
#include <mm/liballoc.h>
|
||||||
#include <mm/pmm.h>
|
#include <mm/pmm.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
@@ -29,7 +30,9 @@ ALIGNED (16) static uint8_t uacpi_memory_buffer[UACPI_MEMORY_BUFFER_MAX];
|
|||||||
* the necessary platform-dependent subsystems/drivers and jump into the init app.
|
* the necessary platform-dependent subsystems/drivers and jump into the init app.
|
||||||
*/
|
*/
|
||||||
void bootmain (void) {
|
void bootmain (void) {
|
||||||
struct cpu* bsp_cpu = cpu_make ();
|
struct limine_mp_response* mp = limine_mp_request.response;
|
||||||
|
|
||||||
|
struct cpu* bsp_cpu = cpu_make (mp->bsp_lapic_id);
|
||||||
|
|
||||||
amd64_init (bsp_cpu, false);
|
amd64_init (bsp_cpu, false);
|
||||||
syscall_init ();
|
syscall_init ();
|
||||||
@@ -46,8 +49,6 @@ void bootmain (void) {
|
|||||||
|
|
||||||
smp_init ();
|
smp_init ();
|
||||||
|
|
||||||
mm_init2 ();
|
|
||||||
|
|
||||||
proc_init ();
|
proc_init ();
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ static void amd64_debug_serial_write (char x) {
|
|||||||
* Formatted printing to serial. serial_lock ensures that all prints are atomic.
|
* Formatted printing to serial. serial_lock ensures that all prints are atomic.
|
||||||
*/
|
*/
|
||||||
void debugprintf (const char* fmt, ...) {
|
void debugprintf (const char* fmt, ...) {
|
||||||
|
spin_lock_ctx_t ctxdbgp;
|
||||||
|
|
||||||
if (!debug_init)
|
if (!debug_init)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -50,14 +52,14 @@ void debugprintf (const char* fmt, ...) {
|
|||||||
|
|
||||||
const char* p = buffer;
|
const char* p = buffer;
|
||||||
|
|
||||||
spin_lock (&serial_lock);
|
spin_lock (&serial_lock, &ctxdbgp);
|
||||||
|
|
||||||
while (*p) {
|
while (*p) {
|
||||||
amd64_debug_serial_write (*p);
|
amd64_debug_serial_write (*p);
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
spin_unlock (&serial_lock);
|
spin_unlock (&serial_lock, &ctxdbgp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize serial */
|
/* Initialize serial */
|
||||||
|
|||||||
@@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#define GDT_KCODE 0x08
|
#define GDT_KCODE 0x08
|
||||||
#define GDT_KDATA 0x10
|
#define GDT_KDATA 0x10
|
||||||
#define GDT_UCODE 0x18
|
#define GDT_UDATA 0x18
|
||||||
#define GDT_UDATA 0x20
|
#define GDT_UCODE 0x20
|
||||||
#define GDT_TSS 0x28
|
#define GDT_TSS 0x28
|
||||||
|
|
||||||
/* Size of kernel stack */
|
/* Size of kernel stack */
|
||||||
|
|||||||
@@ -30,46 +30,90 @@ static uint64_t hpet_period_fs;
|
|||||||
/* Lock, which protects concurrent access. See amd64/smp.c */
|
/* Lock, which protects concurrent access. See amd64/smp.c */
|
||||||
static spin_lock_t hpet_lock = SPIN_LOCK_INIT;
|
static spin_lock_t hpet_lock = SPIN_LOCK_INIT;
|
||||||
|
|
||||||
/* Read a HPET register. Assumes caller holds \ref hpet_lock */
|
/* Read a HPET register. Assumes caller holds hpet_lock */
|
||||||
static uint64_t amd64_hpet_read (uint32_t reg) {
|
static uint64_t amd64_hpet_read64 (uint32_t reg) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
uintptr_t hpet_vaddr = hpet_paddr + (uintptr_t)hhdm->offset;
|
uintptr_t hpet_vaddr = hpet_paddr + (uintptr_t)hhdm->offset;
|
||||||
return (hpet_32bits ? *(volatile uint32_t*)(hpet_vaddr + reg)
|
return *(volatile uint64_t*)(hpet_vaddr + reg);
|
||||||
: *(volatile uint64_t*)(hpet_vaddr + reg));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write a HPET register. Assumes caller holds \ref hpet_lock */
|
static uint32_t amd64_hpet_read32 (uint32_t reg) {
|
||||||
static void amd64_hpet_write (uint32_t reg, uint64_t value) {
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
|
uintptr_t hpet_vaddr = hpet_paddr + (uintptr_t)hhdm->offset;
|
||||||
|
return *(volatile uint32_t*)(hpet_vaddr + reg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write a HPET register. Assumes caller holds hpet_lock */
|
||||||
|
static void amd64_hpet_write64 (uint32_t reg, uint64_t value) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
uintptr_t hpet_vaddr = hpet_paddr + (uintptr_t)hhdm->offset;
|
uintptr_t hpet_vaddr = hpet_paddr + (uintptr_t)hhdm->offset;
|
||||||
if (hpet_32bits)
|
|
||||||
*(volatile uint32_t*)(hpet_vaddr + reg) = (value & 0xFFFFFFFF);
|
|
||||||
else
|
|
||||||
*(volatile uint64_t*)(hpet_vaddr + reg) = value;
|
*(volatile uint64_t*)(hpet_vaddr + reg) = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read current value of \ref HPET_MCVR register. */
|
static void amd64_hpet_write32 (uint32_t reg, uint32_t value) {
|
||||||
static uint64_t amd64_hpet_timestamp (void) { return amd64_hpet_read (HPET_MCVR); }
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
|
uintptr_t hpet_vaddr = hpet_paddr + (uintptr_t)hhdm->offset;
|
||||||
|
*(volatile uint32_t*)(hpet_vaddr + reg) = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read current value of HPET_MCVR register. */
|
||||||
|
|
||||||
|
static uint64_t amd64_hpet_read_counter (void) {
|
||||||
|
uint64_t value;
|
||||||
|
spin_lock_ctx_t ctxhrc;
|
||||||
|
|
||||||
|
spin_lock (&hpet_lock, &ctxhrc);
|
||||||
|
|
||||||
|
if (!hpet_32bits)
|
||||||
|
value = amd64_hpet_read64 (HPET_MCVR);
|
||||||
|
else {
|
||||||
|
uint32_t hi1, lo, hi2;
|
||||||
|
do {
|
||||||
|
hi1 = amd64_hpet_read32 (HPET_MCVR + 4);
|
||||||
|
lo = amd64_hpet_read32 (HPET_MCVR + 0);
|
||||||
|
hi2 = amd64_hpet_read32 (HPET_MCVR + 4);
|
||||||
|
} while (hi1 != hi2);
|
||||||
|
|
||||||
|
value = ((uint64_t)hi1 << 32) | lo;
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&hpet_lock, &ctxhrc);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void amd64_hpet_write_counter (uint64_t value) {
|
||||||
|
spin_lock_ctx_t ctxhwc;
|
||||||
|
|
||||||
|
spin_lock (&hpet_lock, &ctxhwc);
|
||||||
|
|
||||||
|
if (!hpet_32bits)
|
||||||
|
amd64_hpet_write64 (HPET_MCVR, value);
|
||||||
|
else {
|
||||||
|
amd64_hpet_write32 (HPET_MCVR, (uint32_t)value);
|
||||||
|
amd64_hpet_write32 (HPET_MCVR + 4, (uint32_t)(value >> 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&hpet_lock, &ctxhwc);
|
||||||
|
}
|
||||||
|
|
||||||
/* Sleep for a given amount of microseconds. This time can last longer due to \ref hpet_lock being
|
/* Sleep for a given amount of microseconds. This time can last longer due to \ref hpet_lock being
|
||||||
* held. */
|
* held. */
|
||||||
void amd64_hpet_sleep_micro (uint64_t us) {
|
void amd64_hpet_sleep_micro (uint64_t us) {
|
||||||
spin_lock (&hpet_lock);
|
if (hpet_period_fs == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
uint64_t start = amd64_hpet_timestamp ();
|
uint64_t ticks_to_wait = (us * 1000ULL) / (hpet_period_fs / 1000000ULL);
|
||||||
uint64_t target_fs = us * 1000000000ULL;
|
uint64_t start = amd64_hpet_read_counter ();
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
uint64_t current = amd64_hpet_timestamp ();
|
uint64_t now = amd64_hpet_read_counter ();
|
||||||
uint64_t dt = current - start;
|
|
||||||
|
|
||||||
if ((dt * hpet_period_fs) >= target_fs)
|
if ((now - start) >= ticks_to_wait)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
__asm__ volatile ("pause" ::: "memory");
|
__asm__ volatile ("pause" ::: "memory");
|
||||||
}
|
}
|
||||||
|
|
||||||
spin_unlock (&hpet_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize HPET */
|
/* Initialize HPET */
|
||||||
@@ -85,22 +129,14 @@ void amd64_hpet_init (void) {
|
|||||||
hpet_paddr = (uintptr_t)hpet->address.address;
|
hpet_paddr = (uintptr_t)hpet->address.address;
|
||||||
|
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
mm_map_kernel_page (hpet_paddr, (uintptr_t)hhdm->offset + hpet_paddr,
|
mm_map_kernel_page (hpet_paddr, (uintptr_t)hhdm->offset + hpet_paddr, MM_PG_PRESENT | MM_PG_RW);
|
||||||
MM_PG_PRESENT | MM_PG_RW | MM_PD_RELOAD);
|
|
||||||
|
|
||||||
hpet_32bits = (amd64_hpet_read (HPET_GCIDR) & (1 << 13)) ? 0 : 1;
|
uint64_t caps = amd64_hpet_read64 (HPET_GCIDR);
|
||||||
|
hpet_32bits = (caps & (1 << 13)) ? 0 : 1;
|
||||||
|
|
||||||
/* reset */
|
hpet_period_fs = (uint32_t)(caps >> 32);
|
||||||
amd64_hpet_write (HPET_GCR, 0);
|
|
||||||
amd64_hpet_write (HPET_MCVR, 0);
|
|
||||||
amd64_hpet_write (HPET_GCR, 1);
|
|
||||||
|
|
||||||
uint64_t gcidr = amd64_hpet_read (HPET_GCIDR);
|
amd64_hpet_write64 (HPET_GCR, 0);
|
||||||
if (hpet_32bits) {
|
amd64_hpet_write_counter (0);
|
||||||
uint32_t low = (uint32_t)gcidr;
|
amd64_hpet_write64 (HPET_GCR, 1);
|
||||||
uint32_t high = (uint32_t)amd64_hpet_read (HPET_GCIDR + 4);
|
|
||||||
gcidr = (((uint64_t)high << 32) | low);
|
|
||||||
}
|
|
||||||
|
|
||||||
hpet_period_fs = (gcidr >> 32);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ static void amd64_gdt_init (struct cpu* cpu) {
|
|||||||
amd64_gdt_set (&gdt->old[0], 0, 0, 0, 0);
|
amd64_gdt_set (&gdt->old[0], 0, 0, 0, 0);
|
||||||
amd64_gdt_set (&gdt->old[1], 0, 0xFFFFF, 0x9A, 0xA0);
|
amd64_gdt_set (&gdt->old[1], 0, 0xFFFFF, 0x9A, 0xA0);
|
||||||
amd64_gdt_set (&gdt->old[2], 0, 0xFFFFF, 0x92, 0xC0);
|
amd64_gdt_set (&gdt->old[2], 0, 0xFFFFF, 0x92, 0xC0);
|
||||||
amd64_gdt_set (&gdt->old[3], 0, 0xFFFFF, 0xFA, 0xA0);
|
amd64_gdt_set (&gdt->old[3], 0, 0xFFFFF, 0xF2, 0xC0);
|
||||||
amd64_gdt_set (&gdt->old[4], 0, 0xFFFFF, 0xF2, 0xC0);
|
amd64_gdt_set (&gdt->old[4], 0, 0xFFFFF, 0xFA, 0xA0);
|
||||||
amd64_gdt_set (&gdt->tsslow, (tssbase & 0xFFFFFFFF), tsslimit, TSS_PRESENT | TSS, 0);
|
amd64_gdt_set (&gdt->tsslow, (tssbase & 0xFFFFFFFF), tsslimit, TSS_PRESENT | TSS, 0);
|
||||||
|
|
||||||
uint32_t tssbasehigh = (tssbase >> 32);
|
uint32_t tssbasehigh = (tssbase >> 32);
|
||||||
|
|||||||
@@ -7,10 +7,12 @@
|
|||||||
#include <irq/irq.h>
|
#include <irq/irq.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <libk/string.h>
|
#include <libk/string.h>
|
||||||
|
#include <m/syscall_defs.h>
|
||||||
#include <sys/debug.h>
|
#include <sys/debug.h>
|
||||||
#include <sys/irq.h>
|
#include <sys/irq.h>
|
||||||
#include <sys/smp.h>
|
#include <sys/smp.h>
|
||||||
#include <sys/spin.h>
|
#include <sys/spin.h>
|
||||||
|
#include <syscall/syscall.h>
|
||||||
|
|
||||||
/* 8259 PIC defs. */
|
/* 8259 PIC defs. */
|
||||||
#define PIC1 0x20
|
#define PIC1 0x20
|
||||||
@@ -121,6 +123,8 @@ static void amd64_idt_init (void) {
|
|||||||
|
|
||||||
IDT_ENTRY (SCHED_PREEMPT_TIMER, 1);
|
IDT_ENTRY (SCHED_PREEMPT_TIMER, 1);
|
||||||
IDT_ENTRY (TLB_SHOOTDOWN, 1);
|
IDT_ENTRY (TLB_SHOOTDOWN, 1);
|
||||||
|
IDT_ENTRY (CPU_REQUEST_SCHED, 1);
|
||||||
|
IDT_ENTRY (CPU_SPURIOUS, 1);
|
||||||
/* clang-format on */
|
/* clang-format on */
|
||||||
#undef IDT_ENTRY
|
#undef IDT_ENTRY
|
||||||
|
|
||||||
@@ -161,8 +165,21 @@ static void amd64_intr_exception (struct saved_regs* regs) {
|
|||||||
|
|
||||||
/* Handle incoming interrupt, dispatch IRQ handlers. */
|
/* Handle incoming interrupt, dispatch IRQ handlers. */
|
||||||
void amd64_intr_handler (void* stack_ptr) {
|
void amd64_intr_handler (void* stack_ptr) {
|
||||||
|
spin_lock_ctx_t ctxcpu, ctxpr;
|
||||||
|
|
||||||
|
amd64_load_kernel_cr3 ();
|
||||||
|
|
||||||
struct saved_regs* regs = stack_ptr;
|
struct saved_regs* regs = stack_ptr;
|
||||||
|
|
||||||
|
spin_lock (&thiscpu->lock, &ctxcpu);
|
||||||
|
struct proc* proc_current = thiscpu->proc_current;
|
||||||
|
spin_lock (&proc_current->lock, &ctxpr);
|
||||||
|
|
||||||
|
memcpy (&proc_current->pdata.regs, regs, sizeof (struct saved_regs));
|
||||||
|
|
||||||
|
spin_unlock (&proc_current->lock, &ctxpr);
|
||||||
|
spin_unlock (&thiscpu->lock, &ctxcpu);
|
||||||
|
|
||||||
if (regs->trap <= 31) {
|
if (regs->trap <= 31) {
|
||||||
amd64_intr_exception (regs);
|
amd64_intr_exception (regs);
|
||||||
} else {
|
} else {
|
||||||
@@ -171,13 +188,7 @@ void amd64_intr_handler (void* stack_ptr) {
|
|||||||
struct irq* irq = irq_find (regs->trap);
|
struct irq* irq = irq_find (regs->trap);
|
||||||
|
|
||||||
if (irq != NULL) {
|
if (irq != NULL) {
|
||||||
if ((irq->flags & IRQ_INTERRUPT_SAFE))
|
|
||||||
__asm__ volatile ("sti");
|
|
||||||
|
|
||||||
irq->func (irq->arg, stack_ptr);
|
irq->func (irq->arg, stack_ptr);
|
||||||
|
|
||||||
if ((irq->flags & IRQ_INTERRUPT_SAFE))
|
|
||||||
__asm__ volatile ("cli");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,25 +215,7 @@ static void amd64_irq_restore_flags (uint64_t rflags) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Save current interrupt state */
|
/* Save current interrupt state */
|
||||||
void irq_save (void) {
|
void irq_save (spin_lock_ctx_t* ctx) { *ctx = amd64_irq_save_flags (); }
|
||||||
int prev = atomic_fetch_add_explicit (&thiscpu->irq_ctx.nesting, 1, memory_order_acq_rel);
|
|
||||||
if (prev == 0)
|
|
||||||
thiscpu->irq_ctx.rflags = amd64_irq_save_flags ();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Restore interrupt state */
|
/* Restore interrupt state */
|
||||||
void irq_restore (void) {
|
void irq_restore (spin_lock_ctx_t* ctx) { amd64_irq_restore_flags (*ctx); }
|
||||||
int prev = atomic_fetch_sub_explicit (&thiscpu->irq_ctx.nesting, 1, memory_order_acq_rel);
|
|
||||||
if (prev == 1)
|
|
||||||
amd64_irq_restore_flags (thiscpu->irq_ctx.rflags);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Map custom IRQ mappings to legacy IRQs */
|
|
||||||
uint8_t amd64_resolve_irq (uint8_t irq) {
|
|
||||||
static const uint8_t mappings[] = {
|
|
||||||
[SCHED_PREEMPT_TIMER] = 0,
|
|
||||||
[TLB_SHOOTDOWN] = 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
return mappings[irq];
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ struct saved_regs {
|
|||||||
} PACKED;
|
} PACKED;
|
||||||
|
|
||||||
void amd64_load_idt (void);
|
void amd64_load_idt (void);
|
||||||
uint8_t amd64_resolve_irq (uint8_t irq);
|
|
||||||
void amd64_intr_init (void);
|
void amd64_intr_init (void);
|
||||||
|
|
||||||
#endif // _KERNEL_AMD64_INTR_H
|
#endif // _KERNEL_AMD64_INTR_H
|
||||||
|
|||||||
@@ -7,5 +7,6 @@
|
|||||||
#define SCHED_PREEMPT_TIMER 80
|
#define SCHED_PREEMPT_TIMER 80
|
||||||
#define TLB_SHOOTDOWN 81
|
#define TLB_SHOOTDOWN 81
|
||||||
#define CPU_REQUEST_SCHED 82
|
#define CPU_REQUEST_SCHED 82
|
||||||
|
#define CPU_SPURIOUS 255
|
||||||
|
|
||||||
#endif // _KERNEL_AMD64_INTR_DEFS_H
|
#endif // _KERNEL_AMD64_INTR_DEFS_H
|
||||||
|
|||||||
@@ -15,25 +15,33 @@
|
|||||||
amd64_intr ## n:; \
|
amd64_intr ## n:; \
|
||||||
x(n); \
|
x(n); \
|
||||||
cli; \
|
cli; \
|
||||||
;\
|
; \
|
||||||
push_regs; \
|
push_regs; \
|
||||||
;\
|
; \
|
||||||
|
movw $0x10, %ax; \
|
||||||
|
movw %ax, %ds; \
|
||||||
|
movw %ax, %es; \
|
||||||
|
; \
|
||||||
cld; \
|
cld; \
|
||||||
;\
|
; \
|
||||||
movq %rsp, %rdi; \
|
movq %rsp, %rdi; \
|
||||||
;\
|
; \
|
||||||
|
movq %cr3, %rax; pushq %rax; \
|
||||||
|
; \
|
||||||
movq %rsp, %rbp; \
|
movq %rsp, %rbp; \
|
||||||
;\
|
; \
|
||||||
subq $8, %rsp; \
|
subq $8, %rsp; \
|
||||||
andq $~0xF, %rsp; \
|
andq $-16, %rsp; \
|
||||||
;\
|
; \
|
||||||
callq amd64_intr_handler; \
|
callq amd64_intr_handler; \
|
||||||
;\
|
; \
|
||||||
movq %rbp, %rsp; \
|
movq %rbp, %rsp; \
|
||||||
;\
|
; \
|
||||||
|
popq %rax; movq %rax, %cr3; \
|
||||||
|
; \
|
||||||
pop_regs; \
|
pop_regs; \
|
||||||
addq $16, %rsp; \
|
addq $16, %rsp; \
|
||||||
;\
|
; \
|
||||||
iretq;
|
iretq;
|
||||||
|
|
||||||
|
|
||||||
@@ -89,3 +97,4 @@ make_intr_stub(no_err, 47)
|
|||||||
make_intr_stub(no_err, SCHED_PREEMPT_TIMER)
|
make_intr_stub(no_err, SCHED_PREEMPT_TIMER)
|
||||||
make_intr_stub(no_err, TLB_SHOOTDOWN)
|
make_intr_stub(no_err, TLB_SHOOTDOWN)
|
||||||
make_intr_stub(no_err, CPU_REQUEST_SCHED)
|
make_intr_stub(no_err, CPU_REQUEST_SCHED)
|
||||||
|
make_intr_stub(no_err, CPU_SPURIOUS)
|
||||||
|
|||||||
@@ -11,12 +11,10 @@
|
|||||||
#include <sys/mm.h>
|
#include <sys/mm.h>
|
||||||
#include <sys/smp.h>
|
#include <sys/smp.h>
|
||||||
|
|
||||||
/* Present flag */
|
|
||||||
#define AMD64_PG_PRESENT (1 << 0)
|
#define AMD64_PG_PRESENT (1 << 0)
|
||||||
/* Writable flag */
|
|
||||||
#define AMD64_PG_RW (1 << 1)
|
#define AMD64_PG_RW (1 << 1)
|
||||||
/* User-accessible flag */
|
|
||||||
#define AMD64_PG_USER (1 << 2)
|
#define AMD64_PG_USER (1 << 2)
|
||||||
|
#define AMD64_PG_HUGE (1 << 7)
|
||||||
|
|
||||||
/* Auxilary struct for page directory walking */
|
/* Auxilary struct for page directory walking */
|
||||||
struct pg_index {
|
struct pg_index {
|
||||||
@@ -24,9 +22,12 @@ struct pg_index {
|
|||||||
} PACKED;
|
} PACKED;
|
||||||
|
|
||||||
/* Kernel page directory */
|
/* Kernel page directory */
|
||||||
static struct pd kernel_pd = {.lock = SPIN_LOCK_INIT};
|
static struct pd kernel_pd;
|
||||||
/* Lock needed to sync between map/unmap operations and TLB shootdown */
|
static spin_lock_t kernel_pd_lock;
|
||||||
static spin_lock_t mm_lock = SPIN_LOCK_INIT;
|
|
||||||
|
void mm_kernel_lock (spin_lock_ctx_t* ctx) { spin_lock (&kernel_pd_lock, ctx); }
|
||||||
|
|
||||||
|
void mm_kernel_unlock (spin_lock_ctx_t* ctx) { spin_lock (&kernel_pd_lock, ctx); }
|
||||||
|
|
||||||
/* Get current value of CR3 register */
|
/* Get current value of CR3 register */
|
||||||
static uintptr_t amd64_current_cr3 (void) {
|
static uintptr_t amd64_current_cr3 (void) {
|
||||||
@@ -37,9 +38,15 @@ static uintptr_t amd64_current_cr3 (void) {
|
|||||||
|
|
||||||
/* Load kernel CR3 as current CR3 */
|
/* Load kernel CR3 as current CR3 */
|
||||||
void amd64_load_kernel_cr3 (void) {
|
void amd64_load_kernel_cr3 (void) {
|
||||||
|
uintptr_t cr3 = amd64_current_cr3 ();
|
||||||
|
|
||||||
|
if (cr3 != kernel_pd.cr3_paddr) {
|
||||||
__asm__ volatile ("movq %0, %%cr3" ::"r"(kernel_pd.cr3_paddr) : "memory");
|
__asm__ volatile ("movq %0, %%cr3" ::"r"(kernel_pd.cr3_paddr) : "memory");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct pd* mm_get_kernel_pd (void) { return &kernel_pd; }
|
||||||
|
|
||||||
/* Extract PML info from virtual address */
|
/* Extract PML info from virtual address */
|
||||||
static struct pg_index amd64_mm_page_index (uint64_t vaddr) {
|
static struct pg_index amd64_mm_page_index (uint64_t vaddr) {
|
||||||
struct pg_index ret;
|
struct pg_index ret;
|
||||||
@@ -59,9 +66,12 @@ static uint64_t* amd64_mm_next_table (uint64_t* table, uint64_t entry_idx, bool
|
|||||||
|
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
|
|
||||||
if (entry & AMD64_PG_PRESENT)
|
if (entry & AMD64_PG_PRESENT) {
|
||||||
|
if (entry & AMD64_PG_HUGE)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
paddr = entry & ~0xFFFULL;
|
paddr = entry & ~0xFFFULL;
|
||||||
else {
|
} else {
|
||||||
if (!alloc)
|
if (!alloc)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
@@ -104,13 +114,7 @@ static void amd64_reload_cr3 (void) {
|
|||||||
|
|
||||||
/* Map physical address to virtual address with flags. TLB needs to be flushed afterwards. */
|
/* Map physical address to virtual address with flags. TLB needs to be flushed afterwards. */
|
||||||
void mm_map_page (struct pd* pd, uintptr_t paddr, uintptr_t vaddr, uint32_t flags) {
|
void mm_map_page (struct pd* pd, uintptr_t paddr, uintptr_t vaddr, uint32_t flags) {
|
||||||
spin_lock (&mm_lock);
|
|
||||||
|
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
bool do_reload = false;
|
|
||||||
|
|
||||||
if (flags & MM_PD_LOCK)
|
|
||||||
spin_lock (&pd->lock);
|
|
||||||
|
|
||||||
uint64_t amd64_flags = amd64_mm_resolve_flags (flags);
|
uint64_t amd64_flags = amd64_mm_resolve_flags (flags);
|
||||||
|
|
||||||
@@ -119,67 +123,50 @@ void mm_map_page (struct pd* pd, uintptr_t paddr, uintptr_t vaddr, uint32_t flag
|
|||||||
|
|
||||||
uint64_t* pml3 = amd64_mm_next_table (pml4, pg_index.pml4, true);
|
uint64_t* pml3 = amd64_mm_next_table (pml4, pg_index.pml4, true);
|
||||||
if (pml3 == NULL)
|
if (pml3 == NULL)
|
||||||
goto done;
|
return;
|
||||||
|
|
||||||
uint64_t* pml2 = amd64_mm_next_table (pml3, pg_index.pml3, true);
|
uint64_t* pml2 = amd64_mm_next_table (pml3, pg_index.pml3, true);
|
||||||
if (pml2 == NULL)
|
if (pml2 == NULL)
|
||||||
goto done;
|
return;
|
||||||
|
|
||||||
uint64_t* pml1 = amd64_mm_next_table (pml2, pg_index.pml2, true);
|
uint64_t* pml1 = amd64_mm_next_table (pml2, pg_index.pml2, true);
|
||||||
if (pml1 == NULL)
|
if (pml1 == NULL)
|
||||||
goto done;
|
return;
|
||||||
|
|
||||||
uint64_t* pte = &pml1[pg_index.pml1];
|
uint64_t* pte = &pml1[pg_index.pml1];
|
||||||
|
|
||||||
*pte = ((paddr & ~0xFFFULL) | (amd64_flags & 0x7ULL));
|
*pte = ((paddr & ~0xFFFULL) | (amd64_flags & 0x7ULL));
|
||||||
do_reload = true;
|
|
||||||
|
|
||||||
done:
|
|
||||||
if (do_reload && (flags & MM_PD_RELOAD))
|
|
||||||
amd64_reload_cr3 ();
|
|
||||||
|
|
||||||
if (flags & MM_PD_LOCK)
|
|
||||||
spin_unlock (&pd->lock);
|
|
||||||
|
|
||||||
spin_unlock (&mm_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Map a page into kernel page directory */
|
/* Map a page into kernel page directory */
|
||||||
void mm_map_kernel_page (uintptr_t paddr, uintptr_t vaddr, uint32_t flags) {
|
void mm_map_kernel_page (uintptr_t paddr, uintptr_t vaddr, uint32_t flags) {
|
||||||
mm_map_page (&kernel_pd, paddr, vaddr, flags);
|
mm_map_page (&kernel_pd, paddr, vaddr, flags);
|
||||||
|
amd64_reload_cr3 ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unmap a virtual address. TLB needs to be flushed afterwards */
|
/* Unmap a virtual address. TLB needs to be flushed afterwards */
|
||||||
void mm_unmap_page (struct pd* pd, uintptr_t vaddr, uint32_t flags) {
|
void mm_unmap_page (struct pd* pd, uintptr_t vaddr) {
|
||||||
spin_lock (&mm_lock);
|
|
||||||
|
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
bool do_reload = false;
|
|
||||||
|
|
||||||
if (flags & MM_PD_LOCK)
|
|
||||||
spin_lock (&pd->lock);
|
|
||||||
|
|
||||||
uint64_t* pml4 = (uint64_t*)(pd->cr3_paddr + (uintptr_t)hhdm->offset);
|
uint64_t* pml4 = (uint64_t*)(pd->cr3_paddr + (uintptr_t)hhdm->offset);
|
||||||
struct pg_index pg_index = amd64_mm_page_index (vaddr);
|
struct pg_index pg_index = amd64_mm_page_index (vaddr);
|
||||||
|
|
||||||
uint64_t* pml3 = amd64_mm_next_table (pml4, pg_index.pml4, false);
|
uint64_t* pml3 = amd64_mm_next_table (pml4, pg_index.pml4, false);
|
||||||
if (pml3 == NULL)
|
if (pml3 == NULL)
|
||||||
goto done;
|
return;
|
||||||
|
|
||||||
uint64_t* pml2 = amd64_mm_next_table (pml3, pg_index.pml3, false);
|
uint64_t* pml2 = amd64_mm_next_table (pml3, pg_index.pml3, false);
|
||||||
if (pml2 == NULL)
|
if (pml2 == NULL)
|
||||||
goto done;
|
return;
|
||||||
|
|
||||||
uint64_t* pml1 = amd64_mm_next_table (pml2, pg_index.pml2, false);
|
uint64_t* pml1 = amd64_mm_next_table (pml2, pg_index.pml2, false);
|
||||||
if (pml1 == NULL)
|
if (pml1 == NULL)
|
||||||
goto done;
|
return;
|
||||||
|
|
||||||
uint64_t* pte = &pml1[pg_index.pml1];
|
uint64_t* pte = &pml1[pg_index.pml1];
|
||||||
|
|
||||||
if ((*pte) & AMD64_PG_PRESENT) {
|
if ((*pte) & AMD64_PG_PRESENT)
|
||||||
*pte = 0;
|
*pte = 0;
|
||||||
do_reload = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (amd64_mm_is_table_empty (pml1)) {
|
if (amd64_mm_is_table_empty (pml1)) {
|
||||||
uintptr_t pml1_phys = pml2[pg_index.pml2] & ~0xFFFULL;
|
uintptr_t pml1_phys = pml2[pg_index.pml2] & ~0xFFFULL;
|
||||||
@@ -198,28 +185,14 @@ void mm_unmap_page (struct pd* pd, uintptr_t vaddr, uint32_t flags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
|
||||||
if (do_reload && (flags & MM_PD_RELOAD))
|
|
||||||
amd64_reload_cr3 ();
|
|
||||||
|
|
||||||
if (flags & MM_PD_LOCK)
|
|
||||||
spin_unlock (&pd->lock);
|
|
||||||
|
|
||||||
spin_unlock (&mm_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unmap a page from kernel page directory */
|
/* Unmap a page from kernel page directory */
|
||||||
void mm_unmap_kernel_page (uintptr_t vaddr, uint32_t flags) {
|
void mm_unmap_kernel_page (uintptr_t vaddr) {
|
||||||
mm_unmap_page (&kernel_pd, vaddr, flags);
|
mm_unmap_page (&kernel_pd, vaddr);
|
||||||
|
amd64_reload_cr3 ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Lock kernel page directory */
|
|
||||||
void mm_lock_kernel (void) { spin_lock (&kernel_pd.lock); }
|
|
||||||
|
|
||||||
/* Unlock kernel page directory */
|
|
||||||
void mm_unlock_kernel (void) { spin_unlock (&kernel_pd.lock); }
|
|
||||||
|
|
||||||
/* Allocate a userspace-ready page directory */
|
/* Allocate a userspace-ready page directory */
|
||||||
uintptr_t mm_alloc_user_pd_phys (void) {
|
uintptr_t mm_alloc_user_pd_phys (void) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
@@ -238,31 +211,110 @@ uintptr_t mm_alloc_user_pd_phys (void) {
|
|||||||
return cr3;
|
return cr3;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reload after map/unmap operation was performed. This function does the TLB shootdown. */
|
bool mm_validate (struct pd* pd, uintptr_t vaddr) {
|
||||||
void mm_reload (void) {
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
spin_lock (&mm_lock);
|
bool ret = false;
|
||||||
|
|
||||||
struct limine_mp_response* mp = limine_mp_request.response;
|
uint64_t* pml4 = (uint64_t*)(pd->cr3_paddr + (uintptr_t)hhdm->offset);
|
||||||
|
struct pg_index pg_index = amd64_mm_page_index (vaddr);
|
||||||
|
|
||||||
for (size_t i = 0; i < mp->cpu_count; i++) {
|
uint64_t* pml3 = amd64_mm_next_table (pml4, pg_index.pml4, false);
|
||||||
amd64_lapic_ipi (mp->cpus[i]->lapic_id, TLB_SHOOTDOWN);
|
if (pml3 == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
uint64_t* pml2 = amd64_mm_next_table (pml3, pg_index.pml3, false);
|
||||||
|
if (pml2 == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
uint64_t* pml1 = amd64_mm_next_table (pml2, pg_index.pml2, false);
|
||||||
|
if (pml1 == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
uint64_t pte = pml1[pg_index.pml1];
|
||||||
|
ret = (pte & AMD64_PG_PRESENT) != 0;
|
||||||
|
|
||||||
|
done:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mm_validate_buffer (struct pd* pd, uintptr_t vaddr, size_t size) {
|
||||||
|
bool ok = true;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
ok = mm_validate (pd, vaddr + i);
|
||||||
|
if (!ok)
|
||||||
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
spin_unlock (&mm_lock);
|
done:
|
||||||
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TLB shootdown IRQ handler */
|
uintptr_t mm_p2v (struct pd* pd, uintptr_t paddr) {
|
||||||
static void amd64_tlb_shootdown_irq (void* arg, void* regs) {
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
(void)arg, (void)regs;
|
uintptr_t ret = 0;
|
||||||
|
|
||||||
amd64_reload_cr3 ();
|
uint64_t* pml4 = (uint64_t*)(pd->cr3_paddr + (uintptr_t)hhdm->offset);
|
||||||
DEBUG ("cpu %u TLB shootdown\n", thiscpu->id);
|
|
||||||
|
for (size_t i4 = 0; i4 < 512; i4++) {
|
||||||
|
if (!(pml4[i4] & AMD64_PG_PRESENT))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
uint64_t* pml3 = (uint64_t*)((uintptr_t)hhdm->offset + (pml4[i4] & ~0xFFFULL));
|
||||||
|
for (size_t i3 = 0; i3 < 512; i3++) {
|
||||||
|
if (!(pml3[i3] & AMD64_PG_PRESENT))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
uint64_t* pml2 = (uint64_t*)((uintptr_t)hhdm->offset + (pml3[i3] & ~0xFFFULL));
|
||||||
|
for (size_t i2 = 0; i2 < 512; i2++) {
|
||||||
|
if (!(pml2[i2] & AMD64_PG_PRESENT))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
uint64_t* pml1 = (uint64_t*)((uintptr_t)hhdm->offset + (pml2[i2] & ~0xFFFULL));
|
||||||
|
for (size_t i1 = 0; i1 < 512; i1++) {
|
||||||
|
if ((pml1[i1] & AMD64_PG_PRESENT) && ((pml1[i1] & ~0xFFFULL) == (paddr & ~0xFFFULL))) {
|
||||||
|
struct pg_index idx = {i4, i3, i2, i1};
|
||||||
|
ret = (((uint64_t)idx.pml4 << 39) | ((uint64_t)idx.pml3 << 30) |
|
||||||
|
((uint64_t)idx.pml2 << 21) | ((uint64_t)idx.pml1 << 12) | (paddr & 0xFFFULL));
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Continue initializing memory management subsystem for AMD64 after the essential parts were
|
uintptr_t mm_v2p (struct pd* pd, uintptr_t vaddr) {
|
||||||
* initialized */
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
void mm_init2 (void) {
|
uintptr_t ret = 0;
|
||||||
irq_attach (&amd64_tlb_shootdown_irq, NULL, TLB_SHOOTDOWN, IRQ_INTERRUPT_SAFE);
|
|
||||||
|
uint64_t* pml4 = (uint64_t*)(pd->cr3_paddr + (uintptr_t)hhdm->offset);
|
||||||
|
struct pg_index pg_index = amd64_mm_page_index (vaddr);
|
||||||
|
|
||||||
|
uint64_t* pml3 = amd64_mm_next_table (pml4, pg_index.pml4, false);
|
||||||
|
if (pml3 == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
uint64_t* pml2 = amd64_mm_next_table (pml3, pg_index.pml3, false);
|
||||||
|
if (pml2 == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
uint64_t* pml1 = amd64_mm_next_table (pml2, pg_index.pml2, false);
|
||||||
|
if (pml1 == NULL)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
uint64_t pte = pml1[pg_index.pml1];
|
||||||
|
|
||||||
|
if (!(pte & AMD64_PG_PRESENT))
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
ret = ((pte & ~0xFFFULL) | (vaddr & 0xFFFULL));
|
||||||
|
|
||||||
|
done:
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize essentials for the AMD64 memory management subsystem */
|
/* Initialize essentials for the AMD64 memory management subsystem */
|
||||||
|
|||||||
@@ -7,11 +7,9 @@
|
|||||||
#define PAGE_SIZE 4096
|
#define PAGE_SIZE 4096
|
||||||
|
|
||||||
struct pd {
|
struct pd {
|
||||||
spin_lock_t lock;
|
|
||||||
uintptr_t cr3_paddr;
|
uintptr_t cr3_paddr;
|
||||||
};
|
};
|
||||||
|
|
||||||
void amd64_load_kernel_cr3 (void);
|
void amd64_load_kernel_cr3 (void);
|
||||||
void mm_init2 (void);
|
|
||||||
|
|
||||||
#endif // _KERNEL_AMD64_MM_H
|
#endif // _KERNEL_AMD64_MM_H
|
||||||
|
|||||||
@@ -1,16 +1,23 @@
|
|||||||
#include <amd64/gdt.h>
|
#include <amd64/gdt.h>
|
||||||
|
#include <amd64/proc.h>
|
||||||
#include <aux/elf.h>
|
#include <aux/elf.h>
|
||||||
|
#include <libk/align.h>
|
||||||
#include <libk/list.h>
|
#include <libk/list.h>
|
||||||
|
#include <libk/rbtree.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <libk/string.h>
|
#include <libk/string.h>
|
||||||
#include <limine/requests.h>
|
#include <limine/requests.h>
|
||||||
#include <mm/liballoc.h>
|
#include <mm/liballoc.h>
|
||||||
#include <mm/pmm.h>
|
#include <mm/pmm.h>
|
||||||
|
#include <proc/mutex.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
#include <proc/procgroup.h>
|
||||||
|
#include <proc/resource.h>
|
||||||
#include <sync/spin_lock.h>
|
#include <sync/spin_lock.h>
|
||||||
#include <sys/debug.h>
|
#include <sys/debug.h>
|
||||||
|
#include <sys/proc.h>
|
||||||
|
|
||||||
static atomic_int pids = 1;
|
static atomic_int pids = 0;
|
||||||
|
|
||||||
struct proc* proc_from_elf (uint8_t* elf_contents) {
|
struct proc* proc_from_elf (uint8_t* elf_contents) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
@@ -21,30 +28,24 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
|
|||||||
|
|
||||||
memset (proc, 0, sizeof (*proc));
|
memset (proc, 0, sizeof (*proc));
|
||||||
|
|
||||||
proc->pd.lock = SPIN_LOCK_INIT;
|
proc->lock = SPIN_LOCK_INIT;
|
||||||
proc->pd.cr3_paddr = mm_alloc_user_pd_phys ();
|
atomic_store (&proc->state, PROC_READY);
|
||||||
if (proc->pd.cr3_paddr == 0) {
|
proc->pid = atomic_fetch_add (&pids, 1);
|
||||||
|
|
||||||
|
proc->procgroup = procgroup_create ();
|
||||||
|
if (proc->procgroup == NULL) {
|
||||||
free (proc);
|
free (proc);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
procgroup_attach (proc->procgroup, proc);
|
||||||
|
|
||||||
proc->pdata.kernel_stack = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
|
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
|
||||||
if (proc->pdata.kernel_stack == PMM_ALLOC_ERR) {
|
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
||||||
free (proc);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
uintptr_t kernel_stack = proc->pdata.kernel_stack;
|
|
||||||
proc->pdata.kernel_stack += (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
|
||||||
|
|
||||||
proc->pdata.user_stack = pmm_alloc (USTACK_SIZE / PAGE_SIZE);
|
procgroup_map (proc->procgroup, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
|
||||||
if (proc->pdata.user_stack == PMM_ALLOC_ERR) {
|
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW, NULL);
|
||||||
free (proc);
|
|
||||||
pmm_free (kernel_stack, USTACK_SIZE / PAGE_SIZE);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
proc_map (proc, proc->pdata.user_stack, PROC_USTACK_TOP - USTACK_SIZE, USTACK_SIZE / PAGE_SIZE,
|
proc->flags |= PROC_USTK_PREALLOC;
|
||||||
MM_PG_USER | MM_PG_PRESENT | MM_PG_RW);
|
|
||||||
|
|
||||||
struct elf_aux aux = proc_load_segments (proc, elf_contents);
|
struct elf_aux aux = proc_load_segments (proc, elf_contents);
|
||||||
|
|
||||||
@@ -53,35 +54,85 @@ struct proc* proc_from_elf (uint8_t* elf_contents) {
|
|||||||
proc->pdata.regs.rflags = 0x202;
|
proc->pdata.regs.rflags = 0x202;
|
||||||
proc->pdata.regs.cs = GDT_UCODE | 0x03;
|
proc->pdata.regs.cs = GDT_UCODE | 0x03;
|
||||||
proc->pdata.regs.rip = aux.entry;
|
proc->pdata.regs.rip = aux.entry;
|
||||||
|
|
||||||
|
return proc;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, uintptr_t entry,
|
||||||
|
uintptr_t argument_ptr) {
|
||||||
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
|
spin_lock_ctx_t ctxprt;
|
||||||
|
|
||||||
|
struct proc* proc = malloc (sizeof (*proc));
|
||||||
|
if (proc == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memset (proc, 0, sizeof (*proc));
|
||||||
|
|
||||||
proc->lock = SPIN_LOCK_INIT;
|
proc->lock = SPIN_LOCK_INIT;
|
||||||
atomic_store (&proc->state, PROC_READY);
|
atomic_store (&proc->state, PROC_READY);
|
||||||
proc->pid = atomic_fetch_add (&pids, 1);
|
proc->pid = atomic_fetch_add (&pids, 1);
|
||||||
|
|
||||||
|
spin_lock (&proto->lock, &ctxprt);
|
||||||
|
|
||||||
|
proc->procgroup = proto->procgroup;
|
||||||
|
procgroup_attach (proc->procgroup, proc);
|
||||||
|
|
||||||
|
spin_unlock (&proto->lock, &ctxprt);
|
||||||
|
|
||||||
|
uintptr_t kstack_paddr = pmm_alloc (KSTACK_SIZE / PAGE_SIZE);
|
||||||
|
proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE;
|
||||||
|
|
||||||
|
proc->pdata.regs.ss = GDT_UDATA | 0x03;
|
||||||
|
proc->pdata.regs.rsp = (uint64_t)vstack_top;
|
||||||
|
proc->pdata.regs.rflags = 0x202;
|
||||||
|
proc->pdata.regs.cs = GDT_UCODE | 0x03;
|
||||||
|
proc->pdata.regs.rip = (uint64_t)entry;
|
||||||
|
|
||||||
|
proc->uvaddr_argument = argument_ptr;
|
||||||
|
|
||||||
|
proc_init_tls (proc);
|
||||||
|
|
||||||
return proc;
|
return proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_cleanup (struct proc* proc) {
|
void proc_cleanup (struct proc* proc) {
|
||||||
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
proc_sqs_cleanup (proc);
|
||||||
|
proc_mutexes_cleanup (proc);
|
||||||
|
|
||||||
struct list_node_link *mapping_link, *mapping_link_tmp;
|
pmm_free (proc->pdata.kernel_stack, KSTACK_SIZE / PAGE_SIZE);
|
||||||
spin_lock (&proc->pd.lock);
|
procgroup_unmap (proc->procgroup, proc->pdata.tls_vaddr, proc->procgroup->tls.tls_tmpl_pages);
|
||||||
|
|
||||||
list_foreach (proc->mappings, mapping_link, mapping_link_tmp) {
|
procgroup_detach (proc->procgroup, proc);
|
||||||
struct proc_mapping* mapping =
|
|
||||||
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
|
|
||||||
|
|
||||||
pmm_free (mapping->paddr, mapping->size / PAGE_SIZE);
|
|
||||||
list_remove (proc->mappings, mapping_link);
|
|
||||||
free (mapping);
|
|
||||||
}
|
|
||||||
|
|
||||||
spin_unlock (&proc->pd.lock);
|
|
||||||
|
|
||||||
pmm_free (proc->pd.cr3_paddr, 1);
|
|
||||||
|
|
||||||
pmm_free (proc->pdata.kernel_stack - (uintptr_t)hhdm->offset - KSTACK_SIZE,
|
|
||||||
KSTACK_SIZE / PAGE_SIZE);
|
|
||||||
pmm_free (proc->pdata.user_stack, USTACK_SIZE / PAGE_SIZE);
|
|
||||||
|
|
||||||
|
/* clean the process */
|
||||||
free (proc);
|
free (proc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void proc_init_tls (struct proc* proc) {
|
||||||
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
|
|
||||||
|
if (proc->procgroup->tls.tls_tmpl == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
size_t tls_size = proc->procgroup->tls.tls_tmpl_size;
|
||||||
|
size_t pages = proc->procgroup->tls.tls_tmpl_pages;
|
||||||
|
|
||||||
|
uintptr_t tls_paddr;
|
||||||
|
uint32_t flags = MM_PG_USER | MM_PG_PRESENT | MM_PG_RW;
|
||||||
|
|
||||||
|
uintptr_t tls_vaddr = procgroup_map (proc->procgroup, 0, pages, flags, &tls_paddr);
|
||||||
|
|
||||||
|
uintptr_t k_tls_addr = (uintptr_t)hhdm->offset + tls_paddr;
|
||||||
|
|
||||||
|
memset ((void*)k_tls_addr, 0, pages * PAGE_SIZE);
|
||||||
|
memcpy ((void*)k_tls_addr, (void*)proc->procgroup->tls.tls_tmpl, tls_size);
|
||||||
|
|
||||||
|
uintptr_t ktcb = k_tls_addr + tls_size;
|
||||||
|
uintptr_t utcb = tls_vaddr + tls_size;
|
||||||
|
|
||||||
|
*(uintptr_t*)ktcb = utcb;
|
||||||
|
|
||||||
|
proc->pdata.fs_base = utcb;
|
||||||
|
proc->pdata.tls_vaddr = tls_vaddr;
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,17 +4,19 @@
|
|||||||
#include <amd64/intr.h>
|
#include <amd64/intr.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
|
||||||
/// Top of userspace process' stack
|
/* Top of userspace process' stack */
|
||||||
#define PROC_USTACK_TOP 0x00007FFFFFFFF000ULL
|
#define PROC_USTACK_TOP 0x00007FFFFFFFF000ULL
|
||||||
/// Size of userspace process' stack
|
/* Size of userspace process' stack */
|
||||||
#define USTACK_SIZE (256 * PAGE_SIZE)
|
#define USTACK_SIZE (256 * PAGE_SIZE)
|
||||||
|
/* proc_map () base address */
|
||||||
|
#define PROC_MAP_BASE 0x0000700000000000
|
||||||
|
|
||||||
/// Platform-dependent process data
|
/* Platform-dependent process data */
|
||||||
struct proc_platformdata {
|
struct proc_platformdata {
|
||||||
struct saved_regs regs;
|
struct saved_regs regs;
|
||||||
uintptr_t user_stack;
|
|
||||||
uintptr_t kernel_stack;
|
uintptr_t kernel_stack;
|
||||||
uint64_t gs_base;
|
uint64_t fs_base;
|
||||||
|
uintptr_t tls_vaddr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _KERNEL_AMD64_PROC_H
|
#endif // _KERNEL_AMD64_PROC_H
|
||||||
|
|||||||
13
kernel/amd64/procgroup.h
Normal file
13
kernel/amd64/procgroup.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef _KERNEL_AMD64_PROCGRPUP_H
|
||||||
|
#define _KERNEL_AMD64_PROCGRPUP_H
|
||||||
|
|
||||||
|
#include <libk/std.h>
|
||||||
|
|
||||||
|
struct procgroup_tls {
|
||||||
|
uint8_t* tls_tmpl;
|
||||||
|
size_t tls_tmpl_size;
|
||||||
|
size_t tls_tmpl_total_size;
|
||||||
|
size_t tls_tmpl_pages;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _KERNEL_AMD64_PROCGRPUP_H
|
||||||
@@ -35,4 +35,21 @@
|
|||||||
popq % rcx; \
|
popq % rcx; \
|
||||||
popq % rax;
|
popq % rax;
|
||||||
|
|
||||||
|
#define pop_regs_skip_rax \
|
||||||
|
popq % r15; \
|
||||||
|
popq % r14; \
|
||||||
|
popq % r13; \
|
||||||
|
popq % r12; \
|
||||||
|
popq % r11; \
|
||||||
|
popq % r10; \
|
||||||
|
popq % r9; \
|
||||||
|
popq % r8; \
|
||||||
|
popq % rbx; \
|
||||||
|
popq % rbp; \
|
||||||
|
popq % rdi; \
|
||||||
|
popq % rsi; \
|
||||||
|
popq % rdx; \
|
||||||
|
popq % rcx; \
|
||||||
|
addq $8, % rsp
|
||||||
|
|
||||||
#endif // _KERNEL_AMD64_REGSASM_H
|
#endif // _KERNEL_AMD64_REGSASM_H
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
.global amd64_do_sched
|
.global amd64_do_sched
|
||||||
amd64_do_sched:
|
amd64_do_sched:
|
||||||
cli
|
|
||||||
movq %rsi, %cr3
|
movq %rsi, %cr3
|
||||||
movq %rdi, %rsp
|
movq %rdi, %rsp
|
||||||
pop_regs
|
pop_regs
|
||||||
add $16, %rsp
|
addq $16, %rsp
|
||||||
iretq
|
iretq
|
||||||
|
|||||||
@@ -3,12 +3,21 @@
|
|||||||
#include <amd64/sched.h>
|
#include <amd64/sched.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
#include <sys/mm.h>
|
#include <sys/mm.h>
|
||||||
#include <sys/smp.h>
|
#include <sys/smp.h>
|
||||||
|
|
||||||
void do_sched (struct proc* proc) {
|
void do_sched (struct proc* proc, spin_lock_t* cpu_lock, spin_lock_ctx_t* ctxcpu) {
|
||||||
|
spin_lock_ctx_t ctxpr;
|
||||||
|
|
||||||
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
|
|
||||||
thiscpu->tss.rsp0 = proc->pdata.kernel_stack;
|
thiscpu->tss.rsp0 = proc->pdata.kernel_stack;
|
||||||
thiscpu->syscall_kernel_stack = proc->pdata.kernel_stack;
|
thiscpu->syscall_kernel_stack = proc->pdata.kernel_stack;
|
||||||
|
amd64_wrmsr (MSR_FS_BASE, proc->pdata.fs_base);
|
||||||
|
|
||||||
amd64_do_sched ((void*)&proc->pdata.regs, (void*)proc->pd.cr3_paddr);
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
|
spin_unlock (cpu_lock, ctxcpu);
|
||||||
|
|
||||||
|
amd64_do_sched ((void*)&proc->pdata.regs, (void*)proc->procgroup->pd.cr3_paddr);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,10 @@
|
|||||||
#include <libk/string.h>
|
#include <libk/string.h>
|
||||||
#include <limine/requests.h>
|
#include <limine/requests.h>
|
||||||
#include <mm/liballoc.h>
|
#include <mm/liballoc.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
#include <sync/spin_lock.h>
|
#include <sync/spin_lock.h>
|
||||||
#include <sys/debug.h>
|
#include <sys/debug.h>
|
||||||
|
#include <sys/sched.h>
|
||||||
#include <sys/smp.h>
|
#include <sys/smp.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
|
|
||||||
@@ -21,7 +23,7 @@ static struct cpu cpus[CPUS_MAX];
|
|||||||
static atomic_int cpu_init_count;
|
static atomic_int cpu_init_count;
|
||||||
|
|
||||||
/// Allocate a CPU structure
|
/// Allocate a CPU structure
|
||||||
struct cpu* cpu_make (void) {
|
struct cpu* cpu_make (uint64_t lapic_id) {
|
||||||
int id = atomic_fetch_add (&cpu_counter, 1);
|
int id = atomic_fetch_add (&cpu_counter, 1);
|
||||||
|
|
||||||
struct cpu* cpu = &cpus[id];
|
struct cpu* cpu = &cpus[id];
|
||||||
@@ -29,7 +31,7 @@ struct cpu* cpu_make (void) {
|
|||||||
memset (cpu, 0, sizeof (*cpu));
|
memset (cpu, 0, sizeof (*cpu));
|
||||||
cpu->lock = SPIN_LOCK_INIT;
|
cpu->lock = SPIN_LOCK_INIT;
|
||||||
cpu->id = id;
|
cpu->id = id;
|
||||||
cpu->self = cpu;
|
cpu->lapic_id = lapic_id;
|
||||||
|
|
||||||
amd64_wrmsr (MSR_GS_BASE, (uint64_t)cpu);
|
amd64_wrmsr (MSR_GS_BASE, (uint64_t)cpu);
|
||||||
|
|
||||||
@@ -42,48 +44,64 @@ struct cpu* cpu_get (void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cpu_request_sched (struct cpu* cpu) {
|
void cpu_request_sched (struct cpu* cpu) {
|
||||||
struct limine_mp_response* mp = limine_mp_request.response;
|
if (cpu == thiscpu) {
|
||||||
|
proc_sched ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < mp->cpu_count; i++) {
|
amd64_lapic_ipi (cpu->lapic_id, CPU_REQUEST_SCHED);
|
||||||
if (cpu->id == i) {
|
}
|
||||||
amd64_lapic_ipi (mp->cpus[i]->lapic_id, CPU_REQUEST_SCHED);
|
|
||||||
break;
|
struct cpu* cpu_find_lightest (void) {
|
||||||
|
struct cpu* cpu = &cpus[0];
|
||||||
|
|
||||||
|
int load = atomic_load (&cpu->proc_run_q_count);
|
||||||
|
|
||||||
|
for (unsigned int i = 1; i < cpu_counter; i++) {
|
||||||
|
struct cpu* new_cpu = &cpus[i];
|
||||||
|
int new_load = atomic_load (&new_cpu->proc_run_q_count);
|
||||||
|
if (new_load < load) {
|
||||||
|
load = new_load;
|
||||||
|
cpu = new_cpu;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return cpu;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bootstrap code for non-BSP CPUs
|
/// Bootstrap code for non-BSP CPUs
|
||||||
static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) {
|
static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) {
|
||||||
amd64_load_kernel_cr3 ();
|
amd64_load_kernel_cr3 ();
|
||||||
|
|
||||||
struct cpu* cpu = cpu_make ();
|
struct cpu* cpu = cpu_make (mp_info->lapic_id);
|
||||||
|
|
||||||
amd64_init (cpu, true); /* gdt + idt */
|
amd64_init (cpu, true); /* gdt + idt */
|
||||||
syscall_init ();
|
syscall_init ();
|
||||||
|
|
||||||
thiscpu->lapic_ticks = amd64_lapic_init (10000);
|
amd64_lapic_init (1000);
|
||||||
amd64_lapic_tick (thiscpu->lapic_ticks);
|
|
||||||
|
|
||||||
DEBUG ("CPU %u is online!\n", thiscpu->id);
|
DEBUG ("CPU %u is online!\n", thiscpu->id);
|
||||||
|
|
||||||
__asm__ volatile ("sti");
|
|
||||||
|
|
||||||
atomic_fetch_sub (&cpu_init_count, 1);
|
atomic_fetch_sub (&cpu_init_count, 1);
|
||||||
|
|
||||||
for (;;)
|
struct proc* spin_proc = proc_spawn_rd ("spin.exe");
|
||||||
;
|
proc_register (spin_proc, thiscpu);
|
||||||
|
|
||||||
|
spin_lock_ctx_t ctxcpu;
|
||||||
|
spin_lock (&spin_proc->cpu->lock, &ctxcpu);
|
||||||
|
do_sched (spin_proc, &spin_proc->cpu->lock, &ctxcpu);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize SMP subsystem for AMD64. Start AP CPUs
|
/// Initialize SMP subsystem for AMD64. Start AP CPUs
|
||||||
void smp_init (void) {
|
void smp_init (void) {
|
||||||
thiscpu->lapic_ticks = amd64_lapic_init (10000);
|
amd64_lapic_init (1000);
|
||||||
|
|
||||||
struct limine_mp_response* mp = limine_mp_request.response;
|
struct limine_mp_response* mp = limine_mp_request.response;
|
||||||
|
|
||||||
cpu_init_count = mp->cpu_count - 1; /* Don't include BSP */
|
cpu_init_count = mp->cpu_count - 1; /* Don't include BSP */
|
||||||
|
|
||||||
for (size_t i = 0; i < mp->cpu_count; i++) {
|
for (size_t i = 0; i < mp->cpu_count; i++) {
|
||||||
if (mp->cpus[i]->lapic_id != thiscpu->id) {
|
if (mp->cpus[i]->lapic_id != thiscpu->lapic_id) {
|
||||||
DEBUG ("Trying CPU %u\n", mp->cpus[i]->lapic_id);
|
DEBUG ("Trying CPU %u\n", mp->cpus[i]->lapic_id);
|
||||||
mp->cpus[i]->goto_address = &amd64_smp_bootstrap;
|
mp->cpus[i]->goto_address = &amd64_smp_bootstrap;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@
|
|||||||
#define _KERNEL_AMD64_SMP_H
|
#define _KERNEL_AMD64_SMP_H
|
||||||
|
|
||||||
#include <amd64/gdt.h>
|
#include <amd64/gdt.h>
|
||||||
|
#include <amd64/intr.h>
|
||||||
#include <amd64/tss.h>
|
#include <amd64/tss.h>
|
||||||
#include <aux/compiler.h>
|
#include <aux/compiler.h>
|
||||||
#include <libk/rbtree.h>
|
#include <libk/rbtree.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
|
||||||
#define CPUS_MAX 32
|
#define CPUS_MAX 32
|
||||||
|
|
||||||
@@ -14,30 +16,28 @@ struct cpu {
|
|||||||
/* for syscall instruction */
|
/* for syscall instruction */
|
||||||
uintptr_t syscall_user_stack;
|
uintptr_t syscall_user_stack;
|
||||||
uintptr_t syscall_kernel_stack;
|
uintptr_t syscall_kernel_stack;
|
||||||
struct cpu* self;
|
|
||||||
volatile uint8_t kernel_stack[KSTACK_SIZE] ALIGNED (16);
|
volatile uint8_t kernel_stack[KSTACK_SIZE] ALIGNED (16);
|
||||||
volatile uint8_t except_stack[KSTACK_SIZE] ALIGNED (16);
|
volatile uint8_t except_stack[KSTACK_SIZE] ALIGNED (16);
|
||||||
volatile uint8_t irq_stack[KSTACK_SIZE] ALIGNED (16);
|
volatile uint8_t irq_stack[KSTACK_SIZE] ALIGNED (16);
|
||||||
volatile struct gdt_extended gdt ALIGNED (16);
|
volatile struct gdt_extended gdt ALIGNED (16);
|
||||||
volatile struct tss tss;
|
volatile struct tss tss;
|
||||||
|
|
||||||
|
uintptr_t lapic_mmio_base;
|
||||||
uint64_t lapic_ticks;
|
uint64_t lapic_ticks;
|
||||||
|
uint64_t lapic_id;
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
|
|
||||||
struct {
|
|
||||||
uint64_t rflags;
|
|
||||||
atomic_int nesting;
|
|
||||||
} irq_ctx;
|
|
||||||
|
|
||||||
spin_lock_t lock;
|
spin_lock_t lock;
|
||||||
|
|
||||||
struct rb_node_link* proc_run_q;
|
struct list_node_link* proc_run_q;
|
||||||
struct proc* proc_current;
|
struct proc* proc_current;
|
||||||
|
atomic_int proc_run_q_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct cpu* cpu_make (void);
|
struct cpu* cpu_make (uint64_t lapic_id);
|
||||||
struct cpu* cpu_get (void);
|
struct cpu* cpu_get (void);
|
||||||
void cpu_request_sched (struct cpu* cpu);
|
void cpu_request_sched (struct cpu* cpu);
|
||||||
|
struct cpu* cpu_find_lightest (void);
|
||||||
|
|
||||||
#define thiscpu (cpu_get ())
|
#define thiscpu (cpu_get ())
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include <amd64/mm.h>
|
#include <amd64/mm.h>
|
||||||
#include <amd64/msr-index.h>
|
#include <amd64/msr-index.h>
|
||||||
#include <amd64/msr.h>
|
#include <amd64/msr.h>
|
||||||
|
#include <libk/string.h>
|
||||||
|
#include <m/status.h>
|
||||||
#include <m/syscall_defs.h>
|
#include <m/syscall_defs.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
#include <sys/debug.h>
|
#include <sys/debug.h>
|
||||||
@@ -11,28 +13,33 @@
|
|||||||
|
|
||||||
extern void amd64_syscall_entry (void);
|
extern void amd64_syscall_entry (void);
|
||||||
|
|
||||||
int amd64_syscall_dispatch (void* stack_ptr) {
|
uintptr_t amd64_syscall_dispatch (void* stack_ptr) {
|
||||||
|
spin_lock_ctx_t ctxcpu, ctxpr;
|
||||||
|
|
||||||
|
amd64_load_kernel_cr3 ();
|
||||||
struct saved_regs* regs = stack_ptr;
|
struct saved_regs* regs = stack_ptr;
|
||||||
|
|
||||||
|
spin_lock (&thiscpu->lock, &ctxcpu);
|
||||||
|
struct proc* caller = thiscpu->proc_current;
|
||||||
|
spin_lock (&caller->lock, &ctxpr);
|
||||||
|
|
||||||
|
memcpy (&caller->pdata.regs, regs, sizeof (struct saved_regs));
|
||||||
|
|
||||||
|
spin_unlock (&caller->lock, &ctxpr);
|
||||||
|
spin_unlock (&thiscpu->lock, &ctxcpu);
|
||||||
|
|
||||||
int syscall_num = regs->rax;
|
int syscall_num = regs->rax;
|
||||||
syscall_handler_func_t func = syscall_find_handler (syscall_num);
|
syscall_handler_func_t func = syscall_find_handler (syscall_num);
|
||||||
|
|
||||||
if (func == NULL)
|
if (func == NULL) {
|
||||||
return -SR_SYSCALL_NOT_FOUND;
|
return -ST_SYSCALL_NOT_FOUND;
|
||||||
|
}
|
||||||
|
|
||||||
struct proc* caller = thiscpu->proc_current;
|
return func (caller, regs, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
|
||||||
|
|
||||||
__asm__ volatile ("sti");
|
|
||||||
|
|
||||||
int result = func (caller, regs->rdi, regs->rsi, regs->rdx, regs->r10, regs->r8, regs->r9);
|
|
||||||
|
|
||||||
__asm__ volatile ("cli");
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void syscall_init (void) {
|
void syscall_init (void) {
|
||||||
amd64_wrmsr (MSR_STAR, ((uint64_t)GDT_KCODE << 32) | ((uint64_t)(GDT_UCODE - 16) << 48));
|
amd64_wrmsr (MSR_STAR, ((uint64_t)GDT_KCODE << 32) | ((uint64_t)(GDT_KDATA | 0x03) << 48));
|
||||||
amd64_wrmsr (MSR_LSTAR, (uint64_t)&amd64_syscall_entry);
|
amd64_wrmsr (MSR_LSTAR, (uint64_t)&amd64_syscall_entry);
|
||||||
amd64_wrmsr (MSR_SYSCALL_MASK, (1ULL << 9));
|
amd64_wrmsr (MSR_SYSCALL_MASK, (1ULL << 9));
|
||||||
amd64_wrmsr (MSR_EFER, amd64_rdmsr (MSR_EFER) | EFER_SCE);
|
amd64_wrmsr (MSR_EFER, amd64_rdmsr (MSR_EFER) | EFER_SCE);
|
||||||
|
|||||||
@@ -9,30 +9,39 @@ amd64_syscall_entry:
|
|||||||
movq %rsp, %gs:0
|
movq %rsp, %gs:0
|
||||||
movq %gs:8, %rsp
|
movq %gs:8, %rsp
|
||||||
|
|
||||||
pushq $0x23
|
pushq $0x1b
|
||||||
pushq %gs:0
|
pushq %gs:0
|
||||||
pushq %r11
|
pushq %r11
|
||||||
pushq $0x1b
|
pushq $0x23
|
||||||
pushq %rcx
|
pushq %rcx
|
||||||
pushq $0
|
pushq $0
|
||||||
pushq $0
|
pushq $0
|
||||||
|
|
||||||
push_regs
|
push_regs
|
||||||
|
|
||||||
|
movw $0x10, %ax
|
||||||
|
movw %ax, %ds
|
||||||
|
movw %ax, %es
|
||||||
|
movw %ax, %ss
|
||||||
|
|
||||||
cld
|
cld
|
||||||
|
|
||||||
movq %rsp, %rdi
|
movq %rsp, %rdi
|
||||||
|
|
||||||
|
movq %cr3, %rax; pushq %rax
|
||||||
|
|
||||||
movq %rsp, %rbp
|
movq %rsp, %rbp
|
||||||
|
|
||||||
subq $8, %rsp
|
subq $8, %rsp
|
||||||
andq $~0xF, %rsp
|
andq $-16, %rsp
|
||||||
|
|
||||||
callq amd64_syscall_dispatch
|
callq amd64_syscall_dispatch
|
||||||
|
|
||||||
movq %rbp, %rsp
|
movq %rbp, %rsp
|
||||||
|
|
||||||
pop_regs
|
popq %rbx; movq %rbx, %cr3
|
||||||
|
|
||||||
|
pop_regs_skip_rax
|
||||||
|
|
||||||
addq $56, %rsp
|
addq $56, %rsp
|
||||||
movq %gs:0, %rsp
|
movq %gs:0, %rsp
|
||||||
|
|||||||
@@ -3,18 +3,20 @@
|
|||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <mm/liballoc.h>
|
#include <mm/liballoc.h>
|
||||||
#include <sync/spin_lock.h>
|
#include <sync/spin_lock.h>
|
||||||
|
#include <sys/debug.h>
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
#include <amd64/apic.h>
|
#include <amd64/apic.h>
|
||||||
#include <amd64/intr.h>
|
#include <amd64/intr.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* TODO: figure out a generic way to work with IRQs */
|
struct irq* irq_table[0x100];
|
||||||
|
|
||||||
static struct list_node_link* irqs = NULL;
|
static spin_lock_t irqs_lock = SPIN_LOCK_INIT;
|
||||||
static spin_lock_t irqs_lock;
|
|
||||||
|
bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num) {
|
||||||
|
spin_lock_ctx_t ctxiqa;
|
||||||
|
|
||||||
bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num, uint32_t flags) {
|
|
||||||
struct irq* irq = malloc (sizeof (*irq));
|
struct irq* irq = malloc (sizeof (*irq));
|
||||||
if (irq == NULL) {
|
if (irq == NULL) {
|
||||||
return false;
|
return false;
|
||||||
@@ -23,48 +25,22 @@ bool irq_attach (void (*func) (void*, void*), void* arg, uint32_t irq_num, uint3
|
|||||||
irq->func = func;
|
irq->func = func;
|
||||||
irq->arg = arg;
|
irq->arg = arg;
|
||||||
irq->irq_num = irq_num;
|
irq->irq_num = irq_num;
|
||||||
irq->flags = flags;
|
|
||||||
|
|
||||||
spin_lock (&irqs_lock);
|
spin_lock (&irqs_lock, &ctxiqa);
|
||||||
list_append (irqs, &irq->irqs_link);
|
irq_table[irq_num] = irq;
|
||||||
spin_unlock (&irqs_lock);
|
spin_unlock (&irqs_lock, &ctxiqa);
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
|
||||||
uint8_t resolution = amd64_resolve_irq (irq_num);
|
|
||||||
amd64_ioapic_route_irq (irq_num, resolution, 0, amd64_lapic_id ());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void irq_detach (void (*func) (void*, void*)) {
|
|
||||||
struct list_node_link *irq_link, *irq_link_tmp;
|
|
||||||
spin_lock (&irqs_lock);
|
|
||||||
|
|
||||||
list_foreach (irqs, irq_link, irq_link_tmp) {
|
|
||||||
struct irq* irq = list_entry (irq_link, struct irq, irqs_link);
|
|
||||||
|
|
||||||
if ((uintptr_t)irq->func == (uintptr_t)func)
|
|
||||||
list_remove (irqs, irq_link);
|
|
||||||
}
|
|
||||||
|
|
||||||
spin_unlock (&irqs_lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct irq* irq_find (uint32_t irq_num) {
|
struct irq* irq_find (uint32_t irq_num) {
|
||||||
struct list_node_link *irq_link, *irq_link_tmp;
|
spin_lock_ctx_t ctxiqa;
|
||||||
spin_lock (&irqs_lock);
|
|
||||||
|
|
||||||
list_foreach (irqs, irq_link, irq_link_tmp) {
|
spin_lock (&irqs_lock, &ctxiqa);
|
||||||
struct irq* irq = list_entry (irq_link, struct irq, irqs_link);
|
|
||||||
|
struct irq* irq = irq_table[irq_num];
|
||||||
|
|
||||||
|
spin_unlock (&irqs_lock, &ctxiqa);
|
||||||
|
|
||||||
if (irq->irq_num == irq_num) {
|
|
||||||
spin_unlock (&irqs_lock);
|
|
||||||
return irq;
|
return irq;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
spin_unlock (&irqs_lock);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,6 @@
|
|||||||
#include <libk/list.h>
|
#include <libk/list.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
|
||||||
#define IRQ_INTERRUPT_SAFE (1 << 0)
|
|
||||||
#define IRQ_INTERRUPT_UNSAFE (1 << 1)
|
|
||||||
|
|
||||||
typedef void (*irq_func_t) (void* arg, void* regs);
|
typedef void (*irq_func_t) (void* arg, void* regs);
|
||||||
|
|
||||||
struct irq {
|
struct irq {
|
||||||
@@ -15,11 +12,9 @@ struct irq {
|
|||||||
irq_func_t func;
|
irq_func_t func;
|
||||||
void* arg;
|
void* arg;
|
||||||
uint32_t irq_num;
|
uint32_t irq_num;
|
||||||
uint32_t flags;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
bool irq_attach (irq_func_t, void* arg, uint32_t irq_num, uint32_t flags);
|
bool irq_attach (irq_func_t, void* arg, uint32_t irq_num);
|
||||||
void irq_detach (irq_func_t func);
|
|
||||||
struct irq* irq_find (uint32_t irq_num);
|
struct irq* irq_find (uint32_t irq_num);
|
||||||
|
|
||||||
#endif // _KERNEL_IRQ_IRQ_H
|
#endif // _KERNEL_IRQ_IRQ_H
|
||||||
|
|||||||
15
kernel/libk/assert.h
Normal file
15
kernel/libk/assert.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef _KERNEL_LIBK_ASSERT_H
|
||||||
|
#define _KERNEL_LIBK_ASSERT_H
|
||||||
|
|
||||||
|
#include <sys/spin.h>
|
||||||
|
|
||||||
|
#define assert(x) \
|
||||||
|
do { \
|
||||||
|
if (!(x)) { \
|
||||||
|
DEBUG ("%s ssertion failed\n", #x); \
|
||||||
|
spin (); \
|
||||||
|
__builtin_unreachable (); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#endif // _KERNEL_LIBK_ASSERT_H
|
||||||
@@ -125,7 +125,7 @@ struct rb_node_link {
|
|||||||
while (__cur) { \
|
while (__cur) { \
|
||||||
type* __obj = rbtree_entry (__cur, type, member); \
|
type* __obj = rbtree_entry (__cur, type, member); \
|
||||||
if ((keyval) == __obj->keyfield) { \
|
if ((keyval) == __obj->keyfield) { \
|
||||||
(out) = __cur; \
|
(out) = rbtree_entry (__cur, type, member); \
|
||||||
break; \
|
break; \
|
||||||
} else if ((keyval) < __obj->keyfield) \
|
} else if ((keyval) < __obj->keyfield) \
|
||||||
__cur = __cur->left; \
|
__cur = __cur->left; \
|
||||||
|
|||||||
@@ -20,3 +20,4 @@ DECL_REQ (memmap, MEMMAP);
|
|||||||
DECL_REQ (rsdp, RSDP);
|
DECL_REQ (rsdp, RSDP);
|
||||||
DECL_REQ (mp, MP);
|
DECL_REQ (mp, MP);
|
||||||
DECL_REQ (module, MODULE);
|
DECL_REQ (module, MODULE);
|
||||||
|
DECL_REQ (framebuffer, FRAMEBUFFER);
|
||||||
|
|||||||
@@ -10,5 +10,6 @@ EXTERN_REQ (memmap);
|
|||||||
EXTERN_REQ (rsdp);
|
EXTERN_REQ (rsdp);
|
||||||
EXTERN_REQ (mp);
|
EXTERN_REQ (mp);
|
||||||
EXTERN_REQ (module);
|
EXTERN_REQ (module);
|
||||||
|
EXTERN_REQ (framebuffer);
|
||||||
|
|
||||||
#endif // _KERNEL_LIMINE_REQUESTS_H
|
#endif // _KERNEL_LIMINE_REQUESTS_H
|
||||||
|
|||||||
@@ -11,13 +11,13 @@
|
|||||||
|
|
||||||
spin_lock_t _liballoc_lock = SPIN_LOCK_INIT;
|
spin_lock_t _liballoc_lock = SPIN_LOCK_INIT;
|
||||||
|
|
||||||
int liballoc_lock (void) {
|
int liballoc_lock (void* ctx) {
|
||||||
spin_lock (&_liballoc_lock);
|
spin_lock (&_liballoc_lock, (spin_lock_ctx_t*)ctx);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int liballoc_unlock (void) {
|
int liballoc_unlock (void* ctx) {
|
||||||
spin_unlock (&_liballoc_lock);
|
spin_unlock (&_liballoc_lock, (spin_lock_ctx_t*)ctx);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,8 +243,9 @@ void* malloc (size_t size) {
|
|||||||
int index;
|
int index;
|
||||||
void* ptr;
|
void* ptr;
|
||||||
struct boundary_tag* tag = NULL;
|
struct boundary_tag* tag = NULL;
|
||||||
|
spin_lock_ctx_t ctxliba;
|
||||||
|
|
||||||
liballoc_lock ();
|
liballoc_lock (&ctxliba);
|
||||||
|
|
||||||
if (l_initialized == 0) {
|
if (l_initialized == 0) {
|
||||||
for (index = 0; index < MAXEXP; index++) {
|
for (index = 0; index < MAXEXP; index++) {
|
||||||
@@ -272,7 +273,7 @@ void* malloc (size_t size) {
|
|||||||
// No page found. Make one.
|
// No page found. Make one.
|
||||||
if (tag == NULL) {
|
if (tag == NULL) {
|
||||||
if ((tag = allocate_new_tag (size)) == NULL) {
|
if ((tag = allocate_new_tag (size)) == NULL) {
|
||||||
liballoc_unlock ();
|
liballoc_unlock (&ctxliba);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,23 +306,24 @@ void* malloc (size_t size) {
|
|||||||
|
|
||||||
ptr = (void*)((uintptr_t)tag + sizeof (struct boundary_tag));
|
ptr = (void*)((uintptr_t)tag + sizeof (struct boundary_tag));
|
||||||
|
|
||||||
liballoc_unlock ();
|
liballoc_unlock (&ctxliba);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free (void* ptr) {
|
void free (void* ptr) {
|
||||||
int index;
|
int index;
|
||||||
struct boundary_tag* tag;
|
struct boundary_tag* tag;
|
||||||
|
spin_lock_ctx_t ctxliba;
|
||||||
|
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
liballoc_lock ();
|
liballoc_lock (&ctxliba);
|
||||||
|
|
||||||
tag = (struct boundary_tag*)((uintptr_t)ptr - sizeof (struct boundary_tag));
|
tag = (struct boundary_tag*)((uintptr_t)ptr - sizeof (struct boundary_tag));
|
||||||
|
|
||||||
if (tag->magic != LIBALLOC_MAGIC) {
|
if (tag->magic != LIBALLOC_MAGIC) {
|
||||||
liballoc_unlock (); // release the lock
|
liballoc_unlock (&ctxliba); // release the lock
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,7 +356,7 @@ void free (void* ptr) {
|
|||||||
|
|
||||||
liballoc_free (tag, pages);
|
liballoc_free (tag, pages);
|
||||||
|
|
||||||
liballoc_unlock ();
|
liballoc_unlock (&ctxliba);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +367,7 @@ void free (void* ptr) {
|
|||||||
|
|
||||||
insert_tag (tag, index);
|
insert_tag (tag, index);
|
||||||
|
|
||||||
liballoc_unlock ();
|
liballoc_unlock (&ctxliba);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* calloc (size_t nobj, size_t size) {
|
void* calloc (size_t nobj, size_t size) {
|
||||||
@@ -385,6 +387,7 @@ void* realloc (void* p, size_t size) {
|
|||||||
void* ptr;
|
void* ptr;
|
||||||
struct boundary_tag* tag;
|
struct boundary_tag* tag;
|
||||||
int real_size;
|
int real_size;
|
||||||
|
spin_lock_ctx_t ctxliba;
|
||||||
|
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
free (p);
|
free (p);
|
||||||
@@ -394,11 +397,11 @@ void* realloc (void* p, size_t size) {
|
|||||||
return malloc (size);
|
return malloc (size);
|
||||||
|
|
||||||
if (&liballoc_lock != NULL)
|
if (&liballoc_lock != NULL)
|
||||||
liballoc_lock (); // lockit
|
liballoc_lock (&ctxliba); // lockit
|
||||||
tag = (struct boundary_tag*)((uintptr_t)p - sizeof (struct boundary_tag));
|
tag = (struct boundary_tag*)((uintptr_t)p - sizeof (struct boundary_tag));
|
||||||
real_size = tag->size;
|
real_size = tag->size;
|
||||||
if (&liballoc_unlock != NULL)
|
if (&liballoc_unlock != NULL)
|
||||||
liballoc_unlock ();
|
liballoc_unlock (&ctxliba);
|
||||||
|
|
||||||
if ((size_t)real_size > size)
|
if ((size_t)real_size > size)
|
||||||
real_size = size;
|
real_size = size;
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ struct boundary_tag {
|
|||||||
* \return 0 if the lock was acquired successfully. Anything else is
|
* \return 0 if the lock was acquired successfully. Anything else is
|
||||||
* failure.
|
* failure.
|
||||||
*/
|
*/
|
||||||
extern int liballoc_lock ();
|
extern int liballoc_lock (void* ctx);
|
||||||
|
|
||||||
/** This function unlocks what was previously locked by the liballoc_lock
|
/** This function unlocks what was previously locked by the liballoc_lock
|
||||||
* function. If it disabled interrupts, it enables interrupts. If it
|
* function. If it disabled interrupts, it enables interrupts. If it
|
||||||
@@ -55,7 +55,7 @@ extern int liballoc_lock ();
|
|||||||
*
|
*
|
||||||
* \return 0 if the lock was successfully released.
|
* \return 0 if the lock was successfully released.
|
||||||
*/
|
*/
|
||||||
extern int liballoc_unlock ();
|
extern int liballoc_unlock (void* ctx);
|
||||||
|
|
||||||
/** This is the hook into the local system which allocates pages. It
|
/** This is the hook into the local system which allocates pages. It
|
||||||
* accepts an integer parameter which is the number of pages
|
* accepts an integer parameter which is the number of pages
|
||||||
|
|||||||
@@ -100,6 +100,8 @@ static size_t pmm_find_free_space (struct pmm_region* pmm_region, size_t nblks)
|
|||||||
}
|
}
|
||||||
|
|
||||||
physaddr_t pmm_alloc (size_t nblks) {
|
physaddr_t pmm_alloc (size_t nblks) {
|
||||||
|
spin_lock_ctx_t ctxpmmr;
|
||||||
|
|
||||||
for (size_t region = 0; region < PMM_REGIONS_MAX; region++) {
|
for (size_t region = 0; region < PMM_REGIONS_MAX; region++) {
|
||||||
struct pmm_region* pmm_region = &pmm.regions[region];
|
struct pmm_region* pmm_region = &pmm.regions[region];
|
||||||
|
|
||||||
@@ -107,7 +109,7 @@ physaddr_t pmm_alloc (size_t nblks) {
|
|||||||
if (!(pmm_region->flags & PMM_REGION_ACTIVE))
|
if (!(pmm_region->flags & PMM_REGION_ACTIVE))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
spin_lock (&pmm_region->lock);
|
spin_lock (&pmm_region->lock, &ctxpmmr);
|
||||||
|
|
||||||
/* Find starting bit of the free bit range */
|
/* Find starting bit of the free bit range */
|
||||||
size_t bit = pmm_find_free_space (pmm_region, nblks);
|
size_t bit = pmm_find_free_space (pmm_region, nblks);
|
||||||
@@ -116,18 +118,19 @@ physaddr_t pmm_alloc (size_t nblks) {
|
|||||||
if (bit != (size_t)-1) {
|
if (bit != (size_t)-1) {
|
||||||
/* Mark it */
|
/* Mark it */
|
||||||
bm_set_region (&pmm_region->bm, bit, nblks);
|
bm_set_region (&pmm_region->bm, bit, nblks);
|
||||||
spin_unlock (&pmm_region->lock);
|
spin_unlock (&pmm_region->lock, &ctxpmmr);
|
||||||
|
|
||||||
return pmm_region->membase + bit * PAGE_SIZE;
|
return pmm_region->membase + bit * PAGE_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
spin_unlock (&pmm_region->lock);
|
spin_unlock (&pmm_region->lock, &ctxpmmr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return PMM_ALLOC_ERR;
|
return PMM_ALLOC_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pmm_free (physaddr_t p_addr, size_t nblks) {
|
void pmm_free (physaddr_t p_addr, size_t nblks) {
|
||||||
|
spin_lock_ctx_t ctxpmmr;
|
||||||
/* Round down to nearest page boundary */
|
/* Round down to nearest page boundary */
|
||||||
physaddr_t aligned_p_addr = align_down (p_addr, PAGE_SIZE);
|
physaddr_t aligned_p_addr = align_down (p_addr, PAGE_SIZE);
|
||||||
|
|
||||||
@@ -145,11 +148,11 @@ void pmm_free (physaddr_t p_addr, size_t nblks) {
|
|||||||
|
|
||||||
size_t bit = div_align_up (addr, PAGE_SIZE);
|
size_t bit = div_align_up (addr, PAGE_SIZE);
|
||||||
|
|
||||||
spin_lock (&pmm_region->lock);
|
spin_lock (&pmm_region->lock, &ctxpmmr);
|
||||||
|
|
||||||
bm_clear_region (&pmm_region->bm, bit, nblks);
|
bm_clear_region (&pmm_region->bm, bit, nblks);
|
||||||
|
|
||||||
spin_unlock (&pmm_region->lock);
|
spin_unlock (&pmm_region->lock, &ctxpmmr);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
10
kernel/proc/locks.txt
Normal file
10
kernel/proc/locks.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
Lock hierarchy for process scheduling:
|
||||||
|
|
||||||
|
1. proc_tree_lock
|
||||||
|
2. cpu->lock
|
||||||
|
3. procgroup->lock
|
||||||
|
4. proc->lock
|
||||||
|
5. sq->lock
|
||||||
|
|
||||||
|
1. procgroup_tree_lock
|
||||||
|
2. procgroup->lock
|
||||||
130
kernel/proc/mutex.c
Normal file
130
kernel/proc/mutex.c
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
#include <libk/assert.h>
|
||||||
|
#include <libk/rbtree.h>
|
||||||
|
#include <libk/std.h>
|
||||||
|
#include <libk/string.h>
|
||||||
|
#include <mm/liballoc.h>
|
||||||
|
#include <proc/mutex.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
|
#include <proc/suspension_q.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
#include <sys/debug.h>
|
||||||
|
#include <sys/smp.h>
|
||||||
|
#include <sys/spin_lock.h>
|
||||||
|
|
||||||
|
void proc_mutexes_cleanup (struct proc* proc) {
|
||||||
|
spin_lock_ctx_t ctxpg, ctxrs;
|
||||||
|
|
||||||
|
spin_lock (&proc->procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
struct rb_node_link* rnode;
|
||||||
|
rbtree_first (&proc->procgroup->resource_tree, rnode);
|
||||||
|
|
||||||
|
while (rnode) {
|
||||||
|
struct rb_node_link* next;
|
||||||
|
rbtree_next (rnode, next);
|
||||||
|
|
||||||
|
struct proc_resource* resource = rbtree_entry (rnode, struct proc_resource, resource_tree_link);
|
||||||
|
|
||||||
|
rnode = next;
|
||||||
|
|
||||||
|
spin_lock (&resource->lock, &ctxrs);
|
||||||
|
|
||||||
|
if (resource->type != PR_MUTEX) {
|
||||||
|
spin_unlock (&resource->lock, &ctxrs);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resource->u.mutex.owner == proc && resource->u.mutex.locked) {
|
||||||
|
spin_unlock (&resource->lock, &ctxrs);
|
||||||
|
|
||||||
|
proc_mutex_unlock (proc, &resource->u.mutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&proc->procgroup->lock, &ctxpg);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool proc_cleanup_resource_mutex (struct proc_resource* resource) {
|
||||||
|
struct proc_mutex* mutex = &resource->u.mutex;
|
||||||
|
spin_lock_ctx_t ctxmt, ctxsq;
|
||||||
|
|
||||||
|
spin_lock (&mutex->resource->lock, &ctxmt);
|
||||||
|
spin_lock (&mutex->suspension_q.lock, &ctxsq);
|
||||||
|
|
||||||
|
bool reschedule = PROC_NO_RESCHEDULE;
|
||||||
|
|
||||||
|
while (mutex->suspension_q.proc_list != NULL) {
|
||||||
|
struct list_node_link* node = mutex->suspension_q.proc_list;
|
||||||
|
struct proc_sq_entry* sq_entry = list_entry (node, struct proc_sq_entry, sq_link);
|
||||||
|
struct proc* suspended_proc = sq_entry->proc;
|
||||||
|
|
||||||
|
/* we will relock during resume */
|
||||||
|
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
|
||||||
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
||||||
|
|
||||||
|
reschedule = reschedule || proc_sq_resume (suspended_proc, sq_entry);
|
||||||
|
|
||||||
|
/* reacquire */
|
||||||
|
spin_lock (&mutex->resource->lock, &ctxmt);
|
||||||
|
spin_lock (&mutex->suspension_q.lock, &ctxsq);
|
||||||
|
}
|
||||||
|
|
||||||
|
mutex->locked = false;
|
||||||
|
mutex->owner = NULL;
|
||||||
|
|
||||||
|
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
|
||||||
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
||||||
|
|
||||||
|
return reschedule;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool proc_mutex_lock (struct proc* proc, struct proc_mutex* mutex) {
|
||||||
|
spin_lock_ctx_t ctxmt;
|
||||||
|
|
||||||
|
spin_lock (&mutex->resource->lock, &ctxmt);
|
||||||
|
|
||||||
|
if (!mutex->locked || mutex->owner == proc) {
|
||||||
|
mutex->locked = true;
|
||||||
|
mutex->owner = proc;
|
||||||
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
||||||
|
return PROC_NO_RESCHEDULE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return proc_sq_suspend (proc, &mutex->suspension_q, &mutex->resource->lock, &ctxmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool proc_mutex_unlock (struct proc* proc, struct proc_mutex* mutex) {
|
||||||
|
spin_lock_ctx_t ctxmt, ctxsq;
|
||||||
|
|
||||||
|
spin_lock (&mutex->resource->lock, &ctxmt);
|
||||||
|
|
||||||
|
if (mutex->owner != proc) {
|
||||||
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
||||||
|
return PROC_NO_RESCHEDULE;
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_lock (&mutex->suspension_q.lock, &ctxsq);
|
||||||
|
|
||||||
|
struct list_node_link* node = mutex->suspension_q.proc_list;
|
||||||
|
|
||||||
|
if (node) {
|
||||||
|
struct proc_sq_entry* sq_entry = list_entry (node, struct proc_sq_entry, sq_link);
|
||||||
|
struct proc* resumed_proc = sq_entry->proc;
|
||||||
|
|
||||||
|
mutex->owner = resumed_proc;
|
||||||
|
mutex->locked = true;
|
||||||
|
|
||||||
|
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
|
||||||
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
||||||
|
|
||||||
|
return proc_sq_resume (resumed_proc, sq_entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
mutex->locked = false;
|
||||||
|
mutex->owner = NULL;
|
||||||
|
|
||||||
|
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
|
||||||
|
spin_unlock (&mutex->resource->lock, &ctxmt);
|
||||||
|
|
||||||
|
return PROC_NEED_RESCHEDULE;
|
||||||
|
}
|
||||||
23
kernel/proc/mutex.h
Normal file
23
kernel/proc/mutex.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef _KERNEL_PROC_MUTEX_H
|
||||||
|
#define _KERNEL_PROC_MUTEX_H
|
||||||
|
|
||||||
|
#include <libk/std.h>
|
||||||
|
#include <proc/suspension_q.h>
|
||||||
|
|
||||||
|
struct proc;
|
||||||
|
struct proc_resource;
|
||||||
|
|
||||||
|
struct proc_mutex {
|
||||||
|
struct proc_resource* resource;
|
||||||
|
|
||||||
|
bool locked;
|
||||||
|
struct proc_suspension_q suspension_q;
|
||||||
|
struct proc* owner;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool proc_cleanup_resource_mutex (struct proc_resource* resource);
|
||||||
|
bool proc_mutex_lock (struct proc* proc, struct proc_mutex* mutex);
|
||||||
|
bool proc_mutex_unlock (struct proc* proc, struct proc_mutex* mutex);
|
||||||
|
void proc_mutexes_cleanup (struct proc* proc);
|
||||||
|
|
||||||
|
#endif // _KERNEL_PROC_MUTEX_H
|
||||||
@@ -10,6 +10,8 @@
|
|||||||
#include <mm/liballoc.h>
|
#include <mm/liballoc.h>
|
||||||
#include <mm/pmm.h>
|
#include <mm/pmm.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
#include <proc/procgroup.h>
|
||||||
|
#include <proc/resource.h>
|
||||||
#include <rd/rd.h>
|
#include <rd/rd.h>
|
||||||
#include <sync/spin_lock.h>
|
#include <sync/spin_lock.h>
|
||||||
#include <sys/debug.h>
|
#include <sys/debug.h>
|
||||||
@@ -23,42 +25,19 @@
|
|||||||
#include <amd64/intr_defs.h>
|
#include <amd64/intr_defs.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
#define SCHED_REAP_FREQ 10
|
||||||
* Lock ordering:
|
|
||||||
* 1. proc_tree_lock
|
|
||||||
* 2. [cpu]->lock
|
|
||||||
*/
|
|
||||||
|
|
||||||
static struct rb_node_link* proc_tree = NULL;
|
static struct rb_node_link* proc_tree = NULL;
|
||||||
static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;
|
static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT;
|
||||||
|
|
||||||
|
static atomic_int sched_cycles = 0;
|
||||||
|
|
||||||
static bool proc_check_elf (uint8_t* elf) {
|
static bool proc_check_elf (uint8_t* elf) {
|
||||||
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
|
if (!((elf[0] == 0x7F) && (elf[1] == 'E') && (elf[2] == 'L') && (elf[3] == 'F')))
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
|
|
||||||
uint32_t flags) {
|
|
||||||
struct proc_mapping* mapping = malloc (sizeof (*mapping));
|
|
||||||
mapping->paddr = start_paddr;
|
|
||||||
mapping->vaddr = start_vaddr;
|
|
||||||
mapping->size = pages * PAGE_SIZE;
|
|
||||||
|
|
||||||
flags &= ~MM_PD_LOCK; /* clear LOCK flag if present, because we lock manualy */
|
|
||||||
|
|
||||||
spin_lock (&proc->pd.lock);
|
|
||||||
|
|
||||||
list_append (proc->mappings, &mapping->proc_mappings_link);
|
|
||||||
|
|
||||||
for (uintptr_t vpage = start_vaddr, ppage = start_paddr; vpage < start_vaddr + pages * PAGE_SIZE;
|
|
||||||
vpage += PAGE_SIZE, ppage += PAGE_SIZE) {
|
|
||||||
mm_map_page (&proc->pd, ppage, vpage, flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
spin_unlock (&proc->pd.lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
|
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
|
||||||
struct elf_aux aux;
|
struct elf_aux aux;
|
||||||
|
|
||||||
@@ -83,19 +62,37 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
|
|||||||
|
|
||||||
size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);
|
size_t blks = div_align_up (phdr->p_memsz + off, PAGE_SIZE);
|
||||||
|
|
||||||
uintptr_t p_addr = pmm_alloc (blks);
|
|
||||||
if (p_addr == PMM_ALLOC_ERR)
|
|
||||||
DEBUG ("pmm oom error while loading ELF segments! (tried to alloc %zu blks)\n", blks);
|
|
||||||
|
|
||||||
memset ((void*)((uintptr_t)hhdm->offset + p_addr), 0, blks * PAGE_SIZE);
|
|
||||||
memcpy ((void*)((uintptr_t)hhdm->offset + p_addr + off),
|
|
||||||
(void*)((uintptr_t)elf + phdr->p_offset), phdr->p_filesz);
|
|
||||||
|
|
||||||
uint32_t pg_flags = MM_PG_USER | MM_PG_PRESENT;
|
uint32_t pg_flags = MM_PG_USER | MM_PG_PRESENT;
|
||||||
if (phdr->p_flags & PF_W)
|
if (phdr->p_flags & PF_W)
|
||||||
pg_flags |= MM_PG_RW;
|
pg_flags |= MM_PG_RW;
|
||||||
|
|
||||||
proc_map (proc, p_addr, v_addr, blks, pg_flags);
|
uintptr_t p_addr;
|
||||||
|
procgroup_map (proc->procgroup, v_addr, blks, pg_flags, &p_addr);
|
||||||
|
|
||||||
|
memset ((void*)((uintptr_t)hhdm->offset + p_addr), 0, blks * PAGE_SIZE);
|
||||||
|
memcpy ((void*)((uintptr_t)hhdm->offset + p_addr + off),
|
||||||
|
(void*)((uintptr_t)elf + phdr->p_offset), phdr->p_filesz);
|
||||||
|
} break;
|
||||||
|
case PT_TLS: {
|
||||||
|
#if defined(__x86_64__)
|
||||||
|
if (phdr->p_memsz > 0) {
|
||||||
|
size_t tls_align = phdr->p_align ? phdr->p_align : sizeof (uintptr_t);
|
||||||
|
size_t tls_size = align_up (phdr->p_memsz, tls_align);
|
||||||
|
size_t tls_total_needed = tls_size + sizeof (uintptr_t);
|
||||||
|
size_t blks = div_align_up (tls_total_needed, PAGE_SIZE);
|
||||||
|
proc->procgroup->tls.tls_tmpl_pages = blks;
|
||||||
|
proc->procgroup->tls.tls_tmpl_size = tls_size;
|
||||||
|
proc->procgroup->tls.tls_tmpl_total_size = tls_total_needed;
|
||||||
|
|
||||||
|
proc->procgroup->tls.tls_tmpl = malloc (blks * PAGE_SIZE);
|
||||||
|
memset (proc->procgroup->tls.tls_tmpl, 0, blks * PAGE_SIZE);
|
||||||
|
|
||||||
|
memcpy (proc->procgroup->tls.tls_tmpl, (void*)((uintptr_t)elf + phdr->p_offset),
|
||||||
|
phdr->p_filesz);
|
||||||
|
|
||||||
|
proc_init_tls (proc);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,11 +100,10 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
|
|||||||
return aux;
|
return aux;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct proc* proc_spawn_rd (char* name) {
|
struct proc* proc_spawn_rd (char* name) {
|
||||||
struct rd_file* rd_file = rd_get_file (name);
|
struct rd_file* rd_file = rd_get_file (name);
|
||||||
|
|
||||||
bool ok = proc_check_elf (rd_file->content);
|
bool ok = proc_check_elf (rd_file->content);
|
||||||
DEBUG ("ELF magic %s\n", (ok ? "OK" : "BAD"));
|
|
||||||
|
|
||||||
if (!ok)
|
if (!ok)
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -115,117 +111,173 @@ static struct proc* proc_spawn_rd (char* name) {
|
|||||||
return proc_from_elf (rd_file->content);
|
return proc_from_elf (rd_file->content);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void proc_register (struct proc* proc, struct cpu* cpu) {
|
struct proc* proc_find_pid (int pid) {
|
||||||
proc->cpu = cpu;
|
spin_lock_ctx_t ctxprtr;
|
||||||
|
struct proc* proc = NULL;
|
||||||
|
|
||||||
spin_lock (&proc_tree_lock);
|
spin_lock (&proc_tree_lock, &ctxprtr);
|
||||||
spin_lock (&cpu->lock);
|
rbtree_find (struct proc, &proc_tree, pid, proc, proc_tree_link, pid);
|
||||||
|
spin_unlock (&proc_tree_lock, &ctxprtr);
|
||||||
|
|
||||||
|
return proc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void proc_register (struct proc* proc, struct cpu* cpu1) {
|
||||||
|
spin_lock_ctx_t ctxcpu, ctxprtr;
|
||||||
|
|
||||||
|
proc->cpu = cpu1 != NULL ? cpu1 : cpu_find_lightest ();
|
||||||
|
|
||||||
|
struct cpu* cpu = proc->cpu;
|
||||||
|
|
||||||
|
spin_lock (&proc_tree_lock, &ctxprtr);
|
||||||
|
spin_lock (&cpu->lock, &ctxcpu);
|
||||||
|
|
||||||
rbtree_insert (struct proc, &cpu->proc_run_q, &proc->cpu_run_q_link, cpu_run_q_link, pid);
|
|
||||||
rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);
|
rbtree_insert (struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid);
|
||||||
|
|
||||||
|
atomic_fetch_add (&cpu->proc_run_q_count, 1);
|
||||||
|
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
|
||||||
if (cpu->proc_current == NULL)
|
if (cpu->proc_current == NULL)
|
||||||
cpu->proc_current = proc;
|
cpu->proc_current = proc;
|
||||||
|
|
||||||
spin_unlock (&cpu->lock);
|
spin_unlock (&proc_tree_lock, &ctxprtr);
|
||||||
spin_unlock (&proc_tree_lock);
|
spin_unlock (&cpu->lock, &ctxcpu);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct proc* proc_find_sched (void) {
|
/* caller holds cpu->lock */
|
||||||
struct rb_node_link* node = NULL;
|
static struct proc* proc_find_sched (struct cpu* cpu) {
|
||||||
struct proc* start = thiscpu->proc_current;
|
if (!cpu->proc_run_q)
|
||||||
struct proc* proc = NULL;
|
return NULL;
|
||||||
|
|
||||||
if (start)
|
struct list_node_link *current, *start;
|
||||||
node = &start->cpu_run_q_link;
|
|
||||||
|
|
||||||
if (!node)
|
if (cpu->proc_current)
|
||||||
rbtree_first (&thiscpu->proc_run_q, node);
|
current = cpu->proc_current->cpu_run_q_link.next;
|
||||||
|
else
|
||||||
|
current = cpu->proc_run_q;
|
||||||
|
|
||||||
struct rb_node_link* first = node;
|
if (!current)
|
||||||
while (node) {
|
current = cpu->proc_run_q;
|
||||||
proc = rbtree_entry (node, struct proc, cpu_run_q_link);
|
|
||||||
|
start = current;
|
||||||
|
|
||||||
|
do {
|
||||||
|
struct proc* proc = list_entry (current, struct proc, cpu_run_q_link);
|
||||||
|
|
||||||
if (atomic_load (&proc->state) == PROC_READY)
|
if (atomic_load (&proc->state) == PROC_READY)
|
||||||
return proc;
|
return proc;
|
||||||
|
|
||||||
rbtree_next (node, node);
|
current = current->next ? current->next : cpu->proc_run_q;
|
||||||
|
} while (current != start);
|
||||||
if (!node) {
|
|
||||||
rbtree_first (&thiscpu->proc_run_q, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node == first)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_sched (void) {
|
static void proc_reap (void) {
|
||||||
struct proc* next = NULL;
|
struct proc* proc = NULL;
|
||||||
|
struct list_node_link* reap_list = NULL;
|
||||||
|
spin_lock_ctx_t ctxprtr;
|
||||||
|
spin_lock_ctx_t ctxpr;
|
||||||
|
|
||||||
spin_lock (&thiscpu->lock);
|
spin_lock (&proc_tree_lock, &ctxprtr);
|
||||||
|
|
||||||
if (thiscpu->proc_run_q == NULL) {
|
struct rb_node_link* node;
|
||||||
spin_unlock (&thiscpu->lock);
|
rbtree_first (&proc_tree, node);
|
||||||
goto idle;
|
|
||||||
|
while (node) {
|
||||||
|
struct rb_node_link* next;
|
||||||
|
rbtree_next (node, next);
|
||||||
|
proc = rbtree_entry (node, struct proc, proc_tree_link);
|
||||||
|
|
||||||
|
if (atomic_load (&proc->state) == PROC_DEAD) {
|
||||||
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
|
rbtree_delete (&proc_tree, &proc->proc_tree_link);
|
||||||
|
list_append (reap_list, &proc->reap_link);
|
||||||
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
}
|
}
|
||||||
|
|
||||||
next = proc_find_sched ();
|
node = next;
|
||||||
|
}
|
||||||
|
|
||||||
if (next != NULL)
|
spin_unlock (&proc_tree_lock, &ctxprtr);
|
||||||
thiscpu->proc_current = next;
|
|
||||||
|
|
||||||
spin_unlock (&thiscpu->lock);
|
struct list_node_link *reap_link, *reap_link_tmp;
|
||||||
|
list_foreach (reap_list, reap_link, reap_link_tmp) {
|
||||||
|
proc = list_entry (reap_link, struct proc, reap_link);
|
||||||
|
|
||||||
if (next != NULL && atomic_load (&next->state) == PROC_READY)
|
list_remove (reap_list, &proc->reap_link);
|
||||||
do_sched (next);
|
DEBUG ("cleanup PID %d\n", proc->pid);
|
||||||
|
proc_cleanup (proc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void proc_sched (void) {
|
||||||
|
spin_lock_ctx_t ctxcpu;
|
||||||
|
|
||||||
|
int s_cycles = atomic_fetch_add (&sched_cycles, 1);
|
||||||
|
|
||||||
|
if (s_cycles % SCHED_REAP_FREQ == 0)
|
||||||
|
proc_reap ();
|
||||||
|
|
||||||
|
struct proc* next = NULL;
|
||||||
|
struct cpu* cpu = thiscpu;
|
||||||
|
|
||||||
|
spin_lock (&cpu->lock, &ctxcpu);
|
||||||
|
|
||||||
|
next = proc_find_sched (cpu);
|
||||||
|
|
||||||
|
if (next) {
|
||||||
|
cpu->proc_current = next;
|
||||||
|
|
||||||
|
do_sched (next, &cpu->lock, &ctxcpu);
|
||||||
|
} else {
|
||||||
|
cpu->proc_current = NULL;
|
||||||
|
spin_unlock (&cpu->lock, &ctxcpu);
|
||||||
|
|
||||||
idle:
|
|
||||||
spin ();
|
spin ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_kill (struct proc* proc) {
|
void proc_kill (struct proc* proc) {
|
||||||
atomic_store (&proc->state, PROC_DEAD);
|
spin_lock_ctx_t ctxpr, ctxcpu;
|
||||||
|
|
||||||
spin_lock (&proc_tree_lock);
|
|
||||||
rbtree_delete (&proc_tree, &proc->proc_tree_link);
|
|
||||||
spin_unlock (&proc_tree_lock);
|
|
||||||
|
|
||||||
struct cpu* cpu = proc->cpu;
|
struct cpu* cpu = proc->cpu;
|
||||||
spin_lock (&cpu->lock);
|
|
||||||
rbtree_delete (&cpu->proc_run_q, &proc->cpu_run_q_link);
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
spin_unlock (&cpu->lock);
|
atomic_store (&proc->state, PROC_DEAD);
|
||||||
|
proc->cpu = NULL;
|
||||||
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
|
|
||||||
|
spin_lock (&cpu->lock, &ctxcpu);
|
||||||
|
|
||||||
|
list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
|
||||||
|
atomic_fetch_sub (&cpu->proc_run_q_count, 1);
|
||||||
|
if (cpu->proc_current == proc)
|
||||||
|
cpu->proc_current = NULL;
|
||||||
|
|
||||||
|
spin_unlock (&cpu->lock, &ctxcpu);
|
||||||
|
|
||||||
DEBUG ("killed PID %d\n", proc->pid);
|
DEBUG ("killed PID %d\n", proc->pid);
|
||||||
|
|
||||||
proc_cleanup (proc);
|
|
||||||
|
|
||||||
if (cpu == thiscpu)
|
|
||||||
proc_sched ();
|
|
||||||
else
|
|
||||||
cpu_request_sched (cpu);
|
cpu_request_sched (cpu);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void proc_irq_sched (void* arg, void* regs) {
|
static void proc_irq_sched (void* arg, void* regs) {
|
||||||
(void)arg, (void)regs;
|
(void)arg;
|
||||||
proc_sched ();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void proc_irq_cpu_request_sched (void* arg, void* regs) {
|
|
||||||
(void)arg, (void)regs;
|
|
||||||
proc_sched ();
|
proc_sched ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void proc_init (void) {
|
void proc_init (void) {
|
||||||
struct proc* init = proc_spawn_rd ("init.exe");
|
|
||||||
proc_register (init, thiscpu);
|
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
irq_attach (&proc_irq_sched, NULL, SCHED_PREEMPT_TIMER, IRQ_INTERRUPT_SAFE);
|
irq_attach (&proc_irq_sched, NULL, SCHED_PREEMPT_TIMER);
|
||||||
irq_attach (&proc_irq_cpu_request_sched, NULL, CPU_REQUEST_SCHED, IRQ_INTERRUPT_SAFE);
|
irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
do_sched (init);
|
struct proc* spin_proc = proc_spawn_rd ("spin.exe");
|
||||||
|
proc_register (spin_proc, thiscpu);
|
||||||
|
|
||||||
|
struct proc* init = proc_spawn_rd ("init.exe");
|
||||||
|
proc_register (init, NULL);
|
||||||
|
|
||||||
|
spin_lock_ctx_t ctxcpu;
|
||||||
|
spin_lock (&spin_proc->cpu->lock, &ctxcpu);
|
||||||
|
do_sched (spin_proc, &spin_proc->cpu->lock, &ctxcpu);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
#include <libk/list.h>
|
#include <libk/list.h>
|
||||||
#include <libk/rbtree.h>
|
#include <libk/rbtree.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
#include <proc/procgroup.h>
|
||||||
|
#include <proc/resource.h>
|
||||||
|
#include <proc/suspension_q.h>
|
||||||
#include <sync/spin_lock.h>
|
#include <sync/spin_lock.h>
|
||||||
#include <sys/mm.h>
|
#include <sys/mm.h>
|
||||||
|
|
||||||
@@ -14,39 +17,41 @@
|
|||||||
#include <amd64/proc.h> /* USTACK_SIZE */
|
#include <amd64/proc.h> /* USTACK_SIZE */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// Process is ready to run
|
#define PROC_NEED_RESCHEDULE true
|
||||||
|
#define PROC_NO_RESCHEDULE false
|
||||||
|
|
||||||
|
/* process states */
|
||||||
#define PROC_READY 0
|
#define PROC_READY 0
|
||||||
/// Process marked garbage collection
|
|
||||||
#define PROC_DEAD 1
|
#define PROC_DEAD 1
|
||||||
|
#define PROC_SUSPENDED 2
|
||||||
|
|
||||||
|
/* process flags */
|
||||||
|
#define PROC_USTK_PREALLOC (1 << 0)
|
||||||
|
|
||||||
struct cpu;
|
struct cpu;
|
||||||
|
|
||||||
struct proc_mapping {
|
|
||||||
struct list_node_link proc_mappings_link;
|
|
||||||
|
|
||||||
uintptr_t paddr;
|
|
||||||
uintptr_t vaddr;
|
|
||||||
size_t size;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct proc {
|
struct proc {
|
||||||
int pid;
|
int pid;
|
||||||
struct rb_node_link proc_tree_link;
|
struct rb_node_link proc_tree_link;
|
||||||
struct rb_node_link cpu_run_q_link;
|
struct rb_node_link procgroup_memb_tree_link;
|
||||||
|
struct list_node_link cpu_run_q_link;
|
||||||
struct list_node_link* mappings; /* pd.lock implicitly protects this field */
|
struct list_node_link reap_link;
|
||||||
|
struct list_node_link* sq_entries;
|
||||||
|
struct procgroup* procgroup;
|
||||||
struct proc_platformdata pdata;
|
struct proc_platformdata pdata;
|
||||||
struct pd pd;
|
uint32_t flags;
|
||||||
spin_lock_t lock;
|
spin_lock_t lock;
|
||||||
struct cpu* cpu;
|
struct cpu* cpu;
|
||||||
atomic_int state;
|
atomic_int state;
|
||||||
|
uintptr_t uvaddr_argument;
|
||||||
};
|
};
|
||||||
|
|
||||||
void proc_sched (void);
|
void proc_sched (void);
|
||||||
void proc_kill (struct proc* proc);
|
void proc_kill (struct proc* proc);
|
||||||
void proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr, size_t pages,
|
|
||||||
uint32_t flags);
|
|
||||||
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
|
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
|
||||||
|
void proc_register (struct proc* proc, struct cpu* cpu);
|
||||||
|
struct proc* proc_find_pid (int pid);
|
||||||
|
struct proc* proc_spawn_rd (char* name);
|
||||||
void proc_init (void);
|
void proc_init (void);
|
||||||
|
|
||||||
#endif // _KERNEL_PROC_PROC_H
|
#endif // _KERNEL_PROC_PROC_H
|
||||||
|
|||||||
218
kernel/proc/procgroup.c
Normal file
218
kernel/proc/procgroup.c
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
#include <libk/rbtree.h>
|
||||||
|
#include <libk/std.h>
|
||||||
|
#include <mm/liballoc.h>
|
||||||
|
#include <mm/pmm.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
|
#include <proc/procgroup.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
#include <sys/debug.h>
|
||||||
|
#include <sys/mm.h>
|
||||||
|
|
||||||
|
static struct rb_node_link* procgroup_tree = NULL;
|
||||||
|
static spin_lock_t procgroup_tree_lock = SPIN_LOCK_INIT;
|
||||||
|
static atomic_int pgids = 0;
|
||||||
|
|
||||||
|
uintptr_t procgroup_map (struct procgroup* procgroup, uintptr_t vaddr, size_t pages, uint32_t flags,
|
||||||
|
uintptr_t* out_paddr) {
|
||||||
|
spin_lock_ctx_t ctxpg;
|
||||||
|
|
||||||
|
spin_lock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
vaddr = (vaddr == 0) ? procgroup->map_base : vaddr;
|
||||||
|
|
||||||
|
struct proc_mapping* mapping = malloc (sizeof (*mapping));
|
||||||
|
|
||||||
|
if (mapping == NULL) {
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uintptr_t paddr = pmm_alloc (pages);
|
||||||
|
|
||||||
|
if (paddr == PMM_ALLOC_ERR) {
|
||||||
|
free (mapping);
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (out_paddr != NULL)
|
||||||
|
*out_paddr = paddr;
|
||||||
|
|
||||||
|
mapping->paddr = paddr;
|
||||||
|
mapping->vaddr = vaddr;
|
||||||
|
mapping->size = pages * PAGE_SIZE;
|
||||||
|
|
||||||
|
procgroup->map_base += pages * PAGE_SIZE;
|
||||||
|
|
||||||
|
list_append (procgroup->mappings, &mapping->proc_mappings_link);
|
||||||
|
|
||||||
|
for (uintptr_t vpage = vaddr, ppage = paddr; vpage < vaddr + pages * PAGE_SIZE;
|
||||||
|
vpage += PAGE_SIZE, ppage += PAGE_SIZE) {
|
||||||
|
mm_map_page (&procgroup->pd, ppage, vpage, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
return vaddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool procgroup_unmap (struct procgroup* procgroup, uintptr_t start_vaddr, size_t pages) {
|
||||||
|
size_t unmap_size = pages * PAGE_SIZE;
|
||||||
|
uintptr_t end_vaddr = start_vaddr + unmap_size;
|
||||||
|
|
||||||
|
struct list_node_link *mapping_link, *mapping_link_tmp;
|
||||||
|
|
||||||
|
bool used_tail_mapping = false;
|
||||||
|
spin_lock_ctx_t ctxpg;
|
||||||
|
|
||||||
|
struct proc_mapping* tail_mapping = malloc (sizeof (*tail_mapping));
|
||||||
|
if (tail_mapping == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
spin_lock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
|
||||||
|
struct proc_mapping* mapping =
|
||||||
|
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
|
||||||
|
|
||||||
|
uintptr_t m_start = mapping->vaddr;
|
||||||
|
uintptr_t m_end = mapping->vaddr + mapping->size;
|
||||||
|
|
||||||
|
/* check overlap */
|
||||||
|
if ((start_vaddr < m_end) && (end_vaddr > mapping->vaddr)) {
|
||||||
|
uintptr_t free_vstart = (start_vaddr > m_start) ? start_vaddr : m_start;
|
||||||
|
uintptr_t free_vend = (end_vaddr < m_end) ? end_vaddr : m_end;
|
||||||
|
size_t free_size = free_vend - free_vstart;
|
||||||
|
|
||||||
|
uintptr_t ppage_to_free = mapping->paddr + (free_vstart - m_start);
|
||||||
|
pmm_free (ppage_to_free, free_size / PAGE_SIZE);
|
||||||
|
|
||||||
|
/* split in the middle */
|
||||||
|
if ((start_vaddr > m_start) && (end_vaddr < m_end)) {
|
||||||
|
tail_mapping->vaddr = end_vaddr;
|
||||||
|
tail_mapping->paddr = mapping->paddr + (end_vaddr - m_start);
|
||||||
|
tail_mapping->size = m_end - end_vaddr;
|
||||||
|
|
||||||
|
mapping->size = start_vaddr - m_start;
|
||||||
|
|
||||||
|
list_insert_after (procgroup->mappings, &mapping->proc_mappings_link,
|
||||||
|
&tail_mapping->proc_mappings_link);
|
||||||
|
|
||||||
|
used_tail_mapping = true;
|
||||||
|
|
||||||
|
break;
|
||||||
|
} else if ((start_vaddr <= m_start) && (end_vaddr < m_end)) { /* shrink left */
|
||||||
|
size_t diff = end_vaddr - m_start;
|
||||||
|
mapping->vaddr += diff;
|
||||||
|
mapping->paddr += diff;
|
||||||
|
mapping->size -= diff;
|
||||||
|
} else if ((start_vaddr > m_start) && (end_vaddr >= m_end)) { /* shrink right */
|
||||||
|
mapping->size = start_vaddr - m_start;
|
||||||
|
} else { /* full overlap */
|
||||||
|
list_remove (procgroup->mappings, &mapping->proc_mappings_link);
|
||||||
|
free (mapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!used_tail_mapping)
|
||||||
|
free (tail_mapping);
|
||||||
|
|
||||||
|
for (uintptr_t vpage = start_vaddr; vpage < end_vaddr; vpage += PAGE_SIZE) {
|
||||||
|
mm_unmap_page (&procgroup->pd, vpage);
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct procgroup* procgroup_create (void) {
|
||||||
|
spin_lock_ctx_t ctxpgtr;
|
||||||
|
|
||||||
|
struct procgroup* procgroup = malloc (sizeof (*procgroup));
|
||||||
|
if (procgroup == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
procgroup->refs = 0;
|
||||||
|
procgroup->memb_proc_tree = NULL;
|
||||||
|
procgroup->lock = SPIN_LOCK_INIT;
|
||||||
|
procgroup->pgid = atomic_fetch_add (&pgids, 1);
|
||||||
|
procgroup->pd.cr3_paddr = mm_alloc_user_pd_phys ();
|
||||||
|
procgroup->map_base = PROC_MAP_BASE;
|
||||||
|
|
||||||
|
spin_lock (&procgroup_tree_lock, &ctxpgtr);
|
||||||
|
rbtree_insert (struct procgroup, &procgroup_tree, &procgroup->procgroup_tree_link,
|
||||||
|
procgroup_tree_link, pgid);
|
||||||
|
spin_unlock (&procgroup_tree_lock, &ctxpgtr);
|
||||||
|
|
||||||
|
return procgroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
void procgroup_attach (struct procgroup* procgroup, struct proc* proc) {
|
||||||
|
spin_lock_ctx_t ctxpg, ctxpr;
|
||||||
|
|
||||||
|
spin_lock (&procgroup->lock, &ctxpg);
|
||||||
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
|
|
||||||
|
rbtree_insert (struct proc, &procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link,
|
||||||
|
procgroup_memb_tree_link, pid);
|
||||||
|
atomic_fetch_add (&procgroup->refs, 1);
|
||||||
|
|
||||||
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void procgroup_detach (struct procgroup* procgroup, struct proc* proc) {
|
||||||
|
spin_lock_ctx_t ctxpg, ctxpr, ctxpgtr;
|
||||||
|
|
||||||
|
spin_lock (&procgroup->lock, &ctxpg);
|
||||||
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
|
|
||||||
|
rbtree_delete (&procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link);
|
||||||
|
int refs = atomic_fetch_sub (&procgroup->refs, 1);
|
||||||
|
|
||||||
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
if (refs == 1) {
|
||||||
|
spin_lock (&procgroup_tree_lock, &ctxpgtr);
|
||||||
|
spin_lock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
rbtree_delete (&procgroup_tree, &procgroup->procgroup_tree_link);
|
||||||
|
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
spin_unlock (&procgroup_tree_lock, &ctxpgtr);
|
||||||
|
|
||||||
|
/* delete resources */
|
||||||
|
struct rb_node_link* rnode;
|
||||||
|
rbtree_first (&procgroup->resource_tree, rnode);
|
||||||
|
while (rnode) {
|
||||||
|
struct rb_node_link* next;
|
||||||
|
rbtree_next (rnode, next);
|
||||||
|
|
||||||
|
struct proc_resource* resource =
|
||||||
|
rbtree_entry (rnode, struct proc_resource, resource_tree_link);
|
||||||
|
|
||||||
|
rnode = next;
|
||||||
|
|
||||||
|
proc_delete_resource (resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct list_node_link *mapping_link, *mapping_link_tmp;
|
||||||
|
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
|
||||||
|
struct proc_mapping* mapping =
|
||||||
|
list_entry (mapping_link, struct proc_mapping, proc_mappings_link);
|
||||||
|
|
||||||
|
pmm_free (mapping->paddr, mapping->size / PAGE_SIZE);
|
||||||
|
free (mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
pmm_free (procgroup->pd.cr3_paddr, 1);
|
||||||
|
|
||||||
|
free (procgroup->tls.tls_tmpl);
|
||||||
|
|
||||||
|
free (procgroup);
|
||||||
|
}
|
||||||
|
}
|
||||||
43
kernel/proc/procgroup.h
Normal file
43
kernel/proc/procgroup.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#ifndef _KERNEL_PROC_PROCGROUP_H
|
||||||
|
#define _KERNEL_PROC_PROCGROUP_H
|
||||||
|
|
||||||
|
#include <libk/list.h>
|
||||||
|
#include <libk/rbtree.h>
|
||||||
|
#include <libk/std.h>
|
||||||
|
#include <proc/resource.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
#include <sys/mm.h>
|
||||||
|
#include <sys/procgroup.h>
|
||||||
|
|
||||||
|
struct proc;
|
||||||
|
|
||||||
|
struct proc_mapping {
|
||||||
|
struct list_node_link proc_mappings_link;
|
||||||
|
|
||||||
|
uintptr_t paddr;
|
||||||
|
uintptr_t vaddr;
|
||||||
|
size_t size;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct procgroup {
|
||||||
|
int pgid;
|
||||||
|
struct rb_node_link procgroup_tree_link;
|
||||||
|
struct rb_node_link* memb_proc_tree;
|
||||||
|
spin_lock_t lock;
|
||||||
|
atomic_int refs;
|
||||||
|
struct rb_node_link* resource_tree;
|
||||||
|
atomic_int sys_rids;
|
||||||
|
struct pd pd;
|
||||||
|
struct list_node_link* mappings;
|
||||||
|
uintptr_t map_base;
|
||||||
|
struct procgroup_tls tls;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct procgroup* procgroup_create (void);
|
||||||
|
void procgroup_attach (struct procgroup* procgroup, struct proc* proc);
|
||||||
|
void procgroup_detach (struct procgroup* procgroup, struct proc* proc);
|
||||||
|
uintptr_t procgroup_map (struct procgroup* procgroup, uintptr_t vaddr, size_t pages, uint32_t flags,
|
||||||
|
uintptr_t* out_paddr);
|
||||||
|
bool procgroup_unmap (struct procgroup* procgroup, uintptr_t start_vaddr, size_t pages);
|
||||||
|
|
||||||
|
#endif // _KERNEL_PROC_PROCGROUP_H
|
||||||
59
kernel/proc/resource.c
Normal file
59
kernel/proc/resource.c
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#include <libk/assert.h>
|
||||||
|
#include <libk/list.h>
|
||||||
|
#include <libk/rbtree.h>
|
||||||
|
#include <libk/std.h>
|
||||||
|
#include <libk/string.h>
|
||||||
|
#include <mm/liballoc.h>
|
||||||
|
#include <mm/pmm.h>
|
||||||
|
#include <proc/mutex.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
|
#include <proc/procgroup.h>
|
||||||
|
#include <proc/resource.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
#include <sys/debug.h>
|
||||||
|
|
||||||
|
struct proc_resource* proc_find_resource (struct procgroup* procgroup, int rid) {
|
||||||
|
spin_lock_ctx_t ctxpg;
|
||||||
|
struct proc_resource* resource = NULL;
|
||||||
|
|
||||||
|
spin_lock (&procgroup->lock, &ctxpg);
|
||||||
|
rbtree_find (struct proc_resource, &procgroup->resource_tree, rid, resource, resource_tree_link,
|
||||||
|
rid);
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup, int rid) {
|
||||||
|
spin_lock_ctx_t ctxpg;
|
||||||
|
struct proc_resource* resource;
|
||||||
|
|
||||||
|
resource = proc_find_resource (procgroup, rid);
|
||||||
|
if (resource != NULL)
|
||||||
|
return resource;
|
||||||
|
|
||||||
|
resource = malloc (sizeof (*resource));
|
||||||
|
if (resource == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
memset (resource, 0, sizeof (*resource));
|
||||||
|
resource->lock = SPIN_LOCK_INIT;
|
||||||
|
resource->ops.cleanup = &proc_cleanup_resource_mutex;
|
||||||
|
resource->u.mutex.resource = resource;
|
||||||
|
resource->rid = rid;
|
||||||
|
resource->type = PR_MUTEX;
|
||||||
|
|
||||||
|
spin_lock (&procgroup->lock, &ctxpg);
|
||||||
|
rbtree_insert (struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
|
||||||
|
resource_tree_link, rid);
|
||||||
|
spin_unlock (&procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool proc_delete_resource (struct proc_resource* resource) {
|
||||||
|
bool reschedule = resource->ops.cleanup (resource);
|
||||||
|
free (resource);
|
||||||
|
|
||||||
|
return reschedule;
|
||||||
|
}
|
||||||
32
kernel/proc/resource.h
Normal file
32
kernel/proc/resource.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef _KERNEL_PROC_RESOURCE_H
|
||||||
|
#define _KERNEL_PROC_RESOURCE_H
|
||||||
|
|
||||||
|
#include <libk/list.h>
|
||||||
|
#include <libk/rbtree.h>
|
||||||
|
#include <libk/std.h>
|
||||||
|
#include <proc/mutex.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
|
||||||
|
#define PR_MUTEX 1
|
||||||
|
|
||||||
|
struct proc;
|
||||||
|
struct procgroup;
|
||||||
|
|
||||||
|
struct proc_resource {
|
||||||
|
int type;
|
||||||
|
int rid;
|
||||||
|
spin_lock_t lock;
|
||||||
|
struct rb_node_link resource_tree_link;
|
||||||
|
union {
|
||||||
|
struct proc_mutex mutex;
|
||||||
|
} u;
|
||||||
|
struct {
|
||||||
|
bool (*cleanup) (struct proc_resource* resource);
|
||||||
|
} ops;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct proc_resource* proc_find_resource (struct procgroup* procgroup, int rid);
|
||||||
|
struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup, int rid);
|
||||||
|
bool proc_delete_resource (struct proc_resource* resource);
|
||||||
|
|
||||||
|
#endif // _KERNEL_PROC_RESOURCE_H
|
||||||
@@ -1,3 +1,11 @@
|
|||||||
c += proc/proc.c
|
c += proc/proc.c \
|
||||||
|
proc/resource.c \
|
||||||
|
proc/mutex.c \
|
||||||
|
proc/procgroup.c \
|
||||||
|
proc/suspension_q.c
|
||||||
|
|
||||||
o += proc/proc.o
|
o += proc/proc.o \
|
||||||
|
proc/resource.o \
|
||||||
|
proc/mutex.o \
|
||||||
|
proc/procgroup.o \
|
||||||
|
proc/suspension_q.o
|
||||||
|
|||||||
111
kernel/proc/suspension_q.c
Normal file
111
kernel/proc/suspension_q.c
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
#include <libk/list.h>
|
||||||
|
#include <libk/std.h>
|
||||||
|
#include <mm/liballoc.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
|
#include <proc/resource.h>
|
||||||
|
#include <proc/suspension_q.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
#include <sys/smp.h>
|
||||||
|
#include <sys/spin_lock.h>
|
||||||
|
|
||||||
|
bool proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock,
|
||||||
|
spin_lock_ctx_t* ctxrl) {
|
||||||
|
spin_lock_ctx_t ctxpr, ctxcpu, ctxsq;
|
||||||
|
struct cpu* cpu = proc->cpu;
|
||||||
|
|
||||||
|
struct proc_sq_entry* sq_entry = malloc (sizeof (*sq_entry));
|
||||||
|
if (!sq_entry) {
|
||||||
|
spin_unlock (resource_lock, ctxrl);
|
||||||
|
return PROC_NO_RESCHEDULE;
|
||||||
|
}
|
||||||
|
|
||||||
|
sq_entry->proc = proc;
|
||||||
|
sq_entry->sq = sq;
|
||||||
|
|
||||||
|
spin_lock (&cpu->lock, &ctxcpu);
|
||||||
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
|
spin_lock (&sq->lock, &ctxsq);
|
||||||
|
|
||||||
|
spin_unlock (resource_lock, ctxrl);
|
||||||
|
|
||||||
|
atomic_store (&proc->state, PROC_SUSPENDED);
|
||||||
|
|
||||||
|
/* append to sq's list */
|
||||||
|
list_append (sq->proc_list, &sq_entry->sq_link);
|
||||||
|
|
||||||
|
/* append to proc's list */
|
||||||
|
list_append (proc->sq_entries, &sq_entry->proc_link);
|
||||||
|
|
||||||
|
list_remove (cpu->proc_run_q, &proc->cpu_run_q_link);
|
||||||
|
atomic_fetch_sub (&cpu->proc_run_q_count, 1);
|
||||||
|
|
||||||
|
if (cpu->proc_current == proc)
|
||||||
|
cpu->proc_current = NULL;
|
||||||
|
|
||||||
|
proc->cpu = NULL;
|
||||||
|
|
||||||
|
spin_unlock (&sq->lock, &ctxsq);
|
||||||
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
|
spin_unlock (&cpu->lock, &ctxcpu);
|
||||||
|
|
||||||
|
return PROC_NEED_RESCHEDULE;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry) {
|
||||||
|
spin_lock_ctx_t ctxsq, ctxpr, ctxcpu;
|
||||||
|
struct cpu* cpu = cpu_find_lightest ();
|
||||||
|
struct proc_suspension_q* sq = sq_entry->sq;
|
||||||
|
|
||||||
|
spin_lock (&cpu->lock, &ctxcpu);
|
||||||
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
|
spin_lock (&sq->lock, &ctxsq);
|
||||||
|
|
||||||
|
/* remove from sq's list */
|
||||||
|
list_remove (sq->proc_list, &sq_entry->sq_link);
|
||||||
|
|
||||||
|
/* remove from proc's list */
|
||||||
|
list_remove (proc->sq_entries, &sq_entry->proc_link);
|
||||||
|
|
||||||
|
proc->cpu = cpu;
|
||||||
|
|
||||||
|
if (proc->sq_entries == NULL)
|
||||||
|
atomic_store (&proc->state, PROC_READY);
|
||||||
|
|
||||||
|
list_append (cpu->proc_run_q, &proc->cpu_run_q_link);
|
||||||
|
atomic_fetch_add (&cpu->proc_run_q_count, 1);
|
||||||
|
|
||||||
|
spin_unlock (&sq->lock, &ctxsq);
|
||||||
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
|
spin_unlock (&cpu->lock, &ctxcpu);
|
||||||
|
|
||||||
|
free (sq_entry);
|
||||||
|
|
||||||
|
return PROC_NEED_RESCHEDULE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void proc_sqs_cleanup (struct proc* proc) {
|
||||||
|
spin_lock_ctx_t ctxsq, ctxpr;
|
||||||
|
|
||||||
|
spin_lock (&proc->lock, &ctxpr);
|
||||||
|
|
||||||
|
/* clean suspension queue entries */
|
||||||
|
struct list_node_link *sq_link, *sq_link_tmp;
|
||||||
|
list_foreach (proc->sq_entries, sq_link, sq_link_tmp) {
|
||||||
|
struct proc_sq_entry* sq_entry = list_entry (sq_link, struct proc_sq_entry, proc_link);
|
||||||
|
struct proc_suspension_q* sq = sq_entry->sq;
|
||||||
|
|
||||||
|
spin_lock (&sq->lock, &ctxsq);
|
||||||
|
|
||||||
|
/* remove from sq's list */
|
||||||
|
list_remove (sq->proc_list, &sq_entry->sq_link);
|
||||||
|
|
||||||
|
/* remove from proc's list */
|
||||||
|
list_remove (proc->sq_entries, &sq_entry->proc_link);
|
||||||
|
|
||||||
|
spin_unlock (&sq->lock, &ctxsq);
|
||||||
|
|
||||||
|
free (sq_entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock (&proc->lock, &ctxpr);
|
||||||
|
}
|
||||||
26
kernel/proc/suspension_q.h
Normal file
26
kernel/proc/suspension_q.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef _KERNEL_PROC_SUSPENTION_Q_H
|
||||||
|
#define _KERNEL_PROC_SUSPENTION_Q_H
|
||||||
|
|
||||||
|
#include <libk/list.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
|
||||||
|
struct proc;
|
||||||
|
|
||||||
|
struct proc_suspension_q {
|
||||||
|
struct list_node_link* proc_list;
|
||||||
|
spin_lock_t lock;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct proc_sq_entry {
|
||||||
|
struct list_node_link sq_link;
|
||||||
|
struct list_node_link proc_link;
|
||||||
|
struct proc* proc;
|
||||||
|
struct proc_suspension_q* sq;
|
||||||
|
};
|
||||||
|
|
||||||
|
void proc_sqs_cleanup (struct proc* proc);
|
||||||
|
bool proc_sq_suspend (struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock,
|
||||||
|
spin_lock_ctx_t* ctxrl);
|
||||||
|
bool proc_sq_resume (struct proc* proc, struct proc_sq_entry* sq_entry);
|
||||||
|
|
||||||
|
#endif // _KERNEL_PROC_SUSPENTION_Q_H
|
||||||
@@ -3,15 +3,15 @@
|
|||||||
#include <sys/irq.h>
|
#include <sys/irq.h>
|
||||||
#include <sys/spin_lock.h>
|
#include <sys/spin_lock.h>
|
||||||
|
|
||||||
void spin_lock (spin_lock_t* sl) {
|
void spin_lock (spin_lock_t* sl, spin_lock_ctx_t* ctx) {
|
||||||
irq_save ();
|
irq_save (ctx);
|
||||||
|
|
||||||
while (atomic_flag_test_and_set_explicit (sl, memory_order_acquire))
|
while (atomic_flag_test_and_set_explicit (sl, memory_order_acquire))
|
||||||
spin_lock_relax ();
|
spin_lock_relax ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void spin_unlock (spin_lock_t* sl) {
|
void spin_unlock (spin_lock_t* sl, spin_lock_ctx_t* ctx) {
|
||||||
atomic_flag_clear_explicit (sl, memory_order_release);
|
atomic_flag_clear_explicit (sl, memory_order_release);
|
||||||
|
|
||||||
irq_restore ();
|
irq_restore (ctx);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,13 @@
|
|||||||
#define _KERNEL_SYNC_SPIN_LOCK_H
|
#define _KERNEL_SYNC_SPIN_LOCK_H
|
||||||
|
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
#include <sys/spin_lock.h>
|
||||||
|
|
||||||
#define SPIN_LOCK_INIT ATOMIC_FLAG_INIT
|
#define SPIN_LOCK_INIT ATOMIC_FLAG_INIT
|
||||||
|
|
||||||
typedef atomic_flag spin_lock_t;
|
typedef atomic_flag spin_lock_t;
|
||||||
|
|
||||||
void spin_lock (spin_lock_t* sl);
|
void spin_lock (spin_lock_t* sl, spin_lock_ctx_t* ctx);
|
||||||
void spin_unlock (spin_lock_t* sl);
|
void spin_unlock (spin_lock_t* sl, spin_lock_ctx_t* ctx);
|
||||||
|
|
||||||
#endif // _KERNEL_SYNC_SPIN_LOCK_H
|
#endif // _KERNEL_SYNC_SPIN_LOCK_H
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#ifndef _KERNEL_SYS_IRQ_H
|
#ifndef _KERNEL_SYS_IRQ_H
|
||||||
#define _KERNEL_SYS_IRQ_H
|
#define _KERNEL_SYS_IRQ_H
|
||||||
|
|
||||||
void irq_save (void);
|
#include <sys/spin_lock.h>
|
||||||
void irq_restore (void);
|
|
||||||
|
void irq_save (spin_lock_ctx_t* ctx);
|
||||||
|
void irq_restore (spin_lock_ctx_t* ctx);
|
||||||
|
|
||||||
#endif // _KERNEL_SYS_IRQ_H
|
#endif // _KERNEL_SYS_IRQ_H
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define _KERNEL_SYS_MM_H
|
#define _KERNEL_SYS_MM_H
|
||||||
|
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
#include <amd64/mm.h>
|
#include <amd64/mm.h>
|
||||||
@@ -10,17 +11,19 @@
|
|||||||
#define MM_PG_PRESENT (1 << 0)
|
#define MM_PG_PRESENT (1 << 0)
|
||||||
#define MM_PG_RW (1 << 1)
|
#define MM_PG_RW (1 << 1)
|
||||||
#define MM_PG_USER (1 << 2)
|
#define MM_PG_USER (1 << 2)
|
||||||
#define MM_PD_RELOAD (1 << 30)
|
|
||||||
#define MM_PD_LOCK (1 << 31)
|
|
||||||
|
|
||||||
uintptr_t mm_alloc_user_pd_phys (void);
|
uintptr_t mm_alloc_user_pd_phys (void);
|
||||||
void mm_reload (void);
|
void mm_kernel_lock (spin_lock_ctx_t* ctx);
|
||||||
|
void mm_kernel_unlock (spin_lock_ctx_t* ctx);
|
||||||
void mm_map_page (struct pd* pd, uintptr_t paddr, uintptr_t vaddr, uint32_t flags);
|
void mm_map_page (struct pd* pd, uintptr_t paddr, uintptr_t vaddr, uint32_t flags);
|
||||||
void mm_map_kernel_page (uintptr_t paddr, uintptr_t vaddr, uint32_t flags);
|
void mm_map_kernel_page (uintptr_t paddr, uintptr_t vaddr, uint32_t flags);
|
||||||
void mm_unmap_page (struct pd* pd, uintptr_t vaddr, uint32_t flags);
|
void mm_unmap_page (struct pd* pd, uintptr_t vaddr);
|
||||||
void mm_unmap_kernel_page (uintptr_t vaddr, uint32_t flags);
|
void mm_unmap_kernel_page (uintptr_t vaddr);
|
||||||
void mm_lock_kernel (void);
|
bool mm_validate (struct pd* pd, uintptr_t vaddr);
|
||||||
void mm_unlock_kernel (void);
|
bool mm_validate_buffer (struct pd* pd, uintptr_t vaddr, size_t size);
|
||||||
|
uintptr_t mm_p2v (struct pd* pd, uintptr_t paddr);
|
||||||
|
uintptr_t mm_v2p (struct pd* pd, uintptr_t vaddr);
|
||||||
|
struct pd* mm_get_kernel_pd (void);
|
||||||
void mm_init (void);
|
void mm_init (void);
|
||||||
|
|
||||||
#endif // _KERNEL_SYS_MM_H
|
#endif // _KERNEL_SYS_MM_H
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
struct proc;
|
struct proc;
|
||||||
|
|
||||||
struct proc* proc_from_elf (uint8_t* elf_contents);
|
struct proc* proc_from_elf (uint8_t* elf_contents);
|
||||||
|
struct proc* proc_clone (struct proc* proto, uintptr_t vstack_top, uintptr_t entry,
|
||||||
|
uintptr_t argument_ptr);
|
||||||
void proc_cleanup (struct proc* proc);
|
void proc_cleanup (struct proc* proc);
|
||||||
|
void proc_init_tls (struct proc* proc);
|
||||||
|
|
||||||
#endif // _KERNEL_SYS_PROC_H
|
#endif // _KERNEL_SYS_PROC_H
|
||||||
|
|||||||
8
kernel/sys/procgroup.h
Normal file
8
kernel/sys/procgroup.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef _KERNEL_SYS_PROCGROUP_H
|
||||||
|
#define _KERNEL_SYS_PROCGROUP_H
|
||||||
|
|
||||||
|
#if defined(__x86_64__)
|
||||||
|
#include <amd64/procgroup.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // _KERNEL_SYS_PROCGROUP_H
|
||||||
@@ -4,6 +4,6 @@
|
|||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
|
||||||
void do_sched (struct proc* proc);
|
void do_sched (struct proc* proc, spin_lock_t* cpu_lock, spin_lock_ctx_t* ctxcpu);
|
||||||
|
|
||||||
#endif // _KERNEL_SYS_SCHED_H
|
#endif // _KERNEL_SYS_SCHED_H
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
#ifndef _KERNEL_SYS_SPIN_LOCK_H
|
#ifndef _KERNEL_SYS_SPIN_LOCK_H
|
||||||
#define _KERNEL_SYS_SPIN_LOCK_H
|
#define _KERNEL_SYS_SPIN_LOCK_H
|
||||||
|
|
||||||
|
#include <libk/std.h>
|
||||||
|
|
||||||
|
#if defined(__x86_64__)
|
||||||
|
typedef uint64_t spin_lock_ctx_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
void spin_lock_relax (void);
|
void spin_lock_relax (void);
|
||||||
|
|
||||||
#endif // _KERNEL_SYS_SPIN_LOCK_H
|
#endif // _KERNEL_SYS_SPIN_LOCK_H
|
||||||
|
|||||||
@@ -1,28 +1,180 @@
|
|||||||
#include <aux/compiler.h>
|
#include <aux/compiler.h>
|
||||||
|
#include <libk/assert.h>
|
||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
|
#include <limine/requests.h>
|
||||||
|
#include <m/status.h>
|
||||||
#include <m/syscall_defs.h>
|
#include <m/syscall_defs.h>
|
||||||
|
#include <mm/pmm.h>
|
||||||
|
#include <proc/mutex.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
#include <proc/procgroup.h>
|
||||||
|
#include <proc/resource.h>
|
||||||
|
#include <sync/spin_lock.h>
|
||||||
#include <sys/debug.h>
|
#include <sys/debug.h>
|
||||||
|
#include <sys/mm.h>
|
||||||
|
#include <sys/proc.h>
|
||||||
#include <syscall/syscall.h>
|
#include <syscall/syscall.h>
|
||||||
|
|
||||||
#define DEFINE_SYSCALL(name) \
|
#define DEFINE_SYSCALL(name) \
|
||||||
int name (struct proc* proc, uintptr_t UNUSED a1, uintptr_t UNUSED a2, uintptr_t UNUSED a3, \
|
uintptr_t name (struct proc* UNUSED proc, void* UNUSED regs, uintptr_t UNUSED a1, \
|
||||||
uintptr_t UNUSED a4, uintptr_t UNUSED a5, uintptr_t UNUSED a6)
|
uintptr_t UNUSED a2, uintptr_t UNUSED a3, uintptr_t UNUSED a4, \
|
||||||
|
uintptr_t UNUSED a5, uintptr_t UNUSED a6)
|
||||||
|
|
||||||
DEFINE_SYSCALL (sys_proc_quit) {
|
#define SYSRESULT(x) ((uintptr_t)(x))
|
||||||
proc_kill (proc);
|
|
||||||
proc_sched ();
|
static void* sys_get_user_buffer (struct proc* proc, uintptr_t uvaddr, size_t size) {
|
||||||
return SR_OK;
|
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
|
||||||
|
spin_lock_ctx_t ctxpg;
|
||||||
|
|
||||||
|
spin_lock (&proc->procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
if (!mm_validate_buffer (&proc->procgroup->pd, (uintptr_t)uvaddr, size)) {
|
||||||
|
spin_unlock (&proc->procgroup->lock, &ctxpg);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
uintptr_t out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr);
|
||||||
|
|
||||||
|
spin_unlock (&proc->procgroup->lock, &ctxpg);
|
||||||
|
|
||||||
|
uintptr_t out_kvaddr = (uintptr_t)hhdm->offset + out_paddr;
|
||||||
|
|
||||||
|
return (void*)out_kvaddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFINE_SYSCALL (sys_proc_test) {
|
/* int quit (void) */
|
||||||
DEBUG ("test syscall message!\n");
|
DEFINE_SYSCALL (sys_quit) {
|
||||||
return SR_OK;
|
proc_kill (proc);
|
||||||
|
return SYSRESULT (ST_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int test (void) */
|
||||||
|
DEFINE_SYSCALL (sys_test) {
|
||||||
|
char c = (char)a1;
|
||||||
|
DEBUG ("test syscall from %d! %c\n", proc->pid, c);
|
||||||
|
return SYSRESULT (ST_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int map (uintptr_t vaddr, size_t pages, uint32_t flags) */
|
||||||
|
DEFINE_SYSCALL (sys_map) {
|
||||||
|
uintptr_t vaddr = a1;
|
||||||
|
size_t pages = (size_t)a2;
|
||||||
|
uint32_t flags = (uint32_t)a3;
|
||||||
|
|
||||||
|
if (vaddr % PAGE_SIZE != 0)
|
||||||
|
return SYSRESULT (-ST_UNALIGNED);
|
||||||
|
|
||||||
|
return SYSRESULT (procgroup_map (proc->procgroup, vaddr, pages, flags, NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int unmap (uintptr_t vaddr, size_t pages) */
|
||||||
|
DEFINE_SYSCALL (sys_unmap) {
|
||||||
|
uintptr_t vaddr = a1;
|
||||||
|
size_t pages = (size_t)a2;
|
||||||
|
|
||||||
|
if (vaddr % PAGE_SIZE != 0)
|
||||||
|
return SYSRESULT (-ST_UNALIGNED);
|
||||||
|
|
||||||
|
return SYSRESULT (procgroup_unmap (proc->procgroup, vaddr, pages));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int clone (uintptr_t vstack_top, void* entry, void* argument_ptr) */
|
||||||
|
DEFINE_SYSCALL (sys_clone) {
|
||||||
|
uintptr_t vstack_top = a1;
|
||||||
|
uintptr_t entry = a2;
|
||||||
|
uintptr_t argument_ptr = a3;
|
||||||
|
|
||||||
|
struct proc* new = proc_clone (proc, vstack_top, entry, argument_ptr);
|
||||||
|
|
||||||
|
if (new == NULL) {
|
||||||
|
return SYSRESULT (-ST_OOM_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pid = new->pid;
|
||||||
|
|
||||||
|
proc_register (new, NULL);
|
||||||
|
|
||||||
|
return SYSRESULT (pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* void* argument_ptr (void) */
|
||||||
|
DEFINE_SYSCALL (sys_argument_ptr) { return proc->uvaddr_argument; }
|
||||||
|
|
||||||
|
/* int sched (void) */
|
||||||
|
DEFINE_SYSCALL (sys_sched) {
|
||||||
|
proc_sched ();
|
||||||
|
return SYSRESULT (ST_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int mutex_create (int mutex_rid) */
|
||||||
|
DEFINE_SYSCALL (sys_mutex_create) {
|
||||||
|
int mutex_rid = (int)a1;
|
||||||
|
|
||||||
|
struct proc_resource* mutex_resource = proc_create_resource_mutex (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
|
if (mutex_resource == NULL)
|
||||||
|
return SYSRESULT (-ST_OOM_ERROR);
|
||||||
|
|
||||||
|
return SYSRESULT (mutex_resource->rid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int mutex_delete (int mutex_rid) */
|
||||||
|
DEFINE_SYSCALL (sys_mutex_delete) {
|
||||||
|
int mutex_rid = (int)a1;
|
||||||
|
|
||||||
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
|
if (mutex_resource == NULL)
|
||||||
|
return SYSRESULT (-ST_NOT_FOUND);
|
||||||
|
|
||||||
|
if (proc_delete_resource (mutex_resource) == PROC_NEED_RESCHEDULE)
|
||||||
|
proc_sched ();
|
||||||
|
|
||||||
|
return SYSRESULT (ST_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int mutex_lock (int mutex_rid) */
|
||||||
|
DEFINE_SYSCALL (sys_mutex_lock) {
|
||||||
|
int mutex_rid = (int)a1;
|
||||||
|
|
||||||
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
|
if (mutex_resource == NULL)
|
||||||
|
return SYSRESULT (-ST_NOT_FOUND);
|
||||||
|
|
||||||
|
if (proc_mutex_lock (proc, &mutex_resource->u.mutex) == PROC_NEED_RESCHEDULE)
|
||||||
|
proc_sched ();
|
||||||
|
|
||||||
|
return SYSRESULT (ST_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* int mutex_unlock (int mutex_rid) */
|
||||||
|
DEFINE_SYSCALL (sys_mutex_unlock) {
|
||||||
|
int mutex_rid = (int)a1;
|
||||||
|
|
||||||
|
struct proc_resource* mutex_resource = proc_find_resource (proc->procgroup, mutex_rid);
|
||||||
|
|
||||||
|
if (mutex_resource == NULL)
|
||||||
|
return SYSRESULT (-ST_NOT_FOUND);
|
||||||
|
|
||||||
|
if (proc_mutex_unlock (proc, &mutex_resource->u.mutex) == PROC_NEED_RESCHEDULE)
|
||||||
|
proc_sched ();
|
||||||
|
|
||||||
|
return SYSRESULT (ST_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
static syscall_handler_func_t handler_table[] = {
|
static syscall_handler_func_t handler_table[] = {
|
||||||
[SYS_PROC_QUIT] = &sys_proc_quit,
|
[SYS_QUIT] = &sys_quit,
|
||||||
[SYS_PROC_TEST] = &sys_proc_test,
|
[SYS_TEST] = &sys_test,
|
||||||
|
[SYS_MAP] = &sys_map,
|
||||||
|
[SYS_UNMAP] = &sys_unmap,
|
||||||
|
[SYS_CLONE] = &sys_clone,
|
||||||
|
[SYS_ARGUMENT_PTR] = &sys_argument_ptr,
|
||||||
|
[SYS_SCHED] = &sys_sched,
|
||||||
|
[SYS_MUTEX_CREATE] = &sys_mutex_create,
|
||||||
|
[SYS_MUTEX_DELETE] = &sys_mutex_delete,
|
||||||
|
[SYS_MUTEX_LOCK] = &sys_mutex_lock,
|
||||||
|
[SYS_MUTEX_UNLOCK] = &sys_mutex_unlock,
|
||||||
};
|
};
|
||||||
|
|
||||||
syscall_handler_func_t syscall_find_handler (int syscall_num) {
|
syscall_handler_func_t syscall_find_handler (int syscall_num) {
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
#include <libk/std.h>
|
#include <libk/std.h>
|
||||||
#include <proc/proc.h>
|
#include <proc/proc.h>
|
||||||
|
|
||||||
typedef int (*syscall_handler_func_t) (struct proc* proc, uintptr_t a1, uintptr_t a2, uintptr_t a3,
|
typedef uintptr_t (*syscall_handler_func_t) (struct proc* proc, void* regs, uintptr_t a1,
|
||||||
uintptr_t a4, uintptr_t a5, uintptr_t a6);
|
uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5,
|
||||||
|
uintptr_t a6);
|
||||||
|
|
||||||
syscall_handler_func_t syscall_find_handler (int syscall_num);
|
syscall_handler_func_t syscall_find_handler (int syscall_num);
|
||||||
|
|
||||||
|
|||||||
1
libmsl/alloc/.gitignore
vendored
Normal file
1
libmsl/alloc/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.o
|
||||||
386
libmsl/alloc/liballoc.c
Normal file
386
libmsl/alloc/liballoc.c
Normal file
@@ -0,0 +1,386 @@
|
|||||||
|
/* liballoc breaks when optimized too aggressively, for eg. clang's -Oz */
|
||||||
|
#pragma clang optimize off
|
||||||
|
|
||||||
|
#include <alloc/liballoc.h>
|
||||||
|
#include <m/system.h>
|
||||||
|
|
||||||
|
#define LIBALLOC_MUTEX 500
|
||||||
|
|
||||||
|
void liballoc_init (void) { mutex_create (LIBALLOC_MUTEX); }
|
||||||
|
|
||||||
|
void liballoc_deinit (void) { mutex_delete (LIBALLOC_MUTEX); }
|
||||||
|
|
||||||
|
int liballoc_lock (void) { return mutex_lock (LIBALLOC_MUTEX); }
|
||||||
|
|
||||||
|
int liballoc_unlock (void) { return mutex_unlock (LIBALLOC_MUTEX); }
|
||||||
|
|
||||||
|
void* liballoc_alloc (int pages) { return map (0, pages, MAP_FLAGS | MAP_RW); }
|
||||||
|
|
||||||
|
int liballoc_free (void* ptr, int pages) { return unmap ((uintptr_t)ptr, pages); }
|
||||||
|
|
||||||
|
/** Durand's Ridiculously Amazing Super Duper Memory functions. */
|
||||||
|
|
||||||
|
// #define DEBUG
|
||||||
|
|
||||||
|
#define LIBALLOC_MAGIC 0xc001c0de
|
||||||
|
#define MAXCOMPLETE 5
|
||||||
|
#define MAXEXP 32
|
||||||
|
#define MINEXP 8
|
||||||
|
|
||||||
|
#define MODE_BEST 0
|
||||||
|
#define MODE_INSTANT 1
|
||||||
|
|
||||||
|
#define MODE MODE_BEST
|
||||||
|
|
||||||
|
struct boundary_tag* l_freePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
|
||||||
|
int l_completePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
|
||||||
|
|
||||||
|
static int l_initialized = 0; //< Flag to indicate initialization.
|
||||||
|
static int l_pageSize = PAGE_SIZE; //< Individual page size
|
||||||
|
static int l_pageCount = 16; //< Minimum number of pages to allocate.
|
||||||
|
|
||||||
|
// *********** HELPER FUNCTIONS *******************************
|
||||||
|
|
||||||
|
/** Returns the exponent required to manage 'size' amount of memory.
|
||||||
|
*
|
||||||
|
* Returns n where 2^n <= size < 2^(n+1)
|
||||||
|
*/
|
||||||
|
static inline int getexp (unsigned int size) {
|
||||||
|
if (size < (1 << MINEXP)) {
|
||||||
|
return -1; // Smaller than the quantum.
|
||||||
|
}
|
||||||
|
|
||||||
|
int shift = MINEXP;
|
||||||
|
|
||||||
|
while (shift < MAXEXP) {
|
||||||
|
if ((1 << shift) > size)
|
||||||
|
break;
|
||||||
|
shift += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return shift - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void* liballoc_memset (void* s, int c, size_t n) {
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
((char*)s)[i] = c;
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void* liballoc_memcpy (void* s1, const void* s2, size_t n) {
|
||||||
|
char* cdest;
|
||||||
|
char* csrc;
|
||||||
|
unsigned int* ldest = (unsigned int*)s1;
|
||||||
|
unsigned int* lsrc = (unsigned int*)s2;
|
||||||
|
|
||||||
|
while (n >= sizeof (unsigned int)) {
|
||||||
|
*ldest++ = *lsrc++;
|
||||||
|
n -= sizeof (unsigned int);
|
||||||
|
}
|
||||||
|
|
||||||
|
cdest = (char*)ldest;
|
||||||
|
csrc = (char*)lsrc;
|
||||||
|
|
||||||
|
while (n > 0) {
|
||||||
|
*cdest++ = *csrc++;
|
||||||
|
n -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return s1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void insert_tag (struct boundary_tag* tag, int index) {
|
||||||
|
int realIndex;
|
||||||
|
|
||||||
|
if (index < 0) {
|
||||||
|
realIndex = getexp (tag->real_size - sizeof (struct boundary_tag));
|
||||||
|
if (realIndex < MINEXP)
|
||||||
|
realIndex = MINEXP;
|
||||||
|
} else
|
||||||
|
realIndex = index;
|
||||||
|
|
||||||
|
tag->index = realIndex;
|
||||||
|
|
||||||
|
if (l_freePages[realIndex] != NULL) {
|
||||||
|
l_freePages[realIndex]->prev = tag;
|
||||||
|
tag->next = l_freePages[realIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
l_freePages[realIndex] = tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void remove_tag (struct boundary_tag* tag) {
|
||||||
|
if (l_freePages[tag->index] == tag)
|
||||||
|
l_freePages[tag->index] = tag->next;
|
||||||
|
|
||||||
|
if (tag->prev != NULL)
|
||||||
|
tag->prev->next = tag->next;
|
||||||
|
if (tag->next != NULL)
|
||||||
|
tag->next->prev = tag->prev;
|
||||||
|
|
||||||
|
tag->next = NULL;
|
||||||
|
tag->prev = NULL;
|
||||||
|
tag->index = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline struct boundary_tag* melt_left (struct boundary_tag* tag) {
|
||||||
|
struct boundary_tag* left = tag->split_left;
|
||||||
|
|
||||||
|
left->real_size += tag->real_size;
|
||||||
|
left->split_right = tag->split_right;
|
||||||
|
|
||||||
|
if (tag->split_right != NULL)
|
||||||
|
tag->split_right->split_left = left;
|
||||||
|
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline struct boundary_tag* absorb_right (struct boundary_tag* tag) {
|
||||||
|
struct boundary_tag* right = tag->split_right;
|
||||||
|
|
||||||
|
remove_tag (right); // Remove right from free pages.
|
||||||
|
|
||||||
|
tag->real_size += right->real_size;
|
||||||
|
|
||||||
|
tag->split_right = right->split_right;
|
||||||
|
if (right->split_right != NULL)
|
||||||
|
right->split_right->split_left = tag;
|
||||||
|
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline struct boundary_tag* split_tag (struct boundary_tag* tag) {
|
||||||
|
unsigned int remainder = tag->real_size - sizeof (struct boundary_tag) - tag->size;
|
||||||
|
|
||||||
|
struct boundary_tag* new_tag =
|
||||||
|
(struct boundary_tag*)((uintptr_t)tag + sizeof (struct boundary_tag) + tag->size);
|
||||||
|
|
||||||
|
new_tag->magic = LIBALLOC_MAGIC;
|
||||||
|
new_tag->real_size = remainder;
|
||||||
|
|
||||||
|
new_tag->next = NULL;
|
||||||
|
new_tag->prev = NULL;
|
||||||
|
|
||||||
|
new_tag->split_left = tag;
|
||||||
|
new_tag->split_right = tag->split_right;
|
||||||
|
|
||||||
|
if (new_tag->split_right != NULL)
|
||||||
|
new_tag->split_right->split_left = new_tag;
|
||||||
|
tag->split_right = new_tag;
|
||||||
|
|
||||||
|
tag->real_size -= new_tag->real_size;
|
||||||
|
|
||||||
|
insert_tag (new_tag, -1);
|
||||||
|
|
||||||
|
return new_tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ***************************************************************
|
||||||
|
|
||||||
|
static struct boundary_tag* allocate_new_tag (unsigned int size) {
|
||||||
|
unsigned int pages;
|
||||||
|
unsigned int usage;
|
||||||
|
struct boundary_tag* tag;
|
||||||
|
|
||||||
|
// This is how much space is required.
|
||||||
|
usage = size + sizeof (struct boundary_tag);
|
||||||
|
|
||||||
|
// Perfect amount of space
|
||||||
|
pages = usage / l_pageSize;
|
||||||
|
if ((usage % l_pageSize) != 0)
|
||||||
|
pages += 1;
|
||||||
|
|
||||||
|
// Make sure it's >= the minimum size.
|
||||||
|
if (pages < (unsigned int)l_pageCount)
|
||||||
|
pages = l_pageCount;
|
||||||
|
|
||||||
|
tag = (struct boundary_tag*)liballoc_alloc (pages);
|
||||||
|
|
||||||
|
if (tag == NULL)
|
||||||
|
return NULL; // uh oh, we ran out of memory.
|
||||||
|
|
||||||
|
tag->magic = LIBALLOC_MAGIC;
|
||||||
|
tag->size = size;
|
||||||
|
tag->real_size = pages * l_pageSize;
|
||||||
|
tag->index = -1;
|
||||||
|
|
||||||
|
tag->next = NULL;
|
||||||
|
tag->prev = NULL;
|
||||||
|
tag->split_left = NULL;
|
||||||
|
tag->split_right = NULL;
|
||||||
|
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* malloc (size_t size) {
|
||||||
|
int index;
|
||||||
|
void* ptr;
|
||||||
|
struct boundary_tag* tag = NULL;
|
||||||
|
|
||||||
|
liballoc_lock ();
|
||||||
|
|
||||||
|
if (l_initialized == 0) {
|
||||||
|
for (index = 0; index < MAXEXP; index++) {
|
||||||
|
l_freePages[index] = NULL;
|
||||||
|
l_completePages[index] = 0;
|
||||||
|
}
|
||||||
|
l_initialized = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = getexp (size) + MODE;
|
||||||
|
if (index < MINEXP)
|
||||||
|
index = MINEXP;
|
||||||
|
|
||||||
|
// Find one big enough.
|
||||||
|
tag = l_freePages[index]; // Start at the front of the list.
|
||||||
|
while (tag != NULL) {
|
||||||
|
// If there's enough space in this tag.
|
||||||
|
if ((tag->real_size - sizeof (struct boundary_tag)) >= (size + sizeof (struct boundary_tag))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
tag = tag->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No page found. Make one.
|
||||||
|
if (tag == NULL) {
|
||||||
|
if ((tag = allocate_new_tag (size)) == NULL) {
|
||||||
|
liballoc_unlock ();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = getexp (tag->real_size - sizeof (struct boundary_tag));
|
||||||
|
} else {
|
||||||
|
remove_tag (tag);
|
||||||
|
|
||||||
|
if ((tag->split_left == NULL) && (tag->split_right == NULL))
|
||||||
|
l_completePages[index] -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have a free page. Remove it from the free pages list.
|
||||||
|
|
||||||
|
tag->size = size;
|
||||||
|
|
||||||
|
// Removed... see if we can re-use the excess space.
|
||||||
|
|
||||||
|
unsigned int remainder =
|
||||||
|
tag->real_size - size - sizeof (struct boundary_tag) * 2; // Support a new tag + remainder
|
||||||
|
|
||||||
|
if (((int)(remainder) > 0) /*&& ( (tag->real_size - remainder) >= (1<<MINEXP))*/) {
|
||||||
|
int childIndex = getexp (remainder);
|
||||||
|
|
||||||
|
if (childIndex >= 0) {
|
||||||
|
struct boundary_tag* new_tag = split_tag (tag);
|
||||||
|
|
||||||
|
(void)new_tag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = (void*)((uintptr_t)tag + sizeof (struct boundary_tag));
|
||||||
|
|
||||||
|
liballoc_unlock ();
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free (void* ptr) {
|
||||||
|
int index;
|
||||||
|
struct boundary_tag* tag;
|
||||||
|
|
||||||
|
if (ptr == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
liballoc_lock ();
|
||||||
|
|
||||||
|
tag = (struct boundary_tag*)((uintptr_t)ptr - sizeof (struct boundary_tag));
|
||||||
|
|
||||||
|
if (tag->magic != LIBALLOC_MAGIC) {
|
||||||
|
liballoc_unlock (); // release the lock
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// MELT LEFT...
|
||||||
|
while ((tag->split_left != NULL) && (tag->split_left->index >= 0)) {
|
||||||
|
tag = melt_left (tag);
|
||||||
|
remove_tag (tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MELT RIGHT...
|
||||||
|
while ((tag->split_right != NULL) && (tag->split_right->index >= 0)) {
|
||||||
|
tag = absorb_right (tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where is it going back to?
|
||||||
|
index = getexp (tag->real_size - sizeof (struct boundary_tag));
|
||||||
|
if (index < MINEXP)
|
||||||
|
index = MINEXP;
|
||||||
|
|
||||||
|
// A whole, empty block?
|
||||||
|
if ((tag->split_left == NULL) && (tag->split_right == NULL)) {
|
||||||
|
if (l_completePages[index] == MAXCOMPLETE) {
|
||||||
|
// Too many standing by to keep. Free this one.
|
||||||
|
unsigned int pages = tag->real_size / l_pageSize;
|
||||||
|
|
||||||
|
if ((tag->real_size % l_pageSize) != 0)
|
||||||
|
pages += 1;
|
||||||
|
if (pages < (unsigned int)l_pageCount)
|
||||||
|
pages = l_pageCount;
|
||||||
|
|
||||||
|
liballoc_free (tag, pages);
|
||||||
|
|
||||||
|
liballoc_unlock ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
l_completePages[index] += 1; // Increase the count of complete pages.
|
||||||
|
}
|
||||||
|
|
||||||
|
// ..........
|
||||||
|
|
||||||
|
insert_tag (tag, index);
|
||||||
|
|
||||||
|
liballoc_unlock ();
|
||||||
|
}
|
||||||
|
|
||||||
|
void* calloc (size_t nobj, size_t size) {
|
||||||
|
int real_size;
|
||||||
|
void* p;
|
||||||
|
|
||||||
|
real_size = nobj * size;
|
||||||
|
|
||||||
|
p = malloc (real_size);
|
||||||
|
|
||||||
|
liballoc_memset (p, 0, real_size);
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* realloc (void* p, size_t size) {
|
||||||
|
void* ptr;
|
||||||
|
struct boundary_tag* tag;
|
||||||
|
int real_size;
|
||||||
|
|
||||||
|
if (size == 0) {
|
||||||
|
free (p);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (p == NULL)
|
||||||
|
return malloc (size);
|
||||||
|
|
||||||
|
if (&liballoc_lock != NULL)
|
||||||
|
liballoc_lock (); // lockit
|
||||||
|
tag = (struct boundary_tag*)((uintptr_t)p - sizeof (struct boundary_tag));
|
||||||
|
real_size = tag->size;
|
||||||
|
if (&liballoc_unlock != NULL)
|
||||||
|
liballoc_unlock ();
|
||||||
|
|
||||||
|
if ((size_t)real_size > size)
|
||||||
|
real_size = size;
|
||||||
|
|
||||||
|
ptr = malloc (size);
|
||||||
|
liballoc_memcpy (ptr, p, real_size);
|
||||||
|
free (p);
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
94
libmsl/alloc/liballoc.h
Normal file
94
libmsl/alloc/liballoc.h
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
#ifndef _LIBALLOC_H
|
||||||
|
#define _LIBALLOC_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define _ALLOC_SKIP_DEFINE
|
||||||
|
|
||||||
|
// If we are told to not define our own size_t, then we
|
||||||
|
// skip the define.
|
||||||
|
#ifndef _ALLOC_SKIP_DEFINE
|
||||||
|
|
||||||
|
#ifndef _HAVE_SIZE_T
|
||||||
|
#define _HAVE_SIZE_T
|
||||||
|
typedef unsigned int size_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NULL
|
||||||
|
#define NULL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** This is a boundary tag which is prepended to the
|
||||||
|
* page or section of a page which we have allocated. It is
|
||||||
|
* used to identify valid memory blocks that the
|
||||||
|
* application is trying to free.
|
||||||
|
*/
|
||||||
|
struct boundary_tag {
|
||||||
|
unsigned int magic; //< It's a kind of ...
|
||||||
|
unsigned int size; //< Requested size.
|
||||||
|
unsigned int real_size; //< Actual size.
|
||||||
|
int index; //< Location in the page table.
|
||||||
|
|
||||||
|
struct boundary_tag* split_left; //< Linked-list info for broken pages.
|
||||||
|
struct boundary_tag* split_right; //< The same.
|
||||||
|
|
||||||
|
struct boundary_tag* next; //< Linked list info.
|
||||||
|
struct boundary_tag* prev; //< Linked list info.
|
||||||
|
};
|
||||||
|
|
||||||
|
/** This function is supposed to lock the memory data structures. It
|
||||||
|
* could be as simple as disabling interrupts or acquiring a spinlock.
|
||||||
|
* It's up to you to decide.
|
||||||
|
*
|
||||||
|
* \return 0 if the lock was acquired successfully. Anything else is
|
||||||
|
* failure.
|
||||||
|
*/
|
||||||
|
extern int liballoc_lock (void);
|
||||||
|
|
||||||
|
/** This function unlocks what was previously locked by the liballoc_lock
|
||||||
|
* function. If it disabled interrupts, it enables interrupts. If it
|
||||||
|
* had acquiried a spinlock, it releases the spinlock. etc.
|
||||||
|
*
|
||||||
|
* \return 0 if the lock was successfully released.
|
||||||
|
*/
|
||||||
|
extern int liballoc_unlock (void);
|
||||||
|
|
||||||
|
/** This is the hook into the local system which allocates pages. It
|
||||||
|
* accepts an integer parameter which is the number of pages
|
||||||
|
* required. The page size was set up in the liballoc_init function.
|
||||||
|
*
|
||||||
|
* \return NULL if the pages were not allocated.
|
||||||
|
* \return A pointer to the allocated memory.
|
||||||
|
*/
|
||||||
|
extern void* liballoc_alloc (int pages);
|
||||||
|
|
||||||
|
/** This frees previously allocated memory. The void* parameter passed
|
||||||
|
* to the function is the exact same value returned from a previous
|
||||||
|
* liballoc_alloc call.
|
||||||
|
*
|
||||||
|
* The integer value is the number of pages to free.
|
||||||
|
*
|
||||||
|
* \return 0 if the memory was successfully freed.
|
||||||
|
*/
|
||||||
|
extern int liballoc_free (void* ptr, int pages);
|
||||||
|
|
||||||
|
void* malloc (size_t); //< The standard function.
|
||||||
|
void* realloc (void*, size_t); //< The standard function.
|
||||||
|
void* calloc (size_t, size_t); //< The standard function.
|
||||||
|
void free (void*); //< The standard function.
|
||||||
|
|
||||||
|
void liballoc_init (void);
|
||||||
|
void liballoc_deinit (void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
3
libmsl/alloc/src.mk
Normal file
3
libmsl/alloc/src.mk
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
c += alloc/liballoc.c
|
||||||
|
|
||||||
|
o += alloc/liballoc.o
|
||||||
@@ -2,21 +2,16 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int msl_amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
uintptr_t amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
||||||
uintptr_t a5, uintptr_t a6) {
|
uintptr_t a5, uintptr_t a6) {
|
||||||
uint64_t result;
|
uint64_t result;
|
||||||
__asm__ volatile ("movq %1, %%rax\n"
|
__asm__ volatile ("movq %[a4], %%r10\n"
|
||||||
"movq %2, %%rdi\n"
|
"movq %[a5], %%r8\n"
|
||||||
"movq %3, %%rsi\n"
|
"movq %[a6], %%r9\n"
|
||||||
"movq %4, %%rdx\n"
|
|
||||||
"movq %5, %%r10\n"
|
|
||||||
"movq %6, %%r8\n"
|
|
||||||
"movq %7, %%r9\n"
|
|
||||||
"syscall\n"
|
"syscall\n"
|
||||||
"movq %%rax, %0\n"
|
: "=a"(result)
|
||||||
: "=r"(result)
|
: "a"(syscall_num), "D"(a1), "S"(a2),
|
||||||
: "r"((uint64_t)syscall_num), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
|
"d"(a3), [a4] "r"(a4), [a5] "r"(a5), [a6] "r"(a6)
|
||||||
"r"(a6)
|
: "r10", "r8", "r9", "r11", "rcx", "cc", "memory");
|
||||||
: "memory", "cc", "rcx", "r11");
|
return result;
|
||||||
return (int)result;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int msl_amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
uintptr_t amd64_syscall (int syscall_num, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
|
||||||
uintptr_t a5, uintptr_t a6);
|
uintptr_t a5, uintptr_t a6);
|
||||||
|
|
||||||
#endif // _LIBMSL_AMD64_SYSCALL_H
|
#endif // _LIBMSL_AMD64_SYSCALL_H
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <m/proc.h>
|
#include <alloc/liballoc.h>
|
||||||
|
#include <m/system.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
extern volatile uint8_t __bss_start[];
|
extern volatile uint8_t __bss_start[];
|
||||||
@@ -6,7 +7,7 @@ extern volatile uint8_t __bss_end[];
|
|||||||
|
|
||||||
extern void app_main (void);
|
extern void app_main (void);
|
||||||
|
|
||||||
static void msl_clear_bss (void) {
|
static void clear_bss (void) {
|
||||||
uint8_t* p = (uint8_t*)__bss_start;
|
uint8_t* p = (uint8_t*)__bss_start;
|
||||||
while (p < __bss_end) {
|
while (p < __bss_end) {
|
||||||
*p++ = 0;
|
*p++ = 0;
|
||||||
@@ -14,9 +15,9 @@ static void msl_clear_bss (void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void __premain (void) {
|
void __premain (void) {
|
||||||
msl_clear_bss ();
|
clear_bss ();
|
||||||
|
liballoc_init ();
|
||||||
app_main ();
|
app_main ();
|
||||||
|
liballoc_deinit ();
|
||||||
m_proc_quit ();
|
quit ();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
#include <m/syscall.h>
|
|
||||||
#include <m/syscall_defs.h>
|
|
||||||
|
|
||||||
int m_proc_quit (void) { return m_syscall (SYS_PROC_QUIT, 0, 0, 0, 0, 0, 0); }
|
|
||||||
|
|
||||||
int m_proc_test (void) { return m_syscall (SYS_PROC_TEST, 0, 0, 0, 0, 0, 0); }
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#ifndef _LIBMSL_M_PROC_H
|
|
||||||
#define _LIBMSL_M_PROC_H
|
|
||||||
|
|
||||||
int m_proc_quit (void);
|
|
||||||
|
|
||||||
int m_proc_test (void);
|
|
||||||
|
|
||||||
#endif // _LIBMSL_M_PROC_H
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
c += m/proc.c
|
c += m/system.c
|
||||||
|
|
||||||
o += m/proc.o
|
o += m/system.o
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__)
|
||||||
#include <amd64/syscall.h>
|
#include <amd64/syscall.h>
|
||||||
#define m_syscall msl_amd64_syscall
|
#define syscall amd64_syscall
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // _LIBMSL_M_SYSCALL_H
|
#endif // _LIBMSL_M_SYSCALL_H
|
||||||
|
|||||||
36
libmsl/m/system.c
Normal file
36
libmsl/m/system.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <m/syscall.h>
|
||||||
|
#include <m/system.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define do_syscall1(id, a1, a2, a3, a4, a5, a6, ...) \
|
||||||
|
syscall (id, (uintptr_t)a1, (uintptr_t)a2, (uintptr_t)a3, (uintptr_t)a4, (uintptr_t)a5, \
|
||||||
|
(uintptr_t)a6)
|
||||||
|
|
||||||
|
#define do_syscall(...) do_syscall1 (__VA_ARGS__, 0, 0, 0, 0, 0, 0)
|
||||||
|
|
||||||
|
int quit (void) { return do_syscall (SYS_QUIT, 0); }
|
||||||
|
|
||||||
|
int test (char c) { return do_syscall (SYS_TEST, c); }
|
||||||
|
|
||||||
|
int sched (void) { return do_syscall (SYS_SCHED, 0); }
|
||||||
|
|
||||||
|
void* map (uintptr_t vaddr, size_t pages, uint32_t flags) {
|
||||||
|
return (void*)do_syscall (SYS_MAP, vaddr, pages, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
int unmap (uintptr_t vaddr, size_t pages) { return do_syscall (SYS_UNMAP, vaddr, pages); }
|
||||||
|
|
||||||
|
int clone (uintptr_t vstack_top, void (*entry) (void), void* argument_ptr) {
|
||||||
|
return do_syscall (SYS_CLONE, vstack_top, entry, argument_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mutex_create (int mutex_rid) { return do_syscall (SYS_MUTEX_CREATE, mutex_rid); }
|
||||||
|
|
||||||
|
int mutex_delete (int mutex_rid) { return do_syscall (SYS_MUTEX_DELETE, mutex_rid); }
|
||||||
|
|
||||||
|
int mutex_lock (int mutex_rid) { return do_syscall (SYS_MUTEX_LOCK, mutex_rid); }
|
||||||
|
|
||||||
|
int mutex_unlock (int mutex_rid) { return do_syscall (SYS_MUTEX_UNLOCK, mutex_rid); }
|
||||||
|
|
||||||
|
void* argument_ptr (void) { return (void*)do_syscall (SYS_ARGUMENT_PTR, 0); }
|
||||||
28
libmsl/m/system.h
Normal file
28
libmsl/m/system.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef _LIBMSL_M_SYSTEM_H
|
||||||
|
#define _LIBMSL_M_SYSTEM_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#if defined(__x86_64__)
|
||||||
|
#define PAGE_SIZE 4096
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MAP_PRESENT (1 << 0)
|
||||||
|
#define MAP_RW (1 << 1)
|
||||||
|
#define MAP_USER (1 << 2)
|
||||||
|
#define MAP_FLAGS (MAP_PRESENT | MAP_USER)
|
||||||
|
|
||||||
|
int quit (void);
|
||||||
|
int test (char c);
|
||||||
|
int sched (void);
|
||||||
|
void* map (uintptr_t vaddr, size_t pages, uint32_t flags);
|
||||||
|
int unmap (uintptr_t vaddr, size_t pages);
|
||||||
|
int clone (uintptr_t vstack_top, void (*entry) (void), void* argument_ptr);
|
||||||
|
int mutex_create (int mutex_rid);
|
||||||
|
int mutex_delete (int mutex_rid);
|
||||||
|
int mutex_lock (int mutex_rid);
|
||||||
|
int mutex_unlock (int mutex_rid);
|
||||||
|
void* argument_ptr (void);
|
||||||
|
|
||||||
|
#endif // _LIBMSL_M_SYSTEM_H
|
||||||
1
libmsl/proc/.gitignore
vendored
Normal file
1
libmsl/proc/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.o
|
||||||
6
libmsl/proc/local.h
Normal file
6
libmsl/proc/local.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef _LIBMSL_PROC_TLS_H
|
||||||
|
#define _LIBMSL_PROC_TLS_H
|
||||||
|
|
||||||
|
#define LOCAL __thread
|
||||||
|
|
||||||
|
#endif // _LIBMSL_PROC_TLS_H
|
||||||
19
libmsl/proc/proc.c
Normal file
19
libmsl/proc/proc.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include <alloc/liballoc.h>
|
||||||
|
#include <m/status.h>
|
||||||
|
#include <m/system.h>
|
||||||
|
#include <proc/proc.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
int process_spawn (process_func_t func, void* argument_ptr) {
|
||||||
|
void* stack = malloc (PROC_STACK_SIZE);
|
||||||
|
if (stack == NULL)
|
||||||
|
return -ST_OOM_ERROR;
|
||||||
|
|
||||||
|
uintptr_t top = (uintptr_t)stack + PROC_STACK_SIZE;
|
||||||
|
return clone (top, func, argument_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int process_quit (void) { return quit (); }
|
||||||
|
|
||||||
|
void* process_argument (void) { return argument_ptr (); }
|
||||||
14
libmsl/proc/proc.h
Normal file
14
libmsl/proc/proc.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef _LIBMSL_PROC_PROC_H
|
||||||
|
#define _LIBMSL_PROC_PROC_H
|
||||||
|
|
||||||
|
#include <m/system.h>
|
||||||
|
|
||||||
|
#define PROC_STACK_SIZE 256 * PAGE_SIZE
|
||||||
|
|
||||||
|
typedef void (*process_func_t) (void);
|
||||||
|
|
||||||
|
int process_spawn (process_func_t func, void* argument_ptr);
|
||||||
|
int process_quit (void);
|
||||||
|
void* process_argument (void);
|
||||||
|
|
||||||
|
#endif // _LIBMSL_PROC_PROC_H
|
||||||
3
libmsl/proc/src.mk
Normal file
3
libmsl/proc/src.mk
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
c += proc/proc.c
|
||||||
|
|
||||||
|
o += proc/proc.o
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
include $(platform)/src.mk
|
include $(platform)/src.mk
|
||||||
include init/src.mk
|
include init/src.mk
|
||||||
include m/src.mk
|
include m/src.mk
|
||||||
|
include string/src.mk
|
||||||
|
include alloc/src.mk
|
||||||
|
include proc/src.mk
|
||||||
|
|||||||
1
libmsl/string/.gitignore
vendored
Normal file
1
libmsl/string/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.o
|
||||||
3
libmsl/string/src.mk
Normal file
3
libmsl/string/src.mk
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
c += string/string.c
|
||||||
|
|
||||||
|
o += string/string.o
|
||||||
47
libmsl/string/string.c
Normal file
47
libmsl/string/string.c
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string/string.h>
|
||||||
|
|
||||||
|
size_t memset (void* dst, uint8_t b, size_t n) {
|
||||||
|
uint8_t* dst1 = dst;
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
dst1[i] = b;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t memcpy (void* dst, const void* src, size_t n) {
|
||||||
|
uint8_t* dst1 = dst;
|
||||||
|
const uint8_t* src1 = src;
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
dst1[i] = src1[i];
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// SOURCE: https://stackoverflow.com/a/48967408
|
||||||
|
void strncpy (char* dst, const char* src, size_t n) {
|
||||||
|
size_t i = 0;
|
||||||
|
while (i++ != n && (*dst++ = *src++))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t strlen (const char* str) {
|
||||||
|
const char* s;
|
||||||
|
for (s = str; *s; ++s)
|
||||||
|
;
|
||||||
|
return (s - str);
|
||||||
|
}
|
||||||
|
|
||||||
|
int memcmp (const void* s1, const void* s2, size_t n) {
|
||||||
|
unsigned char* p = (unsigned char*)s1;
|
||||||
|
unsigned char* q = (unsigned char*)s2;
|
||||||
|
|
||||||
|
while (n--) {
|
||||||
|
if (*p != *q) {
|
||||||
|
return (int)*p - (int)*q;
|
||||||
|
}
|
||||||
|
p++, q++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
libmsl/string/string.h
Normal file
13
libmsl/string/string.h
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#ifndef _LIBMSL_STRING_STRING_H
|
||||||
|
#define _LIBMSL_STRING_STRING_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
size_t memset (void* dst, uint8_t b, size_t n);
|
||||||
|
size_t memcpy (void* dst, const void* src, size_t n);
|
||||||
|
void strncpy (char* dst, const char* src, size_t n);
|
||||||
|
size_t strlen (const char* str);
|
||||||
|
int memcmp (const void* s1, const void* s2, size_t n);
|
||||||
|
|
||||||
|
#endif // _LIBMSL_STRING_STRING_H
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
apps := init
|
apps := init spin
|
||||||
|
|
||||||
all_apps:
|
all_apps:
|
||||||
@for d in $(apps); do make -C $$d platform=$(platform) all; done
|
@for d in $(apps); do make -C $$d platform=$(platform) all; done
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ clean_libmsl:
|
|||||||
format_libmsl:
|
format_libmsl:
|
||||||
make -C libmsl platform=$(platform) format
|
make -C libmsl platform=$(platform) format
|
||||||
|
|
||||||
.PHONY: all_libmsl clean_libmsl
|
.PHONY: all_libmsl clean_libmsl format_libmsl
|
||||||
@@ -1,7 +1,12 @@
|
|||||||
site_name: MOP3 documentation
|
site_name: MOP3 documentation
|
||||||
|
|
||||||
theme:
|
theme:
|
||||||
name: readthedocs
|
name: material
|
||||||
highlightjs: true
|
highlightjs: true
|
||||||
|
|
||||||
repo_url: https://git.kamkow1lair.pl/kamkow1/mop3.git
|
repo_url: https://git.kamkow1lair.pl/kamkow1/mop3.git
|
||||||
|
|
||||||
|
markdown_extensions:
|
||||||
|
- attr_list
|
||||||
|
- md_in_html
|
||||||
|
- pymdownx.blocks.caption
|
||||||
|
|||||||
2
spin/.gitignore
vendored
Normal file
2
spin/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*.o
|
||||||
|
*.exe
|
||||||
1
spin/Makefile
Normal file
1
spin/Makefile
Normal file
@@ -0,0 +1 @@
|
|||||||
|
include ../make/user.mk
|
||||||
1
spin/app.mk
Normal file
1
spin/app.mk
Normal file
@@ -0,0 +1 @@
|
|||||||
|
app := spin.exe
|
||||||
4
spin/spin.c
Normal file
4
spin/spin.c
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
void app_main (void) {
|
||||||
|
for (;;)
|
||||||
|
;
|
||||||
|
}
|
||||||
3
spin/src.mk
Normal file
3
spin/src.mk
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
c += spin.c
|
||||||
|
|
||||||
|
o += spin.o
|
||||||
Reference in New Issue
Block a user