Add base stuff for preemptive scheduler

This commit is contained in:
Felix Kopp 2020-06-12 11:49:20 +02:00
parent d5c958012b
commit c19e131f20
No known key found for this signature in database
GPG key ID: C478BA0A85F75728
13 changed files with 700 additions and 1 deletions

View file

@ -76,6 +76,7 @@ include arch/Makefile
include init/Makefile
include lib/Makefile
include kernel/Makefile
ARDIX_OBJS = $(ARDIX_SOURCES:.c=.o)
ARDIX_ASM_OBJS = $(ARDIX_ASM_SOURCES:.S=.o)

View file

@ -26,10 +26,15 @@
ARDIX_ARCH_PWD = $(PWD)/arch/at91sam3x8e
ARDIX_SOURCES += \
$(ARDIX_ARCH_PWD)/isr_sched.c \
$(ARDIX_ARCH_PWD)/startup.c
ARDIX_ASM_SOURCES += \
$(ARDIX_ARCH_PWD)/isr_pend_sv.S
CFLAGS += \
-DARCH_AT91SAM3X8E
LDFLAGS += \
-T$(ARDIX_ARCH_PWD)/config.ld \
-T$(ARDIX_ARCH_PWD)/flash.ld

View file

@ -0,0 +1,32 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
/*
* stack size is 32K (uses the entire sram1 bank)
* TODO: modify flash.ld to actually align this properly
*/
STACK_SIZE = 0x8000;

View file

@ -0,0 +1,98 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
.syntax unified
.thumb
.text
.extern sched_process_switch
.thumb_func
.global isr_pend_sv
.type isr_pend_sv, %function
/* void isr_pend_sv(void); */
isr_pend_sv:
/*
* There seems to be a limitation in Thumb which prohibits popping to
* any register > r7. Hence, we need to split the PUSHes and POPs into
* multiple operations and MOV register values around.
*/
/*
* Some registers have already been saved by hardware at this point,
* we only need to take care of r4-r11 and lr (the latter of which is
* required because lr is overwritten when entering the isr).
* The stuff we push onto the stack manually looks about like this:
*
* >>> stack grow direction >>>
* r7 r6 r5 r4 lr r11 r10 r9 r8
*/
push {r4-r7} /* r4-r7 */
mov r3, r8
mov r4, r9
mov r5, r10
mov r6, r11
mov r7, lr
push {r3-r7} /* r8-r11, lr */
/*
* Now that our stack is completely saved, we can proceed to call the
* Kernel's scheduler. This updates `_current_process` to the process
* we want to execute next.
*/
/* TODO: Implement banked stack pointer */
mov r0, sp
bl sched_process_switch /* sp = sched_process_switch(sp); */
mov sp, r0
/*
* The new stack pointer contains the state of the new process, so we
* load it into our registers using the same procedure as above,
* just in reverse order.
*/
pop {r3-r7} /* r8-r11, lr */
mov lr, r7
mov r11, r6
mov r10, r5
mov r9, r4
mov r8, r3
pop {r4-r7} /* r4-r7 */
/*
* ... and now get the fuck outta here, hoping I never need to touch
* this again, even though deep inside I know I will return here at some
* point because something broke (or I feel the need to micro-optimize).
*/
bx lr
.size isr_pend_sv, .-isr_pend_sv

View file

@ -0,0 +1,48 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#include <arch/at91sam3x8e/hardware.h>
#include <arch/at91sam3x8e/interrupt.h>
#include <ardix/sched.h>
#include <toolchain.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
void isr_sys_tick(void)
{
/*
* fire a PendSV interrupt and do the actual context switching there
* because it is faster that way (according to the docs, at least)
*/
SCB_ICSR |= SCB_PENDSVSET_MASK;
}
#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

View file

