From e9ac2611db4aa1e789b3b4df23960377b15215f7 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Wed, 10 Dec 2025 18:03:52 +0100 Subject: [PATCH 01/12] [c++] Add basic C++ apps The apps are: hello-world, simple class & use of std::vector Signed-off-by: Jean-Pierre Miceli --- usr/CMakeLists.txt | 2 +- usr/src/tests/CMakeLists.txt | 5 ++++ usr/src/tests/class_test.cpp | 48 +++++++++++++++++++++++++++++++++++ usr/src/tests/hello_world.cpp | 27 ++++++++++++++++++++ usr/src/tests/vector_test.cpp | 36 ++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 usr/src/tests/CMakeLists.txt create mode 100644 usr/src/tests/class_test.cpp create mode 100644 usr/src/tests/hello_world.cpp create mode 100644 usr/src/tests/vector_test.cpp diff --git a/usr/CMakeLists.txt b/usr/CMakeLists.txt index 387abe07c..f30cd284f 100644 --- a/usr/CMakeLists.txt +++ b/usr/CMakeLists.txt @@ -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) diff --git a/usr/src/tests/CMakeLists.txt b/usr/src/tests/CMakeLists.txt new file mode 100644 index 000000000..63e4efad7 --- /dev/null +++ b/usr/src/tests/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.16) + +add_executable(hello-world.elf hello_world.cpp) +add_executable(class-test.elf class_test.cpp) +add_executable(vector-test.elf vector_test.cpp) diff --git a/usr/src/tests/class_test.cpp b/usr/src/tests/class_test.cpp new file mode 100644 index 000000000..b01eb3864 --- /dev/null +++ b/usr/src/tests/class_test.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2025 Jean-Pierre Miceli + * + * 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 +#include + +class Person { +private: + std::string _name; + int _age; + +public: + // Constructor + Person(const std::string& name, int age) + : _name(name), _age(age) {} + + // Method to print info + void hello() const { + // std::cout << "Hi, I'm " << _name + // << " and I'm " << _age + // << " years old." << std::endl; + + printf("Hi, I'm %s and I'm %d years old.\n"); + } +}; + +int main() +{ + Person p("Jean-Pierre", 30); + p.hello(); + + return 0; +} diff --git a/usr/src/tests/hello_world.cpp b/usr/src/tests/hello_world.cpp new file mode 100644 index 000000000..b4a87e6c8 --- /dev/null +++ b/usr/src/tests/hello_world.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2025 Jean-Pierre Miceli + * + * 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 + +int main() +{ + // printf("JMI\n"); + std::cout << "Hello World!" << std::endl; + + return 0; +} \ No newline at end of file diff --git a/usr/src/tests/vector_test.cpp b/usr/src/tests/vector_test.cpp new file mode 100644 index 000000000..0f429b362 --- /dev/null +++ b/usr/src/tests/vector_test.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2025 Jean-Pierre Miceli + * + * 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 +#include + +int main() { + // Create and initialize a vector with 3 integer values + std::vector numbers = {10, 20, 30}; + + // Compute the sum + int sum = 0; + for (int value : numbers) { + sum += value; + } + + // Output the result + printf("Sum of vector entries: %d\n", sum); + + return 0; +} \ No newline at end of file From d257d657e92c0ed36ab0303c55f9e30b8d77be20 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Wed, 10 Dec 2025 18:05:23 +0100 Subject: [PATCH 02/12] [c++] udpate of CMakeList of usr/scr to compile test c++ apps Signed-off-by: Jean-Pierre Miceli --- usr/src/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/usr/src/CMakeLists.txt b/usr/src/CMakeLists.txt index 8da5d119b..35ebfcf6a 100644 --- a/usr/src/CMakeLists.txt +++ b/usr/src/CMakeLists.txt @@ -22,3 +22,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() + From 302e832eb3232b9d2c82003d843f854cd9a18df1 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Wed, 10 Dec 2025 18:06:33 +0100 Subject: [PATCH 03/12] [c++] Add section used by C++ apps to so3 Signed-off-by: Jean-Pierre Miceli --- so3/kernel/main.c | 2 +- so3/kernel/process.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/so3/kernel/main.c b/so3/kernel/main.c index 8858c2447..1bf429d99 100644 --- a/so3/kernel/main.c +++ b/so3/kernel/main.c @@ -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(); diff --git a/so3/kernel/process.c b/so3/kernel/process.c index d3bee955a..e7c5e21e4 100644 --- a/so3/kernel/process.c +++ b/so3/kernel/process.c @@ -74,10 +74,11 @@ static uint32_t pid_current = 1; static pcb_t *root_process = NULL; /* root process */ /* only the following sections are supported */ -#define SUPPORTED_SECTION_COUNT 8 -static const char *supported_section_names[SUPPORTED_SECTION_COUNT] = { +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. From 07289fa6a6b601cf7cb80ac8a6a7a48785da5da9 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Thu, 11 Dec 2025 15:47:27 +0100 Subject: [PATCH 04/12] [c++] Add 'no-rtti' flag in the musl toolchains Signed-off-by: Jean-Pierre Miceli --- usr/aarch64-linux-musl.cmake | 2 +- usr/arm-linux-musl.cmake | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/usr/aarch64-linux-musl.cmake b/usr/aarch64-linux-musl.cmake index a36f89037..f7c353062 100644 --- a/usr/aarch64-linux-musl.cmake +++ b/usr/aarch64-linux-musl.cmake @@ -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") diff --git a/usr/arm-linux-musl.cmake b/usr/arm-linux-musl.cmake index 4d38558df..f148c3542 100644 --- a/usr/arm-linux-musl.cmake +++ b/usr/arm-linux-musl.cmake @@ -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") From 28fe16dd154fbe19bb7f920371c20d02c4fff0bf Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Thu, 11 Dec 2025 15:48:23 +0100 Subject: [PATCH 05/12] [usr] Move the '.debug_*' sections in a separated file The '.debug_*' sections of the usr apps are move in a seperated file ('app.elf.debug'). The apps is then much lighter. For example, C++ hello-world shrink form 8.2MB to 884K. For debug, the app.elf.debug is linked with the app.elf. Signed-off-by: Jean-Pierre Miceli --- usr/build.sh | 30 ++++++++++++++++++++++++++++++ usr/deploy.sh | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/usr/build.sh b/usr/build.sh index 747697471..d596d04dc 100755 --- a/usr/build.sh +++ b/usr/build.sh @@ -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 @@ -120,4 +147,7 @@ install_directory_root out install_file_elf +strip_debug_info "$PLATFORM" + + exit 0 diff --git a/usr/deploy.sh b/usr/deploy.sh index 8ab92c9b5..c3c2bffd0 100755 --- a/usr/deploy.sh +++ b/usr/deploy.sh @@ -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} From d1b12b8483ff42726ab261bc748a5a6edd46bd39 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Tue, 16 Dec 2025 10:56:34 +0100 Subject: [PATCH 06/12] [c++] Add some more basic tests Signed-off-by: Jean-Pierre Miceli --- usr/src/tests/CMakeLists.txt | 4 ++- usr/src/tests/allocation_test.cpp | 34 +++++++++++++++++++ usr/src/tests/class_test.cpp | 15 ++++---- usr/src/tests/exception_test.cpp | 28 +++++++++++++++ usr/src/tests/hello_world.cpp | 1 - .../{vector_test.cpp => stdlib_test.cpp} | 33 ++++++++++++++++-- 6 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 usr/src/tests/allocation_test.cpp create mode 100644 usr/src/tests/exception_test.cpp rename usr/src/tests/{vector_test.cpp => stdlib_test.cpp} (72%) diff --git a/usr/src/tests/CMakeLists.txt b/usr/src/tests/CMakeLists.txt index 63e4efad7..95e4e692f 100644 --- a/usr/src/tests/CMakeLists.txt +++ b/usr/src/tests/CMakeLists.txt @@ -2,4 +2,6 @@ cmake_minimum_required(VERSION 3.16) add_executable(hello-world.elf hello_world.cpp) add_executable(class-test.elf class_test.cpp) -add_executable(vector-test.elf vector_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) diff --git a/usr/src/tests/allocation_test.cpp b/usr/src/tests/allocation_test.cpp new file mode 100644 index 000000000..9113b0c56 --- /dev/null +++ b/usr/src/tests/allocation_test.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2025 Jean-Pierre Miceli + * + * 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 +#include + +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; +} + diff --git a/usr/src/tests/class_test.cpp b/usr/src/tests/class_test.cpp index b01eb3864..af1d43ec9 100644 --- a/usr/src/tests/class_test.cpp +++ b/usr/src/tests/class_test.cpp @@ -25,17 +25,18 @@ class Person { int _age; public: - // Constructor Person(const std::string& name, int age) - : _name(name), _age(age) {} + : _name(name), _age(age) { + std::cout << "Person constructor" << std::endl; + } + + ~Person() { + std::cout << "Person Destructor" << std::endl; + } // Method to print info void hello() const { - // std::cout << "Hi, I'm " << _name - // << " and I'm " << _age - // << " years old." << std::endl; - - printf("Hi, I'm %s and I'm %d years old.\n"); + std::cout << "Hi, I'm " << _name << " and I'm " << _age << " years old." << std::endl; } }; diff --git a/usr/src/tests/exception_test.cpp b/usr/src/tests/exception_test.cpp new file mode 100644 index 000000000..8945b01c2 --- /dev/null +++ b/usr/src/tests/exception_test.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2025 Jean-Pierre Miceli + * + * 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 + +int main() +{ + try { + throw 42; + } catch (int x) { + std::cout << "caught: " << x << std::endl; + } +} diff --git a/usr/src/tests/hello_world.cpp b/usr/src/tests/hello_world.cpp index b4a87e6c8..c37c206be 100644 --- a/usr/src/tests/hello_world.cpp +++ b/usr/src/tests/hello_world.cpp @@ -20,7 +20,6 @@ int main() { - // printf("JMI\n"); std::cout << "Hello World!" << std::endl; return 0; diff --git a/usr/src/tests/vector_test.cpp b/usr/src/tests/stdlib_test.cpp similarity index 72% rename from usr/src/tests/vector_test.cpp rename to usr/src/tests/stdlib_test.cpp index 0f429b362..1926217ec 100644 --- a/usr/src/tests/vector_test.cpp +++ b/usr/src/tests/stdlib_test.cpp @@ -18,8 +18,10 @@ #include #include +#include -int main() { +void vector_test() +{ // Create and initialize a vector with 3 integer values std::vector numbers = {10, 20, 30}; @@ -30,7 +32,34 @@ int main() { } // Output the result - printf("Sum of vector entries: %d\n", sum); + std::cout << "Sum of vector entries: " << sum << std::endl; +} + + +void string_test() +{ + std::string s = "abc"; + s += "def"; + std::cout << "String: " << s << std::endl; +} + +void map_test() +{ + std::map m; + + m[1] = 2; + + std::cout << "Map m[1]: " << m[1] << std::endl; +} + + +int main() { + + vector_test(); + + string_test(); + + map_test(); return 0; } \ No newline at end of file From 691ef6e39a1396b37a5ba8eba0910d23c034142a Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Tue, 16 Dec 2025 11:13:09 +0100 Subject: [PATCH 07/12] [c++] Fix clang-format Signed-off-by: Jean-Pierre Miceli --- usr/src/tests/allocation_test.cpp | 15 ++++++----- usr/src/tests/class_test.cpp | 40 +++++++++++++++------------- usr/src/tests/exception_test.cpp | 6 ++--- usr/src/tests/hello_world.cpp | 4 +-- usr/src/tests/stdlib_test.cpp | 44 +++++++++++++++---------------- 5 files changed, 56 insertions(+), 53 deletions(-) diff --git a/usr/src/tests/allocation_test.cpp b/usr/src/tests/allocation_test.cpp index 9113b0c56..dca56f86a 100644 --- a/usr/src/tests/allocation_test.cpp +++ b/usr/src/tests/allocation_test.cpp @@ -19,16 +19,17 @@ #include #include -struct Aligned { alignas(64) int x; }; +struct Aligned { + alignas(64) int x; +}; int main() { - void* p = ::operator new(1024); - ::operator delete(p); + void *p = ::operator new(1024); + ::operator delete(p); - Aligned* a = new Aligned; - delete a; + Aligned *a = new Aligned; + delete a; - std::cout << "Test OK" << std::endl; + std::cout << "Test OK" << std::endl; } - diff --git a/usr/src/tests/class_test.cpp b/usr/src/tests/class_test.cpp index af1d43ec9..eef30af66 100644 --- a/usr/src/tests/class_test.cpp +++ b/usr/src/tests/class_test.cpp @@ -20,30 +20,34 @@ #include class Person { -private: - std::string _name; - int _age; + private: + std::string _name; + int _age; -public: - Person(const std::string& name, int age) - : _name(name), _age(age) { - std::cout << "Person constructor" << std::endl; - } + public: + Person(const std::string &name, int age) + : _name(name) + , _age(age) + { + std::cout << "Person constructor" << std::endl; + } - ~Person() { - std::cout << "Person Destructor" << std::endl; - } + ~Person() + { + std::cout << "Person Destructor" << std::endl; + } - // Method to print info - void hello() const { - std::cout << "Hi, I'm " << _name << " and I'm " << _age << " years old." << std::endl; - } + // Method to print info + void hello() const + { + std::cout << "Hi, I'm " << _name << " and I'm " << _age << " years old." << std::endl; + } }; int main() { - Person p("Jean-Pierre", 30); - p.hello(); + Person p("Jean-Pierre", 30); + p.hello(); - return 0; + return 0; } diff --git a/usr/src/tests/exception_test.cpp b/usr/src/tests/exception_test.cpp index 8945b01c2..6026dea25 100644 --- a/usr/src/tests/exception_test.cpp +++ b/usr/src/tests/exception_test.cpp @@ -22,7 +22,7 @@ int main() { try { throw 42; - } catch (int x) { - std::cout << "caught: " << x << std::endl; - } + } catch (int x) { + std::cout << "caught: " << x << std::endl; + } } diff --git a/usr/src/tests/hello_world.cpp b/usr/src/tests/hello_world.cpp index c37c206be..4ef192cd0 100644 --- a/usr/src/tests/hello_world.cpp +++ b/usr/src/tests/hello_world.cpp @@ -20,7 +20,7 @@ int main() { - std::cout << "Hello World!" << std::endl; + std::cout << "Hello World!" << std::endl; - return 0; + return 0; } \ No newline at end of file diff --git a/usr/src/tests/stdlib_test.cpp b/usr/src/tests/stdlib_test.cpp index 1926217ec..a450971be 100644 --- a/usr/src/tests/stdlib_test.cpp +++ b/usr/src/tests/stdlib_test.cpp @@ -22,44 +22,42 @@ void vector_test() { - // Create and initialize a vector with 3 integer values - std::vector numbers = {10, 20, 30}; + // Create and initialize a vector with 3 integer values + std::vector numbers = { 10, 20, 30 }; - // Compute the sum - int sum = 0; - for (int value : numbers) { - sum += value; - } + // Compute the sum + int sum = 0; + for (int value : numbers) { + sum += value; + } - // Output the result - std::cout << "Sum of vector entries: " << sum << std::endl; + // Output the result + std::cout << "Sum of vector entries: " << sum << std::endl; } - void string_test() { - std::string s = "abc"; - s += "def"; - std::cout << "String: " << s << std::endl; + std::string s = "abc"; + s += "def"; + std::cout << "String: " << s << std::endl; } void map_test() { - std::map m; + std::map m; - m[1] = 2; + m[1] = 2; - std::cout << "Map m[1]: " << m[1] << std::endl; + std::cout << "Map m[1]: " << m[1] << std::endl; } +int main() +{ + vector_test(); -int main() { - - vector_test(); - - string_test(); + string_test(); - map_test(); + map_test(); - return 0; + return 0; } \ No newline at end of file From 8075a8388310fa754ddc90cb1f9c2a40c28a7efc Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Tue, 16 Dec 2025 11:18:58 +0100 Subject: [PATCH 08/12] [c++] clang-format / again Signed-off-by: Jean-Pierre Miceli --- so3/kernel/process.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/so3/kernel/process.c b/so3/kernel/process.c index e7c5e21e4..ccc61a5f4 100644 --- a/so3/kernel/process.c +++ b/so3/kernel/process.c @@ -75,8 +75,9 @@ static pcb_t *root_process = NULL; /* root process */ /* only the following sections are supported */ 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", + ".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])) From 71de59c5f044588dd758abb06e1d6493b6fda807 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Mon, 5 Jan 2026 14:37:07 +0100 Subject: [PATCH 09/12] [C++] Add std::thread & std::ofstream (files) tests Signed-off-by: Jean-Pierre Miceli --- usr/src/tests/CMakeLists.txt | 4 ++ usr/src/tests/files_test.cpp | 17 +++++++ usr/src/tests/threads_test.cpp | 88 ++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 usr/src/tests/files_test.cpp create mode 100644 usr/src/tests/threads_test.cpp diff --git a/usr/src/tests/CMakeLists.txt b/usr/src/tests/CMakeLists.txt index 95e4e692f..7ae48e239 100644 --- a/usr/src/tests/CMakeLists.txt +++ b/usr/src/tests/CMakeLists.txt @@ -5,3 +5,7 @@ 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) +add_executable(threads-test.elf threads_test.cpp) +add_executable(files-test.elf files_test.cpp) + + diff --git a/usr/src/tests/files_test.cpp b/usr/src/tests/files_test.cpp new file mode 100644 index 000000000..f5316b2db --- /dev/null +++ b/usr/src/tests/files_test.cpp @@ -0,0 +1,17 @@ + + + +#include +#include + +int main() { + std::ofstream file("example.txt"); + if (!file) { + std::cerr << "Failed to open file\n"; + return 1; + } + + file << "Hello from C++\n"; + // file closes automatically (RAII) + return 0; +} \ No newline at end of file diff --git a/usr/src/tests/threads_test.cpp b/usr/src/tests/threads_test.cpp new file mode 100644 index 000000000..5503c1e0e --- /dev/null +++ b/usr/src/tests/threads_test.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2025 Jean-Pierre Miceli + * + * 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 +#include +#include +#include +#include + +#include + +struct ThreadArgs { + long thread_id; + std::mutex* mtx; +}; + +// Thread function +long simple_thread(ThreadArgs args) +{ + std::cout << "Thread " << args.thread_id << ": Executing" << std::endl; + + // Lock the mutex + args.mtx->lock(); + std::cout << "Thread " << args.thread_id << ": Locked mutex" << std::endl; + + std::this_thread::sleep_for(std::chrono::seconds(3)); + + std::cout << "Thread " << args.thread_id << ": Unlocking mutex" << std::endl; + args.mtx->unlock(); + + std::cout << "Thread " << args.thread_id << ": Thread finished" << std::endl; + + return args.thread_id; +} + +int main() +{ + std::mutex mtx; + + printf("== C++ threads tests ==\n"); + + // Create thread arguments + ThreadArgs args1{1, &mtx}; + ThreadArgs args2{2, &mtx}; + + // Store threads and their return values + std::vector threads; + long ret1, ret2; + + // Launch threads + std::thread t1([&]() { ret1 = simple_thread(args1); }); + std::thread t2([&]() { ret2 = simple_thread(args2); }); + + threads.push_back(std::move(t1)); + threads.push_back(std::move(t2)); + + // Wait for threads to finish + for (auto& t : threads) { + if (t.joinable()) { + t.join(); + } + } + + // Check return values + if (ret1 != 1) { + std::cout << "Unexpected return value for thread 1" << std::endl; + } + if (ret2 != 2) { + std::cout << "Unexpected return value for thread 2" << std::endl; + } + + std::cout << "All threads finished" << std::endl; + return 0; +} From 9152910a3ae95ff617d4fc0cc5ad69bade2e692e Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Mon, 5 Jan 2026 14:44:02 +0100 Subject: [PATCH 10/12] [C++] fix src format Signed-off-by: Jean-Pierre Miceli --- so3/arch/arm32/thread.c | 2 +- so3/arch/arm64/thread.c | 2 +- so3/kernel/process.c | 2 +- usr/src/tests/files_test.cpp | 22 ++++---- usr/src/tests/threads_test.cpp | 94 +++++++++++++++++----------------- 5 files changed, 61 insertions(+), 61 deletions(-) diff --git a/so3/arch/arm32/thread.c b/so3/arch/arm32/thread.c index ff26aa07f..8148e60dd 100644 --- a/so3/arch/arm32/thread.c +++ b/so3/arch/arm32/thread.c @@ -77,7 +77,7 @@ void arch_restart_user_thread(tcb_t *tcb, th_fn_t fn_entry, addr_t stack_top) /* Reset all user's registers to zero except for PC, PSTATE and SP * which needs to be set to the properly start the thread. */ - *user_regs = (cpu_regs_t) { + *user_regs = (cpu_regs_t){ .pc = (u32) fn_entry, .psr = PSR_USR_MODE, .sp_usr = stack_top, diff --git a/so3/arch/arm64/thread.c b/so3/arch/arm64/thread.c index 0daf82571..0bb8a7d42 100644 --- a/so3/arch/arm64/thread.c +++ b/so3/arch/arm64/thread.c @@ -76,7 +76,7 @@ void arch_restart_user_thread(tcb_t *tcb, th_fn_t fn_entry, addr_t stack_top) /* Reset all user's registers to zero except for PC, PSTATE and SP * which needs to be set to the properly start the program */ - *user_regs = (cpu_regs_t) { + *user_regs = (cpu_regs_t){ .pc = (u64) fn_entry, .pstate = PSR_MODE_EL0t, .sp_usr = stack_top, diff --git a/so3/kernel/process.c b/so3/kernel/process.c index ccc61a5f4..fe922f5cd 100644 --- a/so3/kernel/process.c +++ b/so3/kernel/process.c @@ -346,7 +346,7 @@ void create_root_process(void) (void *) __root_proc_end - (void *) __root_proc_start, false); /* Start main user thread. */ - thread_args = (clone_args_t) { + thread_args = (clone_args_t){ .name = "root_proc", .pcb = pcb, .stack = pcb->stack_top, diff --git a/usr/src/tests/files_test.cpp b/usr/src/tests/files_test.cpp index f5316b2db..731ed9686 100644 --- a/usr/src/tests/files_test.cpp +++ b/usr/src/tests/files_test.cpp @@ -1,17 +1,17 @@ - #include #include -int main() { - std::ofstream file("example.txt"); - if (!file) { - std::cerr << "Failed to open file\n"; - return 1; - } +int main() +{ + std::ofstream file("example.txt"); + if (!file) { + std::cerr << "Failed to open file\n"; + return 1; + } - file << "Hello from C++\n"; - // file closes automatically (RAII) - return 0; -} \ No newline at end of file + file << "Hello from C++\n"; + // file closes automatically + return 0; +} diff --git a/usr/src/tests/threads_test.cpp b/usr/src/tests/threads_test.cpp index 5503c1e0e..bb2d77a4f 100644 --- a/usr/src/tests/threads_test.cpp +++ b/usr/src/tests/threads_test.cpp @@ -24,65 +24,65 @@ #include struct ThreadArgs { - long thread_id; - std::mutex* mtx; + long thread_id; + std::mutex *mtx; }; // Thread function long simple_thread(ThreadArgs args) { - std::cout << "Thread " << args.thread_id << ": Executing" << std::endl; + std::cout << "Thread " << args.thread_id << ": Executing" << std::endl; - // Lock the mutex - args.mtx->lock(); - std::cout << "Thread " << args.thread_id << ": Locked mutex" << std::endl; + // Lock the mutex + args.mtx->lock(); + std::cout << "Thread " << args.thread_id << ": Locked mutex" << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(3)); + std::this_thread::sleep_for(std::chrono::seconds(3)); - std::cout << "Thread " << args.thread_id << ": Unlocking mutex" << std::endl; - args.mtx->unlock(); + std::cout << "Thread " << args.thread_id << ": Unlocking mutex" << std::endl; + args.mtx->unlock(); - std::cout << "Thread " << args.thread_id << ": Thread finished" << std::endl; + std::cout << "Thread " << args.thread_id << ": Thread finished" << std::endl; - return args.thread_id; + return args.thread_id; } int main() { - std::mutex mtx; - - printf("== C++ threads tests ==\n"); - - // Create thread arguments - ThreadArgs args1{1, &mtx}; - ThreadArgs args2{2, &mtx}; - - // Store threads and their return values - std::vector threads; - long ret1, ret2; - - // Launch threads - std::thread t1([&]() { ret1 = simple_thread(args1); }); - std::thread t2([&]() { ret2 = simple_thread(args2); }); - - threads.push_back(std::move(t1)); - threads.push_back(std::move(t2)); - - // Wait for threads to finish - for (auto& t : threads) { - if (t.joinable()) { - t.join(); - } - } - - // Check return values - if (ret1 != 1) { - std::cout << "Unexpected return value for thread 1" << std::endl; - } - if (ret2 != 2) { - std::cout << "Unexpected return value for thread 2" << std::endl; - } - - std::cout << "All threads finished" << std::endl; - return 0; + std::mutex mtx; + + printf("== C++ threads tests ==\n"); + + // Create thread arguments + ThreadArgs args1{ 1, &mtx }; + ThreadArgs args2{ 2, &mtx }; + + // Store threads and their return values + std::vector threads; + long ret1, ret2; + + // Launch threads + std::thread t1([&]() { ret1 = simple_thread(args1); }); + std::thread t2([&]() { ret2 = simple_thread(args2); }); + + threads.push_back(std::move(t1)); + threads.push_back(std::move(t2)); + + // Wait for threads to finish + for (auto &t : threads) { + if (t.joinable()) { + t.join(); + } + } + + // Check return values + if (ret1 != 1) { + std::cout << "Unexpected return value for thread 1" << std::endl; + } + if (ret2 != 2) { + std::cout << "Unexpected return value for thread 2" << std::endl; + } + + std::cout << "All threads finished" << std::endl; + return 0; } From 72e20fc180e6544f8c5c1b13b2af778340ab9b64 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Mon, 5 Jan 2026 14:49:31 +0100 Subject: [PATCH 11/12] Fix clang format Signed-off-by: Jean-Pierre Miceli --- so3/arch/arm32/thread.c | 2 +- so3/arch/arm64/thread.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/so3/arch/arm32/thread.c b/so3/arch/arm32/thread.c index 8148e60dd..ff26aa07f 100644 --- a/so3/arch/arm32/thread.c +++ b/so3/arch/arm32/thread.c @@ -77,7 +77,7 @@ void arch_restart_user_thread(tcb_t *tcb, th_fn_t fn_entry, addr_t stack_top) /* Reset all user's registers to zero except for PC, PSTATE and SP * which needs to be set to the properly start the thread. */ - *user_regs = (cpu_regs_t){ + *user_regs = (cpu_regs_t) { .pc = (u32) fn_entry, .psr = PSR_USR_MODE, .sp_usr = stack_top, diff --git a/so3/arch/arm64/thread.c b/so3/arch/arm64/thread.c index 0bb8a7d42..0daf82571 100644 --- a/so3/arch/arm64/thread.c +++ b/so3/arch/arm64/thread.c @@ -76,7 +76,7 @@ void arch_restart_user_thread(tcb_t *tcb, th_fn_t fn_entry, addr_t stack_top) /* Reset all user's registers to zero except for PC, PSTATE and SP * which needs to be set to the properly start the program */ - *user_regs = (cpu_regs_t){ + *user_regs = (cpu_regs_t) { .pc = (u64) fn_entry, .pstate = PSR_MODE_EL0t, .sp_usr = stack_top, From 9584310fe3dbc5b0388171c6f4f4617172c5ba32 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Mon, 5 Jan 2026 14:53:38 +0100 Subject: [PATCH 12/12] clang format again Signed-off-by: Jean-Pierre Miceli --- so3/kernel/process.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/so3/kernel/process.c b/so3/kernel/process.c index fe922f5cd..ccc61a5f4 100644 --- a/so3/kernel/process.c +++ b/so3/kernel/process.c @@ -346,7 +346,7 @@ void create_root_process(void) (void *) __root_proc_end - (void *) __root_proc_start, false); /* Start main user thread. */ - thread_args = (clone_args_t){ + thread_args = (clone_args_t) { .name = "root_proc", .pcb = pcb, .stack = pcb->stack_top,