Compare commits

..

32 Commits

Author SHA1 Message Date
wash b92542c688 syscall: handle: add stub implementation of kern_handle_control 2026-04-01 18:41:33 +01:00
wash 3c494f0c4d libmango: add kern_handle_control syscall 2026-04-01 18:40:58 +01:00
wash b8d3125233 syscall: task: initial implementation of task_duplicate 2026-04-01 18:40:28 +01:00
wash 304ba8b254 libmango: add task_duplicate syscall 2026-04-01 18:39:38 +01:00
wash 06fe1e3704 vm: address-space: implement resolving accesses to copy-on-write pages 2026-04-01 18:38:17 +01:00
wash 6365154b75 syscall: add missing call to put_current_task 2026-04-01 18:37:10 +01:00
wash a0cab068da vm: address-space: implement copy-on-write address-space duplication 2026-04-01 18:36:25 +01:00
wash c8202c6741 vm: controller: implement detach of vm-object whose attachment was deferred 2026-04-01 18:35:08 +01:00
wash 2cbfa7d7d2 vm: object: implement creating copy-on-write duplicates of vm-objects 2026-04-01 18:33:24 +01:00
wash 4143e12a29 vm: add a copy-on-write ref count to vm_page 2026-04-01 18:32:32 +01:00
wash f28fab7afa vm: object: implement vm_object_put_page to add existing pages to a vmo 2026-04-01 18:31:05 +01:00
wash 6c5bf2344f sched: thread: implement cloning of userspace thread contexts 2026-04-01 18:29:54 +01:00
wash 61050fd54b kernel: atomic: change atomic_t to 32-bit int 2026-04-01 18:27:31 +01:00
wash 8595f705af vm: address-space: fix vm-object and address space not being unlocked when a demand-map fails 2026-04-01 18:26:27 +01:00
wash dd34b1d80f pmap: add pmap_get to retrieve existing page table entries 2026-04-01 18:24:13 +01:00
wash 876f91d8be sched: thread: add cpu context pointer usable during interrupts and syscalls 2026-04-01 18:22:23 +01:00
wash 7bcd1577be pmap: fix PFN() not clearing upper PTE control bits 2026-04-01 18:20:50 +01:00
wash db1a200eea vm: object: fix vm_object_cleanup referencing a vmo controller after the pointer is erased 2026-04-01 18:19:23 +01:00
wash f45b759a4c vm: object: fix vm_object_get_page ignoring VMO_REQUEST_MISSING_PAGE 2026-04-01 18:18:25 +01:00
wash 512356ac2d sched: enforce ref-counting on current task/thread pointers 2026-04-01 18:17:05 +01:00
wash 15c2207ab9 x86_64: pmap: enable kernel-mode write-protection 2026-04-01 18:06:50 +01:00
wash 8b41f5e681 kernel: remove unused functionality 2026-03-29 14:54:07 +01:00
wash 325699d64a kernel: fix sys_msg_send returning without unlocking self 2026-03-29 11:51:26 +01:00
wash 286016040c kernel: only show task name/id in log output if TRACE is enabled 2026-03-29 11:51:05 +01:00
wash 04617e81e3 kernel: add a syscall to query generic information about an object 2026-03-29 11:50:37 +01:00
wash 62770f4ab2 kernel: replace kern_handle_duplicate with the more powerful kern_handle_transfer
this syscall can move and copy handles within the current task, or from/to
other tasks
2026-03-29 11:48:59 +01:00
wash 9001f8e064 kernel: handle: rename handle transfer mode constants 2026-03-29 11:48:31 +01:00
wash 537242e606 kernel: handle: add support for allocating a specific handle value 2026-03-29 11:47:22 +01:00
wash dfffb45e66 libc: adjust formatting 2026-03-29 11:43:39 +01:00
wash f5a83af0d7 kernel: remove SEND_BLOCKED and REPLY_BLOCKED statuses from ports
this allows a port to be used by multiple threads at the same time
2026-03-29 11:42:23 +01:00
wash 7d25f1c31a x86_64: suppress serial port input logging 2026-03-29 11:32:03 +01:00
wash 95d33ddcb9 kernel: msg: async messages no longer hold a pointer to the thread/port that sent them
this prevents a race condition where an event is sent as a port is being destroyed.
when the server gets around to handling the event, it now refers to a different port
that was created in the mean-time.
2026-03-25 20:19:19 +00:00
61 changed files with 1451 additions and 682 deletions
View File
+2 -1
View File
@@ -1,8 +1,8 @@
#ifndef ARCH_PAGING_H_ #ifndef ARCH_PAGING_H_
#define ARCH_PAGING_H_ #define ARCH_PAGING_H_
#include <kernel/types.h>
#include <kernel/compiler.h> #include <kernel/compiler.h>
#include <kernel/types.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -55,6 +55,7 @@ enum page_size {
defined in pmap_ctrl.S */ defined in pmap_ctrl.S */
extern int gigabyte_pages(void); extern int gigabyte_pages(void);
extern int enable_nx(void); extern int enable_nx(void);
extern int enable_wp(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
@@ -31,6 +31,12 @@ extern kern_status_t ml_thread_prepare_user_context(
virt_addr_t *kernel_sp, virt_addr_t *kernel_sp,
const uintptr_t *args, const uintptr_t *args,
size_t nr_args); size_t nr_args);
/* prepare the stack so that ml_thread_switch_user can jump to usermode
* with the specified register context */
extern kern_status_t ml_thread_clone_user_context(
const struct ml_cpu_context *ctx,
uintptr_t return_value,
virt_addr_t *kernel_sp);
extern kern_status_t ml_thread_config_get( extern kern_status_t ml_thread_config_get(
struct thread *thread, struct thread *thread,
+14
View File
@@ -8,6 +8,7 @@
#include <kernel/panic.h> #include <kernel/panic.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/syscall.h> #include <kernel/syscall.h>
#include <kernel/thread.h>
#include <stddef.h> #include <stddef.h>
#define MAX_ISR_HANDLERS 16 #define MAX_ISR_HANDLERS 16
@@ -166,6 +167,12 @@ int idt_load(struct idt_ptr *ptr)
void isr_dispatch(struct ml_cpu_context *regs) void isr_dispatch(struct ml_cpu_context *regs)
{ {
struct thread *thr = get_current_thread();
if (thr) {
thr->tr_irqctx = regs;
put_current_thread(thr);
}
int_hook h = isr_handlers[regs->int_no]; int_hook h = isr_handlers[regs->int_no];
if (h) { if (h) {
h(regs); h(regs);
@@ -188,6 +195,13 @@ void irq_dispatch(struct ml_cpu_context *regs)
end_charge_period(); end_charge_period();
irq_ack(regs->int_no); irq_ack(regs->int_no);
struct thread *thr = get_current_thread();
if (thr) {
thr->tr_irqctx = regs;
put_current_thread(thr);
}
struct queue *hooks = &irq_hooks[regs->int_no - IRQ0]; struct queue *hooks = &irq_hooks[regs->int_no - IRQ0];
queue_foreach(struct irq_hook, hook, hooks, irq_entry) queue_foreach(struct irq_hook, hook, hooks, irq_entry)
{ {
+4 -2
View File
@@ -217,16 +217,18 @@ static void print_stack_trace(
void ml_print_stack_trace(uintptr_t ip) void ml_print_stack_trace(uintptr_t ip)
{ {
struct task *task = current_task(); struct task *task = get_current_task();
struct address_space *space = task ? task->t_address_space : NULL; struct address_space *space = task ? task->t_address_space : NULL;
uintptr_t bp; uintptr_t bp;
asm volatile("mov %%rbp, %0" : "=r"(bp)); asm volatile("mov %%rbp, %0" : "=r"(bp));
print_stack_trace(space, ip, bp); print_stack_trace(space, ip, bp);
put_current_task(task);
} }
void ml_print_stack_trace_irq(struct ml_cpu_context *ctx) void ml_print_stack_trace_irq(struct ml_cpu_context *ctx)
{ {
struct task *task = current_task(); struct task *task = get_current_task();
struct address_space *space = task ? task->t_address_space : NULL; struct address_space *space = task ? task->t_address_space : NULL;
print_stack_trace(space, ctx->rip, ctx->rbp); print_stack_trace(space, ctx->rip, ctx->rbp);
put_current_task(task);
} }
+84 -7
View File
@@ -19,7 +19,7 @@
#define PTR_TO_ENTRY(x) (((x) & ~VM_PAGE_MASK) | PTE_PRESENT | PTE_RW | PTE_USR) #define PTR_TO_ENTRY(x) (((x) & ~VM_PAGE_MASK) | PTE_PRESENT | PTE_RW | PTE_USR)
#define ENTRY_TO_PTR(x) ((x) & ~VM_PAGE_MASK) #define ENTRY_TO_PTR(x) ((x) & ~VM_PAGE_MASK)
#define PFN(x) ((x) >> VM_PAGE_SHIFT) #define PFN(x) (((x) >> VM_PAGE_SHIFT) & 0xFFFFFFFFFF)
static int can_use_gbpages = 0; static int can_use_gbpages = 0;
static pmap_t kernel_pmap; static pmap_t kernel_pmap;
@@ -136,6 +136,81 @@ static void delete_pdir(phys_addr_t pd)
kfree(pdir); kfree(pdir);
} }
kern_status_t pmap_get(
pmap_t pmap,
virt_addr_t pv,
pfn_t *out_pfn,
vm_prot_t *out_prot)
{
unsigned int pml4t_index = BAD_INDEX, pdpt_index = BAD_INDEX,
pd_index = BAD_INDEX, pt_index = BAD_INDEX;
pml4t_index = (pv >> 39) & 0x1FF;
pdpt_index = (pv >> 30) & 0x1FF;
pd_index = (pv >> 21) & 0x1FF;
pt_index = (pv >> 12) & 0x1FF;
/* 1. get PML4T (mandatory) */
struct pml4t *pml4t = vm_phys_to_virt(ENTRY_TO_PTR(pmap));
if (!pml4t) {
return KERN_INVALID_ARGUMENT;
}
/* 2. traverse PML4T, get PDPT (mandatory) */
struct pdpt *pdpt = NULL;
if (!pml4t->p_entries[pml4t_index]) {
return KERN_NO_ENTRY;
} else {
pdpt = vm_phys_to_virt(
ENTRY_TO_PTR(pml4t->p_entries[pml4t_index]));
}
/* 3. traverse PDPT, get PDIR (optional, 4K and 2M only) */
struct pdir *pdir = NULL;
if (!pdpt->p_entries[pdpt_index]
|| pdpt->p_pages[pdpt_index] & PTE_PAGESIZE) {
return KERN_NO_ENTRY;
} else {
pdir = vm_phys_to_virt(
ENTRY_TO_PTR(pdpt->p_entries[pdpt_index]));
}
/* 4. traverse PDIR, get PTAB (optional, 4K only) */
struct ptab *ptab = NULL;
if (!pdir->p_entries[pd_index]
|| pdir->p_pages[pd_index] & PTE_PAGESIZE) {
/* entry is null, or points to a hugepage */
return KERN_NO_ENTRY;
} else {
ptab = vm_phys_to_virt(ENTRY_TO_PTR(pdir->p_entries[pd_index]));
}
uint64_t pte = ptab->p_pages[pt_index];
if (out_pfn) {
*out_pfn = PFN(pte);
}
if (out_prot) {
if (pte & PTE_PRESENT) {
*out_prot |= VM_PROT_USER;
}
if (pte & PTE_RW) {
*out_pfn |= (VM_PROT_READ | VM_PROT_WRITE);
}
if (pte & PTE_USR) {
*out_pfn |= VM_PROT_USER;
}
if (!(pte & PTE_NX)) {
*out_pfn |= VM_PROT_EXEC;
}
}
return KERN_OK;
}
static kern_status_t do_pmap_add( static kern_status_t do_pmap_add(
pmap_t pmap, pmap_t pmap,
virt_addr_t pv, virt_addr_t pv,
@@ -351,6 +426,8 @@ void pmap_bootstrap(void)
can_use_gbpages == 1 ? "en" : "dis"); can_use_gbpages == 1 ? "en" : "dis");
enable_nx(); enable_nx();
printk("pmap: NX protection enabled"); printk("pmap: NX protection enabled");
enable_wp();
printk("pmap: kernel-mode write protection enabled");
enum page_size hugepage = PS_2M; enum page_size hugepage = PS_2M;
if (can_use_gbpages) { if (can_use_gbpages) {
@@ -494,11 +571,7 @@ kern_status_t pmap_handle_fault(
{ {
// log_fault(fault_addr, flags); // log_fault(fault_addr, flags);
if (flags & PMAP_FAULT_PRESENT) { struct task *task = get_current_task();
return KERN_FATAL_ERROR;
}
struct task *task = current_task();
if (!task) { if (!task) {
return KERN_FATAL_ERROR; return KERN_FATAL_ERROR;
} }
@@ -509,7 +582,11 @@ kern_status_t pmap_handle_fault(
} }
/* this must be called with `space` unlocked. */ /* this must be called with `space` unlocked. */
return address_space_demand_map(space, fault_addr, flags); kern_status_t status
= address_space_demand_map(space, fault_addr, flags);
put_current_task(task);
return status;
} }
kern_status_t pmap_add( kern_status_t pmap_add(
+12
View File
@@ -30,6 +30,7 @@ gigabyte_pages:
pop %rbp pop %rbp
ret ret
.global enable_nx .global enable_nx
.type enable_nx, @function .type enable_nx, @function
@@ -40,3 +41,14 @@ enable_nx:
wrmsr wrmsr
ret ret
.global enable_wp
.type enable_wp, @function
enable_wp:
mov %cr0, %rax
or $0x10000, %rax
mov %rax, %cr0
ret
+2
View File
@@ -138,6 +138,7 @@ static struct console serialcon = {
static int serial_irq1(void) static int serial_irq1(void)
{ {
#if 0
if (serial_received(COM1)) { if (serial_received(COM1)) {
unsigned char c = serial_recv_byte(COM1); unsigned char c = serial_recv_byte(COM1);
printk("serial: COM1 received %c", c); printk("serial: COM1 received %c", c);
@@ -147,6 +148,7 @@ static int serial_irq1(void)
unsigned char c = serial_recv_byte(COM3); unsigned char c = serial_recv_byte(COM3);
printk("serial: COM3 received %c", c); printk("serial: COM3 received %c", c);
} }
#endif
return 0; return 0;
} }
+28 -6
View File
@@ -80,6 +80,21 @@ extern kern_status_t ml_thread_prepare_user_context(
return KERN_OK; return KERN_OK;
} }
kern_status_t ml_thread_clone_user_context(
const struct ml_cpu_context *src_ctx,
uintptr_t return_value,
virt_addr_t *kernel_sp)
{
(*kernel_sp) -= sizeof(struct ml_cpu_context);
struct ml_cpu_context *ctx = (struct ml_cpu_context *)(*kernel_sp);
memcpy(ctx, src_ctx, sizeof *ctx);
ctx->rax = return_value;
return KERN_OK;
}
kern_status_t ml_thread_config_get( kern_status_t ml_thread_config_get(
struct thread *thread, struct thread *thread,
kern_config_key_t key, kern_config_key_t key,
@@ -95,25 +110,30 @@ kern_status_t ml_thread_config_set(
const void *ptr, const void *ptr,
size_t len) size_t len)
{ {
struct thread *self = get_current_thread();
kern_status_t status = KERN_OK;
switch (key) { switch (key) {
case THREAD_CFG_FSBASE: case THREAD_CFG_FSBASE:
if (len != sizeof(thread->tr_ml.tr_fsbase)) { if (len != sizeof(thread->tr_ml.tr_fsbase)) {
return KERN_INVALID_ARGUMENT; status = KERN_INVALID_ARGUMENT;
break;
} }
thread->tr_ml.tr_fsbase = *(virt_addr_t *)ptr; thread->tr_ml.tr_fsbase = *(virt_addr_t *)ptr;
if (thread == current_thread()) { if (thread == self) {
wrmsr(MSR_FS_BASE, thread->tr_ml.tr_fsbase); wrmsr(MSR_FS_BASE, thread->tr_ml.tr_fsbase);
} }
break; break;
case THREAD_CFG_GSBASE: case THREAD_CFG_GSBASE:
if (len != sizeof(thread->tr_ml.tr_gsbase)) { if (len != sizeof(thread->tr_ml.tr_gsbase)) {
return KERN_INVALID_ARGUMENT; status = KERN_INVALID_ARGUMENT;
break;
} }
thread->tr_ml.tr_gsbase = *(virt_addr_t *)ptr; thread->tr_ml.tr_gsbase = *(virt_addr_t *)ptr;
if (thread == current_thread()) { if (thread == self) {
/* we're in the kernel right now, so the user and kernel /* we're in the kernel right now, so the user and kernel
* gs-base registers are swapped. when we return to * gs-base registers are swapped. when we return to
* usermode, this value will be swapped back into * usermode, this value will be swapped back into
@@ -123,8 +143,10 @@ kern_status_t ml_thread_config_set(
break; break;
default: default:
return KERN_INVALID_ARGUMENT; status = KERN_INVALID_ARGUMENT;
break;
} }
return KERN_OK; put_current_thread(self);
return status;
} }
-189
View File
@@ -1,189 +0,0 @@
#include <kernel/ringbuffer.h>
#include <kernel/sched.h>
#include <kernel/vm.h>
size_t ringbuffer_unread(struct ringbuffer *ring_buffer)
{
if (ring_buffer->r_read_ptr == ring_buffer->r_write_ptr) {
return 0;
}
if (ring_buffer->r_read_ptr > ring_buffer->r_write_ptr) {
return (ring_buffer->r_size - ring_buffer->r_read_ptr)
+ ring_buffer->r_write_ptr;
} else {
return (ring_buffer->r_write_ptr - ring_buffer->r_read_ptr);
}
}
size_t ringbuffer_avail(struct ringbuffer *ring_buffer)
{
if (ring_buffer->r_read_ptr == ring_buffer->r_write_ptr) {
return ring_buffer->r_size - 1;
}
if (ring_buffer->r_read_ptr > ring_buffer->r_write_ptr) {
return ring_buffer->r_read_ptr - ring_buffer->r_write_ptr - 1;
} else {
return (ring_buffer->r_size - ring_buffer->r_write_ptr)
+ ring_buffer->r_read_ptr - 1;
}
}
static inline void increment_read(struct ringbuffer *ring_buffer)
{
ring_buffer->r_read_ptr++;
if (ring_buffer->r_read_ptr == ring_buffer->r_size) {
ring_buffer->r_read_ptr = 0;
}
}
static inline void increment_write(struct ringbuffer *ring_buffer)
{
ring_buffer->r_write_ptr++;
if (ring_buffer->r_write_ptr == ring_buffer->r_size) {
ring_buffer->r_write_ptr = 0;
}
}
size_t ringbuffer_read(
struct ringbuffer *ring_buffer,
size_t size,
void *p,
mango_flags_t flags)
{
if (!ring_buffer) {
return 0;
}
unsigned char *buffer = p;
unsigned long lock_flags;
size_t collected = 0;
while (collected < size) {
spin_lock_irqsave(&ring_buffer->r_lock, &lock_flags);
while (ringbuffer_unread(ring_buffer) > 0 && collected < size) {
buffer[collected]
= ring_buffer
->r_buffer[ring_buffer->r_read_ptr];
increment_read(ring_buffer);
collected++;
}
wakeup_queue(&ring_buffer->r_wait_writers);
if (flags & S_NOBLOCK) {
spin_unlock_irqrestore(
&ring_buffer->r_lock,
lock_flags);
break;
}
struct wait_item waiter;
wait_item_init(&waiter, current_thread());
thread_wait_begin(&waiter, &ring_buffer->r_wait_readers);
spin_unlock_irqrestore(&ring_buffer->r_lock, lock_flags);
if (collected < size) {
schedule(SCHED_NORMAL);
}
thread_wait_end(&waiter, &ring_buffer->r_wait_readers);
}
wakeup_queue(&ring_buffer->r_wait_writers);
return collected;
}
size_t ringbuffer_write(
struct ringbuffer *ring_buffer,
size_t size,
const void *p,
mango_flags_t flags)
{
if (!ring_buffer || !size) {
return 0;
}
const unsigned char *buffer = p;
unsigned long lock_flags;
size_t written = 0;
while (written < size) {
spin_lock_irqsave(&ring_buffer->r_lock, &lock_flags);
while (ringbuffer_avail(ring_buffer) > 0 && written < size) {
ring_buffer->r_buffer[ring_buffer->r_write_ptr]
= buffer[written];
increment_write(ring_buffer);
written++;
}
wakeup_queue(&ring_buffer->r_wait_readers);
if (flags & S_NOBLOCK) {
spin_unlock_irqrestore(
&ring_buffer->r_lock,
lock_flags);
break;
}
struct wait_item waiter;
wait_item_init(&waiter, current_thread());
thread_wait_begin(&waiter, &ring_buffer->r_wait_writers);
spin_unlock_irqrestore(&ring_buffer->r_lock, lock_flags);
if (written < size) {
schedule(SCHED_NORMAL);
}
thread_wait_end(&waiter, &ring_buffer->r_wait_writers);
}
wakeup_queue(&ring_buffer->r_wait_readers);
return written;
}
struct ringbuffer *ringbuffer_create(size_t size)
{
struct ringbuffer *out = kzalloc(sizeof(struct ringbuffer), VM_NORMAL);
if (!out) {
return NULL;
}
if (ringbuffer_init(out, size) != KERN_OK) {
kfree(out);
return NULL;
}
return out;
}
void ringbuffer_destroy(struct ringbuffer *ring_buffer)
{
ringbuffer_deinit(ring_buffer);
kfree(ring_buffer);
}
kern_status_t ringbuffer_init(struct ringbuffer *buf, size_t size)
{
buf->r_buffer = kmalloc(size, VM_NORMAL);
if (!buf->r_buffer) {
return KERN_NO_MEMORY;
}
buf->r_write_ptr = 0;
buf->r_read_ptr = 0;
buf->r_size = size;
buf->r_lock = SPIN_LOCK_INIT;
return KERN_OK;
}
kern_status_t ringbuffer_deinit(struct ringbuffer *buf)
{
kfree(buf->r_buffer);
buf->r_buffer = NULL;
return KERN_OK;
}
+6
View File
@@ -105,6 +105,12 @@ extern kern_status_t address_space_release(
virt_addr_t base, virt_addr_t base,
size_t length); size_t length);
/* duplicate all of the mappings in `src` within `dest. the duplication will use
* copy-on-write; page data will not be copied until it is written to. */
extern kern_status_t address_space_duplicate(
struct address_space *dest,
struct address_space *src);
extern bool address_space_validate_access( extern bool address_space_validate_access(
struct address_space *region, struct address_space *region,
virt_addr_t base, virt_addr_t base,
+1 -1
View File
@@ -4,7 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
typedef int64_t atomic_t; typedef int32_t atomic_t;
/* load and return the value pointed to by `v` */ /* load and return the value pointed to by `v` */
static inline atomic_t atomic_load(atomic_t *v) static inline atomic_t atomic_load(atomic_t *v)
-49
View File
@@ -1,49 +0,0 @@
#ifndef KERNEL_FB_H_
#define KERNEL_FB_H_
#include <stdint.h>
enum framebuffer_flags {
FB_MODE_RGB = 0x01u,
FB_MODE_VGATEXT = 0x02u,
FB_MODE_PALETTE = 0x04u,
};
struct framebuffer_bitfield {
uint32_t b_offset;
uint16_t b_length;
};
struct framebuffer_varinfo {
enum framebuffer_flags fb_flags;
uint32_t fb_xres;
uint32_t fb_yres;
uint32_t fb_bpp;
uint32_t fb_stride;
union {
struct {
uint32_t fb_xcells;
uint32_t fb_ycells;
};
struct {
struct framebuffer_bitfield fb_red;
struct framebuffer_bitfield fb_green;
struct framebuffer_bitfield fb_blue;
struct framebuffer_bitfield fb_alpha;
};
struct {
uintptr_t fb_palette_addr;
uint16_t fb_palette_nr_colours;
};
};
};
struct framebuffer_fixedinfo {
uint64_t fb_baseptr;
};
#endif
-11
View File
@@ -1,11 +0,0 @@
#ifndef KERNEL_FLAGS_H_
#define KERNEL_FLAGS_H_
#include <stdint.h>
typedef enum {
S_NORMAL = 0x00u,
S_NOBLOCK = 0x01u,
} mango_flags_t;
#endif
+1
View File
@@ -47,6 +47,7 @@ extern void handle_table_destroy(struct handle_table *tab);
extern kern_status_t handle_table_alloc_handle( extern kern_status_t handle_table_alloc_handle(
struct handle_table *tab, struct handle_table *tab,
kern_handle_t value,
struct handle **out_slot, struct handle **out_slot,
kern_handle_t *out_handle); kern_handle_t *out_handle);
extern kern_status_t handle_table_free_handle( extern kern_status_t handle_table_free_handle(
-184
View File
@@ -1,184 +0,0 @@
#ifndef KERNEL_INPUT_H_
#define KERNEL_INPUT_H_
#include <stdint.h>
#include <kernel/queue.h>
#include <mango/status.h>
enum input_event_hook_flags {
INPUT_HOOK_SQUASH_EVENT = 0x01u,
};
struct device;
enum input_event_type {
INPUT_TYPE_UNKNOWN = 0x00u,
INPUT_TYPE_KEY = 0x01u,
INPUT_TYPE_MOTION = 0x02u,
};
enum input_event_motion_type {
INPUT_MOTION_TYPE_MOUSE = 0x01u,
INPUT_MOTION_TYPE_SCROLL = 0x02u,
};
enum input_keycode {
KEY_UNKNOWN = 0x00u,
KEY_A = 0x01u,
KEY_B = 0x02u,
KEY_C = 0x03u,
KEY_D = 0x04u,
KEY_E = 0x05u,
KEY_F = 0x06u,
KEY_G = 0x07u,
KEY_H = 0x08u,
KEY_I = 0x09u,
KEY_J = 0x0Au,
KEY_K = 0x0Bu,
KEY_L = 0x0Cu,
KEY_M = 0x0Du,
KEY_N = 0x0Eu,
KEY_O = 0x0Fu,
KEY_P = 0x10u,
KEY_Q = 0x11u,
KEY_R = 0x12u,
KEY_S = 0x13u,
KEY_T = 0x14u,
KEY_U = 0x15u,
KEY_V = 0x16u,
KEY_W = 0x17u,
KEY_X = 0x18u,
KEY_Y = 0x19u,
KEY_Z = 0x1Au,
KEY_KEY_1 = 0x1Bu,
KEY_KEY_2 = 0x1Cu,
KEY_KEY_3 = 0x1Du,
KEY_KEY_4 = 0x1Eu,
KEY_KEY_5 = 0x1Fu,
KEY_KEY_6 = 0x20u,
KEY_KEY_7 = 0x21u,
KEY_KEY_8 = 0x22u,
KEY_KEY_9 = 0x23u,
KEY_KEY_0 = 0x24u,
KEY_ENTER = 0x25u,
KEY_ESCAPE = 0x26u,
KEY_BACKSPACE = 0x27u,
KEY_TAB = 0x28u,
KEY_SPACE = 0x29u,
KEY_MINUS = 0x2Au,
KEY_EQUALS = 0x2Bu,
KEY_LEFT_BRACE = 0x2Cu,
KEY_RIGHT_BRACE = 0x2Du,
KEY_BACKSLASH = 0x2Eu,
KEY_NON_US_HASH = 0x2Fu,
KEY_SEMICOLON = 0x30u,
KEY_APOSTROPHE = 0x31u,
KEY_GRAVE_ACCENT = 0x32u,
KEY_COMMA = 0x33u,
KEY_DOT = 0x34u,
KEY_SLASH = 0x35u,
KEY_CAPS_LOCK = 0x36u,
KEY_F1 = 0x37u,
KEY_F2 = 0x38u,
KEY_F3 = 0x39u,
KEY_F4 = 0x3Au,
KEY_F5 = 0x3Bu,
KEY_F6 = 0x3Cu,
KEY_F7 = 0x3Du,
KEY_F8 = 0x3Eu,
KEY_F9 = 0x3Fu,
KEY_F10 = 0x40u,
KEY_F11 = 0x41u,
KEY_F12 = 0x42u,
KEY_PRINT_SCREEN = 0x43u,
KEY_SCROLL_LOCK = 0x44u,
KEY_PAUSE = 0x45u,
KEY_INSERT = 0x46u,
KEY_HOME = 0x47u,
KEY_PAGE_UP = 0x48u,
KEY_DELETE = 0x49u,
KEY_END = 0x4Au,
KEY_PAGE_DOWN = 0x4Bu,
KEY_RIGHT = 0x4Cu,
KEY_LEFT = 0x4Du,
KEY_DOWN = 0x4Eu,
KEY_UP = 0x4Fu,
KEY_NUM_LOCK = 0x50u,
KEY_KEYPAD_SLASH = 0x51u,
KEY_KEYPAD_ASTERISK = 0x52u,
KEY_KEYPAD_MINUS = 0x53u,
KEY_KEYPAD_PLUS = 0x54u,
KEY_KEYPAD_ENTER = 0x55u,
KEY_KEYPAD_1 = 0x56u,
KEY_KEYPAD_2 = 0x57u,
KEY_KEYPAD_3 = 0x58u,
KEY_KEYPAD_4 = 0x59u,
KEY_KEYPAD_5 = 0x5Au,
KEY_KEYPAD_6 = 0x5Bu,
KEY_KEYPAD_7 = 0x5Cu,
KEY_KEYPAD_8 = 0x5Du,
KEY_KEYPAD_9 = 0x5Eu,
KEY_KEYPAD_0 = 0x5Fu,
KEY_KEYPAD_DOT = 0x60u,
KEY_NON_US_BACKSLASH = 0x61u,
KEY_KEYPAD_EQUALS = 0x62u,
KEY_MENU = 0x63u,
KEY_LEFT_CTRL = 0x64u,
KEY_LEFT_SHIFT = 0x65u,
KEY_LEFT_ALT = 0x66u,
KEY_LEFT_META = 0x67u,
KEY_RIGHT_CTRL = 0x68u,
KEY_RIGHT_SHIFT = 0x69u,
KEY_RIGHT_ALT = 0x6Au,
KEY_RIGHT_META = 0x6Bu,
KEY_MEDIA_MUTE = 0x6Cu,
KEY_MEDIA_VOLUME_INCREMENT = 0x6Du,
KEY_MEDIA_VOLUME_DECREMENT = 0x6Eu,
};
enum input_key_state {
INPUT_KEYSTATE_DOWN = 0x00u,
INPUT_KEYSTATE_UP = 0x01u,
};
enum input_button {
INPUT_BUTTON_MOUSE_LEFT = 0x00u,
INPUT_BUTTON_MOUSE_MIDDLE = 0x01u,
INPUT_BUTTON_MOUSE_RIGHT = 0x02u,
INPUT_BUTTON_MOUSE_BACK = 0x03u,
INPUT_BUTTON_MOUSE_FORWARD = 0x04u,
};
enum input_button_state {
INPUT_BUTTON_DOWN = 0x00u,
INPUT_BUTTON_UP = 0x01u,
};
struct input_event {
enum input_event_type ev_type;
union {
struct {
enum input_event_motion_type type;
int16_t movement_x;
int16_t movement_y;
} ev_motion;
struct {
enum input_button button;
enum input_button_state state;
} ev_button;
struct {
enum input_keycode key;
enum input_key_state state;
} ev_key;
};
};
struct input_event_hook {
void(*hook_callback)(struct device *, struct input_event *, enum input_event_hook_flags *, void *);
void *hook_arg;
struct queue_entry hook_head;
};
#endif
+14 -2
View File
@@ -21,8 +21,20 @@ struct msg {
enum kmsg_status msg_status; enum kmsg_status msg_status;
struct btree_node msg_node; struct btree_node msg_node;
msgid_t msg_id; msgid_t msg_id;
struct port *msg_sender_port; union {
struct thread *msg_sender_thread; /* only valid for asynchronous messages (msg_status ==
* KMSG_ASYNC) */
struct {
koid_t msg_sender_port_id;
tid_t msg_sender_thread_id;
};
/* only valid for synchronous messages (msg_status !=
* KMSG_ASYNC) */
struct {
struct port *msg_sender_port;
struct thread *msg_sender_thread;
};
};
kern_status_t msg_result; kern_status_t msg_result;
kern_msg_type_t msg_type; kern_msg_type_t msg_type;
-1
View File
@@ -2,7 +2,6 @@
#define KERNEL_OBJECT_H_ #define KERNEL_OBJECT_H_
#include <kernel/atomic.h> #include <kernel/atomic.h>
#include <kernel/flags.h>
#include <kernel/locks.h> #include <kernel/locks.h>
#include <kernel/vm.h> #include <kernel/vm.h>
#include <kernel/wait.h> #include <kernel/wait.h>
+5 -1
View File
@@ -9,7 +9,6 @@
#include <stddef.h> #include <stddef.h>
#define PMAP_INVALID ML_PMAP_INVALID #define PMAP_INVALID ML_PMAP_INVALID
#define PFN(x) ((x) >> VM_PAGE_SHIFT)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -57,6 +56,11 @@ extern kern_status_t pmap_handle_fault(
virt_addr_t fault_addr, virt_addr_t fault_addr,
enum pmap_fault_flags flags); enum pmap_fault_flags flags);
extern kern_status_t pmap_get(
pmap_t pmap,
virt_addr_t p,
pfn_t *out_pfn,
vm_prot_t *out_prot);
extern kern_status_t pmap_add( extern kern_status_t pmap_add(
pmap_t pmap, pmap_t pmap,
virt_addr_t p, virt_addr_t p,
-6
View File
@@ -9,12 +9,6 @@ enum port_status {
PORT_OFFLINE = 0, PORT_OFFLINE = 0,
/* port is connected and ready to send messages */ /* port is connected and ready to send messages */
PORT_READY, PORT_READY,
/* port has sent a message, and is waiting for the remote to receive it
*/
PORT_SEND_BLOCKED,
/* port has sent a message, and the remote has received it. waiting for
* the remote to reply. */
PORT_REPLY_BLOCKED,
}; };
struct port { struct port {
-45
View File
@@ -1,45 +0,0 @@
#ifndef KERNEL_RINGBUFFER_H_
#define KERNEL_RINGBUFFER_H_
#include <kernel/flags.h>
#include <kernel/locks.h>
#include <kernel/types.h>
#include <kernel/wait.h>
struct ringbuffer {
unsigned char *r_buffer;
size_t r_write_ptr;
size_t r_read_ptr;
size_t r_size;
spin_lock_t r_lock;
struct waitqueue r_wait_readers;
struct waitqueue r_wait_writers;
};
extern struct ringbuffer *ringbuffer_create(size_t size);
extern void ringbuffer_destroy(struct ringbuffer *buf);
extern kern_status_t ringbuffer_init(struct ringbuffer *buf, size_t size);
extern kern_status_t ringbuffer_deinit(struct ringbuffer *buf);
extern size_t ringbuffer_unread(struct ringbuffer *buf);
extern size_t ringbuffer_avail(struct ringbuffer *buf);
extern size_t ringbuffer_read(
struct ringbuffer *buf,
size_t size,
void *buffer,
mango_flags_t flags);
extern size_t ringbuffer_write(
struct ringbuffer *buf,
size_t size,
const void *buffer,
mango_flags_t flags);
/* TODO */
// extern size_t ringbuffer_peek(struct ringbuffer *buf, size_t at, size_t size,
// void *buffer); extern size_t ringbuffer_skip(struct ringbuffer *buf, size_t
// count);
extern int ringbuffer_write_would_block(struct ringbuffer *buf);
#endif
+4 -2
View File
@@ -76,8 +76,10 @@ extern struct runqueue *cpu_rq(unsigned int cpu);
extern cycles_t default_quantum(void); extern cycles_t default_quantum(void);
extern bool need_resched(void); extern bool need_resched(void);
extern struct task *current_task(void); extern struct task *get_current_task(void);
extern struct thread *current_thread(void); extern struct thread *get_current_thread(void);
extern void put_current_task(struct task *task);
extern void put_current_thread(struct thread *thread);
extern struct runqueue *select_rq_for_thread(struct thread *thr); extern struct runqueue *select_rq_for_thread(struct thread *thr);
extern void schedule_thread_on_cpu(struct thread *thr); extern void schedule_thread_on_cpu(struct thread *thr);
+18 -2
View File
@@ -66,6 +66,9 @@ extern kern_status_t sys_task_config_set(
kern_config_key_t key, kern_config_key_t key,
const void *ptr, const void *ptr,
size_t len); size_t len);
extern kern_status_t sys_task_duplicate(
kern_handle_t *out_task,
kern_handle_t *out_address_space);
extern kern_status_t sys_thread_self(kern_handle_t *out); extern kern_status_t sys_thread_self(kern_handle_t *out);
extern kern_status_t sys_thread_start(kern_handle_t thread); extern kern_status_t sys_thread_start(kern_handle_t thread);
@@ -143,9 +146,19 @@ extern kern_status_t sys_address_space_release(
extern kern_status_t sys_kern_log(const char *s); extern kern_status_t sys_kern_log(const char *s);
extern kern_status_t sys_kern_handle_close(kern_handle_t handle); extern kern_status_t sys_kern_handle_close(kern_handle_t handle);
extern kern_status_t sys_kern_handle_duplicate( extern kern_status_t sys_kern_handle_transfer(
kern_handle_t src_task_handle,
kern_handle_t src_handle,
kern_handle_t dest_task_handle,
kern_handle_t dest_handle,
unsigned int mode,
kern_handle_t *out_handle);
extern kern_status_t sys_kern_handle_control(
kern_handle_t task,
kern_handle_t handle, kern_handle_t handle,
kern_handle_t *out); uint32_t set_mask,
uint32_t clear_mask,
uint32_t *out_flags);
extern kern_status_t sys_kern_config_get( extern kern_status_t sys_kern_config_get(
kern_config_key_t key, kern_config_key_t key,
void *ptr, void *ptr,
@@ -191,6 +204,9 @@ extern kern_status_t sys_msg_write(
extern kern_status_t sys_kern_object_wait( extern kern_status_t sys_kern_object_wait(
kern_wait_item_t *items, kern_wait_item_t *items,
size_t nr_items); size_t nr_items);
extern kern_status_t sys_kern_object_query(
kern_handle_t object,
kern_object_info_t *out);
extern kern_status_t sys_vm_controller_create(kern_handle_t *out); extern kern_status_t sys_vm_controller_create(kern_handle_t *out);
extern kern_status_t sys_vm_controller_recv( extern kern_status_t sys_vm_controller_recv(
-14
View File
@@ -1,14 +0,0 @@
#ifndef KERNEL_TEST_H_
#define KERNEL_TEST_H_
#ifdef __cplusplus
extern "C" {
#endif
extern int run_all_tests(void);
#ifdef __cplusplus
}
#endif
#endif
+9
View File
@@ -8,6 +8,8 @@
#define THREAD_KSTACK_ORDER VM_PAGE_4K #define THREAD_KSTACK_ORDER VM_PAGE_4K
struct ml_cpu_context;
enum thread_state { enum thread_state {
THREAD_READY = 1, THREAD_READY = 1,
THREAD_SLEEPING = 2, THREAD_SLEEPING = 2,
@@ -39,6 +41,9 @@ struct thread {
virt_addr_t tr_ip, tr_sp, tr_bp; virt_addr_t tr_ip, tr_sp, tr_bp;
virt_addr_t tr_cpu_user_sp, tr_cpu_kernel_sp; virt_addr_t tr_cpu_user_sp, tr_cpu_kernel_sp;
/* only valid within an interrupt or syscall context */
struct ml_cpu_context *tr_irqctx;
struct ml_thread tr_ml; struct ml_thread tr_ml;
struct runqueue *tr_rq; struct runqueue *tr_rq;
@@ -57,6 +62,10 @@ extern kern_status_t thread_init_user(
virt_addr_t sp, virt_addr_t sp,
const uintptr_t *args, const uintptr_t *args,
size_t nr_args); size_t nr_args);
extern kern_status_t thread_init_user_clone(
struct thread *thr,
const struct thread *src,
uintptr_t return_value);
extern int thread_priority(struct thread *thr); extern int thread_priority(struct thread *thr);
extern void thread_awaken(struct thread *thr); extern void thread_awaken(struct thread *thr);
extern void thread_exit(void); extern void thread_exit(void);
+7
View File
@@ -84,11 +84,18 @@ extern struct vm_object *vm_object_create_in_place(
size_t data_len, size_t data_len,
vm_prot_t prot); vm_prot_t prot);
/* create a copy-on-write duplicate of a vm-object */
extern struct vm_object *vm_object_duplicate_cow(struct vm_object *vmo);
extern struct vm_page *vm_object_get_page( extern struct vm_page *vm_object_get_page(
struct vm_object *vo, struct vm_object *vo,
off_t offset, off_t offset,
enum vm_object_flags flags, enum vm_object_flags flags,
unsigned long *irq_flags); unsigned long *irq_flags);
extern kern_status_t vm_object_put_page(
struct vm_object *vo,
off_t offset,
struct vm_page *pg);
extern kern_status_t vm_object_read( extern kern_status_t vm_object_read(
struct vm_object *vo, struct vm_object *vo,
+4 -15
View File
@@ -1,6 +1,7 @@
#ifndef KERNEL_VM_H_ #ifndef KERNEL_VM_H_
#define KERNEL_VM_H_ #define KERNEL_VM_H_
#include <kernel/atomic.h>
#include <kernel/bitmap.h> #include <kernel/bitmap.h>
#include <kernel/btree.h> #include <kernel/btree.h>
#include <kernel/locks.h> #include <kernel/locks.h>
@@ -14,8 +15,6 @@
extern "C" { extern "C" {
#endif #endif
struct bcache;
/* maximum number of NUMA nodes */ /* maximum number of NUMA nodes */
#define VM_MAX_NODES 64 #define VM_MAX_NODES 64
/* maximum number of memory zones per node */ /* maximum number of memory zones per node */
@@ -25,11 +24,6 @@ struct bcache;
/* maximum number of sparse memory sectors */ /* maximum number of sparse memory sectors */
#define VM_MAX_SECTORS 8192 #define VM_MAX_SECTORS 8192
/* maximum number of disk sectors that can be stored in a single
page. AKA the number of bits in the sector bitmap.
used by the block cache */
#define VM_MAX_SECTORS_PER_PAGE 32
#define VM_CHECK_ALIGN(p, mask) ((((p) & (mask)) == (p)) ? 1 : 0) #define VM_CHECK_ALIGN(p, mask) ((((p) & (mask)) == (p)) ? 1 : 0)
#define VM_CACHE_INITIALISED(c) ((c)->c_obj_count != 0) #define VM_CACHE_INITIALISED(c) ((c)->c_obj_count != 0)
@@ -207,7 +201,6 @@ struct vm_page {
/* owner-specific pointer */ /* owner-specific pointer */
union { union {
struct vm_slab *p_slab; struct vm_slab *p_slab;
struct bcache *p_bcache;
void *p_priv0; void *p_priv0;
}; };
@@ -218,8 +211,6 @@ struct vm_page {
lists. lists.
- vm_object uses it to maintain a btree of allocated pages keyed - vm_object uses it to maintain a btree of allocated pages keyed
by offset/size. by offset/size.
- the block cache uses this to maintain a tree of pages keyed by
block number.
*/ */
union { union {
struct queue_entry p_list; struct queue_entry p_list;
@@ -231,15 +222,13 @@ struct vm_page {
}; };
union { union {
/* used by bcache when sector size is < page size. bitmap of /* how many vm_areas reference this vm_page. if >0, the page is
* present/missing sectors */ * subject to copy-on-write. */
DECLARE_BITMAP(p_blockbits, VM_MAX_SECTORS_PER_PAGE); atomic_t p_cow_ref;
uint32_t p_priv2; uint32_t p_priv2;
}; };
union { union {
/* sector address, used by bcache */
sectors_t p_blockid;
/* offset of this page within the vm_object it is a part of */ /* offset of this page within the vm_object it is a part of */
off_t p_vmo_offset; off_t p_vmo_offset;
+2 -4
View File
@@ -5,7 +5,6 @@
#include <kernel/cpu.h> #include <kernel/cpu.h>
#include <kernel/handle.h> #include <kernel/handle.h>
#include <kernel/init.h> #include <kernel/init.h>
#include <kernel/input.h>
#include <kernel/libc/stdio.h> #include <kernel/libc/stdio.h>
#include <kernel/machine/init.h> #include <kernel/machine/init.h>
#include <kernel/object.h> #include <kernel/object.h>
@@ -14,7 +13,6 @@
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/task.h> #include <kernel/task.h>
#include <kernel/test.h>
#include <kernel/thread.h> #include <kernel/thread.h>
#include <kernel/vm-object.h> #include <kernel/vm-object.h>
#include <stdint.h> #include <stdint.h>
@@ -46,8 +44,8 @@ static void hang(void)
void background_thread(void) void background_thread(void)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
struct thread *thread = current_thread(); struct thread *thread = get_current_thread();
printk("background_thread() running on processor %u", this_cpu()); printk("background_thread() running on processor %u", this_cpu());
milli_sleep(1000); milli_sleep(1000);
+10 -8
View File
@@ -2,6 +2,7 @@
#include <kernel/channel.h> #include <kernel/channel.h>
#include <kernel/msg.h> #include <kernel/msg.h>
#include <kernel/port.h> #include <kernel/port.h>
#include <kernel/printk.h>
#include <kernel/task.h> #include <kernel/task.h>
#include <kernel/thread.h> #include <kernel/thread.h>
#include <kernel/util.h> #include <kernel/util.h>
@@ -101,7 +102,6 @@ static struct msg *get_next_msg(
switch (msg->msg_status) { switch (msg->msg_status) {
case KMSG_WAIT_RECEIVE: case KMSG_WAIT_RECEIVE:
msg->msg_status = KMSG_WAIT_REPLY; msg->msg_status = KMSG_WAIT_REPLY;
msg->msg_sender_port->p_status = PORT_REPLY_BLOCKED;
channel->c_msg_waiting--; channel->c_msg_waiting--;
return msg; return msg;
case KMSG_ASYNC: case KMSG_ASYNC:
@@ -139,7 +139,6 @@ extern kern_status_t channel_recv_msg(
kern_msg_t *out_msg, kern_msg_t *out_msg,
unsigned long *irq_flags) unsigned long *irq_flags)
{ {
struct thread *self = current_thread();
struct msg *msg = NULL; struct msg *msg = NULL;
unsigned long msg_lock_flags; unsigned long msg_lock_flags;
@@ -157,12 +156,12 @@ extern kern_status_t channel_recv_msg(
/* msg is now set to the next message to process */ /* msg is now set to the next message to process */
if (msg->msg_type != KERN_MSG_TYPE_DATA) { if (msg->msg_type != KERN_MSG_TYPE_DATA) {
/* event messages as asynchronous */ /* event messages are asynchronous */
out_msg->msg_id = msg->msg_id; out_msg->msg_id = msg->msg_id;
out_msg->msg_type = msg->msg_type; out_msg->msg_type = msg->msg_type;
out_msg->msg_event = msg->msg_event; out_msg->msg_event = msg->msg_event;
out_msg->msg_sender = msg->msg_sender_thread->tr_parent->t_id; out_msg->msg_sender = msg->msg_sender_thread_id;
out_msg->msg_endpoint = msg->msg_sender_port->p_base.ob_id; out_msg->msg_endpoint = msg->msg_sender_port_id;
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags); spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
msg_free(msg); msg_free(msg);
@@ -170,7 +169,7 @@ extern kern_status_t channel_recv_msg(
} }
struct task *sender = msg->msg_sender_thread->tr_parent; struct task *sender = msg->msg_sender_thread->tr_parent;
struct task *receiver = self->tr_parent; struct task *receiver = get_current_task();
struct address_space *src = sender->t_address_space, struct address_space *src = sender->t_address_space,
*dst = receiver->t_address_space; *dst = receiver->t_address_space;
@@ -192,6 +191,7 @@ extern kern_status_t channel_recv_msg(
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
put_current_task(receiver);
return status; return status;
} }
@@ -216,6 +216,7 @@ extern kern_status_t channel_recv_msg(
&receiver->t_handles_lock, &receiver->t_handles_lock,
f); f);
address_space_unlock_pair_irqrestore(src, dst, f); address_space_unlock_pair_irqrestore(src, dst, f);
put_current_task(receiver);
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
@@ -250,11 +251,10 @@ extern kern_status_t channel_reply_msg(
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
struct thread *self = current_thread();
/* the task that is about to receive the response */ /* the task that is about to receive the response */
struct task *receiver = msg->msg_sender_thread->tr_parent; struct task *receiver = msg->msg_sender_thread->tr_parent;
/* the task that is about to send the response */ /* the task that is about to send the response */
struct task *sender = self->tr_parent; struct task *sender = get_current_task();
struct address_space *src = sender->t_address_space, struct address_space *src = sender->t_address_space,
*dst = receiver->t_address_space; *dst = receiver->t_address_space;
@@ -275,6 +275,7 @@ extern kern_status_t channel_reply_msg(
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
put_current_task(sender);
return status; return status;
} }
@@ -299,6 +300,7 @@ extern kern_status_t channel_reply_msg(
&receiver->t_handles_lock, &receiver->t_handles_lock,
f); f);
address_space_unlock_pair_irqrestore(src, dst, f); address_space_unlock_pair_irqrestore(src, dst, f);
put_current_task(sender);
if (status != KERN_OK) { if (status != KERN_OK) {
kmsg_reply_error(msg, status, &msg_lock_flags); kmsg_reply_error(msg, status, &msg_lock_flags);
+23 -3
View File
@@ -32,8 +32,10 @@ static kern_status_t get_data(
{ {
spin_lock_t *lock = NULL; spin_lock_t *lock = NULL;
struct btree *futex_list = NULL; struct btree *futex_list = NULL;
struct task *self = NULL;
if (flags & FUTEX_PRIVATE) { if (flags & FUTEX_PRIVATE) {
struct task *self = current_task(); self = get_current_task();
lock = &self->t_base.ob_lock; lock = &self->t_base.ob_lock;
futex_list = &self->t_futex; futex_list = &self->t_futex;
} else if (flags & FUTEX_SHARED) { } else if (flags & FUTEX_SHARED) {
@@ -48,12 +50,20 @@ static kern_status_t get_data(
if (!futex && !(flags & FUTEX_CREATE)) { if (!futex && !(flags & FUTEX_CREATE)) {
spin_unlock_irqrestore(lock, *irq_flags); spin_unlock_irqrestore(lock, *irq_flags);
if (self) {
put_current_task(self);
}
return KERN_NO_ENTRY; return KERN_NO_ENTRY;
} }
futex = vm_cache_alloc(&futex_cache, VM_NORMAL); futex = vm_cache_alloc(&futex_cache, VM_NORMAL);
if (!futex) { if (!futex) {
spin_unlock_irqrestore(lock, *irq_flags); spin_unlock_irqrestore(lock, *irq_flags);
if (self) {
put_current_task(self);
}
return KERN_NO_MEMORY; return KERN_NO_MEMORY;
} }
@@ -61,6 +71,10 @@ static kern_status_t get_data(
put_futex(futex_list, futex); put_futex(futex_list, futex);
if (self) {
put_current_task(self);
}
*out = futex; *out = futex;
*out_lock = lock; *out_lock = lock;
return KERN_OK; return KERN_OK;
@@ -68,9 +82,10 @@ static kern_status_t get_data(
static kern_status_t cleanup_data(struct futex *futex, unsigned int flags) static kern_status_t cleanup_data(struct futex *futex, unsigned int flags)
{ {
struct task *self = NULL;
struct btree *futex_list = NULL; struct btree *futex_list = NULL;
if (flags & FUTEX_PRIVATE) { if (flags & FUTEX_PRIVATE) {
struct task *self = current_task(); self = get_current_task();
futex_list = &self->t_futex; futex_list = &self->t_futex;
} else if (flags & FUTEX_SHARED) { } else if (flags & FUTEX_SHARED) {
futex_list = &shared_futex_list; futex_list = &shared_futex_list;
@@ -81,12 +96,16 @@ static kern_status_t cleanup_data(struct futex *futex, unsigned int flags)
btree_delete(futex_list, &futex->f_node); btree_delete(futex_list, &futex->f_node);
vm_cache_free(&futex_cache, futex); vm_cache_free(&futex_cache, futex);
if (self) {
put_current_task(self);
}
return KERN_OK; return KERN_OK;
} }
static kern_status_t futex_get_shared(kern_futex_t *futex, futex_key_t *out) static kern_status_t futex_get_shared(kern_futex_t *futex, futex_key_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
struct address_space *space = self->t_address_space; struct address_space *space = self->t_address_space;
unsigned long flags; unsigned long flags;
@@ -97,6 +116,7 @@ static kern_status_t futex_get_shared(kern_futex_t *futex, futex_key_t *out)
out, out,
&flags); &flags);
address_space_unlock_irqrestore(space, flags); address_space_unlock_irqrestore(space, flags);
put_current_task(self);
return status; return status;
} }
+54 -5
View File
@@ -109,7 +109,7 @@ static kern_status_t encode_handle_indices(
return KERN_OK; return KERN_OK;
} }
kern_status_t handle_table_alloc_handle( static kern_status_t alloc_handle(
struct handle_table *tab, struct handle_table *tab,
struct handle **out_slot, struct handle **out_slot,
kern_handle_t *out_handle) kern_handle_t *out_handle)
@@ -164,6 +164,53 @@ kern_status_t handle_table_alloc_handle(
return encode_handle_indices(indices, out_handle); return encode_handle_indices(indices, out_handle);
} }
kern_status_t handle_table_alloc_handle(
struct handle_table *tab,
kern_handle_t value,
struct handle **out_slot,
kern_handle_t *out_handle)
{
if (value == KERN_HANDLE_INVALID) {
return alloc_handle(tab, out_slot, out_handle);
}
unsigned int indices[MAX_TABLE_DEPTH];
if (decode_handle_indices(value, indices) != KERN_OK) {
return KERN_HANDLE_INVALID;
}
int i;
for (i = 0; i < MAX_TABLE_DEPTH - 1; i++) {
struct handle_table *next
= tab->t_subtables.t_subtable_list[indices[i]];
if (!next) {
next = handle_table_create();
tab->t_subtables.t_subtable_list[indices[i]] = next;
}
if (!next) {
return KERN_NO_MEMORY;
}
tab = next;
}
unsigned int handle_index = indices[i];
bitmap_set(tab->t_handles.t_handle_map, handle_index);
struct handle *out = &tab->t_handles.t_handle_list[handle_index];
if (out->h_object) {
object_unref(out->h_object);
out->h_object = NULL;
out->h_flags = 0;
}
*out_slot = out;
*out_handle = value;
return KERN_OK;
}
kern_status_t handle_table_free_handle( kern_status_t handle_table_free_handle(
struct handle_table *tab, struct handle_table *tab,
kern_handle_t handle) kern_handle_t handle)
@@ -294,11 +341,12 @@ kern_status_t handle_table_transfer(
} }
switch (src_handle.hnd_mode) { switch (src_handle.hnd_mode) {
case KERN_MSG_HANDLE_IGNORE: case HANDLE_TRANSFER_IGNORE:
break; break;
case KERN_MSG_HANDLE_MOVE: case HANDLE_TRANSFER_MOVE:
status = handle_table_alloc_handle( status = handle_table_alloc_handle(
dst, dst,
KERN_HANDLE_INVALID,
&dst_entry, &dst_entry,
&dst_value); &dst_value);
if (status != KERN_OK) { if (status != KERN_OK) {
@@ -315,9 +363,10 @@ kern_status_t handle_table_transfer(
dst_handle.hnd_value = dst_value; dst_handle.hnd_value = dst_value;
dst_handle.hnd_result = KERN_OK; dst_handle.hnd_result = KERN_OK;
break; break;
case KERN_MSG_HANDLE_COPY: case HANDLE_TRANSFER_COPY:
status = handle_table_alloc_handle( status = handle_table_alloc_handle(
dst, dst,
KERN_HANDLE_INVALID,
&dst_entry, &dst_entry,
&dst_value); &dst_value);
if (status != KERN_OK) { if (status != KERN_OK) {
@@ -364,7 +413,7 @@ kern_status_t handle_table_transfer(
&handle, &handle,
NULL); NULL);
if (handle.hnd_mode != KERN_MSG_HANDLE_MOVE) { if (handle.hnd_mode != HANDLE_TRANSFER_MOVE) {
continue; continue;
} }
+2 -1
View File
@@ -175,7 +175,7 @@ void object_wait_signal(
uint32_t signals, uint32_t signals,
unsigned long *irq_flags) unsigned long *irq_flags)
{ {
struct thread *self = current_thread(); struct thread *self = get_current_thread();
struct wait_item waiter; struct wait_item waiter;
wait_item_init(&waiter, self); wait_item_init(&waiter, self);
for (;;) { for (;;) {
@@ -189,4 +189,5 @@ void object_wait_signal(
object_lock_irqsave(obj, irq_flags); object_lock_irqsave(obj, irq_flags);
} }
thread_wait_end(&waiter, &obj->ob_wq); thread_wait_end(&waiter, &obj->ob_wq);
put_current_thread(self);
} }
+5 -2
View File
@@ -19,8 +19,8 @@ void panic_irq(struct ml_cpu_context *ctx, const char *fmt, ...)
printk("---[ kernel panic: %s", buf); printk("---[ kernel panic: %s", buf);
printk("kernel: " BUILD_ID ", compiler version: " __VERSION__); printk("kernel: " BUILD_ID ", compiler version: " __VERSION__);
struct task *task = current_task(); struct task *task = get_current_task();
struct thread *thr = current_thread(); struct thread *thr = get_current_thread();
if (task && thr) { if (task && thr) {
printk("task: %s (id: %d, thread: %d)", printk("task: %s (id: %d, thread: %d)",
@@ -31,6 +31,9 @@ void panic_irq(struct ml_cpu_context *ctx, const char *fmt, ...)
printk("task: [bootstrap]"); printk("task: [bootstrap]");
} }
put_current_thread(thr);
put_current_task(task);
printk("cpu: %u", this_cpu()); printk("cpu: %u", this_cpu());
ml_print_cpu_state(ctx); ml_print_cpu_state(ctx);
+14 -7
View File
@@ -37,7 +37,7 @@ struct port *port_cast(struct object *obj)
static void wait_for_reply(struct msg *msg, unsigned long *lock_flags) static void wait_for_reply(struct msg *msg, unsigned long *lock_flags)
{ {
struct wait_item waiter; struct wait_item waiter;
struct thread *self = current_thread(); struct thread *self = get_current_thread();
wait_item_init(&waiter, self); wait_item_init(&waiter, self);
for (;;) { for (;;) {
@@ -52,6 +52,7 @@ static void wait_for_reply(struct msg *msg, unsigned long *lock_flags)
} }
self->tr_state = THREAD_READY; self->tr_state = THREAD_READY;
put_current_thread(self);
} }
struct port *port_create(void) struct port *port_create(void)
@@ -83,8 +84,11 @@ kern_status_t port_connect(struct port *port, struct channel *remote)
msg->msg_status = KMSG_ASYNC; msg->msg_status = KMSG_ASYNC;
msg->msg_type = KERN_MSG_TYPE_EVENT; msg->msg_type = KERN_MSG_TYPE_EVENT;
msg->msg_event = KERN_MSG_EVENT_CONNECTION; msg->msg_event = KERN_MSG_EVENT_CONNECTION;
msg->msg_sender_thread = current_thread(); msg->msg_sender_port_id = port->p_base.ob_id;
msg->msg_sender_port = port;
struct thread *self = get_current_thread();
msg->msg_sender_thread_id = self->tr_id;
put_current_thread(self);
unsigned long flags; unsigned long flags;
channel_lock_irqsave(remote, &flags); channel_lock_irqsave(remote, &flags);
@@ -112,8 +116,11 @@ kern_status_t port_disconnect(struct port *port)
msg->msg_status = KMSG_ASYNC; msg->msg_status = KMSG_ASYNC;
msg->msg_type = KERN_MSG_TYPE_EVENT; msg->msg_type = KERN_MSG_TYPE_EVENT;
msg->msg_event = KERN_MSG_EVENT_DISCONNECTION; msg->msg_event = KERN_MSG_EVENT_DISCONNECTION;
msg->msg_sender_thread = current_thread(); msg->msg_sender_port_id = port->p_base.ob_id;
msg->msg_sender_port = port;
struct thread *self = get_current_thread();
msg->msg_sender_thread_id = self->tr_id;
put_current_thread(self);
unsigned long flags; unsigned long flags;
channel_lock_irqsave(port->p_remote, &flags); channel_lock_irqsave(port->p_remote, &flags);
@@ -136,7 +143,7 @@ kern_status_t port_send_msg(
return KERN_BAD_STATE; return KERN_BAD_STATE;
} }
struct thread *self = current_thread(); struct thread *self = get_current_thread();
struct msg msg; struct msg msg;
memset(&msg, 0x0, sizeof msg); memset(&msg, 0x0, sizeof msg);
msg.msg_type = KERN_MSG_TYPE_DATA; msg.msg_type = KERN_MSG_TYPE_DATA;
@@ -148,7 +155,6 @@ kern_status_t port_send_msg(
unsigned long flags; unsigned long flags;
channel_lock_irqsave(port->p_remote, &flags); channel_lock_irqsave(port->p_remote, &flags);
port->p_status = PORT_SEND_BLOCKED;
channel_enqueue_msg(port->p_remote, &msg); channel_enqueue_msg(port->p_remote, &msg);
channel_unlock_irqrestore(port->p_remote, flags); channel_unlock_irqrestore(port->p_remote, flags);
@@ -157,6 +163,7 @@ kern_status_t port_send_msg(
channel_lock_irqsave(port->p_remote, &flags); channel_lock_irqsave(port->p_remote, &flags);
btree_delete(&port->p_remote->c_msg, &msg.msg_node); btree_delete(&port->p_remote->c_msg, &msg.msg_node);
channel_unlock_irqrestore(port->p_remote, flags); channel_unlock_irqrestore(port->p_remote, flags);
put_current_thread(self);
return msg.msg_result; return msg.msg_result;
} }
+4 -3
View File
@@ -2,7 +2,8 @@
int memcmp(const void *vl, const void *vr, size_t n) int memcmp(const void *vl, const void *vr, size_t n)
{ {
const unsigned char *l=vl, *r=vr; const unsigned char *l = vl, *r = vr;
for (; n && *l == *r; n--, l++, r++); for (; n && *l == *r; n--, l++, r++)
return n ? *l-*r : 0; ;
return n ? *l - *r : 0;
} }
+13 -13
View File
@@ -1,24 +1,24 @@
#include <stdint.h>
#include <kernel/libc/string.h> #include <kernel/libc/string.h>
#include <stdint.h>
static void *memcpy_r(void *dest, const void *src, size_t sz) static void *memcpy_r(void *dest, const void *src, size_t sz)
{ {
unsigned char *d = dest; unsigned char *d = dest;
const unsigned char *s = src; const unsigned char *s = src;
for (size_t i = 0; i < sz; i++) { for (size_t i = 0; i < sz; i++) {
size_t b = sz - i - 1; size_t b = sz - i - 1;
d[b] = s[b]; d[b] = s[b];
} }
return dest; return dest;
} }
void *memmove(void *dest, const void *src, size_t n) void *memmove(void *dest, const void *src, size_t n)
{ {
if (dest < src) { if (dest < src) {
return memcpy(dest, src, n); return memcpy(dest, src, n);
} else { } else {
return memcpy_r(dest, src, n); return memcpy_r(dest, src, n);
} }
} }
+3 -1
View File
@@ -58,6 +58,7 @@
SYSCALL_GATE task_exit SYS_TASK_EXIT 1 SYSCALL_GATE task_exit SYS_TASK_EXIT 1
SYSCALL_GATE task_self SYS_TASK_SELF 1 SYSCALL_GATE task_self SYS_TASK_SELF 1
SYSCALL_GATE task_create SYS_TASK_CREATE 5 SYSCALL_GATE task_create SYS_TASK_CREATE 5
SYSCALL_GATE task_duplicate SYS_TASK_DUPLICATE 2
SYSCALL_GATE task_create_thread SYS_TASK_CREATE_THREAD 6 SYSCALL_GATE task_create_thread SYS_TASK_CREATE_THREAD 6
SYSCALL_GATE task_get_address_space SYS_TASK_GET_ADDRESS_SPACE 1 SYSCALL_GATE task_get_address_space SYS_TASK_GET_ADDRESS_SPACE 1
SYSCALL_GATE task_config_get SYS_TASK_CONFIG_GET 4 SYSCALL_GATE task_config_get SYS_TASK_CONFIG_GET 4
@@ -83,7 +84,7 @@ SYSCALL_GATE address_space_release SYS_ADDRESS_SPACE_RELEASE 3
SYSCALL_GATE kern_log SYS_KERN_LOG 1 SYSCALL_GATE kern_log SYS_KERN_LOG 1
SYSCALL_GATE kern_handle_close SYS_KERN_HANDLE_CLOSE 1 SYSCALL_GATE kern_handle_close SYS_KERN_HANDLE_CLOSE 1
SYSCALL_GATE kern_handle_duplicate SYS_KERN_HANDLE_DUPLICATE 2 SYSCALL_GATE kern_handle_transfer SYS_KERN_HANDLE_TRANSFER 6
SYSCALL_GATE kern_config_get SYS_KERN_CONFIG_GET 3 SYSCALL_GATE kern_config_get SYS_KERN_CONFIG_GET 3
SYSCALL_GATE kern_config_set SYS_KERN_CONFIG_SET 3 SYSCALL_GATE kern_config_set SYS_KERN_CONFIG_SET 3
@@ -105,6 +106,7 @@ SYSCALL_GATE vm_controller_detach_object SYS_VM_CONTROLLER_DETACH_OBJECT 2
SYSCALL_GATE vm_controller_supply_pages SYS_VM_CONTROLLER_SUPPLY_PAGES 6 SYSCALL_GATE vm_controller_supply_pages SYS_VM_CONTROLLER_SUPPLY_PAGES 6
SYSCALL_GATE kern_object_wait SYS_KERN_OBJECT_WAIT 2 SYSCALL_GATE kern_object_wait SYS_KERN_OBJECT_WAIT 2
SYSCALL_GATE kern_object_query SYS_KERN_OBJECT_QUERY 2
SYSCALL_GATE futex_wait SYS_FUTEX_WAIT 3 SYSCALL_GATE futex_wait SYS_FUTEX_WAIT 3
SYSCALL_GATE futex_wake SYS_FUTEX_WAKE 3 SYSCALL_GATE futex_wake SYS_FUTEX_WAKE 3
+12 -2
View File
@@ -5,8 +5,18 @@
#include <mango/types.h> #include <mango/types.h>
extern kern_status_t kern_handle_close(kern_handle_t handle); extern kern_status_t kern_handle_close(kern_handle_t handle);
extern kern_status_t kern_handle_duplicate( extern kern_status_t kern_handle_transfer(
kern_handle_t src_task,
kern_handle_t src_handle,
kern_handle_t dest_task,
kern_handle_t dest_handle,
unsigned int mode,
kern_handle_t *out_handle);
extern kern_status_t kern_handle_control(
kern_handle_t task,
kern_handle_t handle, kern_handle_t handle,
kern_handle_t *out); uint32_t set_mask,
uint32_t clear_mask,
uint32_t *out_flags);
#endif #endif
+3
View File
@@ -4,5 +4,8 @@
#include <mango/types.h> #include <mango/types.h>
extern kern_status_t kern_object_wait(kern_wait_item_t *items, size_t nr_items); extern kern_status_t kern_object_wait(kern_wait_item_t *items, size_t nr_items);
extern kern_status_t kern_object_query(
kern_handle_t handle,
kern_object_info_t *out);
#endif #endif
+3
View File
@@ -33,6 +33,9 @@ extern kern_status_t task_config_set(
kern_config_key_t key, kern_config_key_t key,
const void *ptr, const void *ptr,
size_t len); size_t len);
extern kern_status_t task_duplicate(
kern_handle_t *out_task,
kern_handle_t *out_address_space);
extern kern_status_t thread_self(kern_handle_t *out); extern kern_status_t thread_self(kern_handle_t *out);
extern kern_status_t thread_start(kern_handle_t thread); extern kern_status_t thread_start(kern_handle_t thread);
+4 -1
View File
@@ -3,7 +3,7 @@
#define SYS_KERN_LOG 1 #define SYS_KERN_LOG 1
#define SYS_KERN_HANDLE_CLOSE 2 #define SYS_KERN_HANDLE_CLOSE 2
#define SYS_KERN_HANDLE_DUPLICATE 3 #define SYS_KERN_HANDLE_TRANSFER 3
#define SYS_KERN_CONFIG_GET 4 #define SYS_KERN_CONFIG_GET 4
#define SYS_KERN_CONFIG_SET 5 #define SYS_KERN_CONFIG_SET 5
#define SYS_KERN_OBJECT_WAIT 6 #define SYS_KERN_OBJECT_WAIT 6
@@ -49,5 +49,8 @@
#define SYS_VM_CONTROLLER_SUPPLY_PAGES 46 #define SYS_VM_CONTROLLER_SUPPLY_PAGES 46
#define SYS_FUTEX_WAIT 47 #define SYS_FUTEX_WAIT 47
#define SYS_FUTEX_WAKE 48 #define SYS_FUTEX_WAKE 48
#define SYS_KERN_OBJECT_QUERY 49
#define SYS_TASK_DUPLICATE 50
#define SYS_KERN_HANDLE_CONTROL 51
#endif #endif
+13 -3
View File
@@ -28,17 +28,23 @@
#define THREAD_CFG_FSBASE 0x20001u #define THREAD_CFG_FSBASE 0x20001u
#define THREAD_CFG_GSBASE 0x20002u #define THREAD_CFG_GSBASE 0x20002u
/* user-defined flags that can be set on handles */
#define KERN_HANDLE_FLAG0 0x10000000UL
#define KERN_HANDLE_FLAG1 0x20000000UL
#define KERN_HANDLE_FLAG2 0x40000000UL
#define KERN_HANDLE_FLAG3 0x80000000UL
/* maximum number of handles that can be sent in a single message */ /* maximum number of handles that can be sent in a single message */
#define KERN_MSG_MAX_HANDLES 64 #define KERN_MSG_MAX_HANDLES 64
/* the corresponding handle should be ignored */ /* the corresponding handle should be ignored */
#define KERN_MSG_HANDLE_IGNORE 0 #define HANDLE_TRANSFER_IGNORE 0
/* the corresponding handle should be moved to the recipient task. the handle /* the corresponding handle should be moved to the recipient task. the handle
* will be closed. */ * will be closed. */
#define KERN_MSG_HANDLE_MOVE 1 #define HANDLE_TRANSFER_MOVE 1
/* the corresponding handle should be copied to the recipient task. the handle /* the corresponding handle should be copied to the recipient task. the handle
* will remain valid for the sending task. */ * will remain valid for the sending task. */
#define KERN_MSG_HANDLE_COPY 2 #define HANDLE_TRANSFER_COPY 2
/* maximum number of objects that can be waited on in a single call to /* maximum number of objects that can be waited on in a single call to
* kern_object_wait */ * kern_object_wait */
@@ -109,6 +115,10 @@ typedef unsigned short equeue_packet_type_t;
typedef unsigned int umode_t; typedef unsigned int umode_t;
typedef struct {
koid_t obj_id;
} kern_object_info_t;
typedef struct { typedef struct {
virt_addr_t io_base; virt_addr_t io_base;
size_t io_len; size_t io_len;
+4 -2
View File
@@ -215,18 +215,19 @@ void schedule_thread_on_cpu(struct thread *thr)
void start_charge_period(void) void start_charge_period(void)
{ {
struct thread *self = current_thread(); struct thread *self = get_current_thread();
if (!self) { if (!self) {
return; return;
} }
self->tr_charge_period_start = get_cycles(); self->tr_charge_period_start = get_cycles();
put_current_thread(self);
} }
void end_charge_period(void) void end_charge_period(void)
{ {
preempt_disable(); preempt_disable();
struct thread *self = current_thread(); struct thread *self = get_current_thread();
if (!self) { if (!self) {
return; return;
} }
@@ -244,6 +245,7 @@ void end_charge_period(void)
} }
self->tr_charge_period_start = 0; self->tr_charge_period_start = 0;
put_current_thread(self);
// printk("%llu cycles charged to %s/%u", charge, // printk("%llu cycles charged to %s/%u", charge,
// self->tr_parent->t_name, self->tr_parent->t_id); // self->tr_parent->t_name, self->tr_parent->t_id);
+24 -7
View File
@@ -282,7 +282,7 @@ struct task *task_from_tid(tid_t id)
void task_exit(int status) void task_exit(int status)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
unsigned long flags; unsigned long flags;
task_lock_irqsave(self, &flags); task_lock_irqsave(self, &flags);
struct task *parent = self->t_parent; struct task *parent = self->t_parent;
@@ -295,7 +295,7 @@ void task_exit(int status)
task_unlock(parent); task_unlock(parent);
} }
struct thread *cur_thread = current_thread(); struct thread *cur_thread = get_current_thread();
self->t_state = TASK_STOPPED; self->t_state = TASK_STOPPED;
cur_thread->tr_state = THREAD_STOPPED; cur_thread->tr_state = THREAD_STOPPED;
@@ -340,6 +340,9 @@ void task_exit(int status)
self->t_base.ob_refcount); self->t_base.ob_refcount);
spin_unlock_irqrestore(handles_lock, flags); spin_unlock_irqrestore(handles_lock, flags);
put_current_thread(cur_thread);
put_current_task(self);
while (1) { while (1) {
schedule(SCHED_NORMAL); schedule(SCHED_NORMAL);
} }
@@ -352,8 +355,11 @@ kern_status_t task_open_handle(
kern_handle_t *out) kern_handle_t *out)
{ {
struct handle *handle_data = NULL; struct handle *handle_data = NULL;
kern_status_t status kern_status_t status = handle_table_alloc_handle(
= handle_table_alloc_handle(task->t_handles, &handle_data, out); task->t_handles,
KERN_HANDLE_INVALID,
&handle_data,
out);
if (status != KERN_OK) { if (status != KERN_OK) {
return status; return status;
} }
@@ -407,8 +413,19 @@ struct thread *task_create_thread(struct task *parent)
return thread; return thread;
} }
struct task *current_task(void) struct task *get_current_task(void)
{ {
struct thread *thr = current_thread(); struct thread *thr = get_current_thread();
return thr ? thr->tr_parent : NULL; if (!thr) {
return NULL;
}
struct task *out = task_ref(thr->tr_parent);
put_current_thread(thr);
return out;
}
void put_current_task(struct task *task)
{
task_unref(task);
} }
+56 -3
View File
@@ -101,11 +101,52 @@ kern_status_t thread_init_user(
return KERN_OK; return KERN_OK;
} }
kern_status_t thread_init_user_clone(
struct thread *thr,
const struct thread *src,
uintptr_t return_value)
{
thr->tr_state = THREAD_READY;
thr->tr_quantum_target = default_quantum();
thr->tr_kstack = vm_page_alloc(THREAD_KSTACK_ORDER, VM_NORMAL);
if (!thr->tr_kstack) {
return KERN_NO_MEMORY;
}
thr->tr_sp = (uintptr_t)vm_page_get_vaddr(thr->tr_kstack)
+ vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
thr->tr_bp = thr->tr_sp;
thr->tr_cpu_kernel_sp = thr->tr_sp;
/* the new thread needs two contextx:
* 1) to get the thread running in kernel mode, so that it can
* execute ml_thread_switch_user
* 2) to allow ml_thread_switch_user to jump to the correct place
* in usermode (and with the correct stack).
*
* these two contexts are constructed on the thread's kernel stack
* in reverse order.
*/
/* this context will be used by ml_user_return to jump to userspace
* with the specified instruction pointer and user stack */
ml_thread_clone_user_context(src->tr_irqctx, return_value, &thr->tr_sp);
/* this context will be used by the scheduler and ml_thread_switch to
* jump to ml_user_return in kernel mode with the thread's kernel stack.
*/
ml_thread_prepare_kernel_context(
(uintptr_t)ml_thread_switch_user,
&thr->tr_sp);
return KERN_OK;
}
void thread_free(struct thread *thr) void thread_free(struct thread *thr)
{ {
} }
struct thread *current_thread(void) struct thread *get_current_thread(void)
{ {
struct cpu_data *cpu = get_this_cpu(); struct cpu_data *cpu = get_this_cpu();
if (!cpu) { if (!cpu) {
@@ -113,13 +154,24 @@ struct thread *current_thread(void)
} }
struct thread *out = cpu->c_rq.rq_cur; struct thread *out = cpu->c_rq.rq_cur;
object_ref(&out->tr_base);
put_cpu(cpu); put_cpu(cpu);
return out; return out;
} }
void put_current_thread(struct thread *thr)
{
object_unref(&thr->tr_base);
}
bool need_resched(void) bool need_resched(void)
{ {
return (current_thread()->tr_flags & THREAD_F_NEED_RESCHED) != 0; struct thread *thr = get_current_thread();
bool result = (thr->tr_flags & THREAD_F_NEED_RESCHED) != 0;
put_current_thread(thr);
return result;
} }
int thread_priority(struct thread *thr) int thread_priority(struct thread *thr)
@@ -143,7 +195,7 @@ void thread_awaken(struct thread *thr)
void thread_exit(void) void thread_exit(void)
{ {
struct thread *self = current_thread(); struct thread *self = get_current_thread();
unsigned long flags; unsigned long flags;
thread_lock_irqsave(self, &flags); thread_lock_irqsave(self, &flags);
self->tr_state = THREAD_STOPPED; self->tr_state = THREAD_STOPPED;
@@ -153,6 +205,7 @@ void thread_exit(void)
self->tr_parent->t_id, self->tr_parent->t_id,
self->tr_id); self->tr_id);
thread_unlock_irqrestore(self, flags); thread_unlock_irqrestore(self, flags);
put_current_thread(self);
while (1) { while (1) {
schedule(SCHED_NORMAL); schedule(SCHED_NORMAL);
+2 -1
View File
@@ -44,7 +44,7 @@ unsigned long schedule_timeout(unsigned long ticks)
{ {
struct timer timer; struct timer timer;
struct thread *self = current_thread(); struct thread *self = get_current_thread();
timer.t_entry = QUEUE_ENTRY_INIT; timer.t_entry = QUEUE_ENTRY_INIT;
timer.t_expiry = clock_ticks + ticks; timer.t_expiry = clock_ticks + ticks;
@@ -58,6 +58,7 @@ unsigned long schedule_timeout(unsigned long ticks)
schedule(SCHED_NORMAL); schedule(SCHED_NORMAL);
remove_timer(&timer); remove_timer(&timer);
put_current_thread(self);
return 0; return 0;
} }
+12 -2
View File
@@ -110,7 +110,7 @@ void wakeup_one(struct waitqueue *q)
void sleep_forever(void) void sleep_forever(void)
{ {
struct thread *thr = current_thread(); struct thread *thr = get_current_thread();
struct runqueue *rq = thr->tr_rq; struct runqueue *rq = thr->tr_rq;
unsigned long flags; unsigned long flags;
@@ -121,7 +121,17 @@ void sleep_forever(void)
rq_unlock(rq, flags); rq_unlock(rq, flags);
while (thr->tr_state == THREAD_SLEEPING) { put_current_thread(thr);
while (1) {
thr = get_current_thread();
bool sleep = (thr->tr_state == THREAD_SLEEPING);
put_current_thread(thr);
if (!sleep) {
break;
}
schedule(SCHED_NORMAL); schedule(SCHED_NORMAL);
} }
} }
+32 -6
View File
@@ -11,13 +11,15 @@ kern_status_t sys_address_space_read(
size_t count, size_t count,
size_t *nr_read) size_t *nr_read)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, dst, count)) { if (!validate_access_w(self, dst, count)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (nr_read && !validate_access_w(self, nr_read, sizeof *nr_read)) { if (nr_read && !validate_access_w(self, nr_read, sizeof *nr_read)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -30,12 +32,14 @@ kern_status_t sys_address_space_read(
= task_resolve_handle(self, region_handle, &obj, &handle_flags); = task_resolve_handle(self, region_handle, &obj, &handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
struct address_space *region = address_space_cast(obj); struct address_space *region = address_space_cast(obj);
if (!region) { if (!region) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -52,6 +56,7 @@ kern_status_t sys_address_space_read(
address_space_unlock_irqrestore(region, flags); address_space_unlock_irqrestore(region, flags);
object_unref(obj); object_unref(obj);
put_current_task(self);
return status; return status;
} }
@@ -63,14 +68,16 @@ kern_status_t sys_address_space_write(
size_t count, size_t count,
size_t *nr_written) size_t *nr_written)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_r(self, src, count)) { if (!validate_access_r(self, src, count)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (nr_written if (nr_written
&& !validate_access_w(self, nr_written, sizeof *nr_written)) { && !validate_access_w(self, nr_written, sizeof *nr_written)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -83,12 +90,14 @@ kern_status_t sys_address_space_write(
= task_resolve_handle(self, region_handle, &obj, &handle_flags); = task_resolve_handle(self, region_handle, &obj, &handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
struct address_space *region = address_space_cast(obj); struct address_space *region = address_space_cast(obj);
if (!region) { if (!region) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -105,6 +114,7 @@ kern_status_t sys_address_space_write(
address_space_unlock_irqrestore(region, flags); address_space_unlock_irqrestore(region, flags);
object_unref(obj); object_unref(obj);
put_current_task(self);
return status; return status;
} }
@@ -118,13 +128,14 @@ kern_status_t sys_address_space_map(
vm_prot_t prot, vm_prot_t prot,
virt_addr_t *out_base_address) virt_addr_t *out_base_address)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (out_base_address if (out_base_address
&& !validate_access_r( && !validate_access_r(
self, self,
out_base_address, out_base_address,
sizeof *out_base_address)) { sizeof *out_base_address)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -141,24 +152,28 @@ kern_status_t sys_address_space_map(
&region_flags); &region_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
status = task_resolve_handle(self, object_handle, &vmo_obj, &vmo_flags); status = task_resolve_handle(self, object_handle, &vmo_obj, &vmo_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
struct address_space *region = address_space_cast(region_obj); struct address_space *region = address_space_cast(region_obj);
if (!region) { if (!region) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
struct vm_object *vmo = vm_object_cast(vmo_obj); struct vm_object *vmo = vm_object_cast(vmo_obj);
if (!vmo) { if (!vmo) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -177,6 +192,7 @@ kern_status_t sys_address_space_map(
object_unref(vmo_obj); object_unref(vmo_obj);
object_unref(region_obj); object_unref(region_obj);
put_current_task(self);
return status; return status;
} }
@@ -186,7 +202,7 @@ kern_status_t sys_address_space_unmap(
virt_addr_t base, virt_addr_t base,
size_t length) size_t length)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
kern_status_t status = KERN_OK; kern_status_t status = KERN_OK;
unsigned long flags; unsigned long flags;
@@ -201,12 +217,14 @@ kern_status_t sys_address_space_unmap(
&region_flags); &region_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
struct address_space *region = address_space_cast(region_obj); struct address_space *region = address_space_cast(region_obj);
if (!region) { if (!region) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -215,6 +233,7 @@ kern_status_t sys_address_space_unmap(
status = address_space_unmap(region, base, length); status = address_space_unmap(region, base, length);
object_unref(region_obj); object_unref(region_obj);
put_current_task(self);
return status; return status;
} }
@@ -225,13 +244,14 @@ kern_status_t sys_address_space_reserve(
size_t length, size_t length,
virt_addr_t *out_base_address) virt_addr_t *out_base_address)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (out_base_address if (out_base_address
&& !validate_access_r( && !validate_access_r(
self, self,
out_base_address, out_base_address,
sizeof *out_base_address)) { sizeof *out_base_address)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -248,12 +268,14 @@ kern_status_t sys_address_space_reserve(
&region_flags); &region_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
struct address_space *region = address_space_cast(region_obj); struct address_space *region = address_space_cast(region_obj);
if (!region) { if (!region) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -268,6 +290,7 @@ kern_status_t sys_address_space_reserve(
address_space_unlock_irqrestore(region, flags); address_space_unlock_irqrestore(region, flags);
object_unref(region_obj); object_unref(region_obj);
put_current_task(self);
return status; return status;
} }
@@ -277,7 +300,7 @@ kern_status_t sys_address_space_release(
virt_addr_t base, virt_addr_t base,
size_t length) size_t length)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
kern_status_t status = KERN_OK; kern_status_t status = KERN_OK;
unsigned long flags; unsigned long flags;
@@ -292,12 +315,14 @@ kern_status_t sys_address_space_release(
&region_flags); &region_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
struct address_space *region = address_space_cast(region_obj); struct address_space *region = address_space_cast(region_obj);
if (!region) { if (!region) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -308,6 +333,7 @@ kern_status_t sys_address_space_release(
address_space_unlock_irqrestore(region, flags); address_space_unlock_irqrestore(region, flags);
object_unref(region_obj); object_unref(region_obj);
put_current_task(self);
return status; return status;
} }
+10 -5
View File
@@ -3,21 +3,26 @@
kern_status_t sys_kern_config_get(kern_config_key_t key, void *ptr, size_t len) kern_status_t sys_kern_config_get(kern_config_key_t key, void *ptr, size_t len)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
kern_status_t status = KERN_OK;
switch (key) { switch (key) {
case KERN_CFG_PAGE_SIZE: case KERN_CFG_PAGE_SIZE:
if (!validate_access_w(self, ptr, sizeof(uintptr_t))) { if (!validate_access_w(self, ptr, sizeof(uintptr_t))) {
return KERN_MEMORY_FAULT; status = KERN_MEMORY_FAULT;
break;
} }
*(uint32_t *)ptr = VM_PAGE_SIZE; *(uint32_t *)ptr = VM_PAGE_SIZE;
return KERN_OK; status = KERN_OK;
break;
default: default:
return KERN_INVALID_ARGUMENT; status = KERN_INVALID_ARGUMENT;
break;
} }
return KERN_UNSUPPORTED; put_current_task(self);
return status;
} }
kern_status_t sys_kern_config_set( kern_status_t sys_kern_config_set(
+4 -1
View File
@@ -10,6 +10,7 @@ static const virt_addr_t syscall_table[] = {
SYSCALL_TABLE_ENTRY(TASK_CREATE, task_create), SYSCALL_TABLE_ENTRY(TASK_CREATE, task_create),
SYSCALL_TABLE_ENTRY(TASK_CREATE_THREAD, task_create_thread), SYSCALL_TABLE_ENTRY(TASK_CREATE_THREAD, task_create_thread),
SYSCALL_TABLE_ENTRY(TASK_GET_ADDRESS_SPACE, task_get_address_space), SYSCALL_TABLE_ENTRY(TASK_GET_ADDRESS_SPACE, task_get_address_space),
SYSCALL_TABLE_ENTRY(TASK_DUPLICATE, task_duplicate),
SYSCALL_TABLE_ENTRY(THREAD_SELF, thread_self), SYSCALL_TABLE_ENTRY(THREAD_SELF, thread_self),
SYSCALL_TABLE_ENTRY(THREAD_START, thread_start), SYSCALL_TABLE_ENTRY(THREAD_START, thread_start),
SYSCALL_TABLE_ENTRY(THREAD_EXIT, thread_exit), SYSCALL_TABLE_ENTRY(THREAD_EXIT, thread_exit),
@@ -27,7 +28,8 @@ static const virt_addr_t syscall_table[] = {
SYSCALL_TABLE_ENTRY(ADDRESS_SPACE_RELEASE, address_space_release), SYSCALL_TABLE_ENTRY(ADDRESS_SPACE_RELEASE, address_space_release),
SYSCALL_TABLE_ENTRY(KERN_LOG, kern_log), SYSCALL_TABLE_ENTRY(KERN_LOG, kern_log),
SYSCALL_TABLE_ENTRY(KERN_HANDLE_CLOSE, kern_handle_close), SYSCALL_TABLE_ENTRY(KERN_HANDLE_CLOSE, kern_handle_close),
SYSCALL_TABLE_ENTRY(KERN_HANDLE_DUPLICATE, kern_handle_duplicate), SYSCALL_TABLE_ENTRY(KERN_HANDLE_TRANSFER, kern_handle_transfer),
SYSCALL_TABLE_ENTRY(KERN_HANDLE_CONTROL, kern_handle_control),
SYSCALL_TABLE_ENTRY(KERN_CONFIG_GET, kern_config_get), SYSCALL_TABLE_ENTRY(KERN_CONFIG_GET, kern_config_get),
SYSCALL_TABLE_ENTRY(KERN_CONFIG_SET, kern_config_set), SYSCALL_TABLE_ENTRY(KERN_CONFIG_SET, kern_config_set),
SYSCALL_TABLE_ENTRY(CHANNEL_CREATE, channel_create), SYSCALL_TABLE_ENTRY(CHANNEL_CREATE, channel_create),
@@ -52,6 +54,7 @@ static const virt_addr_t syscall_table[] = {
VM_CONTROLLER_SUPPLY_PAGES, VM_CONTROLLER_SUPPLY_PAGES,
vm_controller_supply_pages), vm_controller_supply_pages),
SYSCALL_TABLE_ENTRY(KERN_OBJECT_WAIT, kern_object_wait), SYSCALL_TABLE_ENTRY(KERN_OBJECT_WAIT, kern_object_wait),
SYSCALL_TABLE_ENTRY(KERN_OBJECT_QUERY, kern_object_query),
SYSCALL_TABLE_ENTRY(FUTEX_WAIT, futex_wait), SYSCALL_TABLE_ENTRY(FUTEX_WAIT, futex_wait),
SYSCALL_TABLE_ENTRY(FUTEX_WAKE, futex_wake), SYSCALL_TABLE_ENTRY(FUTEX_WAKE, futex_wake),
}; };
+6 -2
View File
@@ -8,18 +8,22 @@ kern_status_t sys_futex_wait(
kern_futex_t new_val, kern_futex_t new_val,
unsigned int flags) unsigned int flags)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_r(self, futex, sizeof *futex)) { if (!validate_access_r(self, futex, sizeof *futex)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
futex_key_t key; futex_key_t key;
kern_status_t status = futex_get(futex, &key, flags); kern_status_t status = futex_get(futex, &key, flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
return futex_wait(key, new_val, flags); status = futex_wait(key, new_val, flags);
put_current_task(self);
return status;
} }
kern_status_t sys_futex_wake( kern_status_t sys_futex_wake(
+133 -14
View File
@@ -3,37 +3,156 @@
kern_status_t sys_kern_handle_close(kern_handle_t handle) kern_status_t sys_kern_handle_close(kern_handle_t handle)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
return task_close_handle(self, handle); kern_status_t status = task_close_handle(self, handle);
put_current_task(self);
return status;
} }
kern_status_t sys_kern_handle_duplicate( kern_status_t sys_kern_handle_transfer(
kern_handle_t handle, kern_handle_t src_task_handle,
kern_handle_t *out) kern_handle_t src_handle,
kern_handle_t dest_task_handle,
kern_handle_t dest_handle,
unsigned int mode,
kern_handle_t *out_handle)
{ {
struct task *self = current_task(); switch (mode) {
case HANDLE_TRANSFER_MOVE:
case HANDLE_TRANSFER_COPY:
break;
default:
return KERN_INVALID_ARGUMENT;
}
if (!validate_access_w(self, out, sizeof *out)) { struct task *self = get_current_task();
if (out_handle
&& !validate_access_w(self, out_handle, sizeof *out_handle)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
unsigned long flags; unsigned long flags;
task_lock_irqsave(self, &flags); task_lock_irqsave(self, &flags);
struct object *src_object = NULL;
struct task *src_task = NULL;
struct task *dest_task = NULL;
kern_status_t status = KERN_OK;
struct object *obj = NULL; struct object *obj = NULL;
handle_flags_t handle_flags = 0; handle_flags_t handle_flags = 0;
kern_status_t status
= task_resolve_handle(self, handle, &obj, &handle_flags); if (src_task_handle != KERN_HANDLE_INVALID) {
if (status != KERN_OK) { status = task_resolve_handle(
task_unlock_irqrestore(self, flags); self,
return status; src_task_handle,
&obj,
&handle_flags);
if (status != KERN_OK) {
task_unlock_irqrestore(self, flags);
goto cleanup;
}
src_task = task_cast(obj);
if (!src_task) {
status = KERN_INVALID_ARGUMENT;
goto cleanup;
}
} else {
src_task = self;
} }
status = task_open_handle(self, obj, handle_flags, out); if (dest_task_handle != KERN_HANDLE_INVALID) {
object_unref(obj); status = task_resolve_handle(
self,
dest_task_handle,
&obj,
&handle_flags);
if (status != KERN_OK) {
task_unlock_irqrestore(self, flags);
goto cleanup;
}
dest_task = task_cast(obj);
if (!dest_task) {
status = KERN_INVALID_ARGUMENT;
goto cleanup;
}
} else {
dest_task = self;
}
status = task_resolve_handle(
self,
src_handle,
&src_object,
&handle_flags);
if (status != KERN_OK) {
goto cleanup;
}
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
struct handle *dest = NULL;
task_lock_irqsave(dest_task, &flags);
status = handle_table_alloc_handle(
dest_task->t_handles,
dest_handle,
&dest,
&dest_handle);
task_unlock_irqrestore(dest_task, flags);
if (status != KERN_OK) {
goto cleanup;
}
if (mode == HANDLE_TRANSFER_MOVE) {
object_ref(src_object);
task_lock_irqsave(src_task, &flags);
handle_table_free_handle(src_task->t_handles, src_handle);
task_unlock_irqrestore(src_task, flags);
}
dest->h_object = src_object;
dest->h_flags = handle_flags;
if (out_handle) {
*out_handle = dest_handle;
}
put_current_task(self);
return KERN_OK;
cleanup:
if (src_task && src_task_handle != KERN_HANDLE_INVALID) {
object_unref(&src_task->t_base);
}
if (dest_task && dest_task_handle != KERN_HANDLE_INVALID) {
object_unref(&dest_task->t_base);
}
if (src_object) {
object_unref(src_object);
}
put_current_task(self);
return status; return status;
} }
kern_status_t sys_kern_handle_control(
kern_handle_t task,
kern_handle_t handle,
uint32_t set_mask,
uint32_t clear_mask,
uint32_t *out_flags)
{
return KERN_UNIMPLEMENTED;
}
+8 -2
View File
@@ -5,8 +5,14 @@
kern_status_t sys_kern_log(const char *s) kern_status_t sys_kern_log(const char *s)
{ {
struct task *task = current_task(); #ifdef TRACE
struct thread *thread = current_thread(); struct task *task = get_current_task();
struct thread *thread = get_current_thread();
printk("%s[%d.%d]: %s", task->t_name, task->t_id, thread->tr_id, s); printk("%s[%d.%d]: %s", task->t_name, task->t_id, thread->tr_id, s);
put_current_thread(thread);
put_current_task(task);
#else
printk("%s", s);
#endif
return KERN_OK; return KERN_OK;
} }
+49 -11
View File
@@ -7,13 +7,15 @@
kern_status_t sys_channel_create(unsigned int id, kern_handle_t *out) kern_status_t sys_channel_create(unsigned int id, kern_handle_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
struct channel *channel = channel_create(); struct channel *channel = channel_create();
if (!channel) { if (!channel) {
put_current_task(self);
return KERN_NO_MEMORY; return KERN_NO_MEMORY;
} }
@@ -22,6 +24,7 @@ kern_status_t sys_channel_create(unsigned int id, kern_handle_t *out)
if (task_get_channel(self, id)) { if (task_get_channel(self, id)) {
task_unlock_irqrestore(self, irq_flags); task_unlock_irqrestore(self, irq_flags);
put_current_task(self);
return KERN_NAME_EXISTS; return KERN_NAME_EXISTS;
} }
@@ -31,11 +34,13 @@ kern_status_t sys_channel_create(unsigned int id, kern_handle_t *out)
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, irq_flags); task_unlock_irqrestore(self, irq_flags);
object_unref(&channel->c_base); object_unref(&channel->c_base);
put_current_task(self);
return status; return status;
} }
task_add_channel(self, channel, id); task_add_channel(self, channel, id);
task_unlock_irqrestore(self, irq_flags); task_unlock_irqrestore(self, irq_flags);
put_current_task(self);
*out = handle; *out = handle;
return KERN_OK; return KERN_OK;
@@ -43,13 +48,15 @@ kern_status_t sys_channel_create(unsigned int id, kern_handle_t *out)
kern_status_t sys_port_create(kern_handle_t *out) kern_status_t sys_port_create(kern_handle_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
struct port *port = port_create(); struct port *port = port_create();
if (!port) { if (!port) {
put_current_task(self);
return KERN_NO_MEMORY; return KERN_NO_MEMORY;
} }
@@ -61,6 +68,7 @@ kern_status_t sys_port_create(kern_handle_t *out)
= task_open_handle(self, &port->p_base, 0, &handle); = task_open_handle(self, &port->p_base, 0, &handle);
task_unlock_irqrestore(self, irq_flags); task_unlock_irqrestore(self, irq_flags);
object_unref(&port->p_base); object_unref(&port->p_base);
put_current_task(self);
if (status != KERN_OK) { if (status != KERN_OK) {
return status; return status;
@@ -77,7 +85,7 @@ kern_status_t sys_port_connect(
{ {
unsigned long flags; unsigned long flags;
struct task *self = current_task(); struct task *self = get_current_task();
task_lock_irqsave(self, &flags); task_lock_irqsave(self, &flags);
struct object *port_obj = NULL; struct object *port_obj = NULL;
@@ -88,6 +96,7 @@ kern_status_t sys_port_connect(
&port_obj, &port_obj,
&port_handle_flags); &port_handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
@@ -96,6 +105,7 @@ kern_status_t sys_port_connect(
struct task *remote_task = task_from_tid(task_id); struct task *remote_task = task_from_tid(task_id);
if (!remote_task) { if (!remote_task) {
put_current_task(self);
return KERN_NO_ENTRY; return KERN_NO_ENTRY;
} }
@@ -104,6 +114,7 @@ kern_status_t sys_port_connect(
struct channel *remote = task_get_channel(remote_task, channel_id); struct channel *remote = task_get_channel(remote_task, channel_id);
if (!remote) { if (!remote) {
task_unlock_irqrestore(remote_task, flags); task_unlock_irqrestore(remote_task, flags);
put_current_task(self);
return KERN_NO_ENTRY; return KERN_NO_ENTRY;
} }
@@ -115,6 +126,7 @@ kern_status_t sys_port_connect(
port_unlock_irqrestore(port, flags); port_unlock_irqrestore(port, flags);
object_unref(&remote->c_base); object_unref(&remote->c_base);
object_unref(port_obj); object_unref(port_obj);
put_current_task(self);
return KERN_OK; return KERN_OK;
} }
@@ -123,7 +135,7 @@ kern_status_t sys_port_disconnect(kern_handle_t port_handle)
{ {
unsigned long flags; unsigned long flags;
struct task *self = current_task(); struct task *self = get_current_task();
task_lock_irqsave(self, &flags); task_lock_irqsave(self, &flags);
struct object *port_obj = NULL; struct object *port_obj = NULL;
@@ -134,6 +146,7 @@ kern_status_t sys_port_disconnect(kern_handle_t port_handle)
&port_obj, &port_obj,
&port_handle_flags); &port_handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
@@ -142,6 +155,7 @@ kern_status_t sys_port_disconnect(kern_handle_t port_handle)
struct port *port = port_cast(port_obj); struct port *port = port_cast(port_obj);
if (!port) { if (!port) {
object_unref(port_obj); object_unref(port_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -149,6 +163,7 @@ kern_status_t sys_port_disconnect(kern_handle_t port_handle)
port_lock_irqsave(port, &flags); port_lock_irqsave(port, &flags);
status = port_disconnect(port); status = port_disconnect(port);
port_unlock_irqrestore(port, flags); port_unlock_irqrestore(port, flags);
put_current_task(self);
return status; return status;
} }
@@ -219,13 +234,15 @@ kern_status_t sys_msg_send(
const kern_msg_t *msg, const kern_msg_t *msg,
kern_msg_t *out_reply) kern_msg_t *out_reply)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_msg(self, msg, false)) { if (!validate_msg(self, msg, false)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (!validate_msg(self, out_reply, true)) { if (!validate_msg(self, out_reply, true)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -240,15 +257,17 @@ kern_status_t sys_msg_send(
port_handle, port_handle,
&port_obj, &port_obj,
&port_handle_flags); &port_handle_flags);
task_unlock_irqrestore(self, flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
task_unlock_irqrestore(self, flags);
struct port *port = port_cast(port_obj); struct port *port = port_cast(port_obj);
if (!port) { if (!port) {
object_unref(port_obj); object_unref(port_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -256,15 +275,17 @@ kern_status_t sys_msg_send(
status = port_send_msg(port, msg, out_reply, &flags); status = port_send_msg(port, msg, out_reply, &flags);
port_unlock_irqrestore(port, flags); port_unlock_irqrestore(port, flags);
object_unref(port_obj); object_unref(port_obj);
put_current_task(self);
return status; return status;
} }
kern_status_t sys_msg_recv(kern_handle_t channel_handle, kern_msg_t *out_msg) kern_status_t sys_msg_recv(kern_handle_t channel_handle, kern_msg_t *out_msg)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_msg(self, out_msg, true)) { if (!validate_msg(self, out_msg, true)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -280,6 +301,7 @@ kern_status_t sys_msg_recv(kern_handle_t channel_handle, kern_msg_t *out_msg)
&channel_obj, &channel_obj,
&channel_handle_flags); &channel_handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
@@ -288,6 +310,7 @@ kern_status_t sys_msg_recv(kern_handle_t channel_handle, kern_msg_t *out_msg)
struct channel *channel = channel_cast(channel_obj); struct channel *channel = channel_cast(channel_obj);
if (!channel) { if (!channel) {
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -295,6 +318,7 @@ kern_status_t sys_msg_recv(kern_handle_t channel_handle, kern_msg_t *out_msg)
status = channel_recv_msg(channel, out_msg, &flags); status = channel_recv_msg(channel, out_msg, &flags);
channel_unlock_irqrestore(channel, flags); channel_unlock_irqrestore(channel, flags);
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return status; return status;
} }
@@ -304,9 +328,10 @@ kern_status_t sys_msg_reply(
msgid_t id, msgid_t id,
const kern_msg_t *reply) const kern_msg_t *reply)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_msg(self, reply, true)) { if (!validate_msg(self, reply, true)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -322,6 +347,7 @@ kern_status_t sys_msg_reply(
&channel_obj, &channel_obj,
&channel_handle_flags); &channel_handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
@@ -330,6 +356,7 @@ kern_status_t sys_msg_reply(
struct channel *channel = channel_cast(channel_obj); struct channel *channel = channel_cast(channel_obj);
if (!channel) { if (!channel) {
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -337,6 +364,7 @@ kern_status_t sys_msg_reply(
status = channel_reply_msg(channel, id, reply, &flags); status = channel_reply_msg(channel, id, reply, &flags);
channel_unlock_irqrestore(channel, flags); channel_unlock_irqrestore(channel, flags);
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return status; return status;
} }
@@ -349,13 +377,15 @@ kern_status_t sys_msg_read(
size_t iov_count, size_t iov_count,
size_t *nr_read) size_t *nr_read)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (nr_read && !validate_access_w(self, nr_read, sizeof *nr_read)) { if (nr_read && !validate_access_w(self, nr_read, sizeof *nr_read)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (!validate_iovec(self, iov, iov_count, true)) { if (!validate_iovec(self, iov, iov_count, true)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -371,6 +401,7 @@ kern_status_t sys_msg_read(
&channel_obj, &channel_obj,
&channel_handle_flags); &channel_handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
@@ -379,6 +410,7 @@ kern_status_t sys_msg_read(
struct channel *channel = channel_cast(channel_obj); struct channel *channel = channel_cast(channel_obj);
if (!channel) { if (!channel) {
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -393,6 +425,7 @@ kern_status_t sys_msg_read(
nr_read); nr_read);
channel_unlock_irqrestore(channel, flags); channel_unlock_irqrestore(channel, flags);
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return status; return status;
} }
@@ -405,14 +438,16 @@ kern_status_t sys_msg_write(
size_t iov_count, size_t iov_count,
size_t *nr_written) size_t *nr_written)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (nr_written if (nr_written
&& !validate_access_w(self, nr_written, sizeof *nr_written)) { && !validate_access_w(self, nr_written, sizeof *nr_written)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (!validate_iovec(self, iov, iov_count, false)) { if (!validate_iovec(self, iov, iov_count, false)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -428,6 +463,7 @@ kern_status_t sys_msg_write(
&channel_obj, &channel_obj,
&channel_handle_flags); &channel_handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
@@ -436,6 +472,7 @@ kern_status_t sys_msg_write(
struct channel *channel = channel_cast(channel_obj); struct channel *channel = channel_cast(channel_obj);
if (!channel) { if (!channel) {
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -450,6 +487,7 @@ kern_status_t sys_msg_write(
nr_written); nr_written);
channel_unlock_irqrestore(channel, flags); channel_unlock_irqrestore(channel, flags);
object_unref(channel_obj); object_unref(channel_obj);
put_current_task(self);
return status; return status;
} }
+39 -2
View File
@@ -13,12 +13,13 @@ kern_status_t sys_kern_object_wait(kern_wait_item_t *items, size_t nr_items)
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
struct task *self = current_task(); struct task *self = get_current_task();
struct thread *self_thread = current_thread();
if (!validate_access_rw(self, items, nr_items * sizeof *items)) { if (!validate_access_rw(self, items, nr_items * sizeof *items)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
struct thread *self_thread = get_current_thread();
self_thread->tr_state = THREAD_SLEEPING; self_thread->tr_state = THREAD_SLEEPING;
kern_status_t status = KERN_OK; kern_status_t status = KERN_OK;
@@ -78,5 +79,41 @@ cleanup:
} }
self_thread->tr_state = THREAD_READY; self_thread->tr_state = THREAD_READY;
put_current_thread(self_thread);
put_current_task(self);
return status; return status;
} }
kern_status_t sys_kern_object_query(
kern_handle_t object_handle,
kern_object_info_t *out)
{
struct task *self = get_current_task();
if (!out) {
put_current_task(self);
return KERN_INVALID_ARGUMENT;
}
if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT;
}
struct object *obj = NULL;
handle_flags_t flags = 0;
kern_status_t status
= task_resolve_handle(self, object_handle, &obj, &flags);
if (status != KERN_OK) {
put_current_task(self);
return status;
}
out->obj_id = obj->ob_id;
object_unref(obj);
put_current_task(self);
return KERN_OK;
}
+153 -10
View File
@@ -1,5 +1,6 @@
#include <kernel/address-space.h> #include <kernel/address-space.h>
#include <kernel/machine/cpu.h> #include <kernel/machine/cpu.h>
#include <kernel/panic.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/syscall.h> #include <kernel/syscall.h>
@@ -9,8 +10,9 @@
extern kern_status_t sys_task_exit(int status) extern kern_status_t sys_task_exit(int status)
{ {
#if defined(TRACE) #if defined(TRACE)
struct task *self = current_task(); struct task *self = get_current_task();
printk("%s[%d]: task_exit(%d)", self->t_name, self->t_id, status); printk("%s[%d]: task_exit(%d)", self->t_name, self->t_id, status);
put_current_task(self);
#endif #endif
task_exit(status); task_exit(status);
return KERN_FATAL_ERROR; return KERN_FATAL_ERROR;
@@ -18,8 +20,9 @@ extern kern_status_t sys_task_exit(int status)
kern_status_t sys_task_self(kern_handle_t *out) kern_status_t sys_task_self(kern_handle_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -30,16 +33,19 @@ kern_status_t sys_task_self(kern_handle_t *out)
kern_handle_t handle; kern_handle_t handle;
kern_status_t status = handle_table_alloc_handle( kern_status_t status = handle_table_alloc_handle(
self->t_handles, self->t_handles,
KERN_HANDLE_INVALID,
&handle_slot, &handle_slot,
&handle); &handle);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
object_ref(&self->t_base); object_ref(&self->t_base);
handle_slot->h_object = &self->t_base; handle_slot->h_object = &self->t_base;
put_current_task(self);
*out = handle; *out = handle;
return KERN_OK; return KERN_OK;
@@ -53,13 +59,15 @@ kern_status_t sys_task_create(
kern_handle_t *out_address_space) kern_handle_t *out_address_space)
{ {
unsigned long flags; unsigned long flags;
struct task *self = current_task(); struct task *self = get_current_task();
if (name_len && !validate_access_r(self, name, name_len)) { if (name_len && !validate_access_r(self, name, name_len)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (!validate_access_w(self, out_task, sizeof *out_task)) { if (!validate_access_w(self, out_task, sizeof *out_task)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -67,6 +75,7 @@ kern_status_t sys_task_create(
self, self,
out_address_space, out_address_space,
sizeof *out_address_space)) { sizeof *out_address_space)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -80,6 +89,7 @@ kern_status_t sys_task_create(
&parent_flags); &parent_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -89,22 +99,26 @@ kern_status_t sys_task_create(
kern_handle_t child_handle, space_handle; kern_handle_t child_handle, space_handle;
status = handle_table_alloc_handle( status = handle_table_alloc_handle(
self->t_handles, self->t_handles,
KERN_HANDLE_INVALID,
&child_handle_slot, &child_handle_slot,
&child_handle); &child_handle);
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(parent_obj); object_unref(parent_obj);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
status = handle_table_alloc_handle( status = handle_table_alloc_handle(
self->t_handles, self->t_handles,
KERN_HANDLE_INVALID,
&space_handle_slot, &space_handle_slot,
&space_handle); &space_handle);
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(parent_obj); object_unref(parent_obj);
handle_table_free_handle(self->t_handles, child_handle); handle_table_free_handle(self->t_handles, child_handle);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -118,6 +132,7 @@ kern_status_t sys_task_create(
handle_table_free_handle(self->t_handles, child_handle); handle_table_free_handle(self->t_handles, child_handle);
handle_table_free_handle(self->t_handles, space_handle); handle_table_free_handle(self->t_handles, space_handle);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_NO_MEMORY; return KERN_NO_MEMORY;
} }
@@ -133,6 +148,7 @@ kern_status_t sys_task_create(
object_ref(&child->t_address_space->s_base); object_ref(&child->t_address_space->s_base);
object_unref(parent_obj); object_unref(parent_obj);
put_current_task(self);
*out_task = child_handle; *out_task = child_handle;
*out_address_space = space_handle; *out_address_space = space_handle;
@@ -149,13 +165,15 @@ kern_status_t sys_task_create_thread(
kern_handle_t *out_thread) kern_handle_t *out_thread)
{ {
unsigned long flags; unsigned long flags;
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_r(self, args, nr_args * sizeof(uintptr_t))) { if (!validate_access_r(self, args, nr_args * sizeof(uintptr_t))) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (!validate_access_w(self, out_thread, sizeof *out_thread)) { if (!validate_access_w(self, out_thread, sizeof *out_thread)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -166,6 +184,7 @@ kern_status_t sys_task_create_thread(
= task_resolve_handle(self, task, &target_obj, &target_flags); = task_resolve_handle(self, task, &target_obj, &target_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -175,11 +194,13 @@ kern_status_t sys_task_create_thread(
kern_handle_t out_handle; kern_handle_t out_handle;
status = handle_table_alloc_handle( status = handle_table_alloc_handle(
self->t_handles, self->t_handles,
KERN_HANDLE_INVALID,
&target_handle, &target_handle,
&out_handle); &out_handle);
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(target_obj); object_unref(target_obj);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -194,6 +215,7 @@ kern_status_t sys_task_create_thread(
task_lock_irqsave(self, &flags); task_lock_irqsave(self, &flags);
handle_table_free_handle(self->t_handles, out_handle); handle_table_free_handle(self->t_handles, out_handle);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_NO_MEMORY; return KERN_NO_MEMORY;
} }
@@ -203,6 +225,7 @@ kern_status_t sys_task_create_thread(
task_unlock_irqrestore(target, flags); task_unlock_irqrestore(target, flags);
object_unref(target_obj); object_unref(target_obj);
put_current_task(self);
*out_thread = out_handle; *out_thread = out_handle;
return KERN_OK; return KERN_OK;
@@ -212,8 +235,9 @@ kern_status_t sys_task_get_address_space(
kern_handle_t task_handle, kern_handle_t task_handle,
kern_handle_t *out) kern_handle_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -231,16 +255,19 @@ kern_status_t sys_task_get_address_space(
&handle_flags); &handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
status = handle_table_alloc_handle( status = handle_table_alloc_handle(
self->t_handles, self->t_handles,
KERN_HANDLE_INVALID,
&handle_slot, &handle_slot,
&handle); &handle);
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(task_obj); object_unref(task_obj);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -250,6 +277,7 @@ kern_status_t sys_task_get_address_space(
object_unref(task_obj); object_unref(task_obj);
handle_table_free_handle(self->t_handles, handle); handle_table_free_handle(self->t_handles, handle);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -257,6 +285,7 @@ kern_status_t sys_task_get_address_space(
object_ref(&task->t_address_space->s_base); object_ref(&task->t_address_space->s_base);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
object_unref(task_obj); object_unref(task_obj);
put_current_task(self);
*out = handle; *out = handle;
return KERN_OK; return KERN_OK;
@@ -264,12 +293,13 @@ kern_status_t sys_task_get_address_space(
kern_status_t sys_thread_self(kern_handle_t *out) kern_status_t sys_thread_self(kern_handle_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
struct thread *self_thread = current_thread(); struct thread *self_thread = get_current_thread();
unsigned long flags; unsigned long flags;
task_lock_irqsave(self, &flags); task_lock_irqsave(self, &flags);
@@ -278,16 +308,21 @@ kern_status_t sys_thread_self(kern_handle_t *out)
kern_handle_t handle; kern_handle_t handle;
kern_status_t status = handle_table_alloc_handle( kern_status_t status = handle_table_alloc_handle(
self->t_handles, self->t_handles,
KERN_HANDLE_INVALID,
&handle_slot, &handle_slot,
&handle); &handle);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_thread(self_thread);
put_current_task(self);
return status; return status;
} }
object_ref(&self_thread->tr_base); object_ref(&self_thread->tr_base);
handle_slot->h_object = &self_thread->tr_base; handle_slot->h_object = &self_thread->tr_base;
put_current_thread(self_thread);
put_current_task(self);
*out = handle; *out = handle;
return KERN_OK; return KERN_OK;
@@ -296,7 +331,7 @@ kern_status_t sys_thread_self(kern_handle_t *out)
kern_status_t sys_thread_start(kern_handle_t thread_handle) kern_status_t sys_thread_start(kern_handle_t thread_handle)
{ {
unsigned long flags; unsigned long flags;
struct task *self = current_task(); struct task *self = get_current_task();
struct object *thread_obj; struct object *thread_obj;
handle_flags_t thread_flags; handle_flags_t thread_flags;
@@ -308,6 +343,7 @@ kern_status_t sys_thread_start(kern_handle_t thread_handle)
&thread_flags); &thread_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -316,6 +352,7 @@ kern_status_t sys_thread_start(kern_handle_t thread_handle)
schedule_thread_on_cpu(thread); schedule_thread_on_cpu(thread);
object_unref(thread_obj); object_unref(thread_obj);
put_current_task(self);
return KERN_OK; return KERN_OK;
} }
@@ -334,9 +371,10 @@ kern_status_t sys_thread_config_get(
size_t len) size_t len)
{ {
unsigned long flags; unsigned long flags;
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, ptr, len)) { if (!validate_access_w(self, ptr, len)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -350,6 +388,7 @@ kern_status_t sys_thread_config_get(
&thread_flags); &thread_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -359,6 +398,7 @@ kern_status_t sys_thread_config_get(
status = thread_config_get(thread, key, ptr, len); status = thread_config_get(thread, key, ptr, len);
object_unref(thread_obj); object_unref(thread_obj);
put_current_task(self);
return status; return status;
} }
@@ -370,9 +410,10 @@ kern_status_t sys_thread_config_set(
size_t len) size_t len)
{ {
unsigned long flags; unsigned long flags;
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, ptr, len)) { if (!validate_access_w(self, ptr, len)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -386,6 +427,7 @@ kern_status_t sys_thread_config_set(
&thread_flags); &thread_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -395,6 +437,107 @@ kern_status_t sys_thread_config_set(
status = thread_config_set(thread, key, ptr, len); status = thread_config_set(thread, key, ptr, len);
object_unref(thread_obj); object_unref(thread_obj);
put_current_task(self);
return status; return status;
} }
kern_status_t sys_task_duplicate(
kern_handle_t *out_task,
kern_handle_t *out_address_space)
{
struct task *self = get_current_task();
if (!validate_access_w(self, out_task, sizeof *out_task)) {
put_current_task(self);
return KERN_MEMORY_FAULT;
}
if (!validate_access_w(
self,
out_address_space,
sizeof *out_address_space)) {
put_current_task(self);
return KERN_MEMORY_FAULT;
}
*out_task = KERN_HANDLE_INVALID;
*out_address_space = KERN_HANDLE_INVALID;
kern_status_t status = KERN_OK;
unsigned long flags;
task_lock_irqsave(self, &flags);
struct handle *child_handle_slot = NULL, *space_handle_slot = NULL;
kern_handle_t child_handle, space_handle;
status = handle_table_alloc_handle(
self->t_handles,
KERN_HANDLE_INVALID,
&child_handle_slot,
&child_handle);
if (status != KERN_OK) {
task_unlock_irqrestore(self, flags);
put_current_task(self);
return status;
}
status = handle_table_alloc_handle(
self->t_handles,
KERN_HANDLE_INVALID,
&space_handle_slot,
&space_handle);
if (status != KERN_OK) {
handle_table_free_handle(self->t_handles, child_handle);
task_unlock_irqrestore(self, flags);
put_current_task(self);
return status;
}
struct task *new_task = task_create(self->t_name, strlen(self->t_name));
if (!new_task) {
put_current_task(self);
return KERN_NO_MEMORY;
}
struct thread *new_thread = task_create_thread(new_task);
if (!new_thread) {
handle_table_free_handle(self->t_handles, child_handle);
handle_table_free_handle(self->t_handles, space_handle);
task_unlock_irqrestore(self, flags);
object_unref(&new_task->t_base);
put_current_task(self);
return KERN_NO_MEMORY;
}
struct thread *self_thread = get_current_thread();
thread_init_user_clone(new_thread, self_thread, KERN_OK);
put_current_thread(self_thread);
status = address_space_duplicate(
new_task->t_address_space,
self->t_address_space);
if (status != KERN_OK) {
handle_table_free_handle(self->t_handles, child_handle);
handle_table_free_handle(self->t_handles, space_handle);
task_unlock_irqrestore(self, flags);
object_unref(&new_thread->tr_base);
object_unref(&new_task->t_base);
put_current_task(self);
return status;
}
schedule_thread_on_cpu(new_thread);
child_handle_slot->h_object = &new_task->t_base;
space_handle_slot->h_object = &new_task->t_address_space->s_base;
task_unlock_irqrestore(self, flags);
*out_task = child_handle;
*out_address_space = space_handle;
/* clear TLB */
pmap_switch(self->t_pmap);
put_current_task(self);
return KERN_OK;
}
+33 -6
View File
@@ -7,18 +7,21 @@
kern_status_t sys_vm_controller_create(kern_handle_t *out) kern_status_t sys_vm_controller_create(kern_handle_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
struct vm_controller *ctrl = vm_controller_create(); struct vm_controller *ctrl = vm_controller_create();
if (!ctrl) { if (!ctrl) {
put_current_task(self);
return KERN_NO_MEMORY; return KERN_NO_MEMORY;
} }
kern_status_t status = task_open_handle(self, &ctrl->vc_base, 0, out); kern_status_t status = task_open_handle(self, &ctrl->vc_base, 0, out);
put_current_task(self);
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(&ctrl->vc_base); object_unref(&ctrl->vc_base);
return status; return status;
@@ -31,9 +34,10 @@ kern_status_t sys_vm_controller_recv(
kern_handle_t ctrl_handle, kern_handle_t ctrl_handle,
equeue_packet_page_request_t *out) equeue_packet_page_request_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -50,6 +54,7 @@ kern_status_t sys_vm_controller_recv(
&handle_flags); &handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -57,6 +62,7 @@ kern_status_t sys_vm_controller_recv(
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
if (!ctrl) { if (!ctrl) {
object_unref(ctrl_obj); object_unref(ctrl_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -65,6 +71,7 @@ kern_status_t sys_vm_controller_recv(
vm_controller_unlock_irqrestore(ctrl, flags); vm_controller_unlock_irqrestore(ctrl, flags);
object_unref(ctrl_obj); object_unref(ctrl_obj);
put_current_task(self);
return status; return status;
} }
@@ -74,7 +81,7 @@ kern_status_t sys_vm_controller_recv_async(
kern_handle_t eq_handle, kern_handle_t eq_handle,
equeue_key_t key) equeue_key_t key)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
kern_status_t status = KERN_OK; kern_status_t status = KERN_OK;
unsigned long flags; unsigned long flags;
@@ -85,6 +92,7 @@ kern_status_t sys_vm_controller_recv_async(
status = task_resolve_handle(self, ctrl_handle, &ctrl_obj, &ctrl_flags); status = task_resolve_handle(self, ctrl_handle, &ctrl_obj, &ctrl_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -92,6 +100,7 @@ kern_status_t sys_vm_controller_recv_async(
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(ctrl_obj); object_unref(ctrl_obj);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -102,6 +111,7 @@ kern_status_t sys_vm_controller_recv_async(
if (!ctrl || !eq) { if (!ctrl || !eq) {
object_unref(ctrl_obj); object_unref(ctrl_obj);
object_unref(eq_obj); object_unref(eq_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -111,6 +121,7 @@ kern_status_t sys_vm_controller_recv_async(
object_unref(ctrl_obj); object_unref(ctrl_obj);
object_unref(eq_obj); object_unref(eq_obj);
put_current_task(self);
return status; return status;
} }
@@ -124,13 +135,15 @@ kern_status_t sys_vm_controller_create_object(
vm_prot_t prot, vm_prot_t prot,
kern_handle_t *out) kern_handle_t *out)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_r(self, name, name_len)) { if (!validate_access_r(self, name, name_len)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (!validate_access_w(self, out, sizeof *out)) { if (!validate_access_w(self, out, sizeof *out)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -147,6 +160,7 @@ kern_status_t sys_vm_controller_create_object(
&handle_flags); &handle_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -154,6 +168,7 @@ kern_status_t sys_vm_controller_create_object(
kern_handle_t out_handle = KERN_HANDLE_INVALID; kern_handle_t out_handle = KERN_HANDLE_INVALID;
status = handle_table_alloc_handle( status = handle_table_alloc_handle(
self->t_handles, self->t_handles,
KERN_HANDLE_INVALID,
&out_slot, &out_slot,
&out_handle); &out_handle);
@@ -161,6 +176,7 @@ kern_status_t sys_vm_controller_create_object(
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
if (!ctrl) { if (!ctrl) {
object_unref(ctrl_obj); object_unref(ctrl_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -182,10 +198,12 @@ kern_status_t sys_vm_controller_create_object(
task_lock_irqsave(self, &flags); task_lock_irqsave(self, &flags);
handle_table_free_handle(self->t_handles, out_handle); handle_table_free_handle(self->t_handles, out_handle);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
out_slot->h_object = &out_vmo->vo_base; out_slot->h_object = &out_vmo->vo_base;
put_current_task(self);
*out = out_handle; *out = out_handle;
return KERN_OK; return KERN_OK;
@@ -195,7 +213,7 @@ kern_status_t sys_vm_controller_detach_object(
kern_handle_t ctrl_handle, kern_handle_t ctrl_handle,
kern_handle_t vmo_handle) kern_handle_t vmo_handle)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
kern_status_t status = KERN_OK; kern_status_t status = KERN_OK;
unsigned long flags; unsigned long flags;
@@ -206,6 +224,7 @@ kern_status_t sys_vm_controller_detach_object(
status = task_resolve_handle(self, ctrl_handle, &ctrl_obj, &ctrl_flags); status = task_resolve_handle(self, ctrl_handle, &ctrl_obj, &ctrl_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -213,6 +232,7 @@ kern_status_t sys_vm_controller_detach_object(
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(ctrl_obj); object_unref(ctrl_obj);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -223,6 +243,7 @@ kern_status_t sys_vm_controller_detach_object(
if (!ctrl || !vmo) { if (!ctrl || !vmo) {
object_unref(ctrl_obj); object_unref(ctrl_obj);
object_unref(vmo_obj); object_unref(vmo_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -234,6 +255,7 @@ kern_status_t sys_vm_controller_detach_object(
object_unref(ctrl_obj); object_unref(ctrl_obj);
object_unref(vmo_obj); object_unref(vmo_obj);
put_current_task(self);
return status; return status;
} }
@@ -246,7 +268,7 @@ kern_status_t sys_vm_controller_supply_pages(
off_t src_offset, off_t src_offset,
size_t count) size_t count)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
kern_status_t status = KERN_OK; kern_status_t status = KERN_OK;
unsigned long flags; unsigned long flags;
@@ -257,6 +279,7 @@ kern_status_t sys_vm_controller_supply_pages(
status = task_resolve_handle(self, ctrl_handle, &ctrl_obj, &ctrl_flags); status = task_resolve_handle(self, ctrl_handle, &ctrl_obj, &ctrl_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -264,6 +287,7 @@ kern_status_t sys_vm_controller_supply_pages(
if (status != KERN_OK) { if (status != KERN_OK) {
object_unref(ctrl_obj); object_unref(ctrl_obj);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -272,6 +296,7 @@ kern_status_t sys_vm_controller_supply_pages(
object_unref(ctrl_obj); object_unref(ctrl_obj);
object_unref(dst_obj); object_unref(dst_obj);
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
@@ -284,6 +309,7 @@ kern_status_t sys_vm_controller_supply_pages(
object_unref(ctrl_obj); object_unref(ctrl_obj);
object_unref(dst_obj); object_unref(dst_obj);
object_unref(src_obj); object_unref(src_obj);
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
@@ -302,6 +328,7 @@ kern_status_t sys_vm_controller_supply_pages(
object_unref(ctrl_obj); object_unref(ctrl_obj);
object_unref(dst_obj); object_unref(dst_obj);
object_unref(src_obj); object_unref(src_obj);
put_current_task(self);
return status; return status;
} }
+25 -6
View File
@@ -11,13 +11,15 @@ kern_status_t sys_vm_object_create(
vm_prot_t prot, vm_prot_t prot,
kern_handle_t *out_handle) kern_handle_t *out_handle)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if ((name || name_len) && !validate_access_r(self, name, name_len)) { if ((name || name_len) && !validate_access_r(self, name, name_len)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (!validate_access_w(self, out_handle, sizeof *out_handle)) { if (!validate_access_w(self, out_handle, sizeof *out_handle)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -30,6 +32,7 @@ kern_status_t sys_vm_object_create(
kern_status_t status kern_status_t status
= task_open_handle(self, &obj->vo_base, 0, out_handle); = task_open_handle(self, &obj->vo_base, 0, out_handle);
object_unref(&obj->vo_base); object_unref(&obj->vo_base);
put_current_task(self);
return status; return status;
} }
@@ -41,13 +44,15 @@ kern_status_t sys_vm_object_read(
size_t count, size_t count,
size_t *nr_read) size_t *nr_read)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_w(self, dst, count)) { if (!validate_access_w(self, dst, count)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (nr_read && !validate_access_w(self, nr_read, sizeof *nr_read)) { if (nr_read && !validate_access_w(self, nr_read, sizeof *nr_read)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -55,15 +60,19 @@ kern_status_t sys_vm_object_read(
handle_flags_t flags = 0; handle_flags_t flags = 0;
kern_status_t status = task_resolve_handle(self, object, &obj, &flags); kern_status_t status = task_resolve_handle(self, object, &obj, &flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
struct vm_object *vmo = vm_object_cast(obj); struct vm_object *vmo = vm_object_cast(obj);
if (!vmo) { if (!vmo) {
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
return vm_object_read(vmo, dst, offset, count, nr_read); status = vm_object_read(vmo, dst, offset, count, nr_read);
put_current_task(self);
return status;
} }
kern_status_t sys_vm_object_write( kern_status_t sys_vm_object_write(
@@ -73,14 +82,16 @@ kern_status_t sys_vm_object_write(
size_t count, size_t count,
size_t *nr_written) size_t *nr_written)
{ {
struct task *self = current_task(); struct task *self = get_current_task();
if (!validate_access_r(self, src, count)) { if (!validate_access_r(self, src, count)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
if (nr_written if (nr_written
&& !validate_access_w(self, nr_written, sizeof *nr_written)) { && !validate_access_w(self, nr_written, sizeof *nr_written)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -88,15 +99,19 @@ kern_status_t sys_vm_object_write(
handle_flags_t flags = 0; handle_flags_t flags = 0;
kern_status_t status = task_resolve_handle(self, object, &obj, &flags); kern_status_t status = task_resolve_handle(self, object, &obj, &flags);
if (status != KERN_OK) { if (status != KERN_OK) {
put_current_task(self);
return status; return status;
} }
struct vm_object *vmo = vm_object_cast(obj); struct vm_object *vmo = vm_object_cast(obj);
if (!vmo) { if (!vmo) {
put_current_task(self);
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
return vm_object_write(vmo, src, offset, count, nr_written); status = vm_object_write(vmo, src, offset, count, nr_written);
put_current_task(self);
return status;
} }
kern_status_t sys_vm_object_copy( kern_status_t sys_vm_object_copy(
@@ -114,10 +129,11 @@ kern_status_t sys_vm_object_copy(
src_offset, src_offset,
count, count,
nr_copied); nr_copied);
struct task *self = current_task(); struct task *self = get_current_task();
if (nr_copied if (nr_copied
&& !validate_access_w(self, nr_copied, sizeof *nr_copied)) { && !validate_access_w(self, nr_copied, sizeof *nr_copied)) {
put_current_task(self);
return KERN_MEMORY_FAULT; return KERN_MEMORY_FAULT;
} }
@@ -131,16 +147,19 @@ kern_status_t sys_vm_object_copy(
status = task_resolve_handle(self, dst, &dst_obj, &dst_flags); status = task_resolve_handle(self, dst, &dst_obj, &dst_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
status = task_resolve_handle(self, src, &src_obj, &src_flags); status = task_resolve_handle(self, src, &src_obj, &src_flags);
if (status != KERN_OK) { if (status != KERN_OK) {
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
return status; return status;
} }
task_unlock_irqrestore(self, flags); task_unlock_irqrestore(self, flags);
put_current_task(self);
struct vm_object *dst_vmo = vm_object_cast(dst_obj); struct vm_object *dst_vmo = vm_object_cast(dst_obj);
struct vm_object *src_vmo = vm_object_cast(src_obj); struct vm_object *src_vmo = vm_object_cast(src_obj);
+369 -4
View File
@@ -1206,6 +1206,238 @@ kern_status_t address_space_release(
return status; return status;
} }
static struct vm_area *area_duplicate(struct vm_area *area)
{
struct vm_area *out = vm_cache_alloc(&vm_area_cache, VM_NORMAL);
if (!out) {
return NULL;
}
out->vma_prot = area->vma_prot;
out->vma_object_offset = area->vma_object_offset;
out->vma_base = area->vma_base;
out->vma_limit = area->vma_limit;
return out;
}
static kern_status_t update_area_pte_cow(
struct address_space *src,
struct address_space *dest,
struct vm_area *area)
{
if (!area->vma_object) {
return KERN_OK;
}
for (virt_addr_t i = area->vma_base; i < area->vma_limit;
i += VM_PAGE_SIZE) {
off_t pg_offset = i - area->vma_base + area->vma_object_offset;
struct vm_page *pg = vm_object_get_page(
area->vma_object,
pg_offset,
0,
NULL);
vm_prot_t temp_prot = area->vma_prot;
temp_prot &= ~VM_PROT_WRITE;
if (pg) {
pmap_add(
src->s_pmap,
i,
vm_page_get_pfn(pg),
temp_prot,
PMAP_NORMAL);
pmap_add(
dest->s_pmap,
i,
vm_page_get_pfn(pg),
temp_prot,
PMAP_NORMAL);
tracek("PTE %zx -> %zx [%x]",
i,
vm_page_get_paddr(pg),
temp_prot);
}
}
return KERN_OK;
}
static kern_status_t prepare_duplicate_areas(
struct address_space *src,
struct address_space *dest)
{
struct btree_node *cur_node = btree_first(&src->s_mappings);
while (cur_node) {
struct vm_area *tmp_area
= BTREE_CONTAINER(struct vm_area, vma_node, cur_node);
if (!tmp_area->vma_object) {
cur_node = btree_next(cur_node);
continue;
}
struct vm_object *src_vmo = tmp_area->vma_object;
vm_object_lock(src_vmo);
struct vm_object *dest_vmo = NULL;
struct queue_entry *cur_entry
= queue_first(&src_vmo->vo_mappings);
while (cur_entry) {
struct vm_area *src_area = QUEUE_CONTAINER(
struct vm_area,
vma_object_entry,
cur_entry);
if (src_area->vma_space != src) {
cur_entry = queue_next(cur_entry);
continue;
}
struct vm_area *dest_area = get_entry(
&dest->s_mappings,
src_area->vma_base,
GET_ENTRY_EXACT);
if (!dest_area) {
/* this shouldn't happen. the duplicate vm_areas
* were already created by
* address_space_duplicate */
panic("create_duplicate_vmo: corresponding "
"vm_area is missing");
}
if (dest_area->vma_object) {
cur_entry = queue_next(cur_entry);
continue;
}
if (!dest_vmo) {
tracek("[%zx-%zx %x] creating COW duplicate of "
"vmo %p",
src_area->vma_base,
src_area->vma_limit,
src_area->vma_prot,
src_vmo);
dest_vmo = vm_object_duplicate_cow(src_vmo);
tracek("[%zx-%zx %x] created COW duplicate of "
"vmo %p -> %p",
src_area->vma_base,
src_area->vma_limit,
src_area->vma_prot,
src_vmo,
dest_vmo);
}
dest_area->vma_object = dest_vmo;
update_area_pte_cow(src, dest, src_area);
cur_entry = queue_next(cur_entry);
}
vm_object_unlock(src_vmo);
cur_node = btree_next(cur_node);
}
return KERN_OK;
}
kern_status_t address_space_duplicate(
struct address_space *dest,
struct address_space *src)
{
// address_space_dump(src);
/* clang-format off
* strategy for COW address space duplication:
* 1. duplicate each vm_area in the address space
* a. all details except for the vm_object pointer are copied.
* b. create a duplicate vm_object, where all the details are the
* same, but don't copy the pages or vm_page pointers.
* c. if the vm_object is attached to a vm_controller, don't inform
* the controller yet.
* d. for both the original and duplicate vm_area, duplicate the PTE
* entries, changing all of them to read-only. increment the
* p_cow_ref counters for all committed vm_pages.
* e. use the vm_object's vm_area list, and the vm_area's vma_space
* pointer, to ensure that only one duplicate is created for each
* unique vm-object referenced by an address-space.
* 2. when a page fault occurs:
* a. find the relevant vm_area as normal.
* b. if the faulted page is present and the vm_area's prot flags
* should allow the access, a COW is required.
* c. if the relevant page is already present in the vm_area's
* vm_object, this is the original vm_area. otherwise, this is the
* clone vm_area.
* d. if this is the source vm_area:
* i. decrement p_cow_ref in the page. if it is 0, skip to step v.
* ii. remove the relevant page from the vm_area
* iii. allocate a new page and copy the data.
* iv. add the new page to the vm_object at the same offset.
* v. change the PTE entry to the proper protection flags.
* vi. resume the faulting task.
* e. otherwise, if this is the clone vm_area:
* i. if the vm-object has a controller, send
* PAGE_REQUEST_DUPLICATE to it. the controller needs to
* prepare itself to receive page requests from this vm-object,
* which includes priving it an equeue_key_t.
* i. use the physical address stored in the PTE to find the
* relevant vm_page.
* ii. decrement p_cow_ref in the page.
* iii. if p_cow_ref is > 0, allocate a new page and copy the data.
* otherwise, use the existing page as-is.
* iv. add the page from step iii to the vm_object at the correct
* offset.
* v. change the PTE entry to the proper protection flags.
* vi. resume the faulting task.
* 3. when destroying a vm_area:
* a. for pages already present in a vm-object, handle as normal.
* b. for pages not present in a vm-object, but for which a valid PTE
* exists, use the PTE physical address to find the vm_page.
* c. decrement p_cow_ref in this page.
* d. if p_cow_ref == 0, de-allocate the page.
* clang-format on
*/
struct btree_node *cur = btree_first(&src->s_mappings);
while (cur) {
struct vm_area *src_area
= BTREE_CONTAINER(struct vm_area, vma_node, cur);
struct vm_area *dest_area = area_duplicate(src_area);
tracek("duplicated vm_area [%zx-%zx] %p -> %p",
src_area->vma_base,
src_area->vma_limit,
src_area,
dest_area);
/* TODO handle OOM */
put_entry(&dest->s_mappings, dest_area);
cur = btree_next(cur);
}
cur = btree_first(&src->s_reserved);
while (cur) {
struct vm_area *src_area
= BTREE_CONTAINER(struct vm_area, vma_node, cur);
struct vm_area *dest_area = area_duplicate(src_area);
tracek("duplicated vm_area [r] [%zx-%zx] %p -> %p",
src_area->vma_base,
src_area->vma_limit,
src_area,
dest_area);
/* TODO handle OOM */
put_entry(&dest->s_reserved, dest_area);
cur = btree_next(cur);
}
tracek("preparing duplicate areas");
kern_status_t status = prepare_duplicate_areas(src, dest);
tracek("prepared duplicate areas");
if (status != KERN_OK) {
return status;
}
return KERN_OK;
}
bool address_space_validate_access( bool address_space_validate_access(
struct address_space *region, struct address_space *region,
virt_addr_t ptr, virt_addr_t ptr,
@@ -1261,9 +1493,11 @@ static kern_status_t request_missing_page(
/* here: /* here:
* `region` is locked. * `region` is locked.
* `object` is unlocked. * `object` is unlocked.
* `irq_flags` must be restored when `region` is unlocked. * `irq_flags` must be restored when `region` is
* the relevant page in `object` may or may not be committed. * unlocked.
* if it isn't, it needs to be requested. * the relevant page in `object` may or may
* not be committed. if it isn't, it needs to be
* requested.
*/ */
vm_object_lock(object); vm_object_lock(object);
address_space_unlock(region); address_space_unlock(region);
@@ -1292,7 +1526,129 @@ static kern_status_t request_missing_page(
return status; return status;
} }
/* this function must be called with `region` locked */ /* handle a write to a page that is present and /should be/ writeable, but is
* currently read-only due to COW. */
static kern_status_t handle_cow_access(
struct address_space *region,
virt_addr_t addr,
enum pmap_fault_flags flags,
unsigned long *irq_flags)
{
struct vm_area *area
= get_entry(&region->s_mappings, addr, GET_ENTRY_EXACT);
if (!area || !area->vma_object) {
/* no mapping exists (this shouldn't happen) */
address_space_unlock_irqrestore(region, *irq_flags);
return KERN_NO_ENTRY;
}
if ((area->vma_prot & (VM_PROT_WRITE | VM_PROT_USER))
!= (VM_PROT_WRITE | VM_PROT_USER)) {
/* access denied */
address_space_unlock_irqrestore(region, *irq_flags);
return KERN_ACCESS_DENIED;
}
tracek("cow access %zx", addr);
if (area->vma_object->vo_ctrl) {
panic("COW on controlled vm-object");
}
off_t object_offset = addr - area->vma_base + area->vma_object_offset;
vm_object_lock(area->vma_object);
struct vm_page *pg
= vm_object_get_page(area->vma_object, object_offset, 0, NULL);
bool add_page = false;
if (!pg) {
/* the page hasn't been added to the vmo yet. */
pfn_t pfn;
vm_prot_t prot;
kern_status_t status
= pmap_get(region->s_pmap, addr, &pfn, &prot);
if (status != KERN_OK) {
vm_object_unlock(area->vma_object);
address_space_unlock_irqrestore(region, *irq_flags);
return status;
}
add_page = true;
pg = vm_page_get(pfn * VM_PAGE_SIZE);
tracek("recovered page %zx (%p, %u) from the aether",
vm_page_get_paddr(pg),
pg,
pg->p_cow_ref);
if (!pg) {
/* still can't find the page */
panic("cannot resolve cow-reference to page %zx",
pfn * VM_PAGE_SIZE);
}
if (!pg->p_cow_ref) {
/* still can't find the page */
panic("cow-reference page %zx cow-ref=0",
pfn * VM_PAGE_SIZE);
}
}
atomic_t cow_ref = atomic_sub_fetch(&pg->p_cow_ref, 1);
tracek("decrement cow-ref page %zx -> now %u",
vm_page_get_paddr(pg),
pg->p_cow_ref);
if (cow_ref > 0) {
/* another task is (or will be) referencing this page. create
* a copy */
struct vm_page *dup = vm_page_alloc(VM_PAGE_4K, VM_NORMAL);
if (!dup) {
vm_object_unlock(area->vma_object);
address_space_unlock_irqrestore(region, *irq_flags);
return KERN_NO_MEMORY;
}
void *src = vm_page_get_vaddr(pg);
void *dest = vm_page_get_vaddr(dup);
memcpy(dest, src, vm_page_get_size_bytes(dup));
if (!add_page) {
btree_delete(&area->vma_object->vo_pages, &pg->p_bnode);
}
vm_object_put_page(area->vma_object, object_offset, dup);
tracek("splitting cow page %zx -> %zx",
vm_page_get_paddr(pg),
vm_page_get_paddr(dup));
tracek("put page %zx into area [%zx-%zx] at %zx",
vm_page_get_paddr(dup),
area->vma_base,
area->vma_limit,
object_offset);
pg = dup;
} else if (add_page) {
/* we are the last task referencing this page. add it to our
* vmo and continue */
tracek("claimed cow page %zx", vm_page_get_paddr(pg));
vm_object_put_page(area->vma_object, object_offset, pg);
} else {
/* we are the last task referencing this page, and it is already
* in our vmo. */
tracek("reclaimed cow page %zx", vm_page_get_paddr(pg));
}
kern_status_t status = pmap_add(
region->s_pmap,
addr,
vm_page_get_pfn(pg),
area->vma_prot,
PMAP_NORMAL);
vm_object_unlock(area->vma_object);
address_space_unlock_irqrestore(region, *irq_flags);
return status;
}
/* this function must be called with `region` unlocked */
kern_status_t address_space_demand_map( kern_status_t address_space_demand_map(
struct address_space *region, struct address_space *region,
virt_addr_t addr, virt_addr_t addr,
@@ -1306,6 +1662,13 @@ kern_status_t address_space_demand_map(
unsigned long irq_flags; unsigned long irq_flags;
address_space_lock_irqsave(region, &irq_flags); address_space_lock_irqsave(region, &irq_flags);
const enum pmap_fault_flags cow_flags
= PMAP_FAULT_WRITE | PMAP_FAULT_PRESENT | PMAP_FAULT_USER;
if ((flags & cow_flags) == cow_flags) {
return handle_cow_access(region, addr, flags, &irq_flags);
}
struct vm_area *area struct vm_area *area
= get_entry(&region->s_mappings, addr, GET_ENTRY_EXACT); = get_entry(&region->s_mappings, addr, GET_ENTRY_EXACT);
if (!area || !area->vma_object) { if (!area || !area->vma_object) {
@@ -1348,6 +1711,8 @@ kern_status_t address_space_demand_map(
#endif #endif
if (!pg) { if (!pg) {
vm_object_unlock(area->vma_object);
address_space_unlock_irqrestore(region, irq_flags);
return KERN_FATAL_ERROR; return KERN_FATAL_ERROR;
} }
+13 -2
View File
@@ -159,6 +159,7 @@ kern_status_t vm_controller_recv(
spin_unlock(&req->req_lock); spin_unlock(&req->req_lock);
if (req->req_status == PAGE_REQUEST_ASYNC) { if (req->req_status == PAGE_REQUEST_ASYNC) {
put_current_thread(req->req_sender);
vm_cache_free(&page_request_cache, req); vm_cache_free(&page_request_cache, req);
} }
@@ -215,12 +216,21 @@ kern_status_t vm_controller_detach_object(
return KERN_INVALID_ARGUMENT; return KERN_INVALID_ARGUMENT;
} }
if (vmo->vo_key == 0) {
/* this vmo isn't actually attached to this controller yet.
* this can happen if a controller-attached vmo was duplicated
* via copy-on-write, and the duplicate vmo has not yet been
* accessed. */
vmo->vo_ctrl = NULL;
return KERN_OK;
}
struct page_request *req struct page_request *req
= vm_cache_alloc(&page_request_cache, VM_NORMAL); = vm_cache_alloc(&page_request_cache, VM_NORMAL);
req->req_type = PAGE_REQUEST_DETACH; req->req_type = PAGE_REQUEST_DETACH;
req->req_status = PAGE_REQUEST_ASYNC; req->req_status = PAGE_REQUEST_ASYNC;
req->req_object = vmo->vo_key; req->req_object = vmo->vo_key;
req->req_sender = current_thread(); req->req_sender = get_current_thread();
send_request_async(ctrl, req); send_request_async(ctrl, req);
vmo->vo_ctrl = NULL; vmo->vo_ctrl = NULL;
@@ -236,7 +246,7 @@ static void wait_for_reply(
unsigned long *lock_flags) unsigned long *lock_flags)
{ {
struct wait_item waiter; struct wait_item waiter;
struct thread *self = current_thread(); struct thread *self = get_current_thread();
wait_item_init(&waiter, self); wait_item_init(&waiter, self);
for (;;) { for (;;) {
@@ -251,6 +261,7 @@ static void wait_for_reply(
} }
self->tr_state = THREAD_READY; self->tr_state = THREAD_READY;
put_current_thread(self);
} }
static void fulfill_requests( static void fulfill_requests(
+92 -4
View File
@@ -1,4 +1,5 @@
#include <kernel/address-space.h> #include <kernel/address-space.h>
#include <kernel/panic.h>
#include <kernel/printk.h> #include <kernel/printk.h>
#include <kernel/sched.h> #include <kernel/sched.h>
#include <kernel/util.h> #include <kernel/util.h>
@@ -31,9 +32,10 @@ static kern_status_t vm_object_cleanup(struct object *obj)
if (vmo->vo_ctrl) { if (vmo->vo_ctrl) {
unsigned long flags; unsigned long flags;
vm_controller_lock_irqsave(vmo->vo_ctrl, &flags); struct vm_controller *ctrl = vmo->vo_ctrl;
vm_controller_lock_irqsave(ctrl, &flags);
vm_controller_detach_object(vmo->vo_ctrl, vmo); vm_controller_detach_object(vmo->vo_ctrl, vmo);
vm_controller_unlock_irqrestore(vmo->vo_ctrl, flags); vm_controller_unlock_irqrestore(ctrl, flags);
} }
return KERN_OK; return KERN_OK;
@@ -276,6 +278,43 @@ extern struct vm_object *vm_object_create_in_place(
return vmo; return vmo;
} }
struct vm_object *vm_object_duplicate_cow(struct vm_object *vmo)
{
struct object *obj = object_create(&vm_object_type);
if (!obj) {
return NULL;
}
struct vm_object *out = VM_OBJECT_CAST(obj);
memcpy(out->vo_name, vmo->vo_name, sizeof out->vo_name);
out->vo_flags = vmo->vo_flags;
out->vo_ctrl = vmo->vo_ctrl;
out->vo_prot = vmo->vo_prot;
out->vo_size = vmo->vo_size;
memcpy(out->vo_name, vmo->vo_name, sizeof vmo->vo_name);
struct btree_node *cur = btree_first(&vmo->vo_pages);
while (cur) {
struct vm_page *pg
= BTREE_CONTAINER(struct vm_page, p_bnode, cur);
int x = atomic_fetch_add(&pg->p_cow_ref, 1);
if (x == 0) {
/* this is the first cow-reference for this page, so
* the address-space it belongs to will need one too */
atomic_fetch_add(&pg->p_cow_ref, 1);
}
tracek("convert page %zx to cow %u",
vm_page_get_paddr(pg),
pg->p_cow_ref);
cur = btree_next(cur);
}
return out;
}
static struct vm_page *alloc_page(struct vm_object *vo, off_t offset) static struct vm_page *alloc_page(struct vm_object *vo, off_t offset)
{ {
struct vm_page *page = NULL; struct vm_page *page = NULL;
@@ -341,6 +380,54 @@ static struct vm_page *alloc_page(struct vm_object *vo, off_t offset)
return NULL; return NULL;
} }
kern_status_t vm_object_put_page(
struct vm_object *vo,
off_t offset,
struct vm_page *pg)
{
struct btree_node *cur = vo->vo_pages.b_root;
if (!vo->vo_pages.b_root) {
vo->vo_pages.b_root = &pg->p_bnode;
return KERN_OK;
}
while (cur) {
struct vm_page *page
= BTREE_CONTAINER(struct vm_page, p_bnode, cur);
struct btree_node *next = NULL;
off_t base = page->p_vmo_offset;
off_t limit = base + vm_page_get_size_bytes(page);
if (offset < base) {
next = btree_left(cur);
} else if (offset >= limit) {
next = btree_right(cur);
} else {
panic("vm_object_put_page: page already exists");
return KERN_NAME_EXISTS;
}
if (next) {
cur = next;
continue;
}
pg->p_vmo_offset = offset;
if (offset < base) {
btree_put_left(cur, &pg->p_bnode);
} else {
btree_put_right(cur, &pg->p_bnode);
}
btree_insert_fixup(&vo->vo_pages, &pg->p_bnode);
return KERN_OK;
}
return KERN_FATAL_ERROR;
}
static struct vm_page *get_page(struct vm_object *vo, off_t offset) static struct vm_page *get_page(struct vm_object *vo, off_t offset)
{ {
struct btree_node *cur = vo->vo_pages.b_root; struct btree_node *cur = vo->vo_pages.b_root;
@@ -376,7 +463,7 @@ static kern_status_t request_page(
req.req_type = PAGE_REQUEST_READ; req.req_type = PAGE_REQUEST_READ;
req.req_offset = offset; req.req_offset = offset;
req.req_length = vm_page_order_to_bytes(VM_PAGE_4K); req.req_length = vm_page_order_to_bytes(VM_PAGE_4K);
req.req_sender = current_thread(); req.req_sender = get_current_thread();
object_ref(&vo->vo_base); object_ref(&vo->vo_base);
req.req_object = vo->vo_key; req.req_object = vo->vo_key;
@@ -388,6 +475,7 @@ static kern_status_t request_page(
kern_status_t status kern_status_t status
= vm_controller_send_request(ctrl, &req, irq_flags); = vm_controller_send_request(ctrl, &req, irq_flags);
put_current_thread(req.req_sender);
spin_unlock(&req.req_lock); spin_unlock(&req.req_lock);
vm_controller_unlock_irqrestore(ctrl, *irq_flags); vm_controller_unlock_irqrestore(ctrl, *irq_flags);
object_unref(&vo->vo_base); object_unref(&vo->vo_base);
@@ -412,7 +500,7 @@ struct vm_page *vm_object_get_page(
return pg; return pg;
} }
if (!vo->vo_ctrl) { if (!vo->vo_ctrl || !(flags & VMO_REQUEST_MISSING_PAGE)) {
return NULL; return NULL;
} }