From e5ebd7f3ba8735bec6796d3e61164a8702865dfc Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Mon, 27 Apr 2026 18:06:02 +0200 Subject: [PATCH] Use a big-lock for kernel sychronization instead of fine-grained locking --- kernel/amd64/apic.c | 16 +- kernel/amd64/bootmain.c | 2 + kernel/amd64/debug.c | 13 +- kernel/amd64/hpet.c | 12 +- kernel/amd64/intr.c | 26 ++-- kernel/amd64/proc.c | 21 --- kernel/amd64/sched1.c | 10 +- kernel/amd64/smp.c | 25 +--- kernel/amd64/smp.h | 2 - kernel/amd64/syscall.c | 26 ++-- kernel/device/def_device_op.h | 4 +- kernel/device/device.c | 39 ----- kernel/device/device.h | 11 +- kernel/device/pci/pci.c | 38 ----- kernel/device/ps2/ps2_kb.c | 23 +-- kernel/device/storage/idedrv.c | 23 +-- kernel/device/storage/partdrv.c | 18 +-- kernel/device/storage/partitions.c | 14 +- kernel/device/storage/ramdrv.c | 8 - kernel/device/storage/usbdrv.c | 101 ++++--------- kernel/device/storage/usbdrv.h | 1 - kernel/device/sys/debugconsole.c | 4 - kernel/device/usb/usb.c | 4 +- kernel/device/usb/usb.h | 2 +- kernel/device/usb/xhci.c | 117 +++++++-------- kernel/device/usb/xhci.h | 2 +- kernel/fs/fatfs/fatfs.c | 40 +---- kernel/fs/iso9660fs/iso9660fs.c | 13 +- kernel/fs/tarfs/tarfs.c | 15 +- kernel/fs/vfs.c | 119 +-------------- kernel/fs/vfs.h | 2 - kernel/id/id_alloc.c | 11 -- kernel/id/id_alloc.h | 1 - kernel/irq/irq.c | 13 -- kernel/mm/_malloc_port.c | 4 +- kernel/mm/pmm.c | 22 --- kernel/mm/pmm.h | 1 - kernel/proc/env.c | 13 -- kernel/proc/mail.c | 47 +----- kernel/proc/mutex.c | 51 +------ kernel/proc/proc.c | 85 +---------- kernel/proc/proc.h | 1 - kernel/proc/procgroup.c | 47 ------ kernel/proc/procgroup.h | 1 - kernel/proc/resource.c | 37 ----- kernel/proc/resource.h | 1 - kernel/proc/stream.c | 18 --- kernel/proc/suspension_q.c | 43 +----- kernel/proc/suspension_q.h | 6 +- kernel/sync/biglock.c | 9 ++ kernel/sync/biglock.h | 10 ++ kernel/sync/spin_lock.c | 8 +- kernel/sync/spin_lock.h | 4 +- kernel/sync/src.mk | 6 +- kernel/sys/sched.h | 2 +- kernel/syscall/syscall.c | 226 +---------------------------- 56 files changed, 212 insertions(+), 1206 deletions(-) create mode 100644 kernel/sync/biglock.c create mode 100644 kernel/sync/biglock.h diff --git a/kernel/amd64/apic.c b/kernel/amd64/apic.c index 499b1ca..cfb1d7e 100644 --- a/kernel/amd64/apic.c +++ b/kernel/amd64/apic.c @@ -38,7 +38,6 @@ struct ioapic { struct acpi_madt_ioapic table_data; - spin_lock_t lock; uintptr_t mmio_base; }; @@ -57,23 +56,15 @@ static spin_lock_t lapic_calibration_lock = SPIN_LOCK_INIT; /* Read IOAPIC */ static uint32_t ioapic_read(struct ioapic* ioapic, uint32_t reg) { - uint64_t fia; - - spin_lock(&ioapic->lock, &fia); *(volatile uint32_t*)ioapic->mmio_base = reg; uint32_t ret = *(volatile uint32_t*)(ioapic->mmio_base + 0x10); - spin_unlock(&ioapic->lock, fia); return ret; } /* Write IOAPIC */ static void ioapic_write(struct ioapic* ioapic, uint32_t reg, uint32_t value) { - uint64_t fia; - - spin_lock(&ioapic->lock, &fia); *(volatile uint32_t*)ioapic->mmio_base = reg; *(volatile uint32_t*)(ioapic->mmio_base + 0x10) = value; - spin_unlock(&ioapic->lock, fia); } /* Find an IOAPIC corresposting to provided IRQ */ @@ -181,7 +172,6 @@ void ioapic_init(void) { (uintptr_t)hhdm->offset + (uintptr_t)ioapic_table_data->address, MM_PG_PRESENT | MM_PG_RW); 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), }; @@ -233,9 +223,7 @@ void lapic_timer_unmask(void) { * us - Period length in microseconds */ static uint32_t lapic_calibrate(uint32_t us) { - uint64_t flc; - - spin_lock(&lapic_calibration_lock, &flc); + spin_lock(&lapic_calibration_lock); lapic_write(LAPIC_DCR, DIVIDER_VALUE); @@ -248,7 +236,7 @@ static uint32_t lapic_calibrate(uint32_t us) { uint32_t ticks = 0xFFFFFFFF - lapic_read(LAPIC_TIMCCT); DEBUG("timer ticks = %u\n", ticks); - spin_unlock(&lapic_calibration_lock, flc); + spin_unlock(&lapic_calibration_lock); return ticks; } diff --git a/kernel/amd64/bootmain.c b/kernel/amd64/bootmain.c index 2604e78..769f58c 100644 --- a/kernel/amd64/bootmain.c +++ b/kernel/amd64/bootmain.c @@ -76,6 +76,8 @@ void bootmain(void) { devices_init(); vfs_init(); + intr_disable(); + struct reschedule_ctx rctx; memset(&rctx, 0, sizeof(rctx)); diff --git a/kernel/amd64/debug.c b/kernel/amd64/debug.c index 34d99e3..b4c3c55 100644 --- a/kernel/amd64/debug.c +++ b/kernel/amd64/debug.c @@ -11,11 +11,8 @@ #define PORT_COM1 0x03F8 /* debugprintf buffer size */ #define BUFFER_SIZE 1024 -/* - * Lock, which ensures that prints to the serial port are atomic (ie. one debugprintf is atomic in - * itself). - */ -static spin_lock_t serial_lock = SPIN_LOCK_INIT; + +static spin_lock_t debug_lock = SPIN_LOCK_INIT; static bool debug_is_init = false; @@ -33,8 +30,6 @@ static void debug_serial_write(char x) { * Formatted printing to serial. serial_lock ensures that all prints are atomic. */ void debugprintf(const char* fmt, ...) { - uint64_t f1; - if (!debug_is_init) return; @@ -50,14 +45,14 @@ void debugprintf(const char* fmt, ...) { const char* p = buffer; - spin_lock(&serial_lock, &f1); + spin_lock(&debug_lock); while (*p) { debug_serial_write(*p); p++; } - spin_unlock(&serial_lock, f1); + spin_unlock(&debug_lock); } void debugprintf_nolock(const char* fmt, ...) { diff --git a/kernel/amd64/hpet.c b/kernel/amd64/hpet.c index 4b473b5..27fdb59 100644 --- a/kernel/amd64/hpet.c +++ b/kernel/amd64/hpet.c @@ -59,10 +59,8 @@ static void hpet_write32(uint32_t reg, uint32_t value) { /* Read current value of HPET_MCVR register. */ static uint64_t hpet_read_counter(void) { - uint64_t fh; - uint64_t value; - spin_lock(&hpet_lock, &fh); + spin_lock(&hpet_lock); if (!hpet_32bits) value = hpet_read64(HPET_MCVR); @@ -77,15 +75,13 @@ static uint64_t hpet_read_counter(void) { value = ((uint64_t)hi1 << 32) | lo; } - spin_unlock(&hpet_lock, fh); + spin_unlock(&hpet_lock); return value; } static void hpet_write_counter(uint64_t value) { - uint64_t fh; - - spin_lock(&hpet_lock, &fh); + spin_lock(&hpet_lock); if (!hpet_32bits) hpet_write64(HPET_MCVR, value); @@ -94,7 +90,7 @@ static void hpet_write_counter(uint64_t value) { hpet_write32(HPET_MCVR + 4, (uint32_t)(value >> 32)); } - spin_unlock(&hpet_lock, fh); + spin_unlock(&hpet_lock); } /* Sleep for a given amount of microseconds. This time can last longer due to \ref hpet_lock being diff --git a/kernel/amd64/intr.c b/kernel/amd64/intr.c index 9c8db49..322bfcb 100644 --- a/kernel/amd64/intr.c +++ b/kernel/amd64/intr.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -179,8 +180,11 @@ static void intr_exception(struct saved_regs* regs) { } if (regs->cs == (GDT_UCODE | 0x03)) { + biglock_lock(); + struct reschedule_ctx rctx; memset(&rctx, 0, sizeof(rctx)); + proc_kill(thiscpu->proc_current, &rctx); bool do_thiscpu = false; @@ -193,6 +197,8 @@ static void intr_exception(struct saved_regs* regs) { if (do_thiscpu) cpu_request_sched(thiscpu, true); + + biglock_unlock(); } else { spin(); } @@ -207,33 +213,27 @@ void intr_handler(void* stack_ptr) { if (regs->trap <= 31) { intr_exception(regs); } else { - uint64_t ftc, fpc; - bool user = false; if (regs->cs == (GDT_UCODE | 0x3)) { + biglock_lock(); + user = true; - spin_lock(&thiscpu->lock, &ftc); - struct proc* proc_current = thiscpu->proc_current; - - spin_lock(&proc_current->lock, &fpc); memcpy(&proc_current->pdata.regs, regs, sizeof(struct saved_regs)); - fx_save(proc_current->pdata.fx_env); - - spin_unlock(&proc_current->lock, fpc); - - spin_unlock(&thiscpu->lock, ftc); } lapic_eoi(); struct irq* irq = irq_find(regs->trap); - if (irq == NULL) + if (irq == NULL) { + if (user) + biglock_unlock(); return; + } struct reschedule_ctx rctx; memset(&rctx, 0, sizeof(rctx)); @@ -251,6 +251,8 @@ void intr_handler(void* stack_ptr) { if (do_thiscpu) cpu_request_sched(thiscpu, user); + + biglock_unlock(); } } } diff --git a/kernel/amd64/proc.c b/kernel/amd64/proc.c index 31fcfb0..a9ab5bf 100644 --- a/kernel/amd64/proc.c +++ b/kernel/amd64/proc.c @@ -29,7 +29,6 @@ struct proc* proc_from_elf(uint8_t* elf_contents) { memset(proc, 0, sizeof(*proc)); - proc->lock = SPIN_LOCK_INIT; proc->state = PROC_READY; proc->pid = proc_alloc_pid(); @@ -69,7 +68,6 @@ 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) { - uint64_t fpt; struct limine_hhdm_response* hhdm = limine_hhdm_request.response; struct proc* proc = malloc(sizeof(*proc)); @@ -78,7 +76,6 @@ struct proc* proc_clone(struct proc* proto, uintptr_t vstack_top, uintptr_t entr memset(proc, 0, sizeof(*proc)); - proc->lock = SPIN_LOCK_INIT; proc->state = PROC_READY; proc->pid = proc_alloc_pid(); @@ -87,15 +84,11 @@ struct proc* proc_clone(struct proc* proto, uintptr_t vstack_top, uintptr_t entr return NULL; } - spin_lock(&proto->lock, &fpt); - memcpy(proc->name, proto->name, sizeof(proto->name)); proc->procgroup = proto->procgroup; procgroup_attach(proc->procgroup, proc); - spin_unlock(&proto->lock, fpt); - uintptr_t kstack_paddr = pmm_alloc(KSTACK_SIZE / PAGE_SIZE); proc->pdata.kernel_stack = kstack_paddr + (uintptr_t)hhdm->offset + KSTACK_SIZE; @@ -116,28 +109,14 @@ struct proc* proc_clone(struct proc* proto, uintptr_t vstack_top, uintptr_t entr } void proc_cleanup(struct proc* proc, struct reschedule_ctx* rctx) { - uint64_t fp, fsq; - - spin_lock(&proc->lock, &fp); - spin_lock(&proc->done_sq.lock, &fsq); - while (proc->done_sq.proc_list != NULL) { struct list_node_link* node = proc->done_sq.proc_list; struct proc_sq_entry* sq_entry = list_entry(node, struct proc_sq_entry, sq_link); struct proc* suspended_proc = sq_entry->proc; - spin_unlock(&proc->done_sq.lock, fsq); - spin_unlock(&proc->lock, fp); - proc_sq_resume(suspended_proc, sq_entry, rctx); - - spin_lock(&proc->lock, &fp); - spin_lock(&proc->done_sq.lock, &fsq); } - spin_unlock(&proc->done_sq.lock, fsq); - spin_unlock(&proc->lock, fp); - proc_sqs_cleanup(proc); proc_mutexes_cleanup(proc, rctx); diff --git a/kernel/amd64/sched1.c b/kernel/amd64/sched1.c index 12ff42d..293487b 100644 --- a/kernel/amd64/sched1.c +++ b/kernel/amd64/sched1.c @@ -6,19 +6,16 @@ #include #include #include +#include #include #include #include #include #include -void do_sched(struct proc* proc, spin_lock_t* cpu_lock) { - uint64_t fp; - +void do_sched(struct proc* proc) { intr_disable(); - spin_lock(&proc->lock, &fp); - thiscpu->tss.rsp0 = proc->pdata.kernel_stack; thiscpu->syscall_kernel_stack = proc->pdata.kernel_stack; wrmsr(MSR_FS_BASE, proc->pdata.fs_base); @@ -30,8 +27,7 @@ void do_sched(struct proc* proc, spin_lock_t* cpu_lock) { fx_restore(proc->pdata.fx_env); - spin_unlock(&proc->lock, fp); - spin_unlock(cpu_lock, 0); + biglock_unlock(); do_sched1((void*)®s, cr3); } diff --git a/kernel/amd64/smp.c b/kernel/amd64/smp.c index f5524e5..8c7e69c 100644 --- a/kernel/amd64/smp.c +++ b/kernel/amd64/smp.c @@ -31,7 +31,6 @@ struct cpu* cpu_make(uint64_t lapic_id, uint64_t acpi_id) { struct cpu* cpu = &cpus[id]; memset(cpu, 0, sizeof(*cpu)); - cpu->lock = SPIN_LOCK_INIT; cpu->id = id; cpu->acpi_id = acpi_id; cpu->lapic_id = lapic_id; @@ -56,25 +55,20 @@ void cpu_request_sched(struct cpu* cpu, bool user) { } struct cpu* cpu_find_lightest(void) { - uint64_t fc; struct limine_mp_response* mp = limine_mp_request.response; int start = thiscpu->id; struct cpu* best_cpu = &cpus[start]; - spin_lock(&best_cpu->lock, &fc); int best_load = best_cpu->proc_run_q_count; - spin_unlock(&best_cpu->lock, fc); for (int i = 1; i < (int)mp->cpu_count; i++) { int idx = (start + i) % mp->cpu_count; struct cpu* cpu = &cpus[idx]; - spin_lock(&cpu->lock, &fc); int l = cpu->proc_run_q_count; - spin_unlock(&cpu->lock, fc); if (l < best_load) { best_load = l; @@ -90,8 +84,6 @@ struct cpu* cpu_find_lightest(void) { /// Bootstrap code for non-BSP CPUs static void smp_bootstrap(struct limine_mp_info* mp_info) { - uint64_t fc; - load_kernel_cr3(); struct cpu* cpu = cpu_make(mp_info->lapic_id, mp_info->processor_id); @@ -103,22 +95,19 @@ static void smp_bootstrap(struct limine_mp_info* mp_info) { lapic_init(1000); - DEBUG("CPU %u is online!\n", thiscpu->id); - cpu->kproc = kproc_create(); - atomic_fetch_sub(&cpu_counter, 1); - - intr_enable(); - struct reschedule_ctx rctx; memset(&rctx, 0, sizeof(rctx)); struct proc* spin_proc = proc_from_file(thiscpu->kproc, "sys", "/spin", &rctx); proc_register(spin_proc, thiscpu, &rctx); - spin_lock(&spin_proc->cpu->lock, &fc); - do_sched(spin_proc, &spin_proc->cpu->lock); + atomic_fetch_sub(&cpu_counter, 1); + + intr_enable(); + + do_sched(spin_proc); for (;;) ; @@ -136,10 +125,6 @@ void smp_init(void) { } } - DEBUG("Waiting for other CPUs:\n"); - while (atomic_load(&cpu_counter) > 0) ; - - DEBUG("All CPUs are up!\n"); } diff --git a/kernel/amd64/smp.h b/kernel/amd64/smp.h index ce07bf6..88def39 100644 --- a/kernel/amd64/smp.h +++ b/kernel/amd64/smp.h @@ -28,8 +28,6 @@ struct cpu { uint32_t id; uint32_t acpi_id; - spin_lock_t lock; - struct list_node_link* proc_run_q; struct proc* proc_current; int proc_run_q_count; diff --git a/kernel/amd64/syscall.c b/kernel/amd64/syscall.c index 649a60a..8b52e68 100644 --- a/kernel/amd64/syscall.c +++ b/kernel/amd64/syscall.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -20,26 +21,17 @@ extern void syscall_entry(void); -uintptr_t syscall_dispatch(void* stack_ptr) { - uint64_t fp, fc; - - load_kernel_cr3(); +static uintptr_t syscall_dispatch1(void* stack_ptr) { struct saved_regs* regs = stack_ptr; - spin_lock(&thiscpu->lock, &fc); struct proc* caller = thiscpu->proc_current; - spin_lock(&caller->lock, &fp); - int caller_pid = caller->pid; memcpy(&caller->pdata.regs, regs, sizeof(struct saved_regs)); fx_save(caller->pdata.fx_env); - spin_unlock(&caller->lock, fp); - spin_unlock(&thiscpu->lock, fc); - int syscall_num = regs->rax; syscall_handler_func_t func = syscall_find_handler(syscall_num); @@ -64,9 +56,7 @@ uintptr_t syscall_dispatch(void* stack_ptr) { caller = proc_find_pid(caller_pid); if (caller != NULL) { - spin_lock(&caller->lock, &fp); caller->pdata.regs.rax = r; - spin_unlock(&caller->lock, fp); } bool do_thiscpu = false; @@ -83,6 +73,18 @@ uintptr_t syscall_dispatch(void* stack_ptr) { return r; } +uintptr_t syscall_dispatch(void* stack_ptr) { + load_kernel_cr3(); + + biglock_lock(); + + uintptr_t r = syscall_dispatch1(stack_ptr); + + biglock_unlock(); + + return r; +} + void syscall_init(void) { wrmsr(MSR_STAR, ((uint64_t)GDT_KCODE << 32) | ((uint64_t)(GDT_KDATA | 0x03) << 48)); wrmsr(MSR_LSTAR, (uint64_t)&syscall_entry); diff --git a/kernel/device/def_device_op.h b/kernel/device/def_device_op.h index 2e9e9e3..d3ad91a 100644 --- a/kernel/device/def_device_op.h +++ b/kernel/device/def_device_op.h @@ -5,8 +5,8 @@ #define DEFINE_DEVICE_OP(name) \ int name(struct device* UNUSED device, struct proc* UNUSED proc, \ - struct reschedule_ctx* UNUSED rctx, uint64_t* UNUSED lockflags, void* UNUSED a1, \ - void* UNUSED a2, void* UNUSED a3, void* UNUSED a4) + struct reschedule_ctx* UNUSED rctx, void* UNUSED a1, void* UNUSED a2, void* UNUSED a3, \ + void* UNUSED a4) #define DEFINE_DEVICE_INIT(name) \ bool name(struct device* UNUSED device, void* UNUSED arg, struct proc* UNUSED proc, \ diff --git a/kernel/device/device.c b/kernel/device/device.c index a2c8618..ca3455c 100644 --- a/kernel/device/device.c +++ b/kernel/device/device.c @@ -28,25 +28,18 @@ struct device_table { struct hash_node_link* device_buckets[1024]; - spin_lock_t lock; }; static struct device_table device_table; struct device* device_find(const char* key) { - uint64_t fdt; - struct hash_node_link* found_link = NULL; size_t key_len = strlen_null(key); uint32_t hash = hash_fnv32(key, key_len); - spin_lock(&device_table.lock, &fdt); - hash_find(&device_table, key, key_len, hash, lengthof(device_table.device_buckets), device_buckets, struct device, device_table_link, key, found_link); - spin_unlock(&device_table.lock, fdt); - if (found_link == NULL) { return NULL; } @@ -59,8 +52,6 @@ struct device* device_find(const char* key) { struct device* device_create(int type, const char* key, device_op_func_t* ops, size_t ops_len, device_init_func_t init, device_fini_func_t fini, void* arg, struct proc* proc, struct reschedule_ctx* rctx) { - uint64_t fdt; - if (ops_len >= fieldlengthof(struct device, ops)) return NULL; @@ -86,44 +77,32 @@ struct device* device_create(int type, const char* key, device_op_func_t* ops, s uint32_t device_hash = hash_fnv32(device->key, strlen_null(device->key)); - spin_lock(&device_table.lock, &fdt); - hash_insert(&device_table, &device->device_table_link, device_hash, lengthof(device_table.device_buckets), device_buckets); - spin_unlock(&device_table.lock, fdt); - DEBUG("Created device %s\n", device->key); return device; } void device_delete(const char* key, struct proc* proc, struct reschedule_ctx* rctx) { - uint64_t fdt, fsd, fd; - size_t key_len = strlen_null(key); uint32_t hash = hash_fnv32(key, key_len); struct hash_node_link* found_link; - spin_lock(&device_table.lock, &fdt); - hash_find(&device_table, key, key_len, hash, lengthof(device_table.device_buckets), device_buckets, struct device, device_table_link, key, found_link); if (found_link == NULL) { - spin_unlock(&device_table.lock, fdt); return; } struct device* device = hash_entry(found_link, struct device, device_table_link); if (device == NULL) { - spin_unlock(&device_table.lock, fdt); return; } - spin_lock(&device->lock, &fd); - hash_delete(&device_table, key, strlen_null(key), hash, lengthof(device_table.device_buckets), device_buckets, struct device, device_table_link, key, found_link); @@ -131,8 +110,6 @@ void device_delete(const char* key, struct proc* proc, struct reschedule_ctx* rc list_foreach(device->subdevices, subdevice_link, tmp_subdevice_link) { struct device* subdevice = list_entry(subdevice_link, struct device, subdevices_link); - spin_lock(&subdevice->lock, &fsd); - list_remove(device->subdevices, &subdevice->subdevices_link); size_t sd_key_len = strlen_null(subdevice->key); @@ -144,30 +121,20 @@ void device_delete(const char* key, struct proc* proc, struct reschedule_ctx* rc device->fini(subdevice, proc, rctx); - spin_unlock(&subdevice->lock, fsd); - free(subdevice); } device->fini(device, proc, rctx); - spin_unlock(&device->lock, fd); - free(device); - - spin_unlock(&device_table.lock, fdt); } size_t device_populate_device_infos(struct device_info* infos, size_t count) { - uint64_t fdt, fd; - if (count >= DEVICES_MAX) count = DEVICES_MAX; size_t j = 0; - spin_lock(&device_table.lock, &fdt); - for (size_t i = 0; i < lengthof(device_table.device_buckets); i++) { struct hash_node_link* hash_link = device_table.device_buckets[i]; @@ -175,20 +142,14 @@ size_t device_populate_device_infos(struct device_info* infos, size_t count) { struct device* device = hash_entry(hash_link, struct device, device_table_link); struct device_info* info = &infos[j]; - spin_lock(&device->lock, &fd); - memcpy(info->key, device->key, sizeof(info->key)); info->type = device->type; - spin_unlock(&device->lock, fd); - hash_link = hash_link->next; j++; } } - spin_unlock(&device_table.lock, fdt); - return j; } diff --git a/kernel/device/device.h b/kernel/device/device.h index d8ac054..5ca1e02 100644 --- a/kernel/device/device.h +++ b/kernel/device/device.h @@ -12,16 +12,16 @@ #define DEVICES_MAX 1024 -#define device_op1(d, op, proc, rctx, lockflags, a1, a2, a3, a4, ...) \ - (d)->ops[(op)]((d), (proc), (rctx), lockflags, (void*)(a1), (void*)(a2), (void*)(a3), (void*)(a4)) +#define device_op1(d, op, proc, rctx, a1, a2, a3, a4, ...) \ + (d)->ops[(op)]((d), (proc), (rctx), (void*)(a1), (void*)(a2), (void*)(a3), (void*)(a4)) -#define device_op(d, op, proc, rctx, lockflags, ...) \ - device_op1(d, op, proc, rctx, lockflags, __VA_ARGS__, NULL, NULL, NULL, NULL) +#define device_op(d, op, proc, rctx, ...) \ + device_op1(d, op, proc, rctx, __VA_ARGS__, NULL, NULL, NULL, NULL) struct device; typedef int (*device_op_func_t)(struct device* device, struct proc*, struct reschedule_ctx* rctx, - uint64_t* lockflags, void* a1, void* a2, void* a3, void* a4); + void* a1, void* a2, void* a3, void* a4); typedef bool (*device_init_func_t)(struct device* device, void* arg, struct proc* proc, struct reschedule_ctx* rctx); @@ -31,7 +31,6 @@ typedef void (*device_fini_func_t)(struct device* device, struct proc* proc, struct device { char key[0x100]; device_op_func_t ops[32]; - spin_lock_t lock; struct hash_node_link device_table_link; struct list_node_link subdevices_link; device_init_func_t init; diff --git a/kernel/device/pci/pci.c b/kernel/device/pci/pci.c index fb65b4b..4cc9a6b 100644 --- a/kernel/device/pci/pci.c +++ b/kernel/device/pci/pci.c @@ -16,114 +16,76 @@ static const struct pci_driver_info pci_driver_infos[] = { {.class = 0x0C, .subclass = 0x03, .init = &pci_xhci_init}, }; -static spin_lock_t pci_lock = SPIN_LOCK_INIT; - uint32_t pci_read32(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) { - uint64_t fpci; - uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) | (offset & 0xFC) | ((uint32_t)0x80000000); - spin_lock(&pci_lock, &fpci); - outl(PCI_CONFIG_ADDR, addr); uint32_t r = inl(PCI_CONFIG_DATA); - spin_unlock(&pci_lock, fpci); - return r; } void pci_write32(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t value) { - uint64_t fpci; - uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) | (offset & 0xFC) | ((uint32_t)0x80000000); - spin_lock(&pci_lock, &fpci); - outl(PCI_CONFIG_ADDR, addr); outl(PCI_CONFIG_DATA, value); - - spin_unlock(&pci_lock, fpci); } uint16_t pci_read16(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) { - uint64_t fpci; - uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) | (offset & 0xFC) | ((uint32_t)0x80000000); - spin_lock(&pci_lock, &fpci); - outl(PCI_CONFIG_ADDR, addr); uint16_t r = inw(PCI_CONFIG_DATA + (offset & 2)); - spin_unlock(&pci_lock, fpci); - return r; } void pci_write16(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t value) { - uint64_t fpci; - uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) | (offset & 0xFC) | ((uint32_t)0x80000000); - spin_lock(&pci_lock, &fpci); - outl(PCI_CONFIG_ADDR, addr); outw(PCI_CONFIG_DATA + (offset & 2), value); - - spin_unlock(&pci_lock, fpci); } uint8_t pci_read8(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) { - uint64_t fpci; - uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) | (offset & 0xFC) | ((uint32_t)0x80000000); - spin_lock(&pci_lock, &fpci); - outl(PCI_CONFIG_ADDR, addr); uint8_t r = inb(PCI_CONFIG_DATA + (offset & 3)); - spin_unlock(&pci_lock, fpci); - return r; } void pci_write8(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint8_t value) { - uint64_t fpci; - uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) | (offset & 0xFC) | ((uint32_t)0x80000000); - spin_lock(&pci_lock, &fpci); - outl(PCI_CONFIG_ADDR, addr); outb(PCI_CONFIG_DATA + (offset & 3), value); - - spin_unlock(&pci_lock, fpci); } uint8_t pci_find_cap(uint8_t bus, uint8_t slot, uint8_t func, uint8_t cap_id) { diff --git a/kernel/device/ps2/ps2_kb.c b/kernel/device/ps2/ps2_kb.c index 3b83b75..cf1ae01 100644 --- a/kernel/device/ps2/ps2_kb.c +++ b/kernel/device/ps2/ps2_kb.c @@ -26,7 +26,6 @@ #define PS2KB_RINGBUFFER_MAX 2048 static struct ringbuffer ps2kb_ringbuffer; -static spin_lock_t ps2kb_ringbuffer_lock = SPIN_LOCK_INIT; static struct proc_suspension_q ps2kb_sq; static uint8_t shiftcode[0x100] = { @@ -150,16 +149,11 @@ static int32_t ps2kb_keycode(void) { static void ps2kb_irq(void* arg, void* regs, bool user, struct reschedule_ctx* rctx) { (void)arg, (void)regs, (void)user; - uint64_t frb, fsq; - int32_t keycode = ps2kb_keycode(); if (keycode <= 0 || keycode == 0xFA) return; - spin_lock(&ps2kb_ringbuffer_lock, &frb); - spin_lock(&ps2kb_sq.lock, &fsq); - ringbuffer_push(uint8_t, &ps2kb_ringbuffer, (uint8_t)keycode); struct list_node_link* node = ps2kb_sq.proc_list; @@ -168,27 +162,17 @@ static void ps2kb_irq(void* arg, void* regs, bool user, struct reschedule_ctx* r struct proc_sq_entry* sq_entry = list_entry(node, struct proc_sq_entry, sq_link); struct proc* resumed_proc = sq_entry->proc; - spin_unlock(&ps2kb_sq.lock, fsq); - spin_unlock(&ps2kb_ringbuffer_lock, frb); - proc_sq_resume(resumed_proc, sq_entry, rctx); return; } - - spin_unlock(&ps2kb_sq.lock, fsq); - spin_unlock(&ps2kb_ringbuffer_lock, frb); } DEFINE_DEVICE_OP(ps2kb_read_key) { - uint64_t frb, fsq; - uint8_t* chbuf = (uint8_t*)a1; if (chbuf == NULL) return -ST_BAD_ADDRESS_SPACE; - spin_lock(&ps2kb_ringbuffer_lock, &frb); - size_t prev_count = ps2kb_ringbuffer.count; ringbuffer_pop(uint8_t, &ps2kb_ringbuffer, chbuf); @@ -197,22 +181,17 @@ DEFINE_DEVICE_OP(ps2kb_read_key) { /* didn't pop anything */ if (prev_count == new_count) { - spin_lock(&ps2kb_sq.lock, &fsq); struct list_node_link* node = ps2kb_sq.proc_list; - spin_unlock(&ps2kb_sq.lock, fsq); if (node != NULL) { - spin_unlock(&ps2kb_ringbuffer_lock, frb); return -ST_PERMISSION_ERROR; } - proc_sq_suspend(proc, &ps2kb_sq, &ps2kb_ringbuffer_lock, frb, rctx, NULL, NULL); + proc_sq_suspend(proc, &ps2kb_sq, rctx, NULL, NULL); return ST_OK; } - spin_unlock(&ps2kb_ringbuffer_lock, frb); - return ST_OK; } diff --git a/kernel/device/storage/idedrv.c b/kernel/device/storage/idedrv.c index fa7c4ff..c7667fe 100644 --- a/kernel/device/storage/idedrv.c +++ b/kernel/device/storage/idedrv.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -338,12 +339,12 @@ static int idedrv_do_read_irqs(struct idedrv* idedrv, size_t sector, size_t sect uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28; outb(idedrv->io + IDE_REG_CMD, cmd); - intr_enable(); + biglock_unlock(); while (!atomic_load(&req->done)) spin_lock_relax(); - intr_disable(); + biglock_lock(); free(req); @@ -482,12 +483,12 @@ static int idedrv_do_write_irqs(struct idedrv* idedrv, size_t sector, size_t sec req->sector_done_count = 1; - intr_enable(); + biglock_unlock(); while (!atomic_load(&req->done)) spin_lock_relax(); - intr_disable(); + biglock_lock(); free(req); @@ -616,7 +617,7 @@ DEFINE_DEVICE_OP(idedrv_write) { } DEFINE_DEVICE_OP(idedrv_get_device_type) { - (void)proc, (void)rctx, (void)device, (void)a2, (void)a3, (void)a4, (void)lockflags; + (void)proc, (void)rctx, (void)device, (void)a2, (void)a3, (void)a4; if (a1 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -629,7 +630,7 @@ DEFINE_DEVICE_OP(idedrv_get_device_type) { } DEFINE_DEVICE_OP(idedrv_get_sector_size) { - (void)proc, (void)rctx, (void)a2, (void)a3, (void)a4, (void)lockflags; + (void)proc, (void)rctx, (void)a2, (void)a3, (void)a4; if (a1 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -644,7 +645,7 @@ DEFINE_DEVICE_OP(idedrv_get_sector_size) { } DEFINE_DEVICE_OP(idedrv_get_size) { - (void)proc, (void)rctx, (void)a2, (void)a3, (void)a4, (void)lockflags; + (void)proc, (void)rctx, (void)a2, (void)a3, (void)a4; if (a1 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -659,24 +660,16 @@ DEFINE_DEVICE_OP(idedrv_get_size) { } DEFINE_DEVICE_OP(idedrv_partition_rescan) { - uint64_t fsd; - struct list_node_link *subdevice_link, *tmp_subdevice_link; list_foreach(device->subdevices, subdevice_link, tmp_subdevice_link) { struct device* subdevice = list_entry(subdevice_link, struct device, subdevices_link); - spin_lock(&subdevice->lock, &fsd); list_remove(device->subdevices, &subdevice->subdevices_link); - spin_unlock(&subdevice->lock, fsd); device_delete(subdevice->key, proc, rctx); } - spin_unlock(&device->lock, *lockflags); - int r = device_probe_partitions(proc, rctx, device); - spin_lock(&device->lock, lockflags); - return r; } diff --git a/kernel/device/storage/partdrv.c b/kernel/device/storage/partdrv.c index 2ac6fde..7fe36ce 100644 --- a/kernel/device/storage/partdrv.c +++ b/kernel/device/storage/partdrv.c @@ -31,8 +31,6 @@ DEFINE_DEVICE_FINI(partdrv_fini) { } DEFINE_DEVICE_OP(partdrv_read) { - uint64_t fs; - if (a1 == NULL || a2 == NULL || a3 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -43,16 +41,12 @@ DEFINE_DEVICE_OP(partdrv_read) { size_t sector_count = *(size_t*)a2; uint8_t* buffer = a3; - spin_lock(&super->lock, &fs); - int ret = device_op(super, XDRV_READ, proc, rctx, &fs, §or, §or_count, buffer); - spin_unlock(&super->lock, fs); + int ret = device_op(super, XDRV_READ, proc, rctx, §or, §or_count, buffer); return ret; } DEFINE_DEVICE_OP(partdrv_write) { - uint64_t fs; - if (a1 == NULL || a2 == NULL || a3 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -63,9 +57,7 @@ DEFINE_DEVICE_OP(partdrv_write) { size_t sector_count = *(size_t*)a2; uint8_t* buffer = a3; - spin_lock(&super->lock, &fs); - int ret = device_op(super, XDRV_WRITE, proc, rctx, &fs, §or, §or_count, buffer); - spin_unlock(&super->lock, fs); + int ret = device_op(super, XDRV_WRITE, proc, rctx, §or, §or_count, buffer); return ret; } @@ -82,8 +74,6 @@ DEFINE_DEVICE_OP(partdrv_get_device_type) { } DEFINE_DEVICE_OP(partdrv_get_sector_size) { - uint64_t fs; - if (a1 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -91,9 +81,7 @@ DEFINE_DEVICE_OP(partdrv_get_sector_size) { struct partdrv* partdrv = device->udata; - spin_lock(&partdrv->super->lock, &fs); - device_op(partdrv->super, XDRV_GET_SECTOR_SIZE, proc, rctx, &fs, secsize); - spin_unlock(&partdrv->super->lock, fs); + device_op(partdrv->super, XDRV_GET_SECTOR_SIZE, proc, rctx, secsize); return ST_OK; } diff --git a/kernel/device/storage/partitions.c b/kernel/device/storage/partitions.c index 42f5b53..d597c71 100644 --- a/kernel/device/storage/partitions.c +++ b/kernel/device/storage/partitions.c @@ -14,8 +14,6 @@ static int device_probe_partitions_dos(struct proc* proc, struct reschedule_ctx* rctx, struct device* device) { - uint64_t fd, fsd; - struct dos_mbr mbr; memset(&mbr, 0, sizeof(mbr)); size_t sector = 0; @@ -31,19 +29,15 @@ static int device_probe_partitions_dos(struct proc* proc, struct reschedule_ctx* [XDRV_PARTITION_RESCAN] = &partdrv_partition_rescan, }; - spin_lock(&device->lock, &fd); + device_op(device, XDRV_GET_SECTOR_SIZE, proc, rctx, §or_size); - device_op(device, XDRV_GET_SECTOR_SIZE, proc, rctx, &fd, §or_size); - - int ret = device_op(device, XDRV_READ, proc, rctx, &fd, §or, §or_count, &mbr); + int ret = device_op(device, XDRV_READ, proc, rctx, §or, §or_count, &mbr); if (ret < 0) { - spin_unlock(&device->lock, fd); return ret; } if (!(mbr.valid_sign[0] == 0x55 && mbr.valid_sign[1] == 0xAA)) { - spin_unlock(&device->lock, fd); return -ST_PARTITION_ERROR; } @@ -63,13 +57,9 @@ static int device_probe_partitions_dos(struct proc* proc, struct reschedule_ctx* struct device* part_dev = device_create(DEVICE_TYPE_DRIVE, key, ops, lengthof(ops), &partdrv_init, &partdrv_fini, &init, proc, rctx); - spin_lock(&part_dev->lock, &fsd); list_append(device->subdevices, &part_dev->subdevices_link); - spin_unlock(&part_dev->lock, fsd); } - spin_unlock(&device->lock, fd); - return ST_OK; } diff --git a/kernel/device/storage/ramdrv.c b/kernel/device/storage/ramdrv.c index 27a2dab..e0335e0 100644 --- a/kernel/device/storage/ramdrv.c +++ b/kernel/device/storage/ramdrv.c @@ -116,24 +116,16 @@ DEFINE_DEVICE_OP(ramdrv_write) { } DEFINE_DEVICE_OP(ramdrv_partition_rescan) { - uint64_t fsd; - struct list_node_link *subdevice_link, *tmp_subdevice_link; list_foreach(device->subdevices, subdevice_link, tmp_subdevice_link) { struct device* subdevice = list_entry(subdevice_link, struct device, subdevices_link); - spin_lock(&subdevice->lock, &fsd); list_remove(device->subdevices, &subdevice->subdevices_link); - spin_unlock(&subdevice->lock, fsd); device_delete(subdevice->key, proc, rctx); } - spin_unlock(&device->lock, *lockflags); - int r = device_probe_partitions(proc, rctx, device); - spin_lock(&device->lock, lockflags); - return r; } diff --git a/kernel/device/storage/usbdrv.c b/kernel/device/storage/usbdrv.c index d9a7b5b..1510603 100644 --- a/kernel/device/storage/usbdrv.c +++ b/kernel/device/storage/usbdrv.c @@ -27,35 +27,30 @@ #define SCSI_WRITE10 0x2A static int usb_ms_send_cbw(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint8_t bulk_out_endpoint, uintptr_t cbw_phys, uint64_t* lockflags) { + uint8_t bulk_out_endpoint, uintptr_t cbw_phys) { return xhci_bulk_transfer(xhci, usb_device, bulk_out_endpoint, cbw_phys, - sizeof(struct usb_ms_cbw), lockflags); + sizeof(struct usb_ms_cbw)); } static int usb_ms_read_data(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint8_t bulk_in_endpoint, uintptr_t buffer_phys, size_t buffer_size, - uint64_t* lockflags) { - return xhci_bulk_transfer(xhci, usb_device, bulk_in_endpoint, buffer_phys, buffer_size, - lockflags); + uint8_t bulk_in_endpoint, uintptr_t buffer_phys, size_t buffer_size) { + return xhci_bulk_transfer(xhci, usb_device, bulk_in_endpoint, buffer_phys, buffer_size); } static int usb_ms_write_data(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint8_t bulk_out_endpoint, uintptr_t buffer_phys, size_t buffer_size, - uint64_t* lockflags) { - return xhci_bulk_transfer(xhci, usb_device, bulk_out_endpoint, buffer_phys, buffer_size, - lockflags); + uint8_t bulk_out_endpoint, uintptr_t buffer_phys, size_t buffer_size) { + return xhci_bulk_transfer(xhci, usb_device, bulk_out_endpoint, buffer_phys, buffer_size); } static int usb_ms_read_csw(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint8_t bulk_in_endpoint, uintptr_t csw_phys, uint64_t* lockflags) { - return xhci_bulk_transfer(xhci, usb_device, bulk_in_endpoint, csw_phys, sizeof(struct usb_ms_csw), - lockflags); + uint8_t bulk_in_endpoint, uintptr_t csw_phys) { + return xhci_bulk_transfer(xhci, usb_device, bulk_in_endpoint, csw_phys, + sizeof(struct usb_ms_csw)); } static int usb_ms_scsi_read_capacity(struct xhci* xhci, struct xhci_usb_device* usb_device, uint8_t bulk_in_endpoint, uint8_t bulk_out_endpoint, - size_t* sector_size, size_t* sector_count, - uint64_t* lockflags) { + size_t* sector_size, size_t* sector_count) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; int ret; @@ -83,15 +78,15 @@ static int usb_ms_scsi_read_capacity(struct xhci* xhci, struct xhci_usb_device* cbw->length = 8; memcpy(cbw->data, cdb, sizeof(cdb)); - if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys, lockflags)) < 0) { + if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys)) < 0) { goto done; } - if ((ret = usb_ms_read_data(xhci, usb_device, bulk_in_endpoint, data_phys, 8, lockflags)) < 0) { + if ((ret = usb_ms_read_data(xhci, usb_device, bulk_in_endpoint, data_phys, 8)) < 0) { goto done; } - if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys, lockflags)) < 0) { + if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys)) < 0) { goto done; } @@ -118,8 +113,7 @@ done: } static int usb_ms_scsi_test_unit_ready(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint8_t bulk_in_endpoint, uint8_t bulk_out_endpoint, - uint64_t* lockflags) { + uint8_t bulk_in_endpoint, uint8_t bulk_out_endpoint) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; int ret; @@ -143,11 +137,11 @@ static int usb_ms_scsi_test_unit_ready(struct xhci* xhci, struct xhci_usb_device cbw->length = 0; memcpy(cbw->data, cdb, sizeof(cdb)); - if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys, lockflags)) < 0) { + if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys)) < 0) { goto done; } - if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys, lockflags)) < 0) { + if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys)) < 0) { goto done; } @@ -167,7 +161,7 @@ done: static int usb_ms_scsi_read(struct xhci* xhci, struct xhci_usb_device* usb_device, uint8_t bulk_in_endpoint, uint8_t bulk_out_endpoint, uint8_t* out_buffer, uint32_t lba, uint16_t sector_count, - size_t sector_size, uint64_t* lockflags) { + size_t sector_size) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; int ret; @@ -207,16 +201,16 @@ static int usb_ms_scsi_read(struct xhci* xhci, struct xhci_usb_device* usb_devic cbw->length = sector_count * sector_size; memcpy(cbw->data, cdb, sizeof(cdb)); - if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys, lockflags)) < 0) { + if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys)) < 0) { goto done; } if ((ret = usb_ms_read_data(xhci, usb_device, bulk_in_endpoint, data_phys, - sector_count * sector_size, lockflags)) < 0) { + sector_count * sector_size)) < 0) { goto done; } - if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys, lockflags)) < 0) { + if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys)) < 0) { goto done; } @@ -240,7 +234,7 @@ done: static int usb_ms_scsi_write(struct xhci* xhci, struct xhci_usb_device* usb_device, uint8_t bulk_in_endpoint, uint8_t bulk_out_endpoint, uint8_t* in_buffer, uint32_t lba, uint16_t sector_count, - size_t sector_size, uint64_t* lockflags) { + size_t sector_size) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; int ret; @@ -277,16 +271,16 @@ static int usb_ms_scsi_write(struct xhci* xhci, struct xhci_usb_device* usb_devi cbw->length = sector_count * sector_size; memcpy(cbw->data, cdb, sizeof(cdb)); - if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys, lockflags)) < 0) { + if ((ret = usb_ms_send_cbw(xhci, usb_device, bulk_out_endpoint, cbw_phys)) < 0) { goto done; } if ((ret = usb_ms_write_data(xhci, usb_device, bulk_out_endpoint, buffer_phys, - sector_count * sector_size, lockflags)) < 0) { + sector_count * sector_size)) < 0) { goto done; } - if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys, lockflags)) < 0) { + if ((ret = usb_ms_read_csw(xhci, usb_device, bulk_in_endpoint, csw_phys)) < 0) { goto done; } @@ -333,8 +327,6 @@ static void usbdrv_setup_endpoints(struct usbdrv* usbdrv) { } DEFINE_DEVICE_OP(usbdrv_read) { - uint64_t fd; - if (a1 == NULL || a2 == NULL || a3 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -344,24 +336,13 @@ DEFINE_DEVICE_OP(usbdrv_read) { struct usbdrv* usbdrv = device->udata; - spin_unlock(&device->lock, *lockflags); - spin_lock(&usbdrv->xhci->device->lock, &fd); - spin_lock(&device->lock, lockflags); - int ret = usb_ms_scsi_read(usbdrv->xhci, usbdrv->usb_device, usbdrv->bulk_in.endpoint_addr, usbdrv->bulk_out.endpoint_addr, buffer, sector, sector_count, - usbdrv->sector_size, &fd); - - spin_unlock(&device->lock, *lockflags); - spin_unlock(&usbdrv->xhci->device->lock, fd); - spin_lock(&device->lock, lockflags); - + usbdrv->sector_size); return ret; } DEFINE_DEVICE_OP(usbdrv_write) { - uint64_t fd; - if (a1 == NULL || a2 == NULL || a3 == NULL) return -ST_BAD_ADDRESS_SPACE; @@ -371,18 +352,9 @@ DEFINE_DEVICE_OP(usbdrv_write) { struct usbdrv* usbdrv = device->udata; - spin_unlock(&device->lock, *lockflags); - spin_lock(&usbdrv->xhci->device->lock, &fd); - spin_lock(&device->lock, lockflags); - int ret = usb_ms_scsi_write(usbdrv->xhci, usbdrv->usb_device, usbdrv->bulk_in.endpoint_addr, usbdrv->bulk_out.endpoint_addr, buffer, sector, sector_count, - usbdrv->sector_size, &fd); - - spin_unlock(&device->lock, *lockflags); - spin_unlock(&usbdrv->xhci->device->lock, fd); - spin_lock(&device->lock, lockflags); - + usbdrv->sector_size); return ret; } @@ -424,30 +396,21 @@ DEFINE_DEVICE_OP(usbdrv_get_size) { } DEFINE_DEVICE_OP(usbdrv_partition_rescan) { - uint64_t fsd; - struct list_node_link *subdevice_link, *tmp_subdevice_link; list_foreach(device->subdevices, subdevice_link, tmp_subdevice_link) { struct device* subdevice = list_entry(subdevice_link, struct device, subdevices_link); - spin_lock(&subdevice->lock, &fsd); list_remove(device->subdevices, &subdevice->subdevices_link); - spin_unlock(&subdevice->lock, fsd); device_delete(subdevice->key, proc, rctx); } - spin_unlock(&device->lock, *lockflags); - int r = device_probe_partitions(proc, rctx, device); - spin_lock(&device->lock, lockflags); - return r; } DEFINE_DEVICE_INIT(usbdrv_init) { - uint64_t fd; int ret; struct usbdrv_init* init = arg; @@ -462,39 +425,33 @@ DEFINE_DEVICE_INIT(usbdrv_init) { device->udata = usbdrv; - spin_lock(&usbdrv->xhci->device->lock, &fd); - usbdrv_setup_endpoints(usbdrv); ret = usb_ms_scsi_test_unit_ready(usbdrv->xhci, usbdrv->usb_device, usbdrv->bulk_in.endpoint_addr, - usbdrv->bulk_out.endpoint_addr, init->lockflags); + usbdrv->bulk_out.endpoint_addr); if (ret < 0) { stall_ms(100); ret = usb_ms_scsi_test_unit_ready(usbdrv->xhci, usbdrv->usb_device, usbdrv->bulk_in.endpoint_addr, - usbdrv->bulk_out.endpoint_addr, init->lockflags); + usbdrv->bulk_out.endpoint_addr); } if (ret < 0) { - spin_unlock(&usbdrv->xhci->device->lock, fd); free(usbdrv); return false; } ret = usb_ms_scsi_read_capacity(usbdrv->xhci, usbdrv->usb_device, usbdrv->bulk_in.endpoint_addr, usbdrv->bulk_out.endpoint_addr, &usbdrv->sector_size, - &usbdrv->sector_count, init->lockflags); + &usbdrv->sector_count); if (ret < 0) { - spin_unlock(&usbdrv->xhci->device->lock, fd); free(usbdrv); return false; } - spin_unlock(&usbdrv->xhci->device->lock, fd); - DEBUG("sector_count = %zu, sector_size = %u\n", usbdrv->sector_count, usbdrv->sector_size); return true; diff --git a/kernel/device/storage/usbdrv.h b/kernel/device/storage/usbdrv.h index 5e92c63..222547e 100644 --- a/kernel/device/storage/usbdrv.h +++ b/kernel/device/storage/usbdrv.h @@ -34,7 +34,6 @@ struct device; struct usbdrv_init { struct xhci* xhci; struct xhci_usb_device* usb_device; - uint64_t* lockflags; }; struct usbdrv { diff --git a/kernel/device/sys/debugconsole.c b/kernel/device/sys/debugconsole.c index b3ef2cd..d606d47 100644 --- a/kernel/device/sys/debugconsole.c +++ b/kernel/device/sys/debugconsole.c @@ -14,17 +14,13 @@ DEFINE_DEVICE_INIT(debugconsole_init) { DEFINE_DEVICE_FINI(debugconsole_fini) {} DEFINE_DEVICE_OP(debugconsole_putstr) { - uint64_t fp; - char* string = (char*)a1; size_t* len = (size_t*)a2; if (string == NULL || len == NULL) return -ST_BAD_ADDRESS_SPACE; - spin_lock(&proc->lock, &fp); int pid = proc->pid; - spin_unlock(&proc->lock, fp); debugprintf("(CPU %d; PID %d) %.*s", thiscpu->id, pid, (int)*len, string); diff --git a/kernel/device/usb/usb.c b/kernel/device/usb/usb.c index 2ee8e19..fe0a0bd 100644 --- a/kernel/device/usb/usb.c +++ b/kernel/device/usb/usb.c @@ -15,8 +15,7 @@ static atomic_int usb_ms_counter = 0; static struct device* usb_ms_init(struct xhci* xhci, struct xhci_usb_device* usb_device, - struct proc* proc, struct reschedule_ctx* rctx, - uint64_t* lockflags) { + struct proc* proc, struct reschedule_ctx* rctx) { static device_op_func_t ops[] = { [XDRV_GET_SIZE] = &usbdrv_get_size, [XDRV_GET_SECTOR_SIZE] = &usbdrv_get_sector_size, @@ -29,7 +28,6 @@ static struct device* usb_ms_init(struct xhci* xhci, struct xhci_usb_device* usb struct usbdrv_init init = { .xhci = xhci, .usb_device = usb_device, - .lockflags = lockflags, }; char key[30]; diff --git a/kernel/device/usb/usb.h b/kernel/device/usb/usb.h index aee63bf..e49915f 100644 --- a/kernel/device/usb/usb.h +++ b/kernel/device/usb/usb.h @@ -88,7 +88,7 @@ struct usb_driver_info { uint8_t if_subclass; uint8_t if_proto; struct device* (*init)(struct xhci* xhci, struct xhci_usb_device* usb_device, struct proc* proc, - struct reschedule_ctx* rctx, uint64_t* lockflags); + struct reschedule_ctx* rctx); }; extern struct usb_driver_info usb_driver_infos[USB_DRIVER_MAX_MATCHES]; diff --git a/kernel/device/usb/xhci.c b/kernel/device/usb/xhci.c index 80c08bd..8995a91 100644 --- a/kernel/device/usb/xhci.c +++ b/kernel/device/usb/xhci.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -324,12 +325,8 @@ static void xhci_poll_events(struct xhci* xhci) { static void xhci_irq(void* arg, void* regs, bool user, struct reschedule_ctx* rctx) { (void)user, (void)regs, (void)rctx; - uint64_t fd; - struct xhci* xhci = arg; - spin_lock(&xhci->device->lock, &fd); - uintptr_t ir_base = xhci->xhci_runtime_base + 0x20; /* ack */ @@ -337,8 +334,6 @@ static void xhci_irq(void* arg, void* regs, bool user, struct reschedule_ctx* rc xhci_write32(xhci->xhci_oper_base, XHCI_USBSTS, (1 << 3)); xhci_poll_events(xhci); - - spin_unlock(&xhci->device->lock, fd); } static void xhci_ring_put_trb(struct xhci_ring* ring, struct xhci_trb put_trb) { @@ -369,8 +364,7 @@ static void xhci_ring_put_trb(struct xhci_ring* ring, struct xhci_trb put_trb) { static bool xhci_endpoint0_ctrl_in(struct xhci* xhci, struct xhci_usb_device* usb_device, uint8_t request_type, uint8_t request, uint16_t value, - uint16_t index, uintptr_t data_phys, uint16_t length, - uint64_t* lockflags) { + uint16_t index, uintptr_t data_phys, uint16_t length) { /* clang-format off */ uint64_t setup = ((uint64_t)length << XHCI_SSTRB_PARAM_WLENGTH) | ((uint64_t)index << XHCI_SSTRB_PARAM_WINDEX) @@ -417,14 +411,15 @@ static bool xhci_endpoint0_ctrl_in(struct xhci* xhci, struct xhci_usb_device* us int timeout = 100; - spin_unlock(&xhci->device->lock, *lockflags); - xhci_write32(xhci->xhci_doorbell_base, usb_device->slot_id * 4, 1); - while (atomic_load(&usb_device->endpoint0_ring.pending) && --timeout > 0) + while (atomic_load(&usb_device->endpoint0_ring.pending) && --timeout > 0) { + biglock_unlock(); + stall_ms(1); - spin_lock(&xhci->device->lock, lockflags); + biglock_lock(); + } if (timeout == 0) { DEBUG("timed out\n"); @@ -436,8 +431,7 @@ static bool xhci_endpoint0_ctrl_in(struct xhci* xhci, struct xhci_usb_device* us static bool xhci_endpoint0_ctrl_out(struct xhci* xhci, struct xhci_usb_device* usb_device, uint8_t request_type, uint8_t request, uint16_t value, - uint16_t index, uintptr_t data_phys, uint16_t length, - uint64_t* lockflags) { + uint16_t index, uintptr_t data_phys, uint16_t length) { /* clang-format off */ uint64_t setup = ((uint64_t)length << XHCI_SSTRB_PARAM_WLENGTH) | ((uint64_t)index << XHCI_SSTRB_PARAM_WINDEX) @@ -493,14 +487,15 @@ static bool xhci_endpoint0_ctrl_out(struct xhci* xhci, struct xhci_usb_device* u int timeout = 100; - spin_unlock(&xhci->device->lock, *lockflags); - xhci_write32(xhci->xhci_doorbell_base, usb_device->slot_id * 4, 1); - while (atomic_load(&usb_device->endpoint0_ring.pending) && --timeout > 0) + while (atomic_load(&usb_device->endpoint0_ring.pending) && --timeout > 0) { + biglock_unlock(); + stall_ms(1); - spin_lock(&xhci->device->lock, lockflags); + biglock_lock(); + } if (timeout == 0) { DEBUG("timed out\n"); @@ -510,8 +505,7 @@ static bool xhci_endpoint0_ctrl_out(struct xhci* xhci, struct xhci_usb_device* u return usb_device->endpoint0_ring.last_cmpl_code == 1; } -static void xhci_send_cmd(struct xhci* xhci, uint64_t param, uint32_t status, uint32_t ctrl, - uint64_t* lockflags) { +static void xhci_send_cmd(struct xhci* xhci, uint64_t param, uint32_t status, uint32_t ctrl) { if (xhci->cmd_ring.idx == (xhci->cmd_ring.size - 1)) { struct xhci_trb* link = &xhci->cmd_ring.trbs[xhci->cmd_ring.idx]; link->param = xhci->cmd_ring.phys; @@ -541,14 +535,15 @@ static void xhci_send_cmd(struct xhci* xhci, uint64_t param, uint32_t status, ui int timeout = 100; - spin_unlock(&xhci->device->lock, *lockflags); - xhci_write32(xhci->xhci_doorbell_base, 0, 0); - while (atomic_load(&xhci->event_ring.pending) && --timeout > 0) + while (atomic_load(&xhci->event_ring.pending) && --timeout > 0) { + biglock_unlock(); + stall_ms(1); - spin_lock(&xhci->device->lock, lockflags); + biglock_lock(); + } if (timeout == 0) DEBUG("timed out\n"); @@ -561,8 +556,7 @@ static uint8_t xhci_get_device_ctx_idx(struct usb_endpoint_desc* endpoint) { } static bool xhci_usb_device_setup_init_endpoints(struct xhci* xhci, - struct xhci_usb_device* usb_device, - uint64_t* lockflags) { + struct xhci_usb_device* usb_device) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; uintptr_t input_ctx_phys = pmm_alloc(1); @@ -682,7 +676,7 @@ static bool xhci_usb_device_setup_init_endpoints(struct xhci* xhci, } uint32_t ctrl = (usb_device->slot_id << 24) | (XHCI_TRB_CFG_ENDP_CMD << XHCI_GTRB_TRB_TYPE); - xhci_send_cmd(xhci, input_ctx_phys, 0, ctrl, lockflags); + xhci_send_cmd(xhci, input_ctx_phys, 0, ctrl); pmm_free(input_ctx_phys, 1); @@ -696,8 +690,8 @@ static bool xhci_usb_device_setup_init_endpoints(struct xhci* xhci, } static bool xhci_usb_device_setup_set_config(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint8_t config_val, uint64_t* lockflags) { - bool ok = xhci_endpoint0_ctrl_out(xhci, usb_device, 0x00, 0x09, config_val, 0, 0, 0, lockflags); + uint8_t config_val) { + bool ok = xhci_endpoint0_ctrl_out(xhci, usb_device, 0x00, 0x09, config_val, 0, 0, 0); if (!ok) { DEBUG("Failed to set the config!\n"); @@ -709,8 +703,8 @@ static bool xhci_usb_device_setup_set_config(struct xhci* xhci, struct xhci_usb_ return true; } -static bool xhci_usb_device_setup_addressing(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint64_t* lockflags) { +static bool xhci_usb_device_setup_addressing(struct xhci* xhci, + struct xhci_usb_device* usb_device) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; struct xhci_port* xhci_port; @@ -722,7 +716,7 @@ static bool xhci_usb_device_setup_addressing(struct xhci* xhci, struct xhci_usb_ uint32_t speed = (portsc >> XHCI_PORTSC_PORTSPEED) & 0x0F; xhci->event_ring.last_cmpl_code = 0; - xhci_send_cmd(xhci, 0, 0, XHCI_TRB_SLOT_ENAB_CMD << XHCI_GTRB_TRB_TYPE, lockflags); + xhci_send_cmd(xhci, 0, 0, XHCI_TRB_SLOT_ENAB_CMD << XHCI_GTRB_TRB_TYPE); if (xhci->event_ring.last_cmpl_code != 1) { DEBUG("Enable slot failed\n"); @@ -784,7 +778,7 @@ static bool xhci_usb_device_setup_addressing(struct xhci* xhci, struct xhci_usb_ xhci->event_ring.last_cmpl_code = 0; uint32_t ctrl = (usb_device->slot_id << 24) | (XHCI_TRB_ADDR_DEV_CMD << XHCI_GTRB_TRB_TYPE); - xhci_send_cmd(xhci, input_ctx_phys, 0, ctrl, lockflags); + xhci_send_cmd(xhci, input_ctx_phys, 0, ctrl); pmm_free(input_ctx_phys, 1); @@ -799,8 +793,7 @@ static bool xhci_usb_device_setup_addressing(struct xhci* xhci, struct xhci_usb_ } } -static bool xhci_usb_device_setup_get_info(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint64_t* lockflags) { +static bool xhci_usb_device_setup_get_info(struct xhci* xhci, struct xhci_usb_device* usb_device) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; uintptr_t response_buf_phys = pmm_alloc(1); @@ -808,8 +801,7 @@ static bool xhci_usb_device_setup_get_info(struct xhci* xhci, struct xhci_usb_de void* response_buf = (void*)(response_buf_phys + (uintptr_t)hhdm->offset); memset(response_buf, 0, PAGE_SIZE); - bool ok = xhci_endpoint0_ctrl_in(xhci, usb_device, 0x80, 6, (1 << 8), 0, response_buf_phys, 18, - lockflags); + bool ok = xhci_endpoint0_ctrl_in(xhci, usb_device, 0x80, 6, (1 << 8), 0, response_buf_phys, 18); if (!ok) { pmm_free(response_buf_phys, 1); @@ -828,8 +820,8 @@ static bool xhci_usb_device_setup_get_info(struct xhci* xhci, struct xhci_usb_de return true; } -static bool xhci_usb_device_setup_get_config(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint64_t* lockflags) { +static bool xhci_usb_device_setup_get_config(struct xhci* xhci, + struct xhci_usb_device* usb_device) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; uintptr_t response_buf_phys = pmm_alloc(1); @@ -837,8 +829,7 @@ static bool xhci_usb_device_setup_get_config(struct xhci* xhci, struct xhci_usb_ void* response_buf = (void*)(response_buf_phys + (uintptr_t)hhdm->offset); memset(response_buf, 0, PAGE_SIZE); - bool ok = xhci_endpoint0_ctrl_in(xhci, usb_device, 0x80, 6, (2 << 8), 0, response_buf_phys, 9, - lockflags); + bool ok = xhci_endpoint0_ctrl_in(xhci, usb_device, 0x80, 6, (2 << 8), 0, response_buf_phys, 9); if (!ok) { pmm_free(response_buf_phys, 1); @@ -858,8 +849,7 @@ static bool xhci_usb_device_setup_get_config(struct xhci* xhci, struct xhci_usb_ } static bool xhci_usb_device_setup_get_config_full(struct xhci* xhci, - struct xhci_usb_device* usb_device, - uint64_t* lockflags) { + struct xhci_usb_device* usb_device) { struct limine_hhdm_response* hhdm = limine_hhdm_request.response; uintptr_t response_buf_phys = pmm_alloc(1); @@ -868,7 +858,7 @@ static bool xhci_usb_device_setup_get_config_full(struct xhci* xhci, memset(response_buf, 0, PAGE_SIZE); bool ok = xhci_endpoint0_ctrl_in(xhci, usb_device, 0x80, 6, (2 << 8), 0, response_buf_phys, - usb_device->config_desc.total_length, lockflags); + usb_device->config_desc.total_length); if (!ok) { pmm_free(response_buf_phys, 1); @@ -927,8 +917,7 @@ static bool xhci_usb_device_setup_get_config_full(struct xhci* xhci, return true; } -static void xhci_poll_setup_init_ifs(struct xhci* xhci, struct xhci_usb_device* usb_device, - uint64_t* lockflags) { +static void xhci_poll_setup_init_ifs(struct xhci* xhci, struct xhci_usb_device* usb_device) { struct reschedule_ctx rctx; memset(&rctx, 0, sizeof(rctx)); @@ -941,11 +930,7 @@ static void xhci_poll_setup_init_ifs(struct xhci* xhci, struct xhci_usb_device* if (if_->desc.if_class == info->if_class && if_->desc.if_subclass == info->if_subclass && if_->desc.if_proto == info->if_proto) { - spin_unlock(&xhci->device->lock, *lockflags); - - struct device* device = info->init(xhci, usb_device, thiscpu->kproc, &rctx, lockflags); - - spin_lock(&xhci->device->lock, lockflags); + struct device* device = info->init(xhci, usb_device, thiscpu->kproc, &rctx); if (device == NULL) DEBUG("USB driver failed to initialize. Skipping device!\n"); @@ -956,7 +941,7 @@ static void xhci_poll_setup_init_ifs(struct xhci* xhci, struct xhci_usb_device* } } -static void xhci_poll_setup_devices(struct xhci* xhci, uint64_t* lockflags) { +static void xhci_poll_setup_devices(struct xhci* xhci) { struct list_node_link *usb_device_link, *tmp_usb_device_link; bool ok; @@ -968,26 +953,25 @@ static void xhci_poll_setup_devices(struct xhci* xhci, uint64_t* lockflags) { if (usb_device->slot_id != -1) continue; - ok = xhci_usb_device_setup_addressing(xhci, usb_device, lockflags); + ok = xhci_usb_device_setup_addressing(xhci, usb_device); if (ok) - ok = xhci_usb_device_setup_get_info(xhci, usb_device, lockflags); + ok = xhci_usb_device_setup_get_info(xhci, usb_device); if (ok) - ok = xhci_usb_device_setup_get_config(xhci, usb_device, lockflags); + ok = xhci_usb_device_setup_get_config(xhci, usb_device); if (ok) - ok = xhci_usb_device_setup_get_config_full(xhci, usb_device, lockflags); + ok = xhci_usb_device_setup_get_config_full(xhci, usb_device); if (ok) - ok = xhci_usb_device_setup_set_config(xhci, usb_device, usb_device->config_desc.config_value, - lockflags); + ok = xhci_usb_device_setup_set_config(xhci, usb_device, usb_device->config_desc.config_value); if (ok) - ok = xhci_usb_device_setup_init_endpoints(xhci, usb_device, lockflags); + ok = xhci_usb_device_setup_init_endpoints(xhci, usb_device); if (ok) - xhci_poll_setup_init_ifs(xhci, usb_device, lockflags); + xhci_poll_setup_init_ifs(xhci, usb_device); } } @@ -1022,7 +1006,7 @@ static void xhci_poll_port_changes(struct xhci* xhci, struct proc* proc, } int xhci_bulk_transfer(struct xhci* xhci, struct xhci_usb_device* usb_device, uint8_t endpoint_addr, - uintptr_t buffer_phys, size_t buffer_size, uint64_t* lockflags) { + uintptr_t buffer_phys, size_t buffer_size) { struct xhci_usb_device_endpoint* endpoint = NULL; for (size_t ep = 0; ep < usb_device->endpoints_count; ep++) { @@ -1076,14 +1060,15 @@ int xhci_bulk_transfer(struct xhci* xhci, struct xhci_usb_device* usb_device, ui int timeout = 100; - spin_unlock(&xhci->device->lock, *lockflags); - xhci_write32(xhci->xhci_doorbell_base, usb_device->slot_id * 4, dci); - while (atomic_load(&endpoint->transfer_ring.pending) && --timeout > 0) + while (atomic_load(&endpoint->transfer_ring.pending) && --timeout > 0) { + biglock_unlock(); + stall_ms(1); - spin_lock(&xhci->device->lock, lockflags); + biglock_lock(); + } if (timeout == 0) { DEBUG("bulk transfer timed out\n"); @@ -1097,7 +1082,7 @@ DEFINE_DEVICE_OP(xhci_poll_driver) { struct xhci* xhci = device->udata; xhci_poll_port_changes(xhci, proc, rctx); - xhci_poll_setup_devices(xhci, lockflags); + xhci_poll_setup_devices(xhci); return ST_OK; } diff --git a/kernel/device/usb/xhci.h b/kernel/device/usb/xhci.h index ec11b7f..5a7e6bd 100644 --- a/kernel/device/usb/xhci.h +++ b/kernel/device/usb/xhci.h @@ -430,7 +430,7 @@ struct xhci { }; int xhci_bulk_transfer(struct xhci* xhci, struct xhci_usb_device* usb_device, uint8_t endpoint_addr, - uintptr_t buffer_phys, size_t buffer_size, uint64_t* lockflags); + uintptr_t buffer_phys, size_t buffer_size); DEFINE_DEVICE_INIT(xhci_init); diff --git a/kernel/fs/fatfs/fatfs.c b/kernel/fs/fatfs/fatfs.c index 79bad5b..defdc2c 100644 --- a/kernel/fs/fatfs/fatfs.c +++ b/kernel/fs/fatfs/fatfs.c @@ -20,8 +20,6 @@ static int fat1_diskio_read(struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer, uint32_t sector_count) { - uint64_t fd; - struct vfs_volume* volume = ctx->udata; struct device* back_device = volume->back_device; @@ -32,33 +30,25 @@ static int fat1_diskio_read(struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buf size_t phys_sector, phys_sector_count; int ret; - spin_lock(&back_device->lock, &fd); - - ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, ctx->proc, ctx->rctx, &fd, §or_size); + ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, ctx->proc, ctx->rctx, §or_size); if (ret < 0) { - spin_unlock(&back_device->lock, fd); return 0; } vfs_translate(sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector, &phys_sector_count); - ret = device_op(back_device, XDRV_READ, ctx->proc, ctx->rctx, &fd, &phys_sector, - &phys_sector_count, buffer); + ret = device_op(back_device, XDRV_READ, ctx->proc, ctx->rctx, &phys_sector, &phys_sector_count, + buffer); if (ret < 0) { - spin_unlock(&back_device->lock, fd); return 0; } - spin_unlock(&back_device->lock, fd); - return 1; } static int fat1_diskio_write(struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer, uint32_t sector_count) { - uint64_t fd; - struct vfs_volume* volume = ctx->udata; struct device* back_device = volume->back_device; @@ -69,26 +59,20 @@ static int fat1_diskio_write(struct fatfs_ctx* ctx, uint32_t sector, uint8_t* bu size_t phys_sector, phys_sector_count; int ret; - spin_lock(&back_device->lock, &fd); - - ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, ctx->proc, ctx->rctx, &fd, §or_size); + ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, ctx->proc, ctx->rctx, §or_size); if (ret < 0) { - spin_unlock(&back_device->lock, fd); return 0; } vfs_translate(sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector, &phys_sector_count); - ret = device_op(back_device, XDRV_WRITE, ctx->proc, ctx->rctx, &fd, &phys_sector, - &phys_sector_count, buffer); + ret = device_op(back_device, XDRV_WRITE, ctx->proc, ctx->rctx, &phys_sector, &phys_sector_count, + buffer); if (ret < 0) { - spin_unlock(&back_device->lock, fd); return 0; } - spin_unlock(&back_device->lock, fd); - return 1; } @@ -131,17 +115,13 @@ DEFINE_VFS_UNMOUNT(fatfs_unmount) { } DEFINE_VFS_FORMAT(fatfs16_format) { - uint64_t fd; - struct fatfs_ctx* fatfs_ctx = volume->udata; struct device* back_device = volume->back_device; size_t total_size; fatfs_ctx->proc = proc; fatfs_ctx->rctx = rctx; - spin_lock(&back_device->lock, &fd); - device_op(back_device, XDRV_GET_SIZE, proc, rctx, &fd, &total_size); - spin_unlock(&back_device->lock, fd); + device_op(back_device, XDRV_GET_SIZE, proc, rctx, &total_size); size_t sectors = div_align_up(total_size, FAT_SECTOR_SIZE); int r = fatfs_format_fat16(fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat16"); @@ -149,17 +129,13 @@ DEFINE_VFS_FORMAT(fatfs16_format) { } DEFINE_VFS_FORMAT(fatfs32_format) { - uint64_t fd; - struct fatfs_ctx* fatfs_ctx = volume->udata; struct device* back_device = volume->back_device; size_t total_size; fatfs_ctx->proc = proc; fatfs_ctx->rctx = rctx; - spin_lock(&back_device->lock, &fd); - device_op(back_device, XDRV_GET_SIZE, proc, rctx, &fd, &total_size); - spin_unlock(&back_device->lock, fd); + device_op(back_device, XDRV_GET_SIZE, proc, rctx, &total_size); size_t sectors = div_align_up(total_size, FAT_SECTOR_SIZE); int r = fatfs_format_fat32(fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat32"); diff --git a/kernel/fs/iso9660fs/iso9660fs.c b/kernel/fs/iso9660fs/iso9660fs.c index 2615bd2..ec86e76 100644 --- a/kernel/fs/iso9660fs/iso9660fs.c +++ b/kernel/fs/iso9660fs/iso9660fs.c @@ -17,7 +17,6 @@ #define ISO9660_SECTOR_SIZE 2048 static bool iso9660fs_read_sector(l9660_fs* fs, void* buf, uint32_t sector) { - uint64_t fd; struct vfs_volume* volume = fs->udata; struct device* back_device = volume->back_device; @@ -25,26 +24,20 @@ static bool iso9660fs_read_sector(l9660_fs* fs, void* buf, uint32_t sector) { size_t phys_sector, phys_sector_count; int ret; - spin_lock(&back_device->lock, &fd); - - ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, fs->proc, fs->rctx, &fd, §or_size); + ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, fs->proc, fs->rctx, §or_size); if (ret < 0) { - spin_unlock(&back_device->lock, fd); return false; } vfs_translate(sector, 1, ISO9660_SECTOR_SIZE, sector_size, &phys_sector, &phys_sector_count); - ret = device_op(back_device, XDRV_READ, fs->proc, fs->rctx, &fd, &phys_sector, &phys_sector_count, - buf); + ret = + device_op(back_device, XDRV_READ, fs->proc, fs->rctx, &phys_sector, &phys_sector_count, buf); if (ret < 0) { - spin_unlock(&back_device->lock, fd); return false; } - spin_unlock(&back_device->lock, fd); - return true; } diff --git a/kernel/fs/tarfs/tarfs.c b/kernel/fs/tarfs/tarfs.c index 7622db4..77db872 100644 --- a/kernel/fs/tarfs/tarfs.c +++ b/kernel/fs/tarfs/tarfs.c @@ -66,8 +66,6 @@ static size_t tar_parse(struct tarfs* tarfs, uint8_t* addr, size_t max_size) { } DEFINE_VFS_MOUNT(tarfs_mount) { - uint64_t fd; - struct tarfs* tarfs = malloc(sizeof(*tarfs)); if (tarfs == NULL) @@ -81,18 +79,14 @@ DEFINE_VFS_MOUNT(tarfs_mount) { size_t total_size, sector_size; int ret; - spin_lock(&back_device->lock, &fd); - - ret = device_op(back_device, XDRV_GET_SIZE, proc, rctx, &fd, &total_size); + ret = device_op(back_device, XDRV_GET_SIZE, proc, rctx, &total_size); if (ret < 0) { - spin_unlock(&back_device->lock, fd); free(volume->udata); return ret; } - ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, proc, rctx, &fd, §or_size); + ret = device_op(back_device, XDRV_GET_SECTOR_SIZE, proc, rctx, §or_size); if (ret < 0) { - spin_unlock(&back_device->lock, fd); free(volume->udata); return ret; } @@ -100,7 +94,6 @@ DEFINE_VFS_MOUNT(tarfs_mount) { uint8_t* buffer = malloc(total_size); if (buffer == NULL) { - spin_unlock(&back_device->lock, fd); free(volume->udata); volume->udata = NULL; return ret; @@ -109,11 +102,9 @@ DEFINE_VFS_MOUNT(tarfs_mount) { size_t sector_count = 1; for (size_t sector = 0; sector < total_size / sector_size; sector++) { uint8_t* dest = (uint8_t*)((uintptr_t)buffer + (sector * sector_size)); - ret = device_op(back_device, XDRV_READ, proc, rctx, &fd, §or, §or_count, dest); + ret = device_op(back_device, XDRV_READ, proc, rctx, §or, §or_count, dest); } - spin_unlock(&back_device->lock, fd); - if (ret < 0) { free(buffer); free(volume->udata); diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c index 0d414be..1526bd6 100644 --- a/kernel/fs/vfs.c +++ b/kernel/fs/vfs.c @@ -24,13 +24,9 @@ static struct vfs_volume_table volume_table; size_t volume_populate_volume_infos(struct volume_info* infos, size_t count) { - uint64_t fvt, fv, fd; - if (count >= lengthof(volume_table.volume_buckets)) count = lengthof(volume_table.volume_buckets); - spin_lock(&volume_table.lock, &fvt); - size_t j = 0; for (size_t i = 0; i < lengthof(volume_table.volume_buckets); i++) { @@ -40,36 +36,24 @@ size_t volume_populate_volume_infos(struct volume_info* infos, size_t count) { struct vfs_volume* volume = hash_entry(hash_link, struct vfs_volume, volume_table_link); struct volume_info* info = &infos[j]; - spin_lock(&volume->lock, &fv); - spin_lock(&volume->back_device->lock, &fd); - memcpy(info->volume_name, volume->key, sizeof(info->volume_name)); memcpy(info->device_key, volume->back_device->key, sizeof(info->device_key)); - spin_unlock(&volume->back_device->lock, fd); - spin_unlock(&volume->lock, fv); - hash_link = hash_link->next; j++; } } - spin_unlock(&volume_table.lock, fvt); - return j; } static struct vfs_volume* vfs_find_volume(const char* volume) { - uint64_t fvt; - struct hash_node_link* found_link = NULL; size_t volume_len = strlen_null(volume); uint32_t hash = hash_fnv32(volume, volume_len); - spin_lock(&volume_table.lock, &fvt); hash_find(&volume_table, volume, volume_len, hash, lengthof(volume_table.volume_buckets), volume_buckets, struct vfs_volume, volume_table_link, key, found_link); - spin_unlock(&volume_table.lock, fvt); if (found_link == NULL) return NULL; @@ -79,8 +63,6 @@ static struct vfs_volume* vfs_find_volume(const char* volume) { int vfs_create_volume(struct proc* proc, struct reschedule_ctx* rctx, const char* key, int fs_type, struct device* back_device, bool format) { - uint64_t fvt; - if (strlen_null(key) > VOLUME_MAX) return -ST_OOB_ERROR; @@ -94,7 +76,6 @@ int vfs_create_volume(struct proc* proc, struct reschedule_ctx* rctx, const char memcpy(volume->key, key, strlen_null(key)); volume->fs_type = fs_type; volume->back_device = back_device; - volume->lock = SPIN_LOCK_INIT; switch (volume->fs_type) { case FS_TARFS: @@ -159,45 +140,31 @@ int vfs_create_volume(struct proc* proc, struct reschedule_ctx* rctx, const char uint32_t mp_hash = hash_fnv32(volume->key, strlen_null(volume->key)); - spin_lock(&volume_table.lock, &fvt); - hash_insert(&volume_table, &volume->volume_table_link, mp_hash, lengthof(volume_table.volume_buckets), volume_buckets); - spin_unlock(&volume_table.lock, fvt); - return ST_OK; } int vfs_volume_delete(const char* key) { - uint64_t fv, fvt; - struct hash_node_link* found_link = NULL; size_t key_len = strlen_null(key); uint32_t hash = hash_fnv32(key, key_len); - spin_lock(&volume_table.lock, &fvt); - hash_find(&volume_table, key, key_len, hash, lengthof(volume_table.volume_buckets), volume_buckets, struct vfs_volume, volume_table_link, key, found_link); if (found_link == NULL) { - spin_unlock(&volume_table.lock, fvt); return -ST_NOT_FOUND; } struct vfs_volume* volume = hash_entry(found_link, struct vfs_volume, volume_table_link); if (volume == NULL) { - spin_unlock(&volume_table.lock, fvt); return -ST_NOT_FOUND; } - spin_lock(&volume->lock, &fv); - if (volume->locked) { - spin_unlock(&volume->lock, fv); - spin_unlock(&volume_table.lock, fvt); return -ST_TRY_AGAIN; } @@ -206,53 +173,37 @@ int vfs_volume_delete(const char* key) { int ret = volume->driver_ops.unmount(volume); - spin_unlock(&volume->lock, fv); - free(volume); - spin_unlock(&volume_table.lock, fvt); - return ret; } int vfs_volume_open(struct proc* proc, const char* volume_name, struct reschedule_ctx* rctx) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (!volume->locked) { volume->locked = true; volume->owner = proc; - spin_unlock(&volume->lock, fv); return ST_OK; } else { - proc_sq_suspend(proc, &volume->sq, &volume->lock, fv, rctx, NULL, NULL); + proc_sq_suspend(proc, &volume->sq, rctx, NULL, NULL); return ST_OK; } } int vfs_volume_close(struct proc* proc, const char* volume_name, struct reschedule_ctx* rctx) { - uint64_t fv, fvsq; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_lock(&volume->sq.lock, &fvsq); - struct list_node_link* node = volume->sq.proc_list; if (node) { @@ -262,9 +213,6 @@ int vfs_volume_close(struct proc* proc, const char* volume_name, struct reschedu volume->owner = resumed_proc; volume->locked = true; - spin_unlock(&volume->sq.lock, fvsq); - spin_unlock(&volume->lock, fv); - proc_sq_resume(resumed_proc, sq_entry, rctx); return ST_OK; } @@ -272,184 +220,121 @@ int vfs_volume_close(struct proc* proc, const char* volume_name, struct reschedu volume->locked = false; volume->owner = NULL; - spin_unlock(&volume->sq.lock, fvsq); - spin_unlock(&volume->lock, fv); - return ST_OK; } int vfs_format(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.format(volume, proc, rctx); } int vfs_read_file(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name, const char* path, uint8_t* buffer, size_t off, size_t size) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.read_file(volume, proc, rctx, path, buffer, off, size); } int vfs_write_file(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name, const char* path, uint8_t* buffer, size_t off, size_t size, uint32_t flags) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.write_file(volume, proc, rctx, path, buffer, off, size, flags); } int vfs_create_file(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name, const char* path) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.create_file(volume, proc, rctx, path); } int vfs_describe(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name, const char* path, struct desc* desc) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.describe(volume, proc, rctx, path, desc); } int vfs_read_dir_entry(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name, const char* path, struct dir_entry* entry, size_t entry_num) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.read_dir_entry(volume, proc, rctx, path, entry, entry_num); } int vfs_create_dir(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name, const char* path) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.create_dir(volume, proc, rctx, path); } int vfs_remove(struct proc* proc, struct reschedule_ctx* rctx, const char* volume_name, const char* path) { - uint64_t fv; - struct vfs_volume* volume = vfs_find_volume(volume_name); if (volume == NULL) return -ST_NOT_FOUND; - spin_lock(&volume->lock, &fv); - if (volume->locked && volume->owner != proc) { - spin_unlock(&volume->lock, fv); return -ST_PERMISSION_ERROR; } - spin_unlock(&volume->lock, fv); - return volume->driver_ops.remove(volume, proc, rctx, path); } -void vfs_init(void) { - memset(&volume_table, 0, sizeof(volume_table)); - - volume_table.lock = SPIN_LOCK_INIT; -} +void vfs_init(void) { memset(&volume_table, 0, sizeof(volume_table)); } void vfs_translate(size_t fs_block, size_t fs_block_count, size_t fs_block_size, size_t device_sector_size, size_t* out_phys_sector, size_t* out_sector_count) { diff --git a/kernel/fs/vfs.h b/kernel/fs/vfs.h index 0d17f9c..e9f4579 100644 --- a/kernel/fs/vfs.h +++ b/kernel/fs/vfs.h @@ -23,7 +23,6 @@ struct vfs_volume { char key[VOLUME_MAX]; struct hash_node_link volume_table_link; int fs_type; - spin_lock_t lock; struct proc* owner; bool locked; struct proc_suspension_q sq; @@ -62,7 +61,6 @@ struct vfs_volume { struct vfs_volume_table { struct hash_node_link* volume_buckets[1024]; - spin_lock_t lock; }; int vfs_create_volume(struct proc* proc, struct reschedule_ctx* rctx, const char* key, int fs_type, diff --git a/kernel/id/id_alloc.c b/kernel/id/id_alloc.c index a4c43ef..a84abee 100644 --- a/kernel/id/id_alloc.c +++ b/kernel/id/id_alloc.c @@ -15,7 +15,6 @@ bool id_alloc_init(struct id_alloc* ida, size_t nbits) { return false; bm_init(&ida->bm, buffer, nbits); - ida->lock = SPIN_LOCK_INIT; return true; } @@ -27,10 +26,6 @@ void id_alloc_fini(struct id_alloc* ida) { } int id_alloc(struct id_alloc* ida) { - uint64_t fid; - - spin_lock(&ida->lock, &fid); - size_t start = ida->next_id; size_t current = start; do { @@ -39,24 +34,18 @@ int id_alloc(struct id_alloc* ida) { ida->next_id = (current + 1) % ida->bm.nbits; - spin_unlock(&ida->lock, fid); return (int)current; } current = (current + 1) % ida->bm.nbits; } while (current != start); - spin_unlock(&ida->lock, fid); return -ST_OOM_ERROR; } void id_free(struct id_alloc* ida, int id) { - uint64_t fid; - if (id < 0) return; - spin_lock(&ida->lock, &fid); bm_clear(&ida->bm, (size_t)id); - spin_unlock(&ida->lock, fid); } diff --git a/kernel/id/id_alloc.h b/kernel/id/id_alloc.h index 0c292e6..cccc073 100644 --- a/kernel/id/id_alloc.h +++ b/kernel/id/id_alloc.h @@ -7,7 +7,6 @@ struct id_alloc { struct bm bm; - spin_lock_t lock; int next_id; }; diff --git a/kernel/irq/irq.c b/kernel/irq/irq.c index f499d04..069a297 100644 --- a/kernel/irq/irq.c +++ b/kernel/irq/irq.c @@ -11,11 +11,8 @@ #endif static struct rb_node_link* irq_tree = NULL; -static spin_lock_t irqs_lock = SPIN_LOCK_INIT; bool irq_attach(irq_func_t func, void* arg, uint32_t irq_num) { - uint64_t fiq; - struct irq* irq = malloc(sizeof(*irq)); if (irq == NULL) { return false; @@ -25,33 +22,23 @@ bool irq_attach(irq_func_t func, void* arg, uint32_t irq_num) { irq->arg = arg; irq->irq_num = irq_num; - spin_lock(&irqs_lock, &fiq); rbtree_insert(struct irq, &irq_tree, &irq->irq_tree_link, irq_tree_link, irq_num); - spin_unlock(&irqs_lock, fiq); return true; } void irq_detach(uint32_t irq_num) { - uint64_t fiq; - struct irq* irq = irq_find(irq_num); - spin_lock(&irqs_lock, &fiq); rbtree_delete(&irq_tree, &irq->irq_tree_link); - spin_unlock(&irqs_lock, fiq); free(irq); } struct irq* irq_find(uint32_t irq_num) { - uint64_t fiq; - struct irq* irq = NULL; - spin_lock(&irqs_lock, &fiq); rbtree_find(struct irq, &irq_tree, irq_num, irq, irq_tree_link, irq_num); - spin_unlock(&irqs_lock, fiq); return irq; } diff --git a/kernel/mm/_malloc_port.c b/kernel/mm/_malloc_port.c index 61981e4..098bd21 100644 --- a/kernel/mm/_malloc_port.c +++ b/kernel/mm/_malloc_port.c @@ -24,8 +24,8 @@ #define MALLOC_ALIGNMENT 16 #define HAVE_MORECORE 0 #define NO_MALLOC_STATS 1 -#define USE_LOCKS 1 -#define USE_SPIN_LOCKS 1 +#define USE_LOCKS 0 +#define USE_SPIN_LOCKS 0 #define MALLOC_FAILURE_ACTION \ do { \ DEBUG("malloc failure\n"); \ diff --git a/kernel/mm/pmm.c b/kernel/mm/pmm.c index 0ba060f..976dee8 100644 --- a/kernel/mm/pmm.c +++ b/kernel/mm/pmm.c @@ -70,7 +70,6 @@ void pmm_init(void) { uint8_t* bm_base1 = (uint8_t*)(bm_base + hhdm->offset); /* Init the pm region. */ - pmm_region->lock = SPIN_LOCK_INIT; pmm_region->membase = data_base; pmm_region->size = managed_size; bm_init(&pmm_region->bm, bm_base1, bm_nbits); @@ -115,8 +114,6 @@ static size_t pmm_find_free_space_aligned(struct pmm_region* pmm_region, size_t } uintptr_t pmm_alloc(size_t nblks) { - uint64_t fpr; - for (size_t region = 0; region < PMM_REGIONS_MAX; region++) { struct pmm_region* pmm_region = &pmm.regions[region]; @@ -124,8 +121,6 @@ uintptr_t pmm_alloc(size_t nblks) { if (!(pmm_region->flags & PMM_REGION_ACTIVE)) continue; - spin_lock(&pmm_region->lock, &fpr); - /* Find starting bit of the free bit range */ size_t bit = pmm_find_free_space(pmm_region, nblks); @@ -133,20 +128,15 @@ uintptr_t pmm_alloc(size_t nblks) { if (bit != (size_t)-1) { /* Mark it */ bm_set_region(&pmm_region->bm, bit, nblks); - spin_unlock(&pmm_region->lock, fpr); return pmm_region->membase + bit * PAGE_SIZE; } - - spin_unlock(&pmm_region->lock, fpr); } return PMM_ALLOC_ERR; } uintptr_t pmm_alloc_aligned(size_t nblks, size_t align_pages) { - uint64_t fpr; - for (size_t region = 0; region < PMM_REGIONS_MAX; region++) { struct pmm_region* pmm_region = &pmm.regions[region]; @@ -154,8 +144,6 @@ uintptr_t pmm_alloc_aligned(size_t nblks, size_t align_pages) { if (!(pmm_region->flags & PMM_REGION_ACTIVE)) continue; - spin_lock(&pmm_region->lock, &fpr); - /* Find starting bit of the free bit range */ size_t bit = pmm_find_free_space_aligned(pmm_region, nblks, align_pages); @@ -163,20 +151,15 @@ uintptr_t pmm_alloc_aligned(size_t nblks, size_t align_pages) { if (bit != (size_t)-1) { /* Mark it */ bm_set_region(&pmm_region->bm, bit, nblks); - spin_unlock(&pmm_region->lock, fpr); return pmm_region->membase + bit * PAGE_SIZE; } - - spin_unlock(&pmm_region->lock, fpr); } return PMM_ALLOC_ERR; } void pmm_free(uintptr_t p_addr, size_t nblks) { - uint64_t fpr; - /* Round down to nearest page boundary */ uintptr_t aligned_p_addr = align_down(p_addr, PAGE_SIZE); @@ -194,12 +177,7 @@ void pmm_free(uintptr_t p_addr, size_t nblks) { size_t bit = div_align_up(addr, PAGE_SIZE); - spin_lock(&pmm_region->lock, &fpr); - bm_clear_region(&pmm_region->bm, bit, nblks); - - spin_unlock(&pmm_region->lock, fpr); - break; } } diff --git a/kernel/mm/pmm.h b/kernel/mm/pmm.h index 37d77b3..b6e3d0f 100644 --- a/kernel/mm/pmm.h +++ b/kernel/mm/pmm.h @@ -12,7 +12,6 @@ #define PMM_REGION_ACTIVE (1 << 0) struct pmm_region { - spin_lock_t lock; struct bm bm; uintptr_t membase; size_t size; diff --git a/kernel/proc/env.c b/kernel/proc/env.c index 07b0f51..65a3503 100644 --- a/kernel/proc/env.c +++ b/kernel/proc/env.c @@ -24,8 +24,6 @@ void proc_env_cleanup(struct procgroup* procgroup) { } int proc_env_set(struct procgroup* procgroup, const char* key, void* buffer, size_t data_size) { - uint64_t fpg; - struct proc_env_var* var = malloc(sizeof(*var)); if (var == NULL) @@ -46,30 +44,21 @@ int proc_env_set(struct procgroup* procgroup, const char* key, void* buffer, siz uint32_t env_hash = hash_fnv32(var->key, strlen_null(var->key)); - spin_lock(&procgroup->lock, &fpg); - hash_insert(&procgroup->env, &var->env_link, env_hash, lengthof(procgroup->env.env_var_buckets), env_var_buckets); - spin_unlock(&procgroup->lock, fpg); - return ST_OK; } int proc_env_get(struct procgroup* procgroup, const char* key, void* buffer, size_t size) { - uint64_t fpg; - struct hash_node_link* found_link = NULL; size_t key_len = strlen_null(key); uint32_t hash = hash_fnv32(key, key_len); - spin_lock(&procgroup->lock, &fpg); - hash_find(&procgroup->env, key, key_len, hash, lengthof(procgroup->env.env_var_buckets), env_var_buckets, struct proc_env_var, env_link, key, found_link); if (found_link == NULL) { - spin_unlock(&procgroup->lock, fpg); return -ST_NOT_FOUND; } @@ -77,7 +66,5 @@ int proc_env_get(struct procgroup* procgroup, const char* key, void* buffer, siz memcpy(buffer, var->buffer, min(size, var->data_size)); - spin_unlock(&procgroup->lock, fpg); - return ST_OK; } diff --git a/kernel/proc/mail.c b/kernel/proc/mail.c index c07c763..805fad2 100644 --- a/kernel/proc/mail.c +++ b/kernel/proc/mail.c @@ -13,51 +13,29 @@ static void proc_mail_free_saved_buffer(void* udata) { free(udata); } void proc_cleanup_resource_mail(struct proc_resource* resource, struct reschedule_ctx* rctx) { - uint64_t fr, fssq; - struct proc_mail* mail = &resource->u.mail; - spin_lock(&mail->resource->lock, &fr); - - spin_lock(&mail->send_sq.lock, &fssq); - while (mail->send_sq.proc_list != NULL) { struct list_node_link* node = mail->send_sq.proc_list; struct proc_sq_entry* sq_entry = list_entry(node, struct proc_sq_entry, sq_link); struct proc* suspended_proc = sq_entry->proc; - spin_unlock(&mail->send_sq.lock, fssq); - spin_unlock(&mail->resource->lock, fr); - proc_sq_resume(suspended_proc, sq_entry, rctx); - - spin_lock(&mail->resource->lock, &fr); - spin_lock(&mail->send_sq.lock, &fssq); } - - spin_unlock(&mail->send_sq.lock, fssq); - spin_unlock(&mail->resource->lock, fr); } void proc_mail_send(struct proc* proc, struct proc_mail* mail, struct reschedule_ctx* rctx, void* data, size_t data_size) { - uint64_t fr, frsq; - - spin_lock(&mail->resource->lock, &fr); - /* mail full */ if (mail->packets_count == PROC_MAIL_MAX) { struct mail_saved_buffer* saved_buffer = malloc(sizeof(*saved_buffer)); saved_buffer->buffer = data; saved_buffer->size = data_size; - proc_sq_suspend(proc, &mail->send_sq, &mail->resource->lock, fr, rctx, saved_buffer, - &proc_mail_free_saved_buffer); + proc_sq_suspend(proc, &mail->send_sq, rctx, saved_buffer, &proc_mail_free_saved_buffer); return; } - spin_lock(&mail->recv_sq.lock, &frsq); - /* if receiver available, hand off directly */ struct list_node_link* node = mail->recv_sq.proc_list; @@ -70,15 +48,10 @@ void proc_mail_send(struct proc* proc, struct proc_mail* mail, struct reschedule size_t copy_size = min(data_size, saved_buffer->size); memcpy(saved_buffer->buffer, data, copy_size); - spin_unlock(&mail->recv_sq.lock, frsq); - spin_unlock(&mail->resource->lock, fr); - proc_sq_resume(resumed_proc, sq_entry, rctx); return; } - spin_unlock(&mail->recv_sq.lock, frsq); - /* mail is empty and nobody is waiting */ void* mesg = malloc(data_size); if (mesg != NULL) { @@ -97,16 +70,10 @@ void proc_mail_send(struct proc* proc, struct proc_mail* mail, struct reschedule mail->packets_count++; } } - - spin_unlock(&mail->resource->lock, fr); } void proc_mail_receive(struct proc* proc, struct proc_mail* mail, struct reschedule_ctx* rctx, void* recv_buffer, size_t recv_size) { - uint64_t fr, fssq; - - spin_lock(&mail->resource->lock, &fr); - /* consume mesg if available */ if (mail->packets_count > 0) { struct mail_packet* packet = list_entry(mail->packets, struct mail_packet, packets_link); @@ -117,25 +84,16 @@ void proc_mail_receive(struct proc* proc, struct proc_mail* mail, struct resched free(packet->packet_buffer); free(packet); - /* check for suspended sender */ - spin_lock(&mail->send_sq.lock, &fssq); - struct list_node_link* node = mail->send_sq.proc_list; if (node != NULL) { struct proc_sq_entry* sq_entry = list_entry(node, struct proc_sq_entry, sq_link); struct proc* resumed_proc = sq_entry->proc; - spin_unlock(&mail->send_sq.lock, fssq); - spin_unlock(&mail->resource->lock, fr); - proc_sq_resume(resumed_proc, sq_entry, rctx); return; } - spin_unlock(&mail->send_sq.lock, fssq); - spin_unlock(&mail->resource->lock, fr); - return; } @@ -144,6 +102,5 @@ void proc_mail_receive(struct proc* proc, struct proc_mail* mail, struct resched saved_buffer->size = recv_size; /* nothing to receive */ - proc_sq_suspend(proc, &mail->recv_sq, &mail->resource->lock, fr, rctx, saved_buffer, - &proc_mail_free_saved_buffer); + proc_sq_suspend(proc, &mail->recv_sq, rctx, saved_buffer, &proc_mail_free_saved_buffer); } diff --git a/kernel/proc/mutex.c b/kernel/proc/mutex.c index 3139f0a..89bb458 100644 --- a/kernel/proc/mutex.c +++ b/kernel/proc/mutex.c @@ -12,13 +12,7 @@ #include void proc_mutexes_cleanup(struct proc* proc, struct reschedule_ctx* rctx) { - uint64_t fpg, fr, fp; - - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); - - spin_lock(&procgroup->lock, &fpg); struct rb_node_link* rnode; rbtree_first(&procgroup->resource_tree, rnode); @@ -31,83 +25,46 @@ void proc_mutexes_cleanup(struct proc* proc, struct reschedule_ctx* rctx) { rnode = next; - spin_lock(&resource->lock, &fr); - if (resource->type != PR_MUTEX) { - spin_unlock(&resource->lock, fr); continue; } if (resource->u.mutex.owner == proc && resource->u.mutex.locked) { - spin_unlock(&resource->lock, fr); - proc_mutex_unlock(proc, &resource->u.mutex, rctx); } - - spin_unlock(&resource->lock, fr); } - - spin_unlock(&procgroup->lock, fpg); } void proc_cleanup_resource_mutex(struct proc_resource* resource, struct reschedule_ctx* rctx) { - uint64_t fr, fsq; - struct proc_mutex* mutex = &resource->u.mutex; - spin_lock(&mutex->resource->lock, &fr); - spin_lock(&mutex->suspension_q.lock, &fsq); - 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, fsq); - spin_unlock(&mutex->resource->lock, fr); - proc_sq_resume(suspended_proc, sq_entry, rctx); - - /* reacquire */ - spin_lock(&mutex->resource->lock, &fr); - spin_lock(&mutex->suspension_q.lock, &fsq); } mutex->locked = false; mutex->owner = NULL; - - spin_unlock(&mutex->suspension_q.lock, fsq); - spin_unlock(&mutex->resource->lock, fr); } void proc_mutex_lock(struct proc* proc, struct proc_mutex* mutex, struct reschedule_ctx* rctx) { - uint64_t fr; - - spin_lock(&mutex->resource->lock, &fr); - if (!mutex->locked || mutex->owner == proc) { mutex->locked = true; mutex->owner = proc; - spin_unlock(&mutex->resource->lock, fr); return; } - proc_sq_suspend(proc, &mutex->suspension_q, &mutex->resource->lock, fr, rctx, NULL, NULL); + proc_sq_suspend(proc, &mutex->suspension_q, rctx, NULL, NULL); } void proc_mutex_unlock(struct proc* proc, struct proc_mutex* mutex, struct reschedule_ctx* rctx) { - uint64_t fr, fsq; - - spin_lock(&mutex->resource->lock, &fr); - if (mutex->owner != proc) { - spin_unlock(&mutex->resource->lock, fr); return; } - spin_lock(&mutex->suspension_q.lock, &fsq); - struct list_node_link* node = mutex->suspension_q.proc_list; if (node) { @@ -117,16 +74,10 @@ void proc_mutex_unlock(struct proc* proc, struct proc_mutex* mutex, struct resch mutex->owner = resumed_proc; mutex->locked = true; - spin_unlock(&mutex->suspension_q.lock, fsq); - spin_unlock(&mutex->resource->lock, fr); - proc_sq_resume(resumed_proc, sq_entry, rctx); return; } mutex->locked = false; mutex->owner = NULL; - - spin_unlock(&mutex->suspension_q.lock, fsq); - spin_unlock(&mutex->resource->lock, fr); } diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index aacb44b..0c14b01 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -34,7 +35,6 @@ #define PIDS_MAX 1024 static struct rb_node_link* proc_tree = NULL; -static spin_lock_t proc_tree_lock = SPIN_LOCK_INIT; static struct id_alloc pid_alloc; @@ -49,7 +49,6 @@ struct proc* kproc_create(void) { memset(kproc, 0, sizeof(*kproc)); - kproc->lock = SPIN_LOCK_INIT; kproc->flags |= PROC_KPROC; kproc->state = PROC_READY; kproc->pid = proc_alloc_pid(); @@ -68,14 +67,10 @@ struct proc* kproc_create(void) { } size_t proc_populate_proc_infos(struct proc_info* proc_info, size_t count) { - uint64_t fpt, fp, fpg; - if (count > PIDS_MAX) { count = PIDS_MAX; } - spin_lock(&proc_tree_lock, &fpt); - struct rb_node_link* node; rbtree_first(&proc_tree, node); @@ -89,13 +84,6 @@ size_t proc_populate_proc_infos(struct proc_info* proc_info, size_t count) { node = next; - spin_lock(&proc->lock, &fp); - struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); - - spin_lock(&procgroup->lock, &fpg); - spin_lock(&proc->lock, &fp); - struct cpu* cpu = proc->cpu; proc_info[i].cpu = cpu->id; @@ -106,14 +94,9 @@ size_t proc_populate_proc_infos(struct proc_info* proc_info, size_t count) { proc_info[i].pgid = proc->procgroup->pgid; memcpy(proc_info[i].name, proc->name, sizeof(proc->name)); - spin_unlock(&proc->lock, fp); - spin_unlock(&procgroup->lock, fpg); - i++; } - spin_unlock(&proc_tree_lock, fpt); - return i; } @@ -233,26 +216,16 @@ struct proc* proc_from_file(struct proc* proc1, const char* volume, const char* } struct proc* proc_find_pid(int pid) { - uint64_t fpt; - struct proc* proc = NULL; - spin_lock(&proc_tree_lock, &fpt); rbtree_find(struct proc, &proc_tree, pid, proc, proc_tree_link, pid); - spin_unlock(&proc_tree_lock, fpt); return proc; } void proc_register(struct proc* proc, struct cpu* register_cpu, struct reschedule_ctx* rctx) { - uint64_t fpt, fc, fp; - struct cpu* cpu = register_cpu != NULL ? register_cpu : cpu_find_lightest(); - spin_lock(&proc_tree_lock, &fpt); - spin_lock(&cpu->lock, &fc); - spin_lock(&proc->lock, &fp); - proc->cpu = cpu; rbtree_insert(struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid); @@ -262,29 +235,15 @@ void proc_register(struct proc* proc, struct cpu* register_cpu, struct reschedul if (cpu->proc_current == NULL) cpu->proc_current = proc; - spin_unlock(&proc->lock, fp); - spin_unlock(&cpu->lock, fc); - spin_unlock(&proc_tree_lock, fpt); - rctx_insert_cpu(rctx, cpu); } void proc_register_partial(struct proc* proc) { - uint64_t fpt, fp; - - spin_lock(&proc_tree_lock, &fpt); - spin_lock(&proc->lock, &fp); - rbtree_insert(struct proc, &proc_tree, &proc->proc_tree_link, proc_tree_link, pid); - - spin_unlock(&proc->lock, fp); - spin_unlock(&proc_tree_lock, fpt); } /* caller holds cpu->lock */ static struct proc* proc_find_sched(struct cpu* cpu) { - uint64_t fp; - struct list_node_link *current, *start; if (!cpu->proc_run_q) @@ -300,17 +259,12 @@ static struct proc* proc_find_sched(struct cpu* cpu) { do { struct proc* proc = list_entry(current, struct proc, cpu_run_q_link); - spin_lock(&proc->lock, &fp); - int state = proc->state; if (state == PROC_READY && !(proc->flags & PROC_KPROC)) { - spin_unlock(&proc->lock, fp); return proc; } - spin_unlock(&proc->lock, fp); - current = current->next; if (!current) @@ -323,66 +277,40 @@ static struct proc* proc_find_sched(struct cpu* cpu) { void proc_sched(bool user) { struct proc* next = NULL; struct cpu* cpu = thiscpu; - uint64_t fc; retry: - spin_lock(&cpu->lock, &fc); - next = proc_find_sched(cpu); if (next) { cpu->proc_current = next; if (user) - do_sched(next, &cpu->lock); - else - spin_unlock(&cpu->lock, fc); + do_sched(next); } else { - spin_unlock(&cpu->lock, fc); - spin_lock_relax(); - goto retry; } } void proc_kill(struct proc* proc, struct reschedule_ctx* rctx) { - uint64_t fp, fc, fpt; - - spin_lock(&proc->lock, &fp); - if ((proc->flags & PROC_KPROC)) { - spin_unlock(&proc->lock, fp); return; } struct cpu* cpu = proc->cpu; - spin_unlock(&proc->lock, fp); - - spin_lock(&proc_tree_lock, &fpt); - spin_lock(&cpu->lock, &fc); - spin_lock(&proc->lock, &fp); list_remove(cpu->proc_run_q, &proc->cpu_run_q_link); cpu->proc_run_q_count--; rbtree_delete(&proc_tree, &proc->proc_tree_link); - spin_unlock(&proc->lock, fp); - spin_unlock(&cpu->lock, fc); - spin_unlock(&proc_tree_lock, fpt); - DEBUG("killed PID %d\n", proc->pid); proc_cleanup(proc, rctx); rctx_insert_cpu(rctx, cpu); } void proc_wait_for(struct proc* proc, struct reschedule_ctx* rctx, struct proc* wait_proc) { - uint64_t fwp; - - spin_lock(&wait_proc->lock, &fwp); - proc_sq_suspend(proc, &wait_proc->done_sq, NULL, 0, rctx, NULL, NULL); - spin_unlock(&wait_proc->lock, fwp); + proc_sq_suspend(proc, &wait_proc->done_sq, rctx, NULL, NULL); } void proc_irq_sched(void* arg, void* regs, bool user, struct reschedule_ctx* rctx) { @@ -392,8 +320,6 @@ void proc_irq_sched(void* arg, void* regs, bool user, struct reschedule_ctx* rct } void proc_init(void) { - uint64_t fc; - struct reschedule_ctx rctx; memset(&rctx, 0, sizeof(rctx)); @@ -403,6 +329,7 @@ void proc_init(void) { struct proc* init = proc_from_file(thiscpu->kproc, "sys", "/init", &rctx); proc_register(init, thiscpu, &rctx); - spin_lock(&spin_proc->cpu->lock, &fc); - do_sched(spin_proc, &spin_proc->cpu->lock); + intr_enable(); + + do_sched(spin_proc); } diff --git a/kernel/proc/proc.h b/kernel/proc/proc.h index 2c109ec..3f56fb2 100644 --- a/kernel/proc/proc.h +++ b/kernel/proc/proc.h @@ -41,7 +41,6 @@ struct proc { struct procgroup* procgroup; struct proc_platformdata pdata; uint32_t flags; - spin_lock_t lock; struct cpu* cpu; int state; uintptr_t uvaddr_argument; diff --git a/kernel/proc/procgroup.c b/kernel/proc/procgroup.c index 11cd844..0904f28 100644 --- a/kernel/proc/procgroup.c +++ b/kernel/proc/procgroup.c @@ -16,36 +16,26 @@ #define PGIDS_MAX 1024 static struct rb_node_link* procgroup_tree = NULL; -static spin_lock_t procgroup_tree_lock = SPIN_LOCK_INIT; static struct id_alloc pgid_alloc; void procgroup_pgid_alloc_init(void) { id_alloc_init(&pgid_alloc, PGIDS_MAX); } struct procgroup* procgroup_find(int pgid) { - uint64_t fpgt; - struct procgroup* procgroup = NULL; - spin_lock(&procgroup_tree_lock, &fpgt); rbtree_find(struct procgroup, &procgroup_tree, pgid, procgroup, procgroup_tree_link, pgid); - spin_unlock(&procgroup_tree_lock, fpgt); return procgroup; } uintptr_t procgroup_map(struct procgroup* procgroup, uintptr_t vaddr, size_t pages, uint32_t flags, uintptr_t* out_paddr) { - uint64_t fpg; - - spin_lock(&procgroup->lock, &fpg); - vaddr = (vaddr == 0) ? procgroup->map_base : vaddr; struct proc_mapping* mapping = malloc(sizeof(*mapping)); if (mapping == NULL) { - spin_unlock(&procgroup->lock, fpg); return 0; } @@ -53,7 +43,6 @@ uintptr_t procgroup_map(struct procgroup* procgroup, uintptr_t vaddr, size_t pag if (paddr == PMM_ALLOC_ERR) { free(mapping); - spin_unlock(&procgroup->lock, fpg); return 0; } @@ -73,13 +62,10 @@ uintptr_t procgroup_map(struct procgroup* procgroup, uintptr_t vaddr, size_t pag mm_map_page(&procgroup->pd, ppage, vpage, flags); } - spin_unlock(&procgroup->lock, fpg); - return vaddr; } bool procgroup_unmap(struct procgroup* procgroup, uintptr_t start_vaddr, size_t pages) { - uint64_t fpg; size_t unmap_size = pages * PAGE_SIZE; uintptr_t end_vaddr = start_vaddr + unmap_size; @@ -91,8 +77,6 @@ bool procgroup_unmap(struct procgroup* procgroup, uintptr_t start_vaddr, size_t if (tail_mapping == NULL) return false; - spin_lock(&procgroup->lock, &fpg); - list_foreach(procgroup->mappings, mapping_link, mapping_link_tmp) { struct proc_mapping* mapping = list_entry(mapping_link, struct proc_mapping, proc_mappings_link); @@ -144,14 +128,10 @@ bool procgroup_unmap(struct procgroup* procgroup, uintptr_t start_vaddr, size_t mm_unmap_page(&procgroup->pd, vpage); } - spin_unlock(&procgroup->lock, fpg); - return true; } struct procgroup* procgroup_create(void) { - uint64_t fpgt; - struct procgroup* procgroup = malloc(sizeof(*procgroup)); if (procgroup == NULL) { return NULL; @@ -173,7 +153,6 @@ struct procgroup* procgroup_create(void) { } procgroup->memb_proc_tree = NULL; - procgroup->lock = SPIN_LOCK_INIT; procgroup->pd.cr3_paddr = mm_alloc_user_pd_phys(); procgroup->map_base = PROC_MAP_BASE; @@ -195,38 +174,20 @@ struct procgroup* procgroup_create(void) { return NULL; } - spin_lock(&procgroup_tree_lock, &fpgt); rbtree_insert(struct procgroup, &procgroup_tree, &procgroup->procgroup_tree_link, procgroup_tree_link, pgid); - spin_unlock(&procgroup_tree_lock, fpgt); return procgroup; } void procgroup_attach(struct procgroup* procgroup, struct proc* proc) { - uint64_t fpg, fp; - - spin_lock(&procgroup->lock, &fpg); - spin_lock(&proc->lock, &fp); - rbtree_insert(struct proc, &procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link, procgroup_memb_tree_link, pid); - - spin_unlock(&proc->lock, fp); - spin_unlock(&procgroup->lock, fpg); } static void procgroup_delete(struct procgroup* procgroup, struct reschedule_ctx* rctx) { - uint64_t fpg, fpgt; - - spin_lock(&procgroup_tree_lock, &fpgt); - spin_lock(&procgroup->lock, &fpg); - rbtree_delete(&procgroup_tree, &procgroup->procgroup_tree_link); - spin_unlock(&procgroup->lock, fpg); - spin_unlock(&procgroup_tree_lock, fpgt); - /* delete resources */ struct rb_node_link* rnode; rbtree_first(&procgroup->resource_tree, rnode); @@ -264,17 +225,9 @@ static void procgroup_delete(struct procgroup* procgroup, struct reschedule_ctx* } void procgroup_detach(struct procgroup* procgroup, struct proc* proc, struct reschedule_ctx* rctx) { - uint64_t fpg, fp; - - spin_lock(&procgroup->lock, &fpg); - spin_lock(&proc->lock, &fp); - rbtree_delete(&procgroup->memb_proc_tree, &proc->procgroup_memb_tree_link); struct rb_node_link* memb_tree = procgroup->memb_proc_tree; - spin_unlock(&proc->lock, fp); - spin_unlock(&procgroup->lock, fpg); - if (memb_tree == NULL) { procgroup_delete(procgroup, rctx); } diff --git a/kernel/proc/procgroup.h b/kernel/proc/procgroup.h index 6bd56f6..a2b1bff 100644 --- a/kernel/proc/procgroup.h +++ b/kernel/proc/procgroup.h @@ -29,7 +29,6 @@ struct procgroup { int pgid; struct rb_node_link procgroup_tree_link; struct rb_node_link* memb_proc_tree; - spin_lock_t lock; struct rb_node_link* resource_tree; struct id_alloc rid_alloc; struct pd pd; diff --git a/kernel/proc/resource.c b/kernel/proc/resource.c index d85abce..6aaa223 100644 --- a/kernel/proc/resource.c +++ b/kernel/proc/resource.c @@ -17,21 +17,15 @@ #include struct proc_resource* proc_find_resource(struct procgroup* procgroup, int rid) { - uint64_t fr; - struct proc_resource* resource = NULL; - spin_lock(&procgroup->lock, &fr); rbtree_find(struct proc_resource, &procgroup->resource_tree, rid, resource, resource_tree_link, rid); - spin_unlock(&procgroup->lock, fr); return resource; } struct proc_resource* proc_create_resource_mutex(struct procgroup* procgroup) { - uint64_t fpg; - struct proc_resource* resource; resource = malloc(sizeof(*resource)); @@ -40,17 +34,13 @@ struct proc_resource* proc_create_resource_mutex(struct procgroup* procgroup) { memset(resource, 0, sizeof(*resource)); - spin_lock(&procgroup->lock, &fpg); - resource->rid = id_alloc(&procgroup->rid_alloc); if (resource->rid < 0) { free(resource); - spin_unlock(&procgroup->lock, fpg); return NULL; } - resource->lock = SPIN_LOCK_INIT; resource->ops.cleanup = &proc_cleanup_resource_mutex; resource->u.mutex.resource = resource; resource->type = PR_MUTEX; @@ -58,14 +48,10 @@ struct proc_resource* proc_create_resource_mutex(struct procgroup* procgroup) { rbtree_insert(struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link, resource_tree_link, rid); - spin_unlock(&procgroup->lock, fpg); - return resource; } struct proc_resource* proc_create_resource_mail(struct procgroup* procgroup) { - uint64_t fpg; - struct proc_resource* resource; resource = malloc(sizeof(*resource)); @@ -74,17 +60,13 @@ struct proc_resource* proc_create_resource_mail(struct procgroup* procgroup) { memset(resource, 0, sizeof(*resource)); - spin_lock(&procgroup->lock, &fpg); - resource->rid = id_alloc(&procgroup->rid_alloc); if (resource->rid < 0) { free(resource); - spin_unlock(&procgroup->lock, fpg); return NULL; } - resource->lock = SPIN_LOCK_INIT; resource->ops.cleanup = &proc_cleanup_resource_mail; resource->u.mail.resource = resource; resource->type = PR_MAIL; @@ -92,14 +74,10 @@ struct proc_resource* proc_create_resource_mail(struct procgroup* procgroup) { rbtree_insert(struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link, resource_tree_link, rid); - spin_unlock(&procgroup->lock, fpg); - return resource; } struct proc_resource* proc_create_resource_stream(struct procgroup* procgroup) { - uint64_t fpg; - struct proc_resource* resource; resource = malloc(sizeof(*resource)); @@ -108,48 +86,33 @@ struct proc_resource* proc_create_resource_stream(struct procgroup* procgroup) { memset(resource, 0, sizeof(*resource)); - spin_lock(&procgroup->lock, &fpg); - resource->rid = id_alloc(&procgroup->rid_alloc); if (resource->rid < 0) { free(resource); - spin_unlock(&procgroup->lock, fpg); return NULL; } - resource->lock = SPIN_LOCK_INIT; resource->ops.cleanup = &proc_cleanup_resource_stream; resource->u.stream.resource = resource; resource->type = PR_STREAM; if (!ringbuffer_init(&resource->u.stream.ringbuffer, PROC_STREAM_MAX, 1)) { free(resource); - spin_unlock(&procgroup->lock, fpg); return NULL; } rbtree_insert(struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link, resource_tree_link, rid); - spin_unlock(&procgroup->lock, fpg); - return resource; } void proc_delete_resource(struct procgroup* procgroup, struct proc_resource* resource, struct reschedule_ctx* rctx) { - uint64_t fr, fpg; - - spin_lock(&procgroup->lock, &fpg); - spin_lock(&resource->lock, &fr); - id_free(&procgroup->rid_alloc, resource->rid); rbtree_delete(&procgroup->resource_tree, &resource->resource_tree_link); - spin_unlock(&resource->lock, fr); - spin_unlock(&procgroup->lock, fpg); - resource->ops.cleanup(resource, rctx); free(resource); } diff --git a/kernel/proc/resource.h b/kernel/proc/resource.h index 73eb5da..2c1e9c6 100644 --- a/kernel/proc/resource.h +++ b/kernel/proc/resource.h @@ -21,7 +21,6 @@ struct reschedule_ctx; struct proc_resource { int type; int rid; - spin_lock_t lock; struct rb_node_link resource_tree_link; union { struct proc_mutex mutex; diff --git a/kernel/proc/stream.c b/kernel/proc/stream.c index 581ce63..9950cb1 100644 --- a/kernel/proc/stream.c +++ b/kernel/proc/stream.c @@ -8,24 +8,14 @@ #include void proc_stream_write(struct proc_stream* stream, void* data, size_t data_size) { - uint64_t fr; - - spin_lock(&stream->resource->lock, &fr); - for (size_t i = 0; i < data_size; i++) ringbuffer_push(uint8_t, &stream->ringbuffer, ((uint8_t*)data)[i]); - - spin_unlock(&stream->resource->lock, fr); } size_t proc_stream_read(struct proc_stream* stream, void* out_data, size_t data_size) { - uint64_t fr; - size_t bytes = 0; uint8_t* p = (uint8_t*)out_data; - spin_lock(&stream->resource->lock, &fr); - for (size_t i = 0; i < data_size; i++) { if (stream->ringbuffer.count == 0) { break; @@ -36,21 +26,13 @@ size_t proc_stream_read(struct proc_stream* stream, void* out_data, size_t data_ bytes++; } - spin_unlock(&stream->resource->lock, fr); - return bytes; } void proc_cleanup_resource_stream(struct proc_resource* resource, struct reschedule_ctx* rctx) { (void)rctx; - uint64_t fr; - struct proc_stream* stream = &resource->u.stream; - spin_lock(&stream->resource->lock, &fr); - ringbuffer_fini(&stream->ringbuffer); - - spin_unlock(&stream->resource->lock, fr); } diff --git a/kernel/proc/suspension_q.c b/kernel/proc/suspension_q.c index ef79328..70736eb 100644 --- a/kernel/proc/suspension_q.c +++ b/kernel/proc/suspension_q.c @@ -10,19 +10,12 @@ #include #include -int proc_sq_suspend(struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock, - uint64_t lockflags, struct reschedule_ctx* rctx, void* udata, - sq_entry_free_udata_func_t free_func) { - uint64_t fc, fp, fsq; - - spin_lock(&proc->lock, &fp); +int proc_sq_suspend(struct proc* proc, struct proc_suspension_q* sq, struct reschedule_ctx* rctx, + void* udata, sq_entry_free_udata_func_t free_func) { struct cpu* cpu = proc->cpu; - spin_unlock(&proc->lock, fp); struct proc_sq_entry* sq_entry = malloc(sizeof(*sq_entry)); if (!sq_entry) { - if (resource_lock != NULL) - spin_unlock(resource_lock, lockflags); return -ST_OOM_ERROR; } @@ -31,13 +24,6 @@ int proc_sq_suspend(struct proc* proc, struct proc_suspension_q* sq, spin_lock_t sq_entry->udata = udata; sq_entry->free_func = free_func; - spin_lock(&cpu->lock, &fc); - spin_lock(&proc->lock, &fp); - spin_lock(&sq->lock, &fsq); - - if (resource_lock != NULL) - spin_unlock(resource_lock, lockflags); - proc->state = PROC_SUSPENDED; /* append to sq's list */ @@ -50,26 +36,15 @@ int proc_sq_suspend(struct proc* proc, struct proc_suspension_q* sq, spin_lock_t cpu->proc_run_q_count--; int state = proc->state; - - spin_unlock(&sq->lock, fsq); - spin_unlock(&proc->lock, fp); - spin_unlock(&cpu->lock, fc); - rctx_insert_cpu(rctx, cpu); return state; } int proc_sq_resume(struct proc* proc, struct proc_sq_entry* sq_entry, struct reschedule_ctx* rctx) { - uint64_t fc, fp, fsq; - struct cpu* cpu = cpu_find_lightest(); struct proc_suspension_q* sq = sq_entry->sq; - spin_lock(&cpu->lock, &fc); - spin_lock(&proc->lock, &fp); - spin_lock(&sq->lock, &fsq); - /* remove from sq's list */ list_remove(sq->proc_list, &sq_entry->sq_link); @@ -86,10 +61,6 @@ int proc_sq_resume(struct proc* proc, struct proc_sq_entry* sq_entry, struct res int state = proc->state; - spin_unlock(&sq->lock, fsq); - spin_unlock(&proc->lock, fp); - spin_unlock(&cpu->lock, fc); - if (sq_entry->free_func != NULL) sq_entry->free_func(sq_entry->udata); free(sq_entry); @@ -100,30 +71,20 @@ int proc_sq_resume(struct proc* proc, struct proc_sq_entry* sq_entry, struct res } void proc_sqs_cleanup(struct proc* proc) { - uint64_t fsq, fp; - - spin_lock(&proc->lock, &fp); - /* 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, &fsq); - /* 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, fsq); - if (sq_entry->free_func != NULL) sq_entry->free_func(sq_entry->udata); free(sq_entry); } - - spin_unlock(&proc->lock, fp); } diff --git a/kernel/proc/suspension_q.h b/kernel/proc/suspension_q.h index ca04aae..c73d4e9 100644 --- a/kernel/proc/suspension_q.h +++ b/kernel/proc/suspension_q.h @@ -12,7 +12,6 @@ typedef void (*sq_entry_free_udata_func_t)(void* udata); struct proc_suspension_q { struct list_node_link* proc_list; - spin_lock_t lock; }; struct proc_sq_entry { @@ -26,9 +25,8 @@ struct proc_sq_entry { void proc_sqs_cleanup(struct proc* proc); -int proc_sq_suspend(struct proc* proc, struct proc_suspension_q* sq, spin_lock_t* resource_lock, - uint64_t lockflags, struct reschedule_ctx* rctx, void* udata, - sq_entry_free_udata_func_t free_func); +int proc_sq_suspend(struct proc* proc, struct proc_suspension_q* sq, struct reschedule_ctx* rctx, + void* udata, sq_entry_free_udata_func_t free_func); int proc_sq_resume(struct proc* proc, struct proc_sq_entry* sq_entry, struct reschedule_ctx* rctx); diff --git a/kernel/sync/biglock.c b/kernel/sync/biglock.c new file mode 100644 index 0000000..53c926d --- /dev/null +++ b/kernel/sync/biglock.c @@ -0,0 +1,9 @@ +#include +#include +#include + +static spin_lock_t biglock = SPIN_LOCK_INIT; + +void biglock_lock(void) { spin_lock(&biglock); } + +void biglock_unlock(void) { spin_unlock(&biglock); } diff --git a/kernel/sync/biglock.h b/kernel/sync/biglock.h new file mode 100644 index 0000000..c9b7de7 --- /dev/null +++ b/kernel/sync/biglock.h @@ -0,0 +1,10 @@ +#ifndef _KERNEL_SYNC_BIGLOCK_H +#define _KERNEL_SYNC_BIGLOCK_H + +#include + +void biglock_lock(void); + +void biglock_unlock(void); + +#endif // _KERNEL_SYNC_BIGLOCK_H diff --git a/kernel/sync/spin_lock.c b/kernel/sync/spin_lock.c index dc5881a..09f503e 100644 --- a/kernel/sync/spin_lock.c +++ b/kernel/sync/spin_lock.c @@ -2,13 +2,9 @@ #include #include -void spin_lock(spin_lock_t* sl, uint64_t* flags) { - spin_lock_save_flags(flags); +void spin_lock(spin_lock_t* sl) { while (atomic_flag_test_and_set_explicit(sl, memory_order_acquire)) spin_lock_relax(); } -void spin_unlock(spin_lock_t* sl, uint64_t flags) { - atomic_flag_clear_explicit(sl, memory_order_release); - spin_lock_restore_flags(flags); -} +void spin_unlock(spin_lock_t* sl) { atomic_flag_clear_explicit(sl, memory_order_release); } diff --git a/kernel/sync/spin_lock.h b/kernel/sync/spin_lock.h index 6fc6da8..7cd6d1b 100644 --- a/kernel/sync/spin_lock.h +++ b/kernel/sync/spin_lock.h @@ -8,8 +8,8 @@ typedef atomic_flag spin_lock_t; -void spin_lock(spin_lock_t* sl, uint64_t* flags); +void spin_lock(spin_lock_t* sl); -void spin_unlock(spin_lock_t* sl, uint64_t flags); +void spin_unlock(spin_lock_t* sl); #endif // _KERNEL_SYNC_SPIN_LOCK_H diff --git a/kernel/sync/src.mk b/kernel/sync/src.mk index 17eabd6..b9d2de1 100644 --- a/kernel/sync/src.mk +++ b/kernel/sync/src.mk @@ -1,3 +1,5 @@ -c += sync/spin_lock.c +c += sync/spin_lock.c \ + sync/biglock.c -o += sync/spin_lock.o +o += sync/spin_lock.o \ + sync/biglock.o diff --git a/kernel/sys/sched.h b/kernel/sys/sched.h index 1326e09..9b03f05 100644 --- a/kernel/sys/sched.h +++ b/kernel/sys/sched.h @@ -4,6 +4,6 @@ #include #include -void do_sched(struct proc* proc, spin_lock_t* cpu_lock); +void do_sched(struct proc* proc); #endif // _KERNEL_SYS_SCHED_H diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 96c1c84..8ac9873 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -36,21 +36,14 @@ #define SYSRESULT(x) ((uintptr_t)(x)) static void* sys_get_user_buffer(struct procgroup* procgroup, uintptr_t uvaddr, size_t size) { - uint64_t fpg; - struct limine_hhdm_response* hhdm = limine_hhdm_request.response; - spin_lock(&procgroup->lock, &fpg); - if (!mm_validate_buffer(&procgroup->pd, (uintptr_t)uvaddr, size)) { - spin_unlock(&procgroup->lock, fpg); return NULL; } uintptr_t out_paddr = mm_v2p(&procgroup->pd, uvaddr); - spin_unlock(&procgroup->lock, fpg); - uintptr_t out_kvaddr = (uintptr_t)hhdm->offset + out_paddr; return (void*)out_kvaddr; @@ -72,8 +65,6 @@ DEFINE_SYSCALL(sys_test) { /* int map (uintptr_t vaddr, size_t pages, uint32_t flags) */ DEFINE_SYSCALL(sys_map) { - uint64_t fp; - uintptr_t vaddr = a1; size_t pages = (size_t)a2; uint32_t flags = (uint32_t)a3; @@ -81,26 +72,20 @@ DEFINE_SYSCALL(sys_map) { if (vaddr % PAGE_SIZE != 0) return SYSRESULT(-ST_UNALIGNED); - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); return SYSRESULT(procgroup_map(procgroup, vaddr, pages, flags, NULL)); } /* int unmap (uintptr_t vaddr, size_t pages) */ DEFINE_SYSCALL(sys_unmap) { - uint64_t fp; - uintptr_t vaddr = a1; size_t pages = (size_t)a2; if (vaddr % PAGE_SIZE != 0) return SYSRESULT(-ST_UNALIGNED); - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); return SYSRESULT(procgroup_unmap(procgroup, vaddr, pages)); } @@ -126,11 +111,7 @@ DEFINE_SYSCALL(sys_clone) { /* void* argument_ptr (void) */ DEFINE_SYSCALL(sys_argument_ptr) { - uint64_t fp; - - spin_lock(&proc->lock, &fp); uintptr_t p = proc->uvaddr_argument; - spin_unlock(&proc->lock, fp); return p; } @@ -143,33 +124,23 @@ DEFINE_SYSCALL(sys_sched) { /* int mutex_create (void) */ DEFINE_SYSCALL(sys_mutex_create) { - uint64_t fr, fp; - - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct proc_resource* mutex_resource = proc_create_resource_mutex(procgroup); if (mutex_resource == NULL) return SYSRESULT(-ST_OOM_ERROR); - spin_lock(&mutex_resource->lock, &fr); int rid = mutex_resource->rid; - spin_unlock(&mutex_resource->lock, fr); return SYSRESULT(rid); } /* int mutex_delete (int mutex_rid) */ DEFINE_SYSCALL(sys_mutex_delete) { - uint64_t fp; - int mutex_rid = (int)a1; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct proc_resource* mutex_resource = proc_find_resource(procgroup, mutex_rid); @@ -183,13 +154,9 @@ DEFINE_SYSCALL(sys_mutex_delete) { /* int mutex_lock (int mutex_rid) */ DEFINE_SYSCALL(sys_mutex_lock) { - uint64_t fp; - int mutex_rid = (int)a1; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct proc_resource* mutex_resource = proc_find_resource(procgroup, mutex_rid); @@ -203,13 +170,9 @@ DEFINE_SYSCALL(sys_mutex_lock) { /* int mutex_unlock (int mutex_rid) */ DEFINE_SYSCALL(sys_mutex_unlock) { - uint64_t fp; - int mutex_rid = (int)a1; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct proc_resource* mutex_resource = proc_find_resource(procgroup, mutex_rid); @@ -223,15 +186,11 @@ DEFINE_SYSCALL(sys_mutex_unlock) { /* int mail_send (int pgid, void* mesg, size_t mesg_size) */ DEFINE_SYSCALL(sys_mail_send) { - uint64_t fp; - int pgid = (int)a1; uintptr_t uvaddr_mesg = a2; size_t mesg_size = (size_t)a3; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); void* mesg = sys_get_user_buffer(procgroup, uvaddr_mesg, mesg_size); @@ -255,14 +214,10 @@ DEFINE_SYSCALL(sys_mail_send) { /* int mail_receive (void* recv_mesg, size_t mesg_size) */ DEFINE_SYSCALL(sys_mail_receive) { - uint64_t fp; - uintptr_t uvaddr_mesg = a1; size_t mesg_size = (size_t)a2; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); void* mesg = sys_get_user_buffer(procgroup, uvaddr_mesg, mesg_size); @@ -281,8 +236,6 @@ DEFINE_SYSCALL(sys_mail_receive) { /* int device_do (int device_id, int cmd, void* a1, void* a2, void* a3, void* a4) */ DEFINE_SYSCALL(sys_device_do) { - uint64_t fpg, fd, fp; - struct limine_hhdm_response* hhdm = limine_hhdm_request.response; uintptr_t uvaddr_key = a1; @@ -293,19 +246,14 @@ DEFINE_SYSCALL(sys_device_do) { uintptr_t ua4 = a6, ka4 = 0; uintptr_t out_paddr; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); if (!(cmd >= 0 && cmd < (int)fieldlengthof(struct device, ops))) return SYSRESULT(-ST_BAD_DEVICE_OP); - spin_lock(&procgroup->lock, &fpg); - out_paddr = mm_v2p(&procgroup->pd, uvaddr_key); if (out_paddr == 0) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_BAD_ADDRESS_SPACE); } @@ -327,31 +275,22 @@ DEFINE_SYSCALL(sys_device_do) { if (out_paddr != 0) ka4 = (uintptr_t)hhdm->offset + out_paddr; - spin_unlock(&procgroup->lock, fpg); - struct device* device = device_find(key); if (device == NULL) return SYSRESULT(-ST_NOT_FOUND); - spin_lock(&device->lock, &fd); - if (device->ops[cmd] == NULL) { - spin_unlock(&device->lock, fd); return SYSRESULT(-ST_NOT_FOUND); } - int ret = device_op(device, cmd, proc, rctx, &fd, ka1, ka2, ka3, ka4); - - spin_unlock(&device->lock, fd); + int ret = device_op(device, cmd, proc, rctx, ka1, ka2, ka3, ka4); return SYSRESULT(ret); } /* int exec (char* volume, char* path) */ DEFINE_SYSCALL(sys_exec) { - uint64_t fpg, fp; - uintptr_t uvaddr_volume = a1; uintptr_t uvaddr_path = a2; @@ -359,26 +298,19 @@ DEFINE_SYSCALL(sys_exec) { uintptr_t out_paddr; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; int pid1 = proc->pid; - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr); - spin_lock(&procgroup->lock, &fpg); - out_paddr = mm_v2p(&procgroup->pd, uvaddr_volume); if (out_paddr == 0) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_BAD_ADDRESS_SPACE); } @@ -387,12 +319,9 @@ DEFINE_SYSCALL(sys_exec) { struct proc* new = proc_from_file(proc, volume, path, rctx); if (new == NULL) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_EXEC_ERROR); } - spin_unlock(&procgroup->lock, fpg); - int pid = new->pid; new->exec_pid = pid1; @@ -403,8 +332,6 @@ DEFINE_SYSCALL(sys_exec) { /* int exec_partial (char* volume, char* path) */ DEFINE_SYSCALL(sys_exec_partial) { - uint64_t fpg, fp; - uintptr_t uvaddr_volume = a1; uintptr_t uvaddr_path = a2; @@ -412,26 +339,19 @@ DEFINE_SYSCALL(sys_exec_partial) { uintptr_t out_paddr; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; int pid1 = proc->pid; - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr); - spin_lock(&procgroup->lock, &fpg); - out_paddr = mm_v2p(&procgroup->pd, uvaddr_volume); if (out_paddr == 0) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_BAD_ADDRESS_SPACE); } @@ -440,12 +360,9 @@ DEFINE_SYSCALL(sys_exec_partial) { struct proc* new = proc_from_file(proc, volume, path, rctx); if (new == NULL) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_EXEC_ERROR); } - spin_unlock(&procgroup->lock, fpg); - int pid = new->pid; new->exec_pid = pid1; new->state = PROC_PARTIAL; @@ -457,8 +374,6 @@ DEFINE_SYSCALL(sys_exec_partial) { /* int exec_partial_fini (int pid) */ DEFINE_SYSCALL(sys_exec_partial_fini) { - uint64_t fp, fc; - int pid = (int)a1; struct proc* target_proc = proc_find_pid(pid); @@ -466,29 +381,18 @@ DEFINE_SYSCALL(sys_exec_partial_fini) { if (target_proc == NULL) return SYSRESULT(-ST_NOT_FOUND); - spin_lock(&target_proc->lock, &fp); - if (target_proc->state != PROC_PARTIAL) { - spin_unlock(&target_proc->lock, fp); return SYSRESULT(-ST_NOT_PARTIAL); } - spin_unlock(&target_proc->lock, fp); - struct cpu* cpu = cpu_find_lightest(); - spin_lock(&cpu->lock, &fc); - spin_lock(&target_proc->lock, &fp); - target_proc->cpu = cpu; target_proc->state = PROC_READY; cpu->proc_run_q_count++; list_append(cpu->proc_run_q, &target_proc->cpu_run_q_link); - spin_unlock(&target_proc->lock, fp); - spin_unlock(&cpu->lock, fc); - rctx_insert_cpu(rctx, cpu); return SYSRESULT(ST_OK); @@ -496,21 +400,15 @@ DEFINE_SYSCALL(sys_exec_partial_fini) { /* int volume_open (char* volume) */ DEFINE_SYSCALL(sys_volume_open) { - uint64_t fpg, fp; - uintptr_t uvaddr_volume = a1; struct limine_hhdm_response* hhdm = limine_hhdm_request.response; uintptr_t out_paddr; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_volume); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -522,28 +420,20 @@ DEFINE_SYSCALL(sys_volume_open) { if (ret < 0) return SYSRESULT(ret); - spin_lock(&proc->lock, &fp); strncpy(proc->cwv, volume, VOLUME_MAX); - spin_unlock(&proc->lock, fp); return SYSRESULT(ST_OK); } /* int volume_close (void) */ DEFINE_SYSCALL(sys_volume_close) { - uint64_t fp; - char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); int ret = vfs_volume_close(proc, cwv, rctx); if (ret == ST_OK) { - spin_lock(&proc->lock, &fp); memset(proc->cwv, 0, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); } return SYSRESULT(ret); @@ -551,8 +441,6 @@ DEFINE_SYSCALL(sys_volume_close) { /* int read_file (char* path, size_t off, uint8_t* buffer, size_t size) */ DEFINE_SYSCALL(sys_read_file) { - uint64_t fpg, fp; - uintptr_t uvaddr_path = a1; size_t off = (size_t)a2; uintptr_t uvaddr_buffer = a3; @@ -563,14 +451,10 @@ DEFINE_SYSCALL(sys_read_file) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -589,8 +473,6 @@ DEFINE_SYSCALL(sys_read_file) { /* int describe (char* path, struct desc* desc) */ DEFINE_SYSCALL(sys_describe) { - uint64_t fpg, fp; - uintptr_t uvaddr_path = a1; uintptr_t uvaddr_desc = a2; @@ -599,14 +481,10 @@ DEFINE_SYSCALL(sys_describe) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -625,8 +503,6 @@ DEFINE_SYSCALL(sys_describe) { /* int get_procgroup (int pid) */ DEFINE_SYSCALL(sys_get_procgroup) { - uint64_t fp, fpg; - int pid = (int)a1; struct proc* target_proc = proc_find_pid(pid); @@ -634,21 +510,15 @@ DEFINE_SYSCALL(sys_get_procgroup) { if (target_proc == NULL) return SYSRESULT(-ST_NOT_FOUND); - spin_lock(&target_proc->lock, &fp); struct procgroup* procgroup = target_proc->procgroup; - spin_unlock(&target_proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); int pgid = procgroup->pgid; - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(pgid); } /* int read_dir_entry (char* path, struct dir_entry* entry, size_t entry_num) */ DEFINE_SYSCALL(sys_read_dir_entry) { - uint64_t fpg, fp; - uintptr_t uvaddr_path = a1; uintptr_t uvaddr_entry = a2; size_t entry_num = (size_t)a3; @@ -658,14 +528,10 @@ DEFINE_SYSCALL(sys_read_dir_entry) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -684,8 +550,6 @@ DEFINE_SYSCALL(sys_read_dir_entry) { /* int create_file (char* path) */ DEFINE_SYSCALL(sys_create_file) { - uint64_t fpg, fp; - uintptr_t uvaddr_path = a1; struct limine_hhdm_response* hhdm = limine_hhdm_request.response; @@ -693,14 +557,10 @@ DEFINE_SYSCALL(sys_create_file) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -714,8 +574,6 @@ DEFINE_SYSCALL(sys_create_file) { /* int write_file (char* path, size_t off, uint8_t* buffer, size_t size, uint32_t flags) */ DEFINE_SYSCALL(sys_write_file) { - uint64_t fpg, fp; - uintptr_t uvaddr_path = a1; size_t off = (size_t)a2; uintptr_t uvaddr_buffer = a3; @@ -727,14 +585,10 @@ DEFINE_SYSCALL(sys_write_file) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -753,11 +607,7 @@ DEFINE_SYSCALL(sys_write_file) { /* int get_exec_pid (void) */ DEFINE_SYSCALL(sys_get_exec_pid) { - uint64_t fp; - - spin_lock(&proc->lock, &fp); int exec_pid = proc->exec_pid; - spin_unlock(&proc->lock, fp); return SYSRESULT(exec_pid); } @@ -792,8 +642,6 @@ DEFINE_SYSCALL(sys_kill) { /* int create_dir (char* path) */ DEFINE_SYSCALL(sys_create_dir) { - uint64_t fpg, fp; - uintptr_t uvaddr_path = a1; struct limine_hhdm_response* hhdm = limine_hhdm_request.response; @@ -801,14 +649,10 @@ DEFINE_SYSCALL(sys_create_dir) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -822,8 +666,6 @@ DEFINE_SYSCALL(sys_create_dir) { /* int remove (char* path) */ DEFINE_SYSCALL(sys_remove) { - uint64_t fpg, fp; - uintptr_t uvaddr_path = a1; struct limine_hhdm_response* hhdm = limine_hhdm_request.response; @@ -831,14 +673,10 @@ DEFINE_SYSCALL(sys_remove) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_path); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -852,8 +690,6 @@ DEFINE_SYSCALL(sys_remove) { /* int create_volume (char* key, int fs_type, char* device_key) */ DEFINE_SYSCALL(sys_create_volume) { - uint64_t fpg, fp; - uintptr_t uvaddr_key = a1; int type = (int)a2; uintptr_t uvaddr_device_key = a3; @@ -863,17 +699,12 @@ DEFINE_SYSCALL(sys_create_volume) { uintptr_t out_paddr; char cwv[VOLUME_MAX]; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; memcpy(cwv, proc->cwv, sizeof(proc->cwv)); - spin_unlock(&proc->lock, fp); - - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_key); if (out_paddr == 0) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_BAD_ADDRESS_SPACE); } @@ -882,14 +713,11 @@ DEFINE_SYSCALL(sys_create_volume) { out_paddr = mm_v2p(&procgroup->pd, uvaddr_device_key); if (out_paddr == 0) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_BAD_ADDRESS_SPACE); } const char* device_key = (const char*)((uintptr_t)hhdm->offset + out_paddr); - spin_unlock(&procgroup->lock, fpg); - struct device* device = device_find(device_key); if (device == NULL) @@ -900,8 +728,6 @@ DEFINE_SYSCALL(sys_create_volume) { /* int env_set (int pgid, char* key, void* buffer, size_t size) */ DEFINE_SYSCALL(sys_env_set) { - uint64_t fp, fpg; - int pgid = (int)a1; uintptr_t uvaddr_key = a2; uintptr_t uvaddr_buffer = a3; @@ -916,23 +742,16 @@ DEFINE_SYSCALL(sys_env_set) { uintptr_t out_paddr; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); - - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_key); if (out_paddr == 0) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_BAD_ADDRESS_SPACE); } const char* key = (const char*)((uintptr_t)hhdm->offset + out_paddr); - spin_unlock(&procgroup->lock, fpg); - void* buffer = sys_get_user_buffer(procgroup, uvaddr_buffer, size); if (buffer == NULL) @@ -943,8 +762,6 @@ DEFINE_SYSCALL(sys_env_set) { /* int env_get (int pgid, char* key, void* buffer, size_t size) */ DEFINE_SYSCALL(sys_env_get) { - uint64_t fp, fpg; - int pgid = (int)a1; uintptr_t uvaddr_key = a2; uintptr_t uvaddr_buffer = a3; @@ -959,23 +776,16 @@ DEFINE_SYSCALL(sys_env_get) { uintptr_t out_paddr; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); - - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_key); if (out_paddr == 0) { - spin_unlock(&procgroup->lock, fpg); return SYSRESULT(-ST_BAD_ADDRESS_SPACE); } const char* key = (const char*)((uintptr_t)hhdm->offset + out_paddr); - spin_unlock(&procgroup->lock, fpg); - void* buffer = sys_get_user_buffer(procgroup, uvaddr_buffer, size); if (buffer == NULL) @@ -986,19 +796,13 @@ DEFINE_SYSCALL(sys_env_get) { /* int get_self_pid (void) */ DEFINE_SYSCALL(sys_get_self_pid) { - uint64_t fp; - - spin_lock(&proc->lock, &fp); int pid = proc->pid; - spin_unlock(&proc->lock, fp); return SYSRESULT(pid); } /* int stream_write (int pgid, int rid, void* buffer, size_t size) */ DEFINE_SYSCALL(sys_stream_write) { - uint64_t fp; - int pgid = (int)a1; int rid = (int)a2; uintptr_t uvaddr_buffer = a3; @@ -1009,9 +813,7 @@ DEFINE_SYSCALL(sys_stream_write) { if (target_procgroup == NULL) return SYSRESULT(-ST_NOT_FOUND); - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); void* buffer = sys_get_user_buffer(procgroup, uvaddr_buffer, buffer_size); @@ -1030,8 +832,6 @@ DEFINE_SYSCALL(sys_stream_write) { /* int stream_read (int pgid, int rid, void* buffer, size_t size) */ DEFINE_SYSCALL(sys_stream_read) { - uint64_t fp; - int pgid = (int)a1; int rid = (int)a2; uintptr_t uvaddr_buffer = a3; @@ -1042,9 +842,7 @@ DEFINE_SYSCALL(sys_stream_read) { if (target_procgroup == NULL) return SYSRESULT(-ST_NOT_FOUND); - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); void* buffer = sys_get_user_buffer(procgroup, uvaddr_buffer, buffer_size); @@ -1061,14 +859,10 @@ DEFINE_SYSCALL(sys_stream_read) { /* int get_proc_int (struct proc_info* infos, size_t count) */ DEFINE_SYSCALL(sys_get_proc_info) { - uint64_t fp; - uintptr_t uvaddr_infos = a1; size_t infos_count = (size_t)a2; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct proc_info* infos = sys_get_user_buffer(procgroup, uvaddr_infos, infos_count * sizeof(struct proc_info)); @@ -1081,14 +875,10 @@ DEFINE_SYSCALL(sys_get_proc_info) { /* int get_device_info (struct device_info* infos, size_t count) */ DEFINE_SYSCALL(sys_get_device_info) { - uint64_t fp; - uintptr_t uvaddr_infos = a1; size_t infos_count = (size_t)a2; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct device_info* infos = sys_get_user_buffer(procgroup, uvaddr_infos, infos_count * sizeof(struct device_info)); @@ -1101,14 +891,10 @@ DEFINE_SYSCALL(sys_get_device_info) { /* int get_volume_info (struct volume_info* infos, size_t count) */ DEFINE_SYSCALL(sys_get_volume_info) { - uint64_t fp; - uintptr_t uvaddr_infos = a1; size_t infos_count = (size_t)a2; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct volume_info* infos = sys_get_user_buffer(procgroup, uvaddr_infos, infos_count * sizeof(struct volume_info)); @@ -1121,21 +907,15 @@ DEFINE_SYSCALL(sys_get_volume_info) { /* int volume_delete (const char* volume) */ DEFINE_SYSCALL(sys_volume_delete) { - uint64_t fpg, fp; - uintptr_t uvaddr_volume = a1; struct limine_hhdm_response* hhdm = limine_hhdm_request.response; uintptr_t out_paddr; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); - spin_lock(&procgroup->lock, &fpg); out_paddr = mm_v2p(&procgroup->pd, uvaddr_volume); - spin_unlock(&procgroup->lock, fpg); if (out_paddr == 0) return SYSRESULT(-ST_BAD_ADDRESS_SPACE); @@ -1152,13 +932,9 @@ DEFINE_SYSCALL(sys_volume_delete) { /* int date_time (struct date_time* dt) */ DEFINE_SYSCALL(sys_date_time) { - uint64_t fp; - uintptr_t uvaddr_dt = a1; - spin_lock(&proc->lock, &fp); struct procgroup* procgroup = proc->procgroup; - spin_unlock(&proc->lock, fp); struct date_time* dt = sys_get_user_buffer(procgroup, uvaddr_dt, sizeof(struct date_time));