From 26b890f42fbe149bdd69024b38d1d63e743a6cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Thu, 15 Nov 2018 00:00:00 +0000 Subject: [PATCH] Factor common userland and baremetal C functions This allows add.c to run unmodified on both! For that to work, use int main on baremetal, and pass the return value to the final exit. --- baremetal/add.c | 4 +- baremetal/arch/aarch64/add.S | 2 +- baremetal/arch/aarch64/fadd.S | 6 +-- baremetal/arch/arm/add.S | 2 +- baremetal/exit.c | 2 +- baremetal/interactive/assert_fail.c | 4 +- baremetal/interactive/exit1.c | 2 +- baremetal/interactive/hello.c | 3 +- baremetal/interactive/prompt.c | 3 +- baremetal/interactive/return1.c | 1 + baremetal/lib/aarch64.S | 1 - baremetal/lib/arm.S | 1 - baremetal/lib/common.h | 4 -- baremetal/lib/{common.c => syscalls.c} | 5 --- baremetal/return.c | 2 +- build-baremetal | 54 ++++++++++++++++-------- build-userland | 2 + common.c | 7 +++ common.h | 8 ++++ userland/Makefile | 17 +++++--- userland/add.c | 1 + userland/{common.h => common_userland.h} | 0 userland/mmap.c | 2 +- userland/openblas_hello.c | 2 +- userland/pagemap_dump.c | 2 +- userland/virt_to_phys_user.c | 2 +- 26 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 baremetal/interactive/return1.c delete mode 100644 baremetal/lib/common.h rename baremetal/lib/{common.c => syscalls.c} (97%) create mode 100644 common.c create mode 100644 common.h create mode 120000 userland/add.c rename userland/{common.h => common_userland.h} (100%) diff --git a/baremetal/add.c b/baremetal/add.c index 4342cdd..76567b4 100644 --- a/baremetal/add.c +++ b/baremetal/add.c @@ -1,6 +1,6 @@ #include -void main(void) { +int main(void) { int i, j, k; i = 1; /* test-gdb-op1 */ @@ -9,5 +9,5 @@ void main(void) { k = i + j; /* test-gdb-result */ if (k != 3) - assert_fail(); + common_assert_fail(); } diff --git a/baremetal/arch/aarch64/add.S b/baremetal/arch/aarch64/add.S index 2f3a156..6abad47 100644 --- a/baremetal/arch/aarch64/add.S +++ b/baremetal/arch/aarch64/add.S @@ -7,6 +7,6 @@ main: /* test-gdb-result */ cmp x1, #3 beq 1f - bl assert_fail + bl common_assert_fail 1: ret diff --git a/baremetal/arch/aarch64/fadd.S b/baremetal/arch/aarch64/fadd.S index 8afd1cb..e706a13 100644 --- a/baremetal/arch/aarch64/fadd.S +++ b/baremetal/arch/aarch64/fadd.S @@ -12,7 +12,7 @@ main: fmov d3, #4.0 fcmp d2, d3 beq 1f - bl assert_fail + bl common_assert_fail 1: /* Now in 32-bit. */ @@ -26,7 +26,7 @@ main: fmov s3, #4.0 fcmp s2, s3 beq 1f - bl assert_fail + bl common_assert_fail 1: /* Higher registers. */ @@ -40,6 +40,6 @@ main: /* test-gdb-d31 */ fcmp d30, d31 beq 1f - bl assert_fail + bl common_assert_fail 1: ret diff --git a/baremetal/arch/arm/add.S b/baremetal/arch/arm/add.S index 2be5a63..97e5c5b 100644 --- a/baremetal/arch/arm/add.S +++ b/baremetal/arch/arm/add.S @@ -7,6 +7,6 @@ main: /* test-gdb-result */ cmp r1, #3 beq 1f - bl assert_fail + bl common_assert_fail 1: bx lr diff --git a/baremetal/exit.c b/baremetal/exit.c index 07bdfad..de81ab6 100644 --- a/baremetal/exit.c +++ b/baremetal/exit.c @@ -1,7 +1,7 @@ #include #include -void main(void) { +int main(void) { exit(0); } diff --git a/baremetal/interactive/assert_fail.c b/baremetal/interactive/assert_fail.c index b2a70cc..d4ea62c 100644 --- a/baremetal/interactive/assert_fail.c +++ b/baremetal/interactive/assert_fail.c @@ -1,6 +1,6 @@ #include -void main(void) { - assert_fail(); +int main(void) { + common_assert_fail(); } diff --git a/baremetal/interactive/exit1.c b/baremetal/interactive/exit1.c index 35a9780..90f1d14 100644 --- a/baremetal/interactive/exit1.c +++ b/baremetal/interactive/exit1.c @@ -1,6 +1,6 @@ #include #include -void main(void) { +int main(void) { exit(1); } diff --git a/baremetal/interactive/hello.c b/baremetal/interactive/hello.c index 9a6d601..fe49acd 100644 --- a/baremetal/interactive/hello.c +++ b/baremetal/interactive/hello.c @@ -1,5 +1,6 @@ #include -void main(void) { +int main(void) { puts("hello"); + return 0; } diff --git a/baremetal/interactive/prompt.c b/baremetal/interactive/prompt.c index ecae2d3..49f4d8a 100644 --- a/baremetal/interactive/prompt.c +++ b/baremetal/interactive/prompt.c @@ -1,7 +1,7 @@ #include #include -void main(void) { +int main(void) { char c; char *ptr = NULL; size_t alloc_size = 1; @@ -19,4 +19,3 @@ void main(void) { } } } - diff --git a/baremetal/interactive/return1.c b/baremetal/interactive/return1.c new file mode 100644 index 0000000..98c444a --- /dev/null +++ b/baremetal/interactive/return1.c @@ -0,0 +1 @@ +int main(void) { return 1; } diff --git a/baremetal/lib/aarch64.S b/baremetal/lib/aarch64.S index 861a783..8834c86 100644 --- a/baremetal/lib/aarch64.S +++ b/baremetal/lib/aarch64.S @@ -8,5 +8,4 @@ mystart: ldr x0, =stack_top mov sp, x0 bl main - mov x0, #0 bl exit diff --git a/baremetal/lib/arm.S b/baremetal/lib/arm.S index ca08e8e..79fc1d0 100644 --- a/baremetal/lib/arm.S +++ b/baremetal/lib/arm.S @@ -2,5 +2,4 @@ mystart: ldr sp, =stack_top bl main - mov r0, #0 bl exit diff --git a/baremetal/lib/common.h b/baremetal/lib/common.h deleted file mode 100644 index 3e80696..0000000 --- a/baremetal/lib/common.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef COMMON_H -#define COMMON_H -void assert_fail(); -#endif diff --git a/baremetal/lib/common.c b/baremetal/lib/syscalls.c similarity index 97% rename from baremetal/lib/common.c rename to baremetal/lib/syscalls.c index dd5090e..d9fbd61 100644 --- a/baremetal/lib/common.c +++ b/baremetal/lib/syscalls.c @@ -86,8 +86,3 @@ void _exit(int status) { #endif #endif } - -void assert_fail() { - puts("lkmc_test_fail"); - exit(1); -} diff --git a/baremetal/return.c b/baremetal/return.c index 4177b6f..78f2de1 100644 --- a/baremetal/return.c +++ b/baremetal/return.c @@ -1 +1 @@ -void main(void) {} +int main(void) { return 0; } diff --git a/build-baremetal b/build-baremetal index edfca7c..b8e1294 100755 --- a/build-baremetal +++ b/build-baremetal @@ -9,9 +9,16 @@ class BaremetalComponent(common.Component): common.assert_crosstool_ng_supports_arch(args.arch) build_dir = self.get_build_dir(args) bootloader_obj = os.path.join(common.baremetal_build_lib_dir, 'bootloader{}'.format(common.obj_ext)) - common_obj = os.path.join(common.baremetal_build_lib_dir, 'common{}'.format(common.obj_ext)) + common_basename_noext = 'common' + common_src = os.path.join(common.root_dir, common_basename_noext + common.c_ext) + common_obj = os.path.join(common.baremetal_build_lib_dir, common_basename_noext + common.obj_ext) + syscalls_basename_noext = 'syscalls' + syscalls_src = os.path.join(common.baremetal_src_lib_dir, syscalls_basename_noext + common.c_ext) + syscalls_obj = os.path.join(common.baremetal_build_lib_dir, syscalls_basename_noext + common.obj_ext) + common_objs = [common_obj, syscalls_obj] cflags = [ '-I', common.baremetal_src_lib_dir, common.Newline, + '-I', common.root_dir, common.Newline, '-O0', common.Newline, '-ggdb3', common.Newline, '-mcpu={}'.format(common.mcpu), common.Newline, @@ -46,23 +53,27 @@ class BaremetalComponent(common.Component): os.path.join(common.baremetal_src_lib_dir, '{}{}'.format(args.arch, common.asm_ext)), common.Newline, ] ) - common.run_cmd( - [gcc, common.Newline] + - cflags + - [ - '-c', common.Newline, - '-D', 'UART0_ADDR={:#x}'.format(uart_address), common.Newline, - '-o', common_obj, common.Newline, - os.path.join(common.baremetal_src_lib_dir, 'common' + common.c_ext), common.Newline, - ] - ) + for src, obj in [ + (common_src, common_obj), + (syscalls_src, syscalls_obj), + ]: + common.run_cmd( + [gcc, common.Newline] + + cflags + + [ + '-c', common.Newline, + '-D', 'UART0_ADDR={:#x}'.format(uart_address), common.Newline, + '-o', obj, common.Newline, + src, common.Newline, + ] + ) self._build_dir( '', gcc=gcc, cflags=cflags, entry_address=entry_address, bootloader_obj=bootloader_obj, - common_obj=common_obj, + common_objs=common_objs, ) self._build_dir( 'interactive', @@ -70,7 +81,7 @@ class BaremetalComponent(common.Component): cflags=cflags, entry_address=entry_address, bootloader_obj=bootloader_obj, - common_obj=common_obj, + common_objs=common_objs, ) arch_dir = os.path.join('arch', args.arch) if os.path.isdir(os.path.join(common.baremetal_src_dir, arch_dir)): @@ -80,7 +91,7 @@ class BaremetalComponent(common.Component): cflags=cflags, entry_address=entry_address, bootloader_obj=bootloader_obj, - common_obj=common_obj, + common_objs=common_objs, ) arch_dir = os.path.join('arch', args.arch, 'no_bootloader') if os.path.isdir(os.path.join(common.baremetal_src_dir, arch_dir)): @@ -90,7 +101,7 @@ class BaremetalComponent(common.Component): cflags=cflags, entry_address=entry_address, bootloader_obj=bootloader_obj, - common_obj=common_obj, + common_objs=common_objs, bootloader=False, ) @@ -107,7 +118,16 @@ Build the baremetal examples with crosstool-NG. def get_default_args(self): return {'baremetal': 'all'} - def _build_dir(self, subpath, gcc, cflags, entry_address, bootloader_obj, common_obj, bootloader=True): + def _build_dir( + self, + subpath, + gcc, + cflags, + entry_address, + bootloader_obj, + common_objs, + bootloader=True + ): """ Build all .c and .S files in a given subpath of the baremetal source directory non recursively. @@ -144,8 +164,8 @@ Build the baremetal examples with crosstool-NG. '-T', os.path.join(common.baremetal_src_dir, 'link.ld'), common.Newline, ] + bootloader_cmd + + common.add_newlines(common_objs) + [ - common_obj, common.Newline, main_obj, common.Newline, ] ) diff --git a/build-userland b/build-userland index 95001ad..ca6c012 100755 --- a/build-userland +++ b/build-userland @@ -58,6 +58,8 @@ has the OpenBLAS libraries and headers installed. [ 'make', common.Newline, '-j', str(args.nproc), common.Newline, + 'CCFLAGS_SCRIPT={} {}'.format('-I', common.userland_src_dir), common.Newline, + 'COMMON_DIR={}'.format(common.root_dir), common.Newline, 'CC={}'.format(cc), common.Newline, 'CXX={}'.format(cxx), common.Newline, 'PKG_CONFIG={}'.format(common.buildroot_pkg_config), common.Newline, diff --git a/common.c b/common.c new file mode 100644 index 0000000..f51ccd0 --- /dev/null +++ b/common.c @@ -0,0 +1,7 @@ +#include +#include + +void common_assert_fail() { + puts("lkmc_test_fail"); + exit(1); +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..138fa4a --- /dev/null +++ b/common.h @@ -0,0 +1,8 @@ +#ifndef COMMON_H +#define COMMON_H + +/* Common baremetal and userland functionality. */ + +void common_assert_fail(); + +#endif diff --git a/userland/Makefile b/userland/Makefile index 41ab709..ed15216 100644 --- a/userland/Makefile +++ b/userland/Makefile @@ -1,13 +1,17 @@ .PHONY: all clean mkdir +CCFLAGS = -ggdb3 -I$(COMMON_DIR) -O0 -Wall -Werror -Wextra -Wno-unused-function $(CCFLAGS_EXTRA) $(CCFLAGS_SCRIPT) CFLAGS = -fopenmp -std=c99 $(CCFLAGS) $(CFLAGS_EXTRA) CXXFLAGS = -std=c++17 $(CCFLAGS) $(CXXFLAGS_EXTRA) # -Wno-unused-function for function definitions on headers, # because we are lazy to make a shared object. TODO. -CCFLAGS = -ggdb3 -O0 -Wall -Werror -Wextra -Wno-unused-function $(CCFLAGS_EXTRA) +COMMON_DIR = .. +COMMON_BASENAME = common +COMMON_OBJ = $(OUT_DIR)/$(COMMON_BASENAME)$(OBJ_EXT) IN_EXT_C = .c IN_EXT_CXX = .cpp LIBS = -lm +OBJ_EXT = .o OUT_EXT = .out OUT_DIR = . @@ -38,14 +42,17 @@ OUTS := $(addprefix $(OUT_DIR)/,$(OUTS)) all: mkdir $(OUTS) -$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_C) - $(CC) $(CFLAGS) -o '$@' '$<' $(LIBS) +$(COMMON_OBJ): $(COMMON_DIR)/$(COMMON_BASENAME)$(IN_EXT_C) + $(CC) $(CFLAGS) -c -o '$@' '$<' $(LIBS) -$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_CXX) +$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_C) $(COMMON_OBJ) + $(CC) $(CFLAGS) $(COMMON_OBJ) -o '$@' '$<' $(LIBS) + +$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_CXX) $(COMMON_OBJ) $(CXX) $(CXXFLAGS) -o '$@' '$<' $(LIBS) clean: - rm -f *'$(OUT_EXT)' + rm -f *'$(OBJ_EXT)' *'$(OUT_EXT)' mkdir: mkdir -p '$(OUT_DIR)' diff --git a/userland/add.c b/userland/add.c new file mode 120000 index 0000000..b0aea66 --- /dev/null +++ b/userland/add.c @@ -0,0 +1 @@ +../baremetal/add.c \ No newline at end of file diff --git a/userland/common.h b/userland/common_userland.h similarity index 100% rename from userland/common.h rename to userland/common_userland.h diff --git a/userland/mmap.c b/userland/mmap.c index 106a6b9..321c32f 100644 --- a/userland/mmap.c +++ b/userland/mmap.c @@ -10,7 +10,7 @@ #include #include /* sysconf */ -#include "common.h" /* virt_to_phys_user */ +#include "common_userland.h" /* virt_to_phys_user */ enum { BUFFER_SIZE = 4 }; diff --git a/userland/openblas_hello.c b/userland/openblas_hello.c index be3ef2b..c6e00e6 100644 --- a/userland/openblas_hello.c +++ b/userland/openblas_hello.c @@ -1,7 +1,7 @@ /* https://github.com/cirosantilli/linux-kernel-module-cheat#blas * Adapted from: https://github.com/xianyi/OpenBLAS/wiki/User-Manual/59b62f98e7400270fb03ad1d85fba5b64ebbff2b#call-cblas-interface */ -#include "common.h" +#include "common_userland.h" #include #include diff --git a/userland/pagemap_dump.c b/userland/pagemap_dump.c index f4ca4f9..f3553a4 100644 --- a/userland/pagemap_dump.c +++ b/userland/pagemap_dump.c @@ -9,7 +9,7 @@ #include #include -#include "common.h" /* pagemap_get_entry */ +#include "common_userland.h" /* pagemap_get_entry */ int main(int argc, char **argv) { diff --git a/userland/virt_to_phys_user.c b/userland/virt_to_phys_user.c index 4d96966..72cf1fd 100644 --- a/userland/virt_to_phys_user.c +++ b/userland/virt_to_phys_user.c @@ -4,7 +4,7 @@ #include /* printf */ #include /* EXIT_SUCCESS, EXIT_FAILURE, strtoull */ -#include "common.h" /* virt_to_phys_user */ +#include "common_userland.h" /* virt_to_phys_user */ int main(int argc, char **argv) {