Support tls
This commit is contained in:
parent
b9e6d34daf
commit
2f85ac9484
8 changed files with 98 additions and 31 deletions
56
GNUmakefile
56
GNUmakefile
|
@ -19,37 +19,39 @@ include/$(CONFIG_TARGET_TRIPLE):
|
||||||
@echo "GEN $@"
|
@echo "GEN $@"
|
||||||
@./gen-headers.sh $@
|
@./gen-headers.sh $@
|
||||||
|
|
||||||
CPU ?= -mcpu=cortex-a76 -mlittle-endian
|
BUILD_INCLUDE += -I $(TOPDIR)/include \
|
||||||
ARCH += -DAARCH=64 $(CPU)
|
-I $(TOPDIR)/include/$(CONFIG_TARGET_TRIPLE) \
|
||||||
TARGET ?= kernel_2712
|
-I $(TOPDIR)/nolibc/include \
|
||||||
|
-I $(TOPDIR)/openlibm/include \
|
||||||
|
-I $(TOPDIR)/openlibm/src
|
||||||
|
|
||||||
DEFINE += -DSSP_GUARD_SYMBOL=__stack_chk_guard \
|
BUILD_AFLAGS += $(BUILD_INCLUDE) \
|
||||||
|
-DAARCH=64 -mcpu=cortex-a76 -mlittle-endian \
|
||||||
|
-DSSP_GUARD_SYMBOL=__stack_chk_guard \
|
||||||
-DSSP_FAIL_SYMBOL=__stack_chk_fail
|
-DSSP_FAIL_SYMBOL=__stack_chk_fail
|
||||||
|
|
||||||
INCLUDE += -I $(TOPDIR)/include -I $(TOPDIR)/include/$(CONFIG_TARGET_TRIPLE) \
|
|
||||||
-I $(TOPDIR)/nolibc/include -I $(TOPDIR)/openlibm/include -I $(TOPDIR)/openlibm/src
|
|
||||||
|
|
||||||
AFLAGS += $(ARCH) $(DEFINE) $(INCLUDE)
|
|
||||||
|
|
||||||
# CFLAGS & LDFLAGS are used with $(CC). Link arguments must be passed with -Wl
|
# CFLAGS & LDFLAGS are used with $(CC). Link arguments must be passed with -Wl
|
||||||
# Also, CONFIG_TARGET_LD_LDFLAGS are specific when we use CONFIG_TARGET_LD
|
# Also, CONFIG_TARGET_LD_LDFLAGS are specific when we use CONFIG_TARGET_LD
|
||||||
|
|
||||||
CFLAGS += -ffreestanding -fstack-protector-strong -nostdlib \
|
BUILD_CFLAGS += $(BUILD_INCLUDE) \
|
||||||
-nostartfiles -mstrict-align -Wall \
|
-ffreestanding -fstack-protector-strong -nostdlib -nostartfiles -mstrict-align -Wall \
|
||||||
$(ARCH) $(DEFINE) $(INCLUDE) $(CONFIG_TARGET_CC_CFLAGS)
|
-DAARCH=64 -mcpu=cortex-a76 -mlittle-endian \
|
||||||
|
-DSSP_GUARD_SYMBOL=__stack_chk_guard \
|
||||||
|
-DSSP_FAIL_SYMBOL=__stack_chk_fail \
|
||||||
|
$(CONFIG_TARGET_CC_CFLAGS)
|
||||||
|
|
||||||
comma := ,
|
comma := ,
|
||||||
empty:=
|
empty:=
|
||||||
space := $(empty) $(empty)
|
space := $(empty) $(empty)
|
||||||
|
|
||||||
LDFLAGS += -static -Wl,--no-warn-rwx-segments \
|
BUILD_CC_LDFLAGS += -static -Wl,--no-warn-rwx-segments \
|
||||||
-Wl,--section-start=.init=0x80000 \
|
-Wl,--section-start=.init=0x80000 \
|
||||||
-Wl,--start-group \
|
-Wl,--start-group \
|
||||||
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
|
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
|
||||||
-Wl,--end-group \
|
-Wl,--end-group \
|
||||||
-Wl,$(subst $(space),$(comma),${CONFIG_TARGET_CC_LDFLAGS})
|
-Wl,$(subst $(space),$(comma),${CONFIG_TARGET_CC_LDFLAGS})
|
||||||
|
|
||||||
LD_LDFLAGS += -static --no-warn-rwx-segments \
|
BUILD_LD_LDFLAGS += -static --no-warn-rwx-segments \
|
||||||
--section-start=.init=0x80000 \
|
--section-start=.init=0x80000 \
|
||||||
--start-group \
|
--start-group \
|
||||||
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
|
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
|
||||||
|
@ -58,7 +60,7 @@ LD_LDFLAGS += -static --no-warn-rwx-segments \
|
||||||
|
|
||||||
SRCS= kernel.c timer.c led.c interrupt_handler.c exception_handler.c \
|
SRCS= kernel.c timer.c led.c interrupt_handler.c exception_handler.c \
|
||||||
mbox.c clock.c crt.c serial.c log.c power.c tag.c coherent_page.c dtb.c \
|
mbox.c clock.c crt.c serial.c log.c power.c tag.c coherent_page.c dtb.c \
|
||||||
synchronize.c spinlock.c memory.c pager.c translation_table.c
|
synchronize.c spinlock.c memory.c pager.c translation_table.c tls.c
|
||||||
|
|
||||||
ASMS= startup.S exception_stub.S
|
ASMS= startup.S exception_stub.S
|
||||||
|
|
||||||
|
@ -72,6 +74,9 @@ ALLTOOLS := $(foreach tool,$(ALLTOOLS),\
|
||||||
$(CONFIG_TARGET_TRIPLE)-$(tool))
|
$(CONFIG_TARGET_TRIPLE)-$(tool))
|
||||||
TOOLCHAIN_FOR_BUILD := $(addprefix bin/build/,$(ALLTOOLS))
|
TOOLCHAIN_FOR_BUILD := $(addprefix bin/build/,$(ALLTOOLS))
|
||||||
|
|
||||||
|
ALLLIBS := libnolibc.a libopenlibm.a libgilbraltar.a
|
||||||
|
LIBS_FOR_BUILD := $(addprefix lib/$(CONFIG_TARGET_TRIPLE)/,$(ALLLIBS))
|
||||||
|
|
||||||
bin/build:
|
bin/build:
|
||||||
@test ! -d $@
|
@test ! -d $@
|
||||||
@mkdir -p $@
|
@mkdir -p $@
|
||||||
|
@ -81,10 +86,10 @@ bin/build/$(CONFIG_TARGET_TRIPLE)-%: toolchain/%.in | bin/build
|
||||||
@sed -e 's!@@CONFIG_TARGET_ARCH@@!$(CONFIG_TARGET_ARCH)!g' \
|
@sed -e 's!@@CONFIG_TARGET_ARCH@@!$(CONFIG_TARGET_ARCH)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_TRIPLE@@!$(CONFIG_TARGET_TRIPLE)!g' \
|
-e 's!@@CONFIG_TARGET_TRIPLE@@!$(CONFIG_TARGET_TRIPLE)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_CC@@!$(CONFIG_TARGET_CC)!g' \
|
-e 's!@@CONFIG_TARGET_CC@@!$(CONFIG_TARGET_CC)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_CC_CFLAGS@@!$(CFLAGS)!g' \
|
-e 's!@@CONFIG_TARGET_CC_CFLAGS@@!$(BUILD_CFLAGS)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_CC_LDFLAGS@@!$(LDFLAGS)!g' \
|
-e 's!@@CONFIG_TARGET_CC_LDFLAGS@@!$(BUILD_CC_LDFLAGS)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_LD@@!$(CONFIG_TARGET_LD)!g' \
|
-e 's!@@CONFIG_TARGET_LD@@!$(CONFIG_TARGET_LD)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_LD_LDFLAGS@@!$(LD_LDFLAGS)!g' \
|
-e 's!@@CONFIG_TARGET_LD_LDFLAGS@@!$(BUILD_LD_LDFLAGS)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_OBJCOPY@@!$(CONFIG_TARGET_OBJCOPY)!g' \
|
-e 's!@@CONFIG_TARGET_OBJCOPY@@!$(CONFIG_TARGET_OBJCOPY)!g' \
|
||||||
-e 's!@@CONFIG_HOST_CC@@!$(CONFIG_HOST_CC)!g' \
|
-e 's!@@CONFIG_HOST_CC@@!$(CONFIG_HOST_CC)!g' \
|
||||||
-e 's!@@CONFIG_TARGET_NM@@!$(CONFIG_TARGET_NM)!g' \
|
-e 's!@@CONFIG_TARGET_NM@@!$(CONFIG_TARGET_NM)!g' \
|
||||||
|
@ -100,11 +105,11 @@ toolchains: bin/build
|
||||||
|
|
||||||
%.o: %.S
|
%.o: %.S
|
||||||
@echo "AS $@"
|
@echo "AS $@"
|
||||||
@$(AS) $(AFLAGS) -c -o $@ $<
|
@$(AS) $(BUILD_AFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
%.o: %.c include/$(CONFIG_TARGET_TRIPLE)
|
%.o: %.c include/$(CONFIG_TARGET_TRIPLE)
|
||||||
@echo "CC $@"
|
@echo "CC $@"
|
||||||
@$(CC) $(CFLAGS) -c -o $@ $<
|
@$(CC) $(BUILD_CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
lib/$(CONFIG_TARGET_TRIPLE):
|
lib/$(CONFIG_TARGET_TRIPLE):
|
||||||
@test ! -d $@
|
@test ! -d $@
|
||||||
|
@ -126,7 +131,7 @@ lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a: openlibm/libopenlibm.a | lib/$(CONFIG
|
||||||
.PHONY: phony-openlibm
|
.PHONY: phony-openlibm
|
||||||
phony-openlibm: include/$(CONFIG_TARGET_TRIPLE)
|
phony-openlibm: include/$(CONFIG_TARGET_TRIPLE)
|
||||||
@$(MAKE) -C openlibm \
|
@$(MAKE) -C openlibm \
|
||||||
"CC=$(CC)" "CPPFLAGS=$(CFLAGS)" libopenlibm.a
|
"CC=$(CC)" "CPPFLAGS=$(BUILD_CFLAGS)" libopenlibm.a
|
||||||
|
|
||||||
openlibm/libopenlibm.a: phony-openlibm
|
openlibm/libopenlibm.a: phony-openlibm
|
||||||
|
|
||||||
|
@ -138,7 +143,7 @@ ocaml:
|
||||||
git apply --directory=$@ "patches/$$VERSION"/*; \
|
git apply --directory=$@ "patches/$$VERSION"/*; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ocaml/Makefile.config: $(TOOLCHAIN_FOR_BUILD) | ocaml
|
ocaml/Makefile.config: $(TOOLCHAIN_FOR_BUILD) $(LIBS_FOR_BUILD) | ocaml
|
||||||
PATH="$$PWD/bin/build:$$PATH" ; \
|
PATH="$$PWD/bin/build:$$PATH" ; \
|
||||||
cd ocaml && \
|
cd ocaml && \
|
||||||
./configure \
|
./configure \
|
||||||
|
@ -164,7 +169,7 @@ $(OCAML_IS_BUILT): ocaml/Makefile.config | build
|
||||||
cd ocaml && ocamlrun tools/stripdebug ocamlopt ocamlopt.tmp
|
cd ocaml && ocamlrun tools/stripdebug ocamlopt ocamlopt.tmp
|
||||||
touch $@
|
touch $@
|
||||||
|
|
||||||
NOLIBC_CFLAGS= $(CFLAGS) \
|
NOLIBC_CFLAGS= $(BUILD_CFLAGS) \
|
||||||
-I $(TOPDIR)/nolibc/include \
|
-I $(TOPDIR)/nolibc/include \
|
||||||
-I $(TOPDIR)/openlibm/src \
|
-I $(TOPDIR)/openlibm/src \
|
||||||
-I $(TOPDIR)/openlibm/include
|
-I $(TOPDIR)/openlibm/include
|
||||||
|
@ -198,9 +203,10 @@ distclean: clean
|
||||||
|
|
||||||
test/%.o: test/%.c
|
test/%.o: test/%.c
|
||||||
@echo "CC $@"
|
@echo "CC $@"
|
||||||
@$(CC) $(CFLAGS) -c -o $@ $<
|
@$(CC) $(BUILD_CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
kernel_2712.img: lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a lib/$(CONFIG_TARGET_TRIPLE)/libnolibc.a lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a test/test.o
|
kernel_2712.img: lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a lib/$(CONFIG_TARGET_TRIPLE)/libnolibc.a lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a test/test.o
|
||||||
@echo "LD ${@:img=elf}"
|
@echo "LD ${@:img=elf}"
|
||||||
@$(CC) test/test.o -o ${@:img=elf} $(CFLAGS) $(LDFLAGS) -Wl,-Map,${@:img=map} -T gilbraltar.ld
|
@$(CC) test/test.o -o ${@:img=elf} $(BUILD_CFLAGS) $(BUILD_CC_LDFLAGS) \
|
||||||
|
-Wl,-Map,${@:img=map} -T gilbraltar.ld
|
||||||
@$(OBJCOPY) ${@:img=elf} -O binary $@
|
@$(OBJCOPY) ${@:img=elf} -O binary $@
|
||||||
|
|
|
@ -463,6 +463,7 @@ CONFIG_TARGET_OBJCOPY=${TARGET_OBJCOPY}
|
||||||
CONFIG_TARGET_OBJDUMP=${TARGET_OBJDUMP}
|
CONFIG_TARGET_OBJDUMP=${TARGET_OBJDUMP}
|
||||||
CONFIG_TARGET_READELF=${TARGET_READELF}
|
CONFIG_TARGET_READELF=${TARGET_READELF}
|
||||||
CONFIG_TARGET_STRIP=${TARGET_STRIP}
|
CONFIG_TARGET_STRIP=${TARGET_STRIP}
|
||||||
|
CONFIG_SYSROOT=${OPT_PREFIX}/gilbraltar-sysroot/
|
||||||
EOM
|
EOM
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
|
@ -2,6 +2,7 @@ ENTRY(_start)
|
||||||
|
|
||||||
PHDRS {
|
PHDRS {
|
||||||
tdata PT_LOAD FLAGS(4);
|
tdata PT_LOAD FLAGS(4);
|
||||||
|
tbss PT_TLS FLAGS(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTIONS
|
SECTIONS
|
||||||
|
@ -49,4 +50,7 @@ SECTIONS
|
||||||
*(COMMON)
|
*(COMMON)
|
||||||
_end = .;
|
_end = .;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ltdata = _edata - _stdata;
|
||||||
|
_ltbss = SIZEOF(.tbss);
|
||||||
}
|
}
|
||||||
|
|
10
include/tls.h
Normal file
10
include/tls.h
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#ifndef __GILBRALTAR_TLS__
|
||||||
|
#define __GILBRALTAR_TLS__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
size_t gilbraltar_tls_size(void);
|
||||||
|
void gilbraltar_set_tls_base(uintptr_t);
|
||||||
|
void gilbraltar_tls_init(uintptr_t);
|
||||||
|
|
||||||
|
#endif
|
|
@ -10,12 +10,14 @@
|
||||||
#include <synchronize.h>
|
#include <synchronize.h>
|
||||||
#include <sysconfig.h>
|
#include <sysconfig.h>
|
||||||
#include <tag.h>
|
#include <tag.h>
|
||||||
|
#include <tls.h>
|
||||||
|
|
||||||
extern int main();
|
extern int main();
|
||||||
|
|
||||||
extern char __bss_start;
|
extern char __bss_start;
|
||||||
extern char _etext[];
|
extern char _etext[];
|
||||||
extern char _end[];
|
extern char _end[];
|
||||||
|
static uintptr_t tls_main;
|
||||||
|
|
||||||
#define PROPTAG_GET_COMMAND_LINE 0x00050001
|
#define PROPTAG_GET_COMMAND_LINE 0x00050001
|
||||||
|
|
||||||
|
@ -87,6 +89,8 @@ void gilbraltar_dump(void) {
|
||||||
coherent_region_start, coherent_region_end);
|
coherent_region_start, coherent_region_end);
|
||||||
gilbraltar_log(INFO, " heap @ 0x%08lx - 0x%08lx\r\n", heap_start,
|
gilbraltar_log(INFO, " heap @ 0x%08lx - 0x%08lx\r\n", heap_start,
|
||||||
heap_end);
|
heap_end);
|
||||||
|
gilbraltar_log(INFO, " tls @ 0x%08lx - 0x%08lx\r\n",
|
||||||
|
tls_main, tls_main + gilbraltar_tls_size());
|
||||||
gilbraltar_log(INFO, "\r\n");
|
gilbraltar_log(INFO, "\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,6 +120,15 @@ void gilbraltar_sysinit(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
gilbraltar_dtb_init();
|
gilbraltar_dtb_init();
|
||||||
|
tls_main = (uintptr_t)calloc(gilbraltar_tls_size(), sizeof(uint8_t));
|
||||||
|
|
||||||
|
if (tls_main == (uintptr_t)NULL) {
|
||||||
|
gilbraltar_log(ERROR, "Impossible to allocate TLS\r\n");
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
gilbraltar_tls_init(tls_main);
|
||||||
|
gilbraltar_set_tls_base(tls_main);
|
||||||
gilbraltar_dump();
|
gilbraltar_dump();
|
||||||
|
|
||||||
int ret = main(ac + 1, cmdline);
|
int ret = main(ac + 1, cmdline);
|
||||||
|
|
|
@ -93,12 +93,10 @@ static uintptr_t sbrk_guard_size;
|
||||||
|
|
||||||
void *sbrk(intptr_t increment) {
|
void *sbrk(intptr_t increment) {
|
||||||
uintptr_t prev, brk;
|
uintptr_t prev, brk;
|
||||||
uintptr_t max = (uintptr_t)&prev - sbrk_guard_size;
|
|
||||||
prev = brk = sbrk_cur;
|
prev = brk = sbrk_cur;
|
||||||
|
|
||||||
brk += increment;
|
brk += increment;
|
||||||
|
if (brk >= sbrk_end || brk < sbrk_start)
|
||||||
if (brk >= max || brk >= sbrk_end || brk < sbrk_start)
|
|
||||||
return ((void *)(-1));
|
return ((void *)(-1));
|
||||||
|
|
||||||
sbrk_cur = brk;
|
sbrk_cur = brk;
|
||||||
|
|
35
kernel/tls.c
Normal file
35
kernel/tls.c
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
extern char _stdata[];
|
||||||
|
#define TDATA ((void *)_stdata)
|
||||||
|
|
||||||
|
extern char _ltdata[], _ltbss[];
|
||||||
|
#define LTDATA ((size_t)_ltdata)
|
||||||
|
#define LTBSS ((size_t)_ltbss)
|
||||||
|
|
||||||
|
struct tcb {
|
||||||
|
void *tp;
|
||||||
|
void *pad;
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t gilbraltar_tls_size(void) {
|
||||||
|
return (LTDATA + LTBSS + sizeof(struct tcb));
|
||||||
|
}
|
||||||
|
|
||||||
|
void gilbraltar_set_tls_base(uintptr_t base) {
|
||||||
|
__asm__ __volatile("msr tpidr_el0, %0" ::"r"(base));
|
||||||
|
}
|
||||||
|
|
||||||
|
static uintptr_t tls_data_offset(uintptr_t tls) {
|
||||||
|
uintptr_t data;
|
||||||
|
data = tls + sizeof(struct tcb);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gilbraltar_tls_init(uintptr_t tls) {
|
||||||
|
uintptr_t *tmp = (uintptr_t *)tls;
|
||||||
|
*tmp = (uintptr_t)tmp;
|
||||||
|
memcpy((void *)tls_data_offset(tls), TDATA, LTDATA);
|
||||||
|
}
|
|
@ -88,7 +88,7 @@ case ${M} in
|
||||||
-DAARCH=64 \
|
-DAARCH=64 \
|
||||||
-mcpu=cortex-a76 \
|
-mcpu=cortex-a76 \
|
||||||
"$@"
|
"$@"
|
||||||
;;
|
;;
|
||||||
link)
|
link)
|
||||||
[ -n "${B}" ] && B="-Wl,-T,gilbraltar.ld"
|
[ -n "${B}" ] && B="-Wl,-T,gilbraltar.ld"
|
||||||
[ -n "${LDFLAGS}" ] && LDFLAGS=$(echo "-Wl @@CONFIG_TARGET_LD_LDFLAGS@@" | tr ' ' ',')
|
[ -n "${LDFLAGS}" ] && LDFLAGS=$(echo "-Wl @@CONFIG_TARGET_LD_LDFLAGS@@" | tr ' ' ',')
|
||||||
|
@ -101,9 +101,9 @@ case ${M} in
|
||||||
-ffreestanding \
|
-ffreestanding \
|
||||||
-fstack-protector-strong \
|
-fstack-protector-strong \
|
||||||
"$@" \
|
"$@" \
|
||||||
@@CONFIG_TARGET_CC_LDFLAGS@@
|
@@CONFIG_TARGET_CC_LDFLAGS@@ \
|
||||||
-Wl,${LDFLAGS} \
|
-Wl,${LDFLAGS} \
|
||||||
${B} \
|
${B} \
|
||||||
-static \
|
-static
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
Loading…
Reference in a new issue