From 6ee08d8b47583036c55bfbeaff526456a66a8ad3 Mon Sep 17 00:00:00 2001 From: June Tate-Gans Date: Tue, 30 Aug 2022 17:33:53 +0000 Subject: [PATCH] Merge "apps: Extract crt0 from C apps" GitOrigin-RevId: af8b6e41c39a9d5d0b85cb5f7b66d986e1bc3cf9 --- apps/c/fibonacci/Makefile | 24 ++--------- apps/c/fibonacci/fibonacci.c | 65 ++--------------------------- apps/c/hello/Makefile | 24 ++--------- apps/c/hello/hello.c | 58 +++---------------------- apps/c/libkata/Makefile | 61 +++++++++++++++++++++++++++ apps/c/libkata/arch/riscv32/arch.mk | 15 +++++++ apps/c/libkata/arch/riscv32/crt0.S | 50 ++++++++++++++++++++++ apps/c/libkata/globals.c | 19 +++++++++ apps/c/libkata/include/kata.h | 38 +++++++++++++++++ apps/c/libkata/make/app.mk | 40 ++++++++++++++++++ apps/c/libkata/make/arch/riscv32.mk | 20 +++++++++ apps/c/libkata/make/common.mk | 45 ++++++++++++++++++++ apps/c/libkata/make/libkata.mk | 17 ++++++++ apps/c/libkata/make/sel4.mk | 26 ++++++++++++ apps/c/libkata/printf.c | 53 +++++++++++++++++++++++ apps/c/suicide/Makefile | 24 ++--------- apps/c/suicide/suicide.c | 29 ++----------- 17 files changed, 408 insertions(+), 200 deletions(-) create mode 100644 apps/c/libkata/Makefile create mode 100644 apps/c/libkata/arch/riscv32/arch.mk create mode 100644 apps/c/libkata/arch/riscv32/crt0.S create mode 100644 apps/c/libkata/globals.c create mode 100644 apps/c/libkata/include/kata.h create mode 100644 apps/c/libkata/make/app.mk create mode 100644 apps/c/libkata/make/arch/riscv32.mk create mode 100644 apps/c/libkata/make/common.mk create mode 100644 apps/c/libkata/make/libkata.mk create mode 100644 apps/c/libkata/make/sel4.mk create mode 100644 apps/c/libkata/printf.c diff --git a/apps/c/fibonacci/Makefile b/apps/c/fibonacci/Makefile index 30272e8..39e67da 100644 --- a/apps/c/fibonacci/Makefile +++ b/apps/c/fibonacci/Makefile @@ -12,24 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -SRC_LIBSEL4 ?= $(ROOTDIR)/kata/kernel/libsel4 -OUT_KATA ?= $(OUT)/kata/riscv32-unknown-elf/release -OUT_TMP ?= $(OUT)/tmp/fibonacci +APPNAME := fibonacci +SOURCES := fibonacci.c -INCLUDES += -I$(SRC_LIBSEL4)/arch_include/riscv -INCLUDES += -I$(SRC_LIBSEL4)/include -INCLUDES += -I$(SRC_LIBSEL4)/mode_include/32 -INCLUDES += -I$(SRC_LIBSEL4)/sel4_arch_include/riscv32/ -INCLUDES += -I$(OUT_KATA)/kernel/gen_config -INCLUDES += -I$(OUT_KATA)/libsel4/autoconf -INCLUDES += -I$(OUT_KATA)/libsel4/gen_config/ -INCLUDES += -I$(OUT_KATA)/libsel4/include -INCLUDES += -I$(OUT_KATA)/libsel4/sel4_arch_include/riscv32 - -OPT:=-O0 -DBG:=-g - -$(OUT_TMP)/fibonacci.elf: fibonacci.c - mkdir -p $(OUT_TMP) - riscv32-unknown-elf-gcc $(DBG) $(OPT) $(INCLUDES) -march=rv32imac -mabi=ilp32 -std=gnu11 -c fibonacci.c -o $(OUT_TMP)/fibonacci.o - riscv32-unknown-elf-gcc $(DBG) -static -nostdlib $(OUT_TMP)/fibonacci.o -o $(OUT_TMP)/fibonacci.elf -lgcc +LIBKATA ?= ../libkata +include $(LIBKATA)/make/app.mk diff --git a/apps/c/fibonacci/fibonacci.c b/apps/c/fibonacci/fibonacci.c index e1944b4..5d686eb 100644 --- a/apps/c/fibonacci/fibonacci.c +++ b/apps/c/fibonacci/fibonacci.c @@ -10,30 +10,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include -#include -#include +#include #include -#include - -__thread seL4_IPCBuffer *__sel4_ipc_buffer; - -char minisel_tls[4096] __attribute__((__aligned__(4096))); - -__attribute__((naked)) void _start() { - asm volatile( - ".option push \n" - ".option norelax \n" - "la gp, __global_pointer$ \n" - "la x4, minisel_tls \n" - "addi sp,sp,-16 \n" - "sw a0, 12(sp) \n" - "sw a1, 8(sp) \n" - "sw a2, 4(sp) \n" - "sw a3, 0(sp) \n" - ".option pop \n" - "j main \n"); -} // How many Fibonacci numbers to write to the log. #define LOG_FIBONACCI_LIMIT 80 @@ -42,41 +20,6 @@ __attribute__((naked)) void _start() { #define INTERRUPTS_PER_VIRT_SEC (1000 / CONFIG_TIMER_TICK_MS) #define INTERRUPTS_PER_WAIT (1 * INTERRUPTS_PER_VIRT_SEC) -// only prints 32-bit "%x" hex values -void minisel_printf(const char *fmt, ...) { -#if CONFIG_PRINTING - va_list args; - va_start(args, fmt); - for (; *fmt; fmt++) { - if (*fmt == '%') { - fmt++; - if (*fmt == 'd') { - uint32_t arg = va_arg(args, uint32_t); - // TODO(sleffler): total hack - int printing = 0; - for (int d = 1000000000; d > 1; d /= 10) { - int n = (arg / d) % 10; - if (printing || n > 0) { - seL4_DebugPutChar('0' + n); - printing = 1; - } - } - seL4_DebugPutChar('0' + (arg % 10)); - } else if (*fmt == 'x') { - uint32_t arg = va_arg(args, uint32_t); - for (int i = 7; i >= 0; i--) { - int n = (arg >> (4 * i)) & 0xF; - seL4_DebugPutChar(n > 9 ? 'A' + n - 10 : '0' + n); - } - } - } else { - seL4_DebugPutChar(*fmt); - } - } - va_end(args); -#endif -} - typedef uint64_t interrupt_count_t; typedef struct { @@ -134,9 +77,9 @@ void fibonacci_log(int pid, const fibonacci_state_t *fibonacci_state, "%llu; rdtime == %llu; virt_sec ~= %.2f\n", fibonacci_state->n, fibonacci_state->f1, interrupt_count, rdtime(), virtual_seconds(interrupt_count)); - minisel_printf(log_buf); + debug_printf(log_buf); #else - minisel_printf( + debug_printf( "[%d]: " "n == %d; " "f == %x; " @@ -153,7 +96,7 @@ int main(int pid, int a1, int a2, int a3) { interrupt_count_t interrupt_count = 0; fibonacci_state_t fibonacci_state; fibonacci_init(&fibonacci_state); - minisel_printf("\nFibonacci: pid %d\n", pid); + debug_printf("\nFibonacci: pid %d\n", pid); while (1) { wait(INTERRUPTS_PER_WAIT, &interrupt_count); if (fibonacci_state.n >= LOG_FIBONACCI_LIMIT) { diff --git a/apps/c/hello/Makefile b/apps/c/hello/Makefile index 534444c..3bdf849 100644 --- a/apps/c/hello/Makefile +++ b/apps/c/hello/Makefile @@ -12,24 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -SRC_LIBSEL4 ?= $(ROOTDIR)/kata/kernel/libsel4 -OUT_KATA ?= $(OUT)/kata/riscv32-unknown-elf/release -OUT_TMP ?= $(OUT)/tmp/hello +APPNAME := hello +SOURCES := hello.c -INCLUDES += -I$(SRC_LIBSEL4)/arch_include/riscv -INCLUDES += -I$(SRC_LIBSEL4)/include -INCLUDES += -I$(SRC_LIBSEL4)/mode_include/32 -INCLUDES += -I$(SRC_LIBSEL4)/sel4_arch_include/riscv32/ -INCLUDES += -I$(OUT_KATA)/kernel/gen_config -INCLUDES += -I$(OUT_KATA)/libsel4/autoconf -INCLUDES += -I$(OUT_KATA)/libsel4/gen_config/ -INCLUDES += -I$(OUT_KATA)/libsel4/include -INCLUDES += -I$(OUT_KATA)/libsel4/sel4_arch_include/riscv32 - -OPT:=-O0 -DBG:=-g - -$(OUT_TMP)/hello.elf: hello.c - mkdir -p $(OUT_TMP) - riscv32-unknown-elf-gcc $(DBG) $(OPT) $(INCLUDES) -march=rv32imac -mabi=ilp32 -std=gnu11 -c hello.c -o $(OUT_TMP)/hello.o - riscv32-unknown-elf-gcc $(DBG) -static -nostdlib $(OUT_TMP)/hello.o -o $(OUT_TMP)/hello.elf +LIBKATA ?= ../libkata +include $(LIBKATA)/make/app.mk diff --git a/apps/c/hello/hello.c b/apps/c/hello/hello.c index c54d8cf..e23c0c6 100644 --- a/apps/c/hello/hello.c +++ b/apps/c/hello/hello.c @@ -9,62 +9,14 @@ // using the seL4_DebugPutChar syscall and is intended as a starting // point for low-level tests. -#include -#include -#include -#include - -__thread seL4_IPCBuffer *__sel4_ipc_buffer; - -char minisel_tls[4096] __attribute__((__aligned__(4096))); - -__attribute__((naked)) void _start() { - asm volatile( - ".option push \n" - ".option norelax \n" - "la gp, __global_pointer$ \n" - "la tp, minisel_tls \n" - "lui t1,0 \n" - "add t1,t1,tp \n" - "sw a0,0(t1) # __sel4_ipc_buffer>\n" - "addi sp,sp,-16 \n" - "sw a0, 12(sp) \n" - "sw a1, 8(sp) \n" - "sw a2, 4(sp) \n" - "sw a3, 0(sp) \n" - ".option pop \n" - "j main \n"); -} - -// only prints 32-bit "%x" hex values -void minisel_printf(const char *fmt, ...) { -#if CONFIG_PRINTING - va_list args; - va_start(args, fmt); - for (; *fmt; fmt++) { - if (*fmt == '%') { - fmt++; - if (*fmt == 'x') { - uint32_t arg = va_arg(args, uint32_t); - for (int i = 7; i >= 0; i--) { - int n = (arg >> (4 * i)) & 0xF; - seL4_DebugPutChar(n > 9 ? 'A' + n - 10 : '0' + n); - } - } - } else { - seL4_DebugPutChar(*fmt); - } - } - va_end(args); -#endif -} +#include int main(int a0, int a1, int a2, int a3) { - minisel_printf("\nI am a C app!\n"); - minisel_printf("a0 %x a1 %x a2 %x a3 %x\n", a0, a1, a2, a3); - minisel_printf("__sel4_ipc_buffer %x\n", __sel4_ipc_buffer); + debug_printf("\nI am a C app!\n"); + debug_printf("a0 %x a1 %x a2 %x a3 %x\n", a0, a1, a2, a3); + debug_printf("__sel4_ipc_buffer %x\n", __sel4_ipc_buffer); - minisel_printf("Done, sleeping in WFI loop\n"); + debug_printf("Done, sleeping in WFI loop\n"); while (1) { asm("wfi"); } diff --git a/apps/c/libkata/Makefile b/apps/c/libkata/Makefile new file mode 100644 index 0000000..8d3122a --- /dev/null +++ b/apps/c/libkata/Makefile @@ -0,0 +1,61 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include make/common.mk +include arch/$(BUILD_ARCH)/arch.mk + +INCLUDES += -Iinclude + +SRC_FILES += \ + printf.c \ + globals.c + +INCLUDE_FILES := \ + include/kata.h + +BUILD_DIR := $(BUILD_ROOT)/libkata +BUILD_ARCH_DIR := $(BUILD_DIR)/arch/$(BUILD_ARCH) + +OBJ_FILES := $(patsubst arch/$(BUILD_ARCH)/%.S,$(BUILD_ARCH_DIR)/%.o,$(SRC_FILES)) +OBJ_FILES := $(patsubst %.c,$(BUILD_DIR)/%.o,$(OBJ_FILES)) + +## Build Rules ########################################### + +$(BUILD_DIR)/libkata.a: $(BUILD_DIR) $(OBJ_FILES) includes + $(AR) rcs $@ $(OBJ_FILES) + +includes: $(INCLUDE_FILES) + mkdir -p $(BUILD_DIR)/include + cp $(INCLUDE_FILES) $(BUILD_DIR)/include + +$(BUILD_DIR)/%.o: %.c + $(CC) $(CFLAGS) -Iinclude -c -o $@ $< + +$(BUILD_DIR)/%.o: %.S + $(AS) $(ASFLAGS) -o $@ $< + +$(BUILD_ARCH_DIR)/%.o: %.c + $(CC) $(CFLAGS) -Iinclude -c -o $@ $< + +$(BUILD_ARCH_DIR)/%.o: %.S + $(AS) $(ASFLAGS) -o $@ $< + +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + mkdir -p $(BUILD_ARCH_DIR) + +clean: + rm -rf $(BUILD_DIR) + +.PHONY: clean includes diff --git a/apps/c/libkata/arch/riscv32/arch.mk b/apps/c/libkata/arch/riscv32/arch.mk new file mode 100644 index 0000000..c0d6c9b --- /dev/null +++ b/apps/c/libkata/arch/riscv32/arch.mk @@ -0,0 +1,15 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +SRC_FILES += arch/riscv32/crt0.S diff --git a/apps/c/libkata/arch/riscv32/crt0.S b/apps/c/libkata/arch/riscv32/crt0.S new file mode 100644 index 0000000..9ae9dca --- /dev/null +++ b/apps/c/libkata/arch/riscv32/crt0.S @@ -0,0 +1,50 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + .section .text._start + .align 2 + .globl _start + .type _start, @function +_start: + .option push + .option norelax + + la gp, __global_pointer$ + la x4, _tls + + /* Setup __sel4_ipc_buffer */ + lui t1, 0 + add t1, t1, tp + sw a0, 0(t1) + + addi sp, sp, -16 + sw a0, 12(sp) + sw a1, 8(sp) + sw a2, 4(sp) + sw a3, 0(sp) + + .option pop + + j main + + .bss + + .section .bss + .align 12 + .globl _tls + .type _tls, tls_object +_tls: + .ds 4096 diff --git a/apps/c/libkata/globals.c b/apps/c/libkata/globals.c new file mode 100644 index 0000000..890f628 --- /dev/null +++ b/apps/c/libkata/globals.c @@ -0,0 +1,19 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +__thread seL4_IPCBuffer *__sel4_ipc_buffer; diff --git a/apps/c/libkata/include/kata.h b/apps/c/libkata/include/kata.h new file mode 100644 index 0000000..db1da22 --- /dev/null +++ b/apps/c/libkata/include/kata.h @@ -0,0 +1,38 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// NOLINT(build/header_guard) +#ifndef KATA_H +#define KATA_H + +#include +#include +#include + +extern __thread seL4_IPCBuffer *__sel4_ipc_buffer; + +#ifdef CONFIG_PRINTING +extern void _debug_printf(const char *fmt, ...); +#define debug_printf(args...) \ + do { \ + _debug_printf(args); \ + } while (0) +#else +#define debug_printf(args...) \ + do { \ + } while (0) +#warning Apps will not log to console because CONFIG_PRINTING is not defined! +#endif // CONFIG_PRINTING + +#endif // KATA_H diff --git a/apps/c/libkata/make/app.mk b/apps/c/libkata/make/app.mk new file mode 100644 index 0000000..23df78a --- /dev/null +++ b/apps/c/libkata/make/app.mk @@ -0,0 +1,40 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +MYDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) + +include $(MYDIR)/common.mk +include $(MYDIR)/libkata.mk + +BUILD_DIR := $(BUILD_ROOT)/$(APPNAME) +INTERMEDIATES := $(patsubst %.c,$(BUILD_DIR)/build/%.o,$(SOURCES)) + +$(BUILD_DIR)/$(APPNAME).elf: $(INTERMEDIATES) $(BUILD_ROOT)/libkata/libkata.a | $(BUILD_DIR) + $(LD) $(LDFLAGS) -o $(BUILD_DIR)/$(APPNAME).elf $(INTERMEDIATES) $(LIBKATA_LIBS) -lgcc + +$(BUILD_DIR)/build/%.o: %.c $(BUILD_ROOT)/libkata/libkata.a | $(BUILD_DIR) + $(CC) $(CFLAGS) $(LIBKATA_INCLUDES) -c -o $@ $< + +$(BUILD_DIR): + mkdir -p $(BUILD_DIR)/build + +clean: + rm -rf $(BUILD_DIR) + +.PHONY: clean + +## libkata build linkage + +$(BUILD_ROOT)/libkata/libkata.a: + make -C $(MYDIR)/.. diff --git a/apps/c/libkata/make/arch/riscv32.mk b/apps/c/libkata/make/arch/riscv32.mk new file mode 100644 index 0000000..d7f4e43 --- /dev/null +++ b/apps/c/libkata/make/arch/riscv32.mk @@ -0,0 +1,20 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +BASE_ARCH_NAME := riscv +ARCH_BITS := 32 +FULL_ARCH_NAME := riscv32 +ARCH_PREFIX := riscv32-unknown-elf +ARCH := rv32imac +ABI := ilp32 diff --git a/apps/c/libkata/make/common.mk b/apps/c/libkata/make/common.mk new file mode 100644 index 0000000..34409d9 --- /dev/null +++ b/apps/c/libkata/make/common.mk @@ -0,0 +1,45 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +MYDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) + +BUILD_TYPE ?= debug +BUILD_ARCH ?= riscv32 + +include $(MYDIR)/arch/$(BUILD_ARCH).mk +include $(MYDIR)/sel4.mk + +ifeq ($(BUILD_TYPE),debug) + DEBUG := -g + OPT := -O0 +else + DEBUG := + OPT := -O0 # TODO(jtgans): Actually optimize in a release build +endif + +ROOTDIR ?= $(MYDIR) +BUILD_ROOT ?= $(ROOTDIR)/out/kata/$(ARCH_PREFIX)/$(BUILD_TYPE)/apps + +CC := $(ARCH_PREFIX)-gcc +AS := $(ARCH_PREFIX)-as +AR := $(ARCH_PREFIX)-ar +LD := $(ARCH_PREFIX)-gcc + +CFLAGS := $(DEBUG) $(OPT) $(INCLUDES) +CFLAGS += -march=$(ARCH) -mabi=$(ABI) +CFLAGS += -std=gnu11 -nostdlib +CFLAGS += -ftls-model=local-exec + +ASFLAGS := -march=$(ARCH) -mabi=$(ABI) +LDFLAGS := $(DEBUG) -nostartfiles -static -nostdlib diff --git a/apps/c/libkata/make/libkata.mk b/apps/c/libkata/make/libkata.mk new file mode 100644 index 0000000..68d8229 --- /dev/null +++ b/apps/c/libkata/make/libkata.mk @@ -0,0 +1,17 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LIBKATA_DIR := $(BUILD_ROOT)/libkata +LIBKATA_INCLUDES := -I$(LIBKATA_DIR)/include +LIBKATA_LIBS := -L$(LIBKATA_DIR) -lkata diff --git a/apps/c/libkata/make/sel4.mk b/apps/c/libkata/make/sel4.mk new file mode 100644 index 0000000..a2f78fc --- /dev/null +++ b/apps/c/libkata/make/sel4.mk @@ -0,0 +1,26 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LIBSEL4_SRC ?= $(ROOTDIR)/kata/kernel/libsel4 +OUT_KATA ?= $(OUT)/kata/$(ARCH_PREFIX)/$(BUILD_TYPE) + +INCLUDES += -I$(LIBSEL4_SRC)/arch_include/$(BASE_ARCH_NAME) +INCLUDES += -I$(LIBSEL4_SRC)/include +INCLUDES += -I$(LIBSEL4_SRC)/mode_include/$(ARCH_BITS) +INCLUDES += -I$(LIBSEL4_SRC)/sel4_arch_include/$(FULL_ARCH_NAME) +INCLUDES += -I$(OUT_KATA)/kernel/gen_config +INCLUDES += -I$(OUT_KATA)/libsel4/autoconf +INCLUDES += -I$(OUT_KATA)/libsel4/gen_config/ +INCLUDES += -I$(OUT_KATA)/libsel4/include +INCLUDES += -I$(OUT_KATA)/libsel4/sel4_arch_include/$(FULL_ARCH_NAME) diff --git a/apps/c/libkata/printf.c b/apps/c/libkata/printf.c new file mode 100644 index 0000000..60e663a --- /dev/null +++ b/apps/c/libkata/printf.c @@ -0,0 +1,53 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#ifdef CONFIG_PRINTING +// only prints 32-bit "%x" hex values +void _debug_printf(const char *fmt, ...) { + va_list args; + va_start(args, fmt); + for (; *fmt; fmt++) { + if (*fmt == '%') { + fmt++; + if (*fmt == 'd') { + uint32_t arg = va_arg(args, uint32_t); + // TODO(sleffler): total hack + int printing = 0; + for (int d = 1000000000; d > 1; d /= 10) { + int n = (arg / d) % 10; + if (printing || n > 0) { + seL4_DebugPutChar('0' + n); + printing = 1; + } + } + seL4_DebugPutChar('0' + (arg % 10)); + } else if (*fmt == 'x') { + uint32_t arg = va_arg(args, uint32_t); + for (int i = 7; i >= 0; i--) { + int n = (arg >> (4 * i)) & 0xF; + seL4_DebugPutChar(n > 9 ? 'A' + n - 10 : '0' + n); + } + } + } else { + seL4_DebugPutChar(*fmt); + } + } + va_end(args); +} +#endif // CONFIG_PRINTING diff --git a/apps/c/suicide/Makefile b/apps/c/suicide/Makefile index 588868b..6a2bba3 100644 --- a/apps/c/suicide/Makefile +++ b/apps/c/suicide/Makefile @@ -12,24 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -SRC_LIBSEL4 ?= $(ROOTDIR)/kata/kernel/libsel4 -OUT_KATA ?= $(OUT)/kata/riscv32-unknown-elf/release -OUT_TMP ?= $(OUT)/tmp/suicide +APPNAME := suicide +SOURCES := suicide.c -INCLUDES += -I$(SRC_LIBSEL4)/arch_include/riscv -INCLUDES += -I$(SRC_LIBSEL4)/include -INCLUDES += -I$(SRC_LIBSEL4)/mode_include/32 -INCLUDES += -I$(SRC_LIBSEL4)/sel4_arch_include/riscv32/ -INCLUDES += -I$(OUT_KATA)/kernel/gen_config -INCLUDES += -I$(OUT_KATA)/libsel4/autoconf -INCLUDES += -I$(OUT_KATA)/libsel4/gen_config/ -INCLUDES += -I$(OUT_KATA)/libsel4/include -INCLUDES += -I$(OUT_KATA)/libsel4/sel4_arch_include/riscv32 - -OPT:=-O0 -DBG:=-g - -$(OUT_TMP)/suicide.elf: suicide.c - mkdir -p $(OUT_TMP) - riscv32-unknown-elf-gcc $(DBG) $(OPT) $(INCLUDES) -march=rv32imac -mabi=ilp32 -std=gnu11 -c suicide.c -o $(OUT_TMP)/suicide.o - riscv32-unknown-elf-gcc $(DBG) -static -nostdlib $(OUT_TMP)/suicide.o -o $(OUT_TMP)/suicide.elf +LIBKATA ?= ../libkata +include $(LIBKATA)/make/app.mk diff --git a/apps/c/suicide/suicide.c b/apps/c/suicide/suicide.c index 900c7ae..35a96b3 100644 --- a/apps/c/suicide/suicide.c +++ b/apps/c/suicide/suicide.c @@ -8,34 +8,11 @@ // derefrences a null pointer to kill itself. It's primary use case is to test // out KataOS' fault handlers. -#include -#include -#include -#include - -__thread seL4_IPCBuffer *__sel4_ipc_buffer; - -char minisel_tls[4096] __attribute__((__aligned__(4096))); - -__attribute__((naked)) void _start() { - asm volatile( - ".option push \n" - ".option norelax \n" - "la gp, __global_pointer$ \n" - "la x4, minisel_tls \n" - "addi sp,sp,-16 \n" - "sw a0, 12(sp) \n" - "sw a1, 8(sp) \n" - "sw a2, 4(sp) \n" - "sw a3, 0(sp) \n" - ".option pop \n" - "j main \n"); -} +#include int main(int a0, int a1, int a2, int a3) { -#if CONFIG_PRINTING - seL4_DebugPutString("Goodbye, cruel world!\n"); -#endif + debug_printf("Goodbye, cruel world!\n"); + while (1) { char *p = 0x0; *p = 'g';