@ -0,0 +1,198 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#pragma once
#include <ardix/types.h>
/** CPU clock frequency in Hertz */
#define F_CPU 84000000UL
/** Pointer size in bytes */
#define PTRSIZE 4
/* The following three structs are basically just for reference */
/**
* A software snapshot of all registers that have not been saved by automated
* hardware routines on ISR entry. Required for scheduling / context switching.
*/
struct reg_sw_snapshot {
uint32_t r4;
uint32_t r5;
uint32_t r6;
uint32_t r7;
uint32_t r8;
uint32_t r9;
uint32_t r10;
uint32_t r11;
/*
* lr is saved by hardware, but we need to store it twice
* because the ISR entry overwrites it
*/
uint32_t lr; /* alias r14 */
};
/**
* All registers that are automatically saved by hardware routines when entering
* an ISR, in the correct order.
*/
struct reg_hw_snapshot {
uint32_t r0;
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r12;
uint32_t lr; /* alias r14 */
uint32_t pc; /* alias r15 */
uint32_t psr;
};
/** Combination of `struct reg_sw_snapshot` and `struct reg_hw_snapshot`. */
struct reg_snapshot {
struct reg_sw_snapshot sw;
struct reg_hw_snapshot hw;
};
/*
* Real-time Timer (RTT)
*/
/** Real-time Timer Mode Register */
#define REG_RTT_MR (*(uint32_t *)0x400E1A30U)
/** Real-time Timer Restart bitmask (for `RTT_MR`) */
#define REG_RTTRST_MASK ((uint32_t)1 << 18)
/** Real-time Timer Increment Interrupt Enable bitmask (for `REG_RTT_MR`) */
#define REG_RTTINCIEN_MASK ((uint32_t)1 << 17)
/** Alarm Interrupt Enable bitmask (for `REG_RTT_MR`) */
#define REG_ALMIEN_MASK ((uint32_t)1 << 16)
/** Real-time Timer Prescaler Value */
#define REG_RTPRES (*(uint16_t *)0x400E1A30U)
/** Real-time Timer Alarm Register */
#define REG_RTT_AR (*(uint32_t *)0x400E1A34U)
/** Real-time Timer Alarm Value */
#define REG_ALMV REG_RTT_AR
/** Real-time timer Value Register */
#define REG_RTT_VR (*(uint32_t *)0x400E1A38U)
/** Current Real-time Value */
#define REG_CRTV REG_RTT_VR
/** Real-time Timer Status Register */
#define REG_RTT_SR (*(const uint32_t *)0x400E1A3CU)
/** Real-time Timer Increment bitmask (for `REG_RTT_SR`) */
#define REG_RTTINC_MASK ((uint32_t)1 << 1)
/** Real-time Timer Alarm Status bitmask (for `REG_RTT_SR`) */
#define REG_ALMS_MASK ((uint32_t)1)
/*
* SysTick
*/
/** SysTick Control and Status Register */
#define REG_SYSTICK_CTRL (*(uint32_t *)0xE000E010U)
/** SysTick CTRL COUNTFLAG bitmask (for `REG_SYSTICK_CTRL`) */
#define REG_SYSTICK_CTRL_COUNTFLAG_MASK ((uint32_t)1 << 16)
/** SysTick CTRL CLKSOURCE bitmask (for `REG_SYSTICK_CTRL`) */
#define REG_SYSTICK_CTRL_CLKSOURCE_MASK ((uint32_t)1 << 2)
/** SysTick CTRL exception request enable bitmask (for `REG_SYSTICK_CTRL`) */
#define REG_SYSTICK_CTRL_TICKINT_MASK ((uint32_t)1 << 1)
/** SysTick CTRL enable bitmask (for `REG_SYSTICK_CTRL`) */
#define REG_SYSTICK_CTRL_ENABLE_MASK ((uint32_t)1)
/** SysTick Reload Value Register */
#define REG_SYSTICK_LOAD (*(uint32_t *)0xE000E014U)
#define REG_SYSTICK_LOAD_RELOAD_MASK ((uint32_t)0x00FFFFFFU)
/** SysTick Current Value Register */
#define REG_SYSTICK_VAL (*(uint32_t *)0xE000E018U)
/** SysTick Current Value bitmask (for `REG_SYSTICK_VAL`) */
#define REG_SYSTICK_VAL_CURRENT_MASK ((uint32_t)0x00FFFFFFU)
/** SysTick Calibration Value Register */
#define REG_SYSTICK_CALIB (*(uint32_t *)0xE000E01CU)
#define REG_SYSTICK_CALIB_NOREF_MASK ((uint32_t)1 << 31)
#define REG_SYSTICK_CALIB_SKEW_MASK ((uint32_t)1 << 30)
#define REG_SYSTICK_CALIB_TENMS_MASK ((uint32_t)0x00FFFFFFU)
/*
* Real-time Clock (RTC)
*/
/** RTC Control Register */
#define REG_RTC_CR (*(uint32_t *)0x400E1A60U)
/*
* Universal Asynchronous Receiver Transceiver (UART)
*/
/** UART Control Register */
#define REG_UART_CR (*(uint32_t *)0x400E0800U)
/** UART Control Register Reset Status Bits bitmask (for `REG_UART_CR`) */
#define REG_RSTSTA_MASK ((uint32_t)1 << 8)
/** UART Control Register Transmitter Disable bitmaask (for `REG_UART_CR`) */
#define REG_TXDIS_MASK ((uint32_t)1 << 7)
/** UART Control Register Transmitter Enable bitmaask (for `REG_UART_CR`) */
#define REG_TXEN_MASK ((uint32_t)1 << 6)
/** UART Control Register Receiver Disable bitmask (for `REG_UART_CR`) */
#define REG_RXDIS_MASK ((uint32_t)1 << 5)
/** UART Control Register Receiver Enable bitmask (for `REG_UART_CR`) */
#define REG_RXEN_MASK ((uint32_t)1 << 4)
/** UART Control Register Reset Transmitter bitmask (for `REG_UART_CR`) */
#define REG_RSTTX_MASK ((uint32_t)1 << 3)
/** UART Control Register Reset Receiver bitmask (for `REG_UART_CR`) */
#define REG_RSTRX_MASK ((uint32_t)1 << 2)
/** UART Mode Register */
#define REG_UART_MR (*(uint32_t *)0x400E0804U)
/*
* Nested Vectored Interrupt Controller
*/
/** System Control Block: Interrupt Control and State Register */
#define SCB_ICSR (*(uint32_t *)0xE000ED04U)
/** ICSR PendSV set-pending bit bitmask (for `SCB_ICSR`) */
#define SCB_PENDSVSET_MASK ((uint32_t)1 << 28)
/** ICSR PendSV clear-pending bit bitmask (for `SCB_ICSR`) */
#define SCB_PENDSVCLR_MASK ((uint32_t)1 << 27)
/** ICSR SysTick exception set-pending bit bitmask (for `SCB_ICSR`) */
#define SCB_PENDSTSET_MASK ((uint32_t)1 << 26)
/** ICSR SysTick exception clear-pending bit bitmask (for `SCB_ICSR`) */
#define SCB_PENDSTCLR_MASK ((uint32_t)1 << 25)
/** ICSR Interrupt pending flag, excluding Faults bitmask (for `SCB_ICSR`) */
#define SCB_ISRPENDING_MASK ((uint32_t)1 << 22)
/**
* ICSR bitmask for highest priority pending & enabled exception
* (for `SCB_ICSR`). The value is shifted 12 to the left.
*/
#define SCB_VECTPENDING_MASK ((uint32_t)0b1111111111u << 12)
/** ICSR bitmask for whether there are preempted active exceptions (for `SCB_ICSR`) */
#define SCB_RETTOBASE_MASK ((uint32_t)1 << 11)
/** ICSR active exception number bitmask (for `SCB_ICSR`) */
#define SCB_VECTACTIVE_MASK ((uint32_t)0b111111111u)

