Skip to content

Commit

Permalink
Single ready queue scheduler (#91)
Browse files Browse the repository at this point in the history
First scheduler implementation with a single shared ready queued. Allow
user thread to be preempted on a tick interrupt.
  • Loading branch information
phaubertin authored Nov 21, 2024
1 parent 0c79544 commit dece0b4
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 256 deletions.
10 changes: 0 additions & 10 deletions include/kernel/domain/entities/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,12 @@ thread_t *thread_new(process_t *process);

void thread_prepare(thread_t *thread, const thread_params_t *params);

void thread_ready(thread_t *thread);

void thread_run_first(thread_t *thread);

void thread_run(thread_t *thread);

void thread_yield_current(void);

void thread_terminate_current(void);

void thread_switch_to(thread_t *to);

void thread_switch_to_and_block(thread_t *to);

void thread_block_current_and_unlock(spinlock_t *lock);

int thread_await(thread_t *thread);

void thread_set_local_storage(thread_t *thread, addr_t addr, size_t size);
Expand Down
37 changes: 37 additions & 0 deletions include/kernel/domain/services/asm/scheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2019-2024 Philippe Aubertin.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef JINUE_KERNEL_SERVICES_ASM_SCHEDULER_H
#define JINUE_KERNEL_SERVICES_ASM_SCHEDULER_H

#define SCHEDULER_BASE_CREDITS 3

#endif
55 changes: 55 additions & 0 deletions include/kernel/domain/services/scheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2019-2024 Philippe Aubertin.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the author nor the names of other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef JINUE_KERNEL_SERVICES_SCHEDULER_H
#define JINUE_KERNEL_SERVICES_SCHEDULER_H

#include <kernel/domain/services/asm/scheduler.h>
#include <kernel/types.h>
#include <stdbool.h>

void reschedule(void);

void scheduler_tick(void);

void ready_thread(thread_t *thread);

void yield_current_thread(void);

void switch_to_thread(thread_t *to);

void switch_to_thread_and_block(thread_t *to);

void block_current_thread_and_unlock(spinlock_t *lock);

void switch_from_exiting_thread(void);

#endif
9 changes: 9 additions & 0 deletions include/kernel/interface/i686/trap.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@
#ifndef JINUE_KERNEL_INTERFACE_I686_TRAP_H
#define JINUE_KERNEL_INTERFACE_I686_TRAP_H

#include <jinue/shared/types.h>
#include <kernel/interface/i686/types.h>

extern int syscall_implementation;

void handle_trap(trapframe_t *trapframe);

/** entry point for Intel fast system call implementation (SYSENTER/SYSEXIT) */
void fast_intel_entry(void);

Expand All @@ -44,4 +49,8 @@ void fast_amd_entry(void);
* first time. See thread_page_create(). */
void return_from_interrupt(void);

static inline jinue_syscall_args_t *trapframe_syscall_args(trapframe_t *trapframe) {
return (jinue_syscall_args_t *)&trapframe->msg_arg0;
}

#endif
1 change: 1 addition & 0 deletions include/kernel/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ struct thread_t {
machine_thread_t machine_thread;
list_node_t thread_list;
thread_state_t state;
int cpu_credits;
process_t *process;
struct thread_t *sender;
struct thread_t *awaiter;
Expand Down
2 changes: 2 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ sources.kernel.c = \
domain/services/ipc.c \
domain/services/logging.c \
domain/services/panic.c \
domain/services/scheduler.c \
domain/config.c \
infrastructure/i686/drivers/vga.c \
infrastructure/i686/drivers/pic8259.c \
Expand All @@ -121,6 +122,7 @@ sources.kernel.c = \
interface/i686/auxv.c \
interface/i686/bootinfo.c \
interface/i686/interrupts.c \
interface/i686/trap.c \
interface/syscalls.c

sources.kernel.nasm = \
Expand Down
3 changes: 2 additions & 1 deletion kernel/application/interrupts/tick.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
*/

#include <kernel/application/interrupts.h>
#include <kernel/domain/services/scheduler.h>

void tick_interrupt(void) {
/* TODO implement something here */
scheduler_tick();
}
4 changes: 2 additions & 2 deletions kernel/application/syscalls/yield_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
*/

#include <kernel/application/syscalls.h>
#include <kernel/domain/entities/thread.h>
#include <kernel/domain/services/scheduler.h>

void yield_thread(void) {
thread_yield_current();
yield_current_thread();
}
Loading

0 comments on commit dece0b4

Please sign in to comment.