~samhsmith/AstraOS

61b2c08f011eb5aea0728cb16bcad83b34391cdb — Sam H Smith 2 years ago 38604ef
fixed kernel threading bug that I created the other day when solving a bug. Aand I fixed a buffer overrun in create ipfc handler
4 files changed, 24 insertions(+), 9 deletions(-)

M src/process.c
M src/process_run.c
M src/syscall.c
M src/tempuser.c
M src/process.c => src/process.c +1 -1
@@ 974,7 974,7 @@ u64 process_ipfc_handler_create(
    
    Kallocation* array = process->ipfc_handler_alloc.memory;
    array[found_index] = alloc;
    *out_handler_id_ptr = found_index;
    if(out_handler_id_ptr) { *out_handler_id_ptr = found_index; }
    return 1;
}


M src/process_run.c => src/process_run.c +1 -3
@@ 73,14 73,13 @@ u64 current_thread_runtimes[KERNEL_MAX_HART_COUNT];
u64 last_mtimes[KERNEL_MAX_HART_COUNT];

/*
 * Make sure you do not have a lock on THREAD_RUNTIME_ARRAY_LOCK
 * Make sure you have a READ lock on THREAD_RUNTIME_ARRAY_LOCK
 * when calling kernel_choose_new_thread
 */

struct xoshiro256ss_state kernel_choose_new_thread_rando_state[KERNEL_MAX_HART_COUNT];
void kernel_choose_new_thread(u64 new_mtime, u64 hart)
{
    rwlock_acquire_read(&THREAD_RUNTIME_ARRAY_LOCK);
    ThreadRuntime* runtime_array = THREAD_RUNTIME_ARRAY_ALLOC.memory;

    if(kernel_current_thread_has_thread[hart])


@@ 223,7 222,6 @@ void kernel_choose_new_thread(u64 new_mtime, u64 hart)
        spinlock_release(&runtime_array[new_thread_runtime].lock);
    }

    rwlock_release_read(&THREAD_RUNTIME_ARRAY_LOCK);
    if(!found_new_thread)
    {
        // Causes the KERNEL nop thread to be loaded

M src/syscall.c => src/syscall.c +6 -4
@@ 230,8 230,8 @@ void syscall_thread_sleep(u64 hart, u64 mtime)
    }
    // go to sleep
    frame->regs[10] = 1;
    rwlock_release_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
    kernel_choose_new_thread(mtime, hart);
    rwlock_release_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
}

void syscall_thread_awake_on_keyboard(u64 hart)


@@ 1738,8 1738,8 @@ void syscall_process_exit(u64 hart, u64 mtime)
    process_flag_all_threads_for_destruction(process);
    rwlock_release_write(&process->process_lock);

    rwlock_release_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
    kernel_choose_new_thread(mtime, hart);
    rwlock_release_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
}

void syscall_process_is_alive(u64 hart)


@@ 1925,7 1925,7 @@ void syscall_IPFC_handler_create(u64 hart)
    u64 user_handler_id_ptr = frame->regs[17];
    current_thread->program_counter += 4;
    
    if((user_stack_pages_start % PAGE_SIZE) != 0)
    if((user_stack_pages_start % PAGE_SIZE) != 0 || user_handler_name_buffer_len > 64)
    {
        frame->regs[10] = 0;
        rwlock_release_write(&process->process_lock);


@@ 2164,7 2164,9 @@ void syscall_IPFC_call(u64 hart, u64 mtime)
    current_thread->is_running = 0;

    rwlock_release_write(&KERNEL_PROCESS_ARRAY_RWLOCK);
    rwlock_acquire_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
    kernel_choose_new_thread(mtime, hart);
    rwlock_release_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
}

void syscall_IPFC_return(u64 hart, u64 mtime)


@@ 2246,8 2248,8 @@ void syscall_IPFC_return(u64 hart, u64 mtime)
    else
    { rwlock_release_write(&process->process_lock); }

    rwlock_release_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
    kernel_choose_new_thread(mtime, hart);
    rwlock_release_read(&KERNEL_PROCESS_ARRAY_RWLOCK);
}

void do_syscall(TrapFrame* frame, u64 mtime, u64 hart)

M src/tempuser.c => src/tempuser.c +16 -1
@@ 421,7 421,10 @@ void thunder_windowed_application_ipfc_api_entry(u64 source_pid, u16 function_in

void program_loader_program(u64 drive1_partitions_directory)
{

    // this enables the use of global variables
    __asm__(".option norelax");
    __asm__("la gp, _global_pointer");
    __asm__(".option relax");

    window_count = 0;
    u8* print_text = "program loader program has started.\n";


@@ 481,6 484,18 @@ void program_loader_program(u64 drive1_partitions_directory)
    f64 rolling_time_passed = 0.0;
    f64 rolling_frame_time = 0.0;

    // setting up twa interface
    {
        u64 handler_name_len = strlen(TWA_IPFC_API_NAME);
        u64 handler_stacks_start = 0x3241234000;
        AOS_alloc_pages(handler_stacks_start, 2);
        if(!AOS_IPFC_handler_create(TWA_IPFC_API_NAME, handler_name_len,
                                    thunder_windowed_application_ipfc_api_entry,
                                    handler_stacks_start, 2, 1, 0)
        )
        { AOS_H_printf("failed to init twa ipfc handler. Something is very wrong.\n"); }
    }

while(1) {

    { // Check for program not alive's