View file

@ -44,7 +44,7 @@ void isr_svc(void);
/** Debug handler (reserved) */
void isr_debug_mon(void);
/** Pending SV interrupt handler */
void isr_pend_sv(void);
extern void isr_pend_sv(void);
/** SysTick interrupt handler */
void isr_sys_tick(void);

36
include/arch/hardware.h Normal file
View file

@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#pragma once
#if defined(ARCH_ATMEGA328P)
#error "ATmega328p is not implemented (yet?)"
#elif defined(ARCH_AT91SAM3X8E)
#include <arch/at91sam3x8e/hardware.h>
#else
#error "Unsupported architecture"
#endif

128
include/ardix/sched.h Normal file
View file

@ -0,0 +1,128 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#pragma once
#include <ardix/types.h>
#include <arch/hardware.h>
#ifndef SCHED_MAXPROC
/** The maximum number of processes. */
#define SCHED_MAXPROC 16
#endif /* SCHED_MAXPROC */
#if SCHED_MAXPROC > 64
#warning "SCHED_MAXPROC is > 64, this could have a significant impact on performance"
#endif /* SCHED_MAXPROC > 64 */
enum proc_state {
/** Process is dead / ddoesn't exist */
PROC_DEAD,
/** Process is ready for execution or currently running. */
PROC_READY,
/** Process is waiting for its next time share. */
PROC_QUEUE,
/** Process is sleeping, `sleep_until` specifies when to wake it up. */
PROC_SLEEP,
};
/** Stores an entire process image. */
struct process {
/** Next process in the (circular) list. */
struct process *next;
/** Stack pointer. */
void *sp;
/** Bottom of the stack (i.e. the highest address). */
void *stack_bottom;
/** If `state` is `PROC_SLEEP`, the last execution time. */
unsigned long int lastexec;
/** If `state` is `PROC_SLEEP`, the amount of us to sleep in total. */
unsigned long int sleep_usecs;
/** Process ID. */
pid_t pid;
/** Process state. */
enum proc_state state;
};
/** The currently executing process. */
extern struct process *_current_process;
/**
* Initialize the scheduler subsystem.
* This sets up a hardware interrupt timer (SysTick for Cortex-M3).
*/
int sched_init(void);
/**
* Switch to the next process (atomic / interrupt handler context only).
* Must be called directly from within an interrupt routine.
* Takes care of the following stuff:
*
* - Selecting a new process to run, or putting the CPU to sleep
* - Updating the `_current_process` pointer
* - Setting the `state` member of both the old and new process to the
* appropriate value
*
* To avoid the horrors of inline assembly, the stack pointer is modified in
* actual assembler files rather than here.
*
* @param curr_sp: The stack pointer of the current process.
* @returns The stack pointer of the new process.
*/
void *sched_process_switch(void *curr_sp);
/**
* Create a new process.
*
* @param exec: The process executor.
* @returns A pointer to the new process, or `NULL` if something went wrong.
*
* TODO: make something like errno to tell what *exactly* went wrong
*/
struct process *sched_process_create(void (*exec)(void));
/**
* Suspend the current process for the specified amount of milliseconds.
* Note that there are slight deviations from this time interval because of the
* round-robin scheduling algorithm.
* If the sleep time is required to be exactly accurate, use `atomic_udelay()`.
* Note, however, that this will block *all* other processes, even including
* I/O, for the entire time period.
*
* @param msecs: The amount of milliseconds to (approximately) sleep for.
*/
void sleep(unsigned long int msecs);
/**
* Block the entire CPU from execution for the specified amount of microseconds.
* Note that this will temporarily disable the scheduler, meaning that *nothing*
* (not even I/O) will be executed. The only reason you would ever want to use
* this is for mission-critical, very short (<= 100 us) periods of time.
*
* @param usecs: The amount of microseconds to halt the CPU for.
*/
void atomic_udelay(unsigned long int usecs);

