Skip to content

Commit

Permalink
Working mpu
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald committed Nov 14, 2024
1 parent 4fd36b5 commit 895dc65
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 181 deletions.
73 changes: 3 additions & 70 deletions edge_os.ld
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,13 @@ SECTIONS
KEEP(*(.svc_data))
PROVIDE(__stop_svc_data = .);
} > RAM
.fs_data :
{
PROVIDE(__start_fs_data = .);
KEEP(*(.fs_data))
PROVIDE(__stop_fs_data = .);
} > RAM
.log_dynamic_data :
{
PROVIDE(__start_log_dynamic_data = .);
KEEP(*(SORT(.log_dynamic_data*)))
PROVIDE(__stop_log_dynamic_data = .);
} > RAM
.cli_sorted_cmd_ptrs :
{
PROVIDE(__start_cli_sorted_cmd_ptrs = .);
KEEP(*(.cli_sorted_cmd_ptrs))
PROVIDE(__stop_cli_sorted_cmd_ptrs = .);
} > RAM
. = ALIGN(16384);
.user_programs_data :
.user_programs_data ALIGN(16384) :
{
PROVIDE(__start_user_programs_data = .);
_build/user_*.o(.data*)
Expand All @@ -53,73 +40,19 @@ SECTIONS

SECTIONS
{
.pwr_mgmt_data :
{
PROVIDE(__start_pwr_mgmt_data = .);
KEEP(*(SORT(.pwr_mgmt_data*)))
PROVIDE(__stop_pwr_mgmt_data = .);
} > FLASH
.log_const_data :
{
PROVIDE(__start_log_const_data = .);
KEEP(*(SORT(.log_const_data*)))
PROVIDE(__stop_log_const_data = .);
} > FLASH
.nrf_balloc :
{
PROVIDE(__start_nrf_balloc = .);
KEEP(*(.nrf_balloc))
PROVIDE(__stop_nrf_balloc = .);
} > FLASH
.nrf_queue :
{
PROVIDE(__start_nrf_queue = .);
KEEP(*(.nrf_queue))
PROVIDE(__stop_nrf_queue = .);
} > FLASH
.cli_command :
{
PROVIDE(__start_cli_command = .);
KEEP(*(.cli_command))
PROVIDE(__stop_cli_command = .);
} > FLASH
.sdh_stack_observers :
{
PROVIDE(__start_sdh_stack_observers = .);
KEEP(*(SORT(.sdh_stack_observers*)))
PROVIDE(__stop_sdh_stack_observers = .);
} > FLASH
.sdh_req_observers :
{
PROVIDE(__start_sdh_req_observers = .);
KEEP(*(SORT(.sdh_req_observers*)))
PROVIDE(__stop_sdh_req_observers = .);
} > FLASH
.sdh_state_observers :
{
PROVIDE(__start_sdh_state_observers = .);
KEEP(*(SORT(.sdh_state_observers*)))
PROVIDE(__stop_sdh_state_observers = .);
} > FLASH
.sdh_ant_observers :
{
PROVIDE(__start_sdh_ant_observers = .);
KEEP(*(SORT(.sdh_ant_observers*)))
PROVIDE(__stop_sdh_ant_observers = .);
} > FLASH
.sdh_ble_observers :
{
PROVIDE(__start_sdh_ble_observers = .);
KEEP(*(SORT(.sdh_ble_observers*)))
PROVIDE(__stop_sdh_ble_observers = .);
} > FLASH
/* TODO: make sure this doesnt cause duplication? */
. = ALIGN(16384);
.user_programs_code :
.user_programs_code ALIGN(16384) :
{
PROVIDE(__start_user_programs_code = .);
_build/user_*.o(.text*)
_build/user_*.o(.rodata*)
KEEP(*(.user_code))
PROVIDE(__end_user_programs_code = .);
ASSERT((__end_user_programs_code - __start_user_programs_code) <= 16384, "Error: .user_programs_code section exceeds 16 KB");
} > FLASH
Expand Down
2 changes: 1 addition & 1 deletion include/ipc/ipc_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace edge {
class IPCManager {
etl::unordered_map<etl::string<20>, uint8_t, MAX_PROCESSES> name_to_id;
etl::unordered_map<ProcessName, uint8_t, MAX_PROCESSES> name_to_id;
etl::array<ProcessCallbackPtr, MAX_PROCESSES> ipc_communicators{nullptr};

IPCManager() = default;
Expand Down
69 changes: 0 additions & 69 deletions include/scheduler.hpp

This file was deleted.

45 changes: 45 additions & 0 deletions include/scheduler/scheduler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once
#include "config.hpp"
#include "util.hpp"
#include "task.hpp"

#include <stdio.h>

#include <cstdint>

static constexpr size_t QUANTUM_MILLIS = 5;

namespace edge {
extern "C" {
void PendSV_Handler();
void SVC_Handler();
}

class Scheduler {
unsigned current_task_index = 0;
uint8_t slices_remaining = 1;

etl::vector<Task, MAX_PROCESSES> task_stack{};

public:
unsigned get_current_task() const { return current_task_index; }

void add_task(void (*function)(void), uint8_t priority = 1);

void start_scheduler();

void change_current_task_priority(uint8_t new_priority);

void yield_current_task();

private:
void update_mpu_with_stack() const;
void handle_first_svc_hit();

friend void PendSV_Handler(void);
friend void SVC_Handler(void);
};

extern Scheduler scheduler;

} // namespace edge
33 changes: 33 additions & 0 deletions include/scheduler/task.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

namespace edge {
static constexpr size_t STACK_SIZE_BYTES = 2048;

class Task {
static constexpr size_t STACK_SIZE_IN_UNSIGNED =
(STACK_SIZE_BYTES / sizeof(unsigned)) - sizeof(exception_stack_registers)
- sizeof(unsigned*) - sizeof(uint8_t);

public:
// ===== DO NOT REARRANGE THESE =====
// Align for MPU purposes
alignas(STACK_SIZE_BYTES) etl::array<unsigned, STACK_SIZE_IN_UNSIGNED> stack{};
exception_stack_registers first_stack_frame;
// ==================================

// needs to point to first_stack_frame
unsigned* stack_ptr_loc{&first_stack_frame.R0};

// This is useful if we want to adjust the ratio of driver to process runtime
uint8_t consecutive_quantums_to_run;

Task(
const exception_stack_registers& initial_stack_frame, uint8_t initial_priority
) :
first_stack_frame(initial_stack_frame),
consecutive_quantums_to_run(initial_priority)
{}
};

static_assert(sizeof(Task) <= STACK_SIZE_BYTES);
} // namespace edge
13 changes: 2 additions & 11 deletions include/userlib/syscalls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,14 @@ void get_button_pressed(
void (*callback)(drivers::ButtonType, drivers::ButtonState)
);

void send_ipc(const ProcessName& name, uint32_t message);
void send_ipc(const char* name, uint32_t message);

void subscribe_ipc(const ProcessName& name, void (*callback)(int message));
void subscribe_ipc(const char* name, void (*callback)(int message));

void set_fault_handler(void (*callback)(FaultType));

// We need a syscall for this because SVC will not be preempted by SysTick
// Technically this is insecure - it's mostly for debugging
void debug_print(const char* string);

// yeah i know
template <size_t Length>
inline void debug_println(const etl::string<Length>& str)
{
etl::string<Length + 1> str2 = str;
str2 += "\n";
debug_print(str2.data());
}

} // namespace edge::userlib
2 changes: 2 additions & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#define USER_CODE __attribute__((section(".user_code")))

namespace edge {
[[noreturn]] void panic(const char* reason);

Expand Down
3 changes: 2 additions & 1 deletion src/fault_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "nrf52.h"
#include "pending_process_callbacks.hpp"
#include "scheduler.hpp"
#include "scheduler/scheduler.hpp"

#include <stdio.h>

Expand Down Expand Up @@ -61,6 +61,7 @@ void FaultHandler::set_fault_callback(

void FaultHandler::fault_triggered(FaultType fault_type, uint32_t* stack_ptr)
{
printf("Fault triggered\n");
uint8_t current_task = scheduler.get_current_task();
if (!error_callbacks[current_task].has_value()) [[unlikely]] {
panic("Error handler not set");
Expand Down
13 changes: 8 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
#include "drivers/driver_enums.hpp"
#include "fault_handler.hpp"
#include "scheduler.hpp"
#include "scheduler/scheduler.hpp"
#include "util.hpp"

#include <stdbool.h>
#include <stdio.h>

extern void task0(void);
extern void ipc_part1(void);
extern void ipc_part2(void);
extern void exception_task(void);

int main(void)
{
printf("Starting EdgeOS\n");

edge::FaultHandler::get();

// edge::scheduler.add_task(exception_task);
edge::scheduler.add_task(task0);
// edge::scheduler.add_task(task1);
edge::scheduler.add_task(exception_task);

edge::scheduler.add_task(ipc_part1);
edge::scheduler.add_task(ipc_part2);

edge::scheduler.start_scheduler();
}
7 changes: 5 additions & 2 deletions src/raw_fault_handling.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "fault_handler.hpp"
#include "nrf52833.h"

#include <stdio.h>

namespace {
extern "C" {
Expand Down Expand Up @@ -32,13 +35,13 @@ void BusFault_Handler_CPP(uint32_t* stack_ptr)
FaultHandler::get().fault_triggered(edge::FaultType::Bus, stack_ptr);
}

void __attribute__((used, naked)) MemManage_Handler(void)
void __attribute__((used, naked)) MemoryManagement_Handler(void)
{
asm volatile("TST LR, #4 \n"
"ITE EQ \n"
"MRSEQ R0, MSP \n"
"MRSNE R0, PSP \n"
"B MemManage_Handler_C \n");
"B MemManage_Handler_CPP \n");
}

void MemManage_Handler_CPP(uint32_t* stack_ptr)
Expand Down
Loading

0 comments on commit 895dc65

Please sign in to comment.