Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion so3/arch/arm32/context.S
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
.global __exec_prologue_user

.globl __get_syscall_args_ext
.globl __get_syscall_arg

.global __mmu_switch_ttbr0
.global __exec
Expand Down
1 change: 0 additions & 1 deletion so3/arch/arm64/context.S
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
.global __exec_prologue_user

.globl __get_syscall_args_ext
.globl __get_syscall_arg

.global __mmu_switch
.global __exec
Expand Down
7 changes: 4 additions & 3 deletions so3/arch/arm64/exception.S
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,14 @@ __prepare_sig_handler:
ldr x1, [x0, #OFFSET_SYS_SIGNUM]
str x1, [sp, #OFFSET_X0]

// Set the handler to the PC
ldr x1, [x0, #OFFSET_SYS_SA]
ldr x2, [x1, #OFFSET_SA_HANDLER]
str x2, [sp, #OFFSET_X1]
str x2, [sp, #OFFSET_PC]

// Set the handler to the PC
// Set restorer to LR so the handler return to it.
ldr x2, [x1, #OFFSET_SA_RESTORER]
str x2, [sp, #OFFSET_PC]
str x2, [sp, #OFFSET_LR]

ret

Expand Down
2 changes: 1 addition & 1 deletion so3/arch/arm64/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void trap_handle(cpu_regs_t *regs)

default:
lprintk("### On CPU %d: ESR_Elx_EC(esr): 0x%lx\n", smp_processor_id(), ESR_ELx_EC(esr));
trap_handle_error(regs->lr);
trap_handle_error(regs->pc);
kernel_panic();
}
}
23 changes: 13 additions & 10 deletions so3/fs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ static int do_write(int fd, const void *buffer, size_t count)
}

/* Low Level mmap */
static int do_mmap(int fd, addr_t virt_addr, uint32_t page_count, off_t offset)
static long do_mmap(int fd, addr_t virt_addr, uint32_t page_count, off_t offset)
{
int gfd;
struct file_operations *fops;
Expand Down Expand Up @@ -499,7 +499,7 @@ static int do_mmap(int fd, addr_t virt_addr, uint32_t page_count, off_t offset)
}

/* Low Level mmap - Anonymous case */
static int do_mmap_anon(int fd, addr_t virt_addr, uint32_t page_count, off_t offset)
static long do_mmap_anon(int fd, addr_t virt_addr, uint32_t page_count, off_t offset)
{
uint32_t page;
pcb_t *pcb;
Expand All @@ -526,7 +526,7 @@ static int do_mmap_anon(int fd, addr_t virt_addr, uint32_t page_count, off_t off

memset((void *) virt_addr, 0, page_count * PAGE_SIZE);

/* WARNIMG - This is a simple/basic way to set the start virtual address:
/* WARNING - This is a simple/basic way to set the start virtual address:
It only increment the start address after each mmap call, no algorithm
to search for available spaces.
*/
Expand Down Expand Up @@ -1045,15 +1045,18 @@ SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned l
int total = 0;

for (i = 0; i < vlen; i++) {
ret = do_write(fd, (const void *) vec[i].iov_base, vec[i].iov_len);
if (ret < 0) {
break;
} else if ((ret >= 0) && (ret < vec[i].iov_len)) {
/* Do nothing for empty buffer */
if (vec[i].iov_len != 0) {
ret = do_write(fd, (const void *) vec[i].iov_base, vec[i].iov_len);
if (ret < 0) {
break;
} else if ((ret >= 0) && (ret < vec[i].iov_len)) {
total += ret;
break;
}

total += ret;
break;
}

total += ret;
}

if (total == 0)
Expand Down
2 changes: 1 addition & 1 deletion so3/include/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ tcb_t *user_thread(clone_args_t *args);
int thread_join(tcb_t *tcb);
void thread_exit(int exit_status);
void clean_thread(tcb_t *tcb);
SYSCALL_DECLARE(thread_yield, void);
SYSCALL_DECLARE(sched_yield, void);

void *thread_idle(void *dummy);

Expand Down
2 changes: 1 addition & 1 deletion so3/include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
#define DT_SOCK 12 /* Socket device */

/* mmap flags options */
#define MAP_ANONYMOUS 0x10 /* don't use a file */
#define MAP_ANONYMOUS 0x20 /* don't use a file */

/* Special value for dirfd used to indicate openat
should use the current working directory. */
Expand Down
2 changes: 1 addition & 1 deletion so3/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void kernel_start(void)
{
lprintk("%s", SO3_BANNER);

LOG_INFO("Now bootstraping the kernel ...");
LOG_INFO("Now bootstraping the kernel ...\n");

/* Memory manager subsystem initialization */
memory_init();
Expand Down
21 changes: 13 additions & 8 deletions so3/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ static uint32_t pid_current = 1;
static pcb_t *root_process = NULL; /* root process */

/* only the following sections are supported */
#define SUPPORTED_SECTION_COUNT 6
static const char *supported_section_names[SUPPORTED_SECTION_COUNT] = {
".text", ".rodata", ".data", ".sbss", ".bss", ".scommon",
static const char *supported_section_names[] = {
".init", ".text", ".rodata", ".data", ".sbss",
".bss", ".scommon", ".fini", ".eh_frame", ".gcc_except_table",
".init_array", ".fini_array", ".data.rel.ro", ".got", ".got.plt",
};
#define SUPPORTED_SECTION_COUNT (sizeof(supported_section_names) / sizeof(supported_section_names[0]))

/*
* Find a process (pcb_t) from its pid.
Expand Down Expand Up @@ -446,7 +448,7 @@ addr_t preserve_args_and_env(int argc, char **argv, char **envp)
/* Save env strings and count how many there are */
saved->envc = 0;
if (envp) {
do {
while (envp[saved->envc]) {
str_len = strlen(envp[saved->envc]) + 1;

/* Ensure the newly copied string will not exceed the buffer size. */
Expand All @@ -459,7 +461,9 @@ addr_t preserve_args_and_env(int argc, char **argv, char **envp)

strcpy(&saved->arg_env[saved->strings_size], envp[saved->envc]);
saved->strings_size += str_len;
} while (envp[saved->envc++]);

saved->envc++;
}
}

return (addr_t) saved;
Expand All @@ -477,11 +481,12 @@ void post_setup_image(args_env_t *args_env, elf_img_info_t *elf_img_info)
args_base = (char *) arch_get_args_base();

/* Save argc as first arguments */
*((int *) args_base) = args_env->argc;
*((long *) args_base) = args_env->argc;

/* Get the base address for the array of pointer for args, env and aux */
argv_p_base = (char **) (args_base + sizeof(int));
env_p_base = (char **) ((addr_t) argv_p_base + args_env->argc * sizeof(char *));
argv_p_base = (char **) (args_base + sizeof(long));
/* Add one to account for the null termination of env */
env_p_base = (char **) ((addr_t) argv_p_base + (args_env->argc + 1) * sizeof(char *));
/* Add one to account for the null termination of env */
aux_elf = (elf_addr_t *) ((addr_t) env_p_base + (args_env->envc + 1) * sizeof(char *));

Expand Down
5 changes: 2 additions & 3 deletions so3/kernel/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
#include <futex.h>
#include <syscall.h>

extern void __get_syscall_args_ext(uint32_t *syscall_no);
extern uint32_t __get_syscall_stack_arg(uint32_t nr);
extern void __get_syscall_args_ext(long *syscall_no);

extern void test_malloc(int test_no);

Expand All @@ -49,7 +48,7 @@ static const syscall_fn_t syscall_table[NR_SYSCALLS] = {

long syscall_handle(syscall_args_t *syscall_args)
{
uint32_t syscall_no;
long syscall_no;

/* Get addtional args of the syscall according to the ARM & SO3 ABI */
__get_syscall_args_ext(&syscall_no);
Expand Down
2 changes: 1 addition & 1 deletion so3/kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ void *thread_idle(void *dummy)
/*
* Yield to another thread, i.e. simply invoke a call to schedule()
*/
SYSCALL_DEFINE0(thread_yield)
SYSCALL_DEFINE0(sched_yield)
{
schedule();
return 0;
Expand Down
8 changes: 4 additions & 4 deletions so3/kernel/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ SYSCALL_DEFINE2(gettimeofday, struct timeval *, ts, void *, tz)

time = NOW();

ts->tv_sec = time / (time_t) 1000000000;
ts->tv_usec = time / (time_t) 1000;
ts->tv_sec = time / SECONDS(1);
ts->tv_usec = (time % SECONDS(1)) / MICROSECS(1);

return 0;
}
Expand Down Expand Up @@ -475,8 +475,8 @@ SYSCALL_DEFINE2(clock_gettime, int, clk_id, struct timespec *, ts)

time = NOW();

ts->tv_sec = time / (time_t) 1000000000;
ts->tv_nsec = time;
ts->tv_sec = time / SECONDS(1);
ts->tv_nsec = time % SECONDS(1);

return 0;
}
3 changes: 2 additions & 1 deletion so3/syscall.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ mmap
mmap2
nanosleep
futex
sched_yield
pipe IPC_PIPE
pipe2 IPC_PIPE
rt_sigaction IPC_SIGNAL
rt_kill IPC_SIGNAL
kill IPC_SIGNAL
sigreturn IPC_SIGNAL
rt_sigreturn IPC_SIGNAL
rt_sigprocmask IPC_SIGNAL
Expand Down
2 changes: 1 addition & 1 deletion usr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

cmake_minimum_required(VERSION 3.16)

project(so3-usr LANGUAGES C ASM)
project(so3-usr LANGUAGES C CXX ASM)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Expand Down
2 changes: 1 addition & 1 deletion usr/aarch64-linux-musl.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ set(CMAKE_ASM_FLAGS "-D__ARM64__ -D__ASSEMBLY__")

set(CMAKE_LINKER "aarch64-linux-musl-ld")

set(CMAKE_EXE_LINKER_FLAGS "-Os -static" CACHE STRING "SO3 usr LDFLAGS for executables")
set(CMAKE_EXE_LINKER_FLAGS "-Os -static -fno-rtti" CACHE STRING "SO3 usr LDFLAGS for executables")
3 changes: 1 addition & 2 deletions usr/arm-linux-musl.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ set(CMAKE_ASM_FLAGS "-D__ARM__ -D__ASSEMBLY__")

set(CMAKE_LINKER "arm-linux-musleabihf-ld")


set(CMAKE_EXE_LINKER_FLAGS "-Os -static" CACHE STRING "SO3 usr LDFLAGS for executables")
set(CMAKE_EXE_LINKER_FLAGS "-Os -static -fno-rtti" CACHE STRING "SO3 usr LDFLAGS for executables")
30 changes: 30 additions & 0 deletions usr/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ usage() {
echo " -h Print this help"
}

# Place the debug info of the applications in a separate file
strip_debug_info()
{
# retrieve the correct objcop tool
# if [ "$1" == "virt32" -o "$1" == "rpi4" ]; then
if [ "$1" = "virt32" ] || [ "$1" = "rpi4" ]; then
OBJCOPY="arm-linux-musleabihf-objcopy"
else
OBJCOPY="aarch64-linux-musl-objcopy"
fi

for app in build/deploy/*.elf; do
[ -e "$app" ] || continue

# 1. Create a file with only '.debug_*' sections
$OBJCOPY --only-keep-debug $app $app.debug

# 2. remove/strip debug info
$OBJCOPY --strip-all $app

# 3. Connect the two files
$OBJCOPY --add-gnu-debuglink=$app.debug $app

done
}


install_file_elf() {
if [ -f $1 ] ; then
for subfolder_app in $(find build/src -type f -iname "*.elf"); do
Expand Down Expand Up @@ -120,4 +147,7 @@ install_directory_root out

install_file_elf

strip_debug_info "$PLATFORM"


exit 0
2 changes: 1 addition & 1 deletion usr/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ echo Deploying user apps into the ramfs partition
cd ../rootfs
./mount.sh ${PLATFORM}
sudo cp -r ../usr/out/* fs
sudo cp -r ../usr/build/deploy/* fs
sudo cp -r ../usr/build/deploy/*.elf fs
./umount.sh ${PLATFORM}

5 changes: 5 additions & 0 deletions usr/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ if (MICROPYTHON AND (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64"))
add_subdirectory(micropython)
endif()

option(WITH_TESTS "Build the test/example apps" ON)
if (WITH_TESTS)
add_subdirectory(tests)
endif()

7 changes: 7 additions & 0 deletions usr/src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.16)

add_executable(hello-world.elf hello_world.cpp)
add_executable(class-test.elf class_test.cpp)
add_executable(stdlib-test.elf stdlib_test.cpp)
add_executable(exception-test.elf exception_test.cpp)
add_executable(allocation-test.elf allocation_test.cpp)
35 changes: 35 additions & 0 deletions usr/src/tests/allocation_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2025 Jean-Pierre Miceli <jean-pierre.miceli@heig-vd.ch>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#include <new>
#include <iostream>

struct Aligned {
alignas(64) int x;
};

int main()
{
void *p = ::operator new(1024);
::operator delete(p);

Aligned *a = new Aligned;
delete a;

std::cout << "Test OK" << std::endl;
}
Loading