Skip to content

Commit

Permalink
[LibOS] asm offset constants generation
Browse files Browse the repository at this point in the history
Auto generate offset constants for assembly. replace magic number with
symbolic constants in LibOS/shim/src/syscallas.S.

This patch also introduces Makefile.rules to accommodate common make rules
so that V=1 (like Linux style make) is accepted.

Signed-off-by: Isaku Yamahata <[email protected]>
  • Loading branch information
yamahata authored and donporter committed Mar 25, 2019
1 parent fb48d0e commit 4b190ba
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 15 deletions.
6 changes: 0 additions & 6 deletions LibOS/shim/include/shim_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@

#define SHIM_TLS_CANARY $xdeadbeef

#if defined(__x86_64__)
# define SHIM_TCB_OFFSET 80
#else
# define SHIM_TCB_OFFSET 44
#endif

#else /* !__ASSEMBLER__ */

#define SHIM_TLS_CANARY 0xdeadbeef
Expand Down
2 changes: 2 additions & 0 deletions LibOS/shim/src/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
libsysdb.so.cached
asm-offsets.h
asm-offsets.s
6 changes: 5 additions & 1 deletion LibOS/shim/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,9 @@ elf/shim_rtld.o: $(wildcard elf/*.h)
@echo [ $@ ]
@$(AS) $(ASFLAGS) $(defs) -E $< -o $@

syscallas.S: asm-offsets.h

include ../../../Makefile.rules

clean:
rm -rf $(addsuffix .o,$(objs)) $(shim_target) $(files_to_build) .lib
rm -rf $(addsuffix .o,$(objs)) $(shim_target) $(files_to_build) .lib $(CLEAN_FILES)
18 changes: 18 additions & 0 deletions LibOS/shim/src/asm-offsets.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stddef.h>

#include <shim_internal.h>
#include <shim_tls.h>

#define OFFSET_T(name, str_t, member) \
asm volatile(".ascii \" #define " #name " %0 \"\n":: \
"i"(offsetof(str_t, member)))

void dummy(void)
{
OFFSET_T(SHIM_TCB_OFFSET, __libc_tcb_t, shim_tcb);
OFFSET_T(TCB_SYSCALL_NR, shim_tcb_t, context.syscall_nr);
OFFSET_T(TCB_SP, shim_tcb_t, context.sp);
OFFSET_T(TCB_RET_IP, shim_tcb_t, context.ret_ip);
OFFSET_T(TCB_REGS, shim_tcb_t, context.regs);
}

18 changes: 10 additions & 8 deletions LibOS/shim/src/syscallas.S
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <shim_tls.h>
#include <shim_unistd_defs.h>

#include "asm-offsets.h"

.global syscalldb
.type syscalldb, @function
.extern shim_table, debug_unsupp
Expand Down Expand Up @@ -62,22 +64,22 @@ isdef:
pushq %r14
pushq %r15

movq %rax, %fs:(SHIM_TCB_OFFSET + 24)
movq %rax, %fs:(SHIM_TCB_OFFSET + TCB_SYSCALL_NR)
leaq 16(%rbp), %rax
movq %rax, %fs:(SHIM_TCB_OFFSET + 32)
movq %rax, %fs:(SHIM_TCB_OFFSET + TCB_SP)
movq 8(%rbp), %rax
movq %rax, %fs:(SHIM_TCB_OFFSET + 40)
movq %rsp, %fs:(SHIM_TCB_OFFSET + 48)
movq %rax, %fs:(SHIM_TCB_OFFSET + TCB_RET_IP)
movq %rsp, %fs:(SHIM_TCB_OFFSET + TCB_REGS)

/* Translating x86_64 kernel calling convention to user-space
* calling convention */
movq %r10, %rcx
call *%rbx

movq $0, %fs:(SHIM_TCB_OFFSET + 24)
movq $0, %fs:(SHIM_TCB_OFFSET + 32)
movq $0, %fs:(SHIM_TCB_OFFSET + 40)
movq $0, %fs:(SHIM_TCB_OFFSET + 48)
movq $0, %fs:(SHIM_TCB_OFFSET + TCB_SYSCALL_NR)
movq $0, %fs:(SHIM_TCB_OFFSET + TCB_SP)
movq $0, %fs:(SHIM_TCB_OFFSET + TCB_RET_IP)
movq $0, %fs:(SHIM_TCB_OFFSET + TCB_REGS)

popq %r15
popq %r14
Expand Down
46 changes: 46 additions & 0 deletions Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
ifeq ("$(origin V)", "command line")
BUILD_VERBOSE = $(V)
endif
ifndef BUILD_VERBOSE
BUILD_VERBOSE = 0
endif

ifeq ($(BUILD_VERBOSE),1)
quiet =
Q =
else
quiet = quiet_
Q = @
endif

export Q quiet BUILD_VERBOSE

squote := '
escsq = $(subst $(squote),'\$(squote)',$1)

echo-cmd = $(if $($(quiet)cmd_$(1)), echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
cmd = @$(echo-cmd) $(cmd_$(1))


quiet_cmd_asm_offsets_s = [ $@ ]
cmd_asm_offsets_s = $(CC) $(CFLAGS) $(defs) -S $< -o $@

asm-offsets.s: asm-offsets.c $(headers)
$(call cmd,asm_offsets_s)
CLEAN_FILES += asm-offsets.s


quiet_cmd_asm_offsets_h = [ $@ ]
cmd_asm_offsets_h = \
(set -e; \
echo "/* DO NOT MODIFY. THIS FILE WAS AUTO-GENERATED. */"; \
echo "\#ifndef _ASM_OFFSETS_H_"; \
echo "\#define _ASM_OFFSETS_H_"; \
echo ""; \
awk '/\.ascii \" \#define/{val=$$5; gsub("\\$$", "", val); print $$3" "$$4" "val}' $^; \
echo ""; \
echo "\#endif") > $@

asm-offsets.h: asm-offsets.s
$(call cmd,asm_offsets_h)
CLEAN_FILES += asm-offests.h

0 comments on commit 4b190ba

Please sign in to comment.