42
include/ardix/types.h Normal file
View file

@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#pragma once
#include <stdint.h>
#ifndef __SIZE_TYPE__
#define __SIZE_TYPE__ unsigned long int
#endif /* __SIZE_TYPE__ */
/** Unsigned size specifier. */
typedef __SIZE_TYPE__ size_t;
/** Signed size specifier (negative sizes mean error codes). */
typedef signed long int ssize_t;
/** Process identifier. */
typedef int8_t pid_t;

View file

@ -39,6 +39,11 @@
#define __always_inline inline __attribute__((always_inline))
#endif /* __always_inline */
#ifndef __naked
/** Function attribute for disabling register saving. */
#define __naked __attribute__((naked))
#endif
#ifndef __weak
/**
* Add the `weak` attribute to a symbol.

29
kernel/Makefile Normal file
View file

@ -0,0 +1,29 @@
#
# Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
#
# 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 copyright holder nor the names of its contributors may be
# used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
#
ARDIX_KERNEL_PWD = $(PWD)/kernel
ARDIX_SOURCES += \
$(ARDIX_KERNEL_PWD)/sched.c

77
kernel/sched.c Normal file
View file

@ -0,0 +1,77 @@
/*
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
*
* 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 copyright holder nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#include <ardix/clock.h>
#include <ardix/sched.h>
#include <ardix/types.h>
#include <stddef.h>
extern uint32_t _sstack;
extern uint32_t _estack;
struct process *_current_process;
static struct process procs[SCHED_MAXPROC + 1];
int sched_init(void)
{
int i;
_current_process = &procs[0];
_current_process->next = _current_process;
_current_process->sp = &_sstack;
_current_process->stack_bottom = &_estack;
_current_process->pid = 0;
_current_process->state = PROC_READY;
for (i = 1; i < SCHED_MAXPROC + 1; i++) {
procs[i].state = PROC_DEAD;
procs[i].pid = -1;
}
/* TODO: initialize SysTick */
return 0;
}
void *sched_process_switch(void *curr_sp)
{
struct process *nextproc = _current_process;
_current_process->sp = curr_sp;
while (true) {
nextproc = nextproc->next;
if (nextproc->state == PROC_QUEUE) {
_current_process->state = PROC_QUEUE;
nextproc->state = PROC_READY;
_current_process = nextproc;
break;
}
}
return _current_process->sp;
}