Skip to content

Commit

Permalink
app/idt: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanbulck committed Jul 28, 2020
1 parent 56fb7f3 commit 8845a6b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 377 deletions.
2 changes: 1 addition & 1 deletion app/idt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LDFLAGS += -lsgx-step -lsgx_urts \
-lsgx_uae_service -pthread $(SUBDIRS:%=-L %) -L$(SGX_SDK)/lib64/

SOURCES = $(shell ls *.c)
OBJECTS = $(SOURCES:.c=.o) asm.o
OBJECTS = $(SOURCES:.c=.o)
OUTPUT = app

BUILDDIRS = $(SUBDIRS:%=build-%)
Expand Down
353 changes: 47 additions & 306 deletions app/idt/README.md

Large diffs are not rendered by default.

32 changes: 0 additions & 32 deletions app/idt/asm.S

This file was deleted.

114 changes: 76 additions & 38 deletions app/idt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +25,112 @@
#include "libsgxstep/sched.h"
#include "libsgxstep/config.h"

#define DO_APIC_SW_IRQ 1
#define DO_APIC_TMR_IRQ 1
#define DO_APIC_SW_IRQ 0
#define DO_USER_HANDLER 0
#define DO_USER_LOOP 0

void __ss_irq_handler(void);
extern int volatile __ss_irq_fired, __ss_irq_count, __ss_irq_cpl;
#define DO_EXEC_PRIV 1

/* ------------------------------------------------------------ */
/* This code may execute with ring0 privileges */
int my_cpl = -1;

void do_irq_loop(void)
void pre_irq(void)
{
my_cpl = get_cpl();
__ss_irq_fired = 0;
apic_timer_irq(10);
while(!__ss_irq_fired);
}

void my_ring0_func(void)
void do_irq_sw(void)
{
my_cpl = get_cpl();
pre_irq();
asm("int %0\n\t" ::"i"(IRQ_VECTOR):);
}
/* ------------------------------------------------------------ */

int main( int argc, char **argv )
void do_irq_tmr(void)
{
idt_t idt = {0};
ASSERT( !claim_cpu(VICTIM_CPU) );
pre_irq();
apic_timer_irq(10);
while(!__ss_irq_fired);
}

info_event("Establishing user space IDT mapping");
map_idt(&idt);
#if DO_USER_HANDLER
install_user_asm_irq_handler(&idt, __ss_irq_handler, IRQ_VECTOR);
#else
install_kernel_irq_handler(&idt, __ss_irq_handler, IRQ_VECTOR);
#endif
//dump_idt(&idt);
/* ------------------------------------------------------------ */

#if !DO_USER_LOOP
exec_priv(my_ring0_func);
info("back from my_ring0_func w CPL=%d", my_cpl);
#endif
void post_irq(void)
{
ASSERT(__ss_irq_fired);
info("returned from IRQ: my_cpl=%d; irq_cpl=%d; count=%02d; flags=%p",
my_cpl, __ss_irq_cpl, __ss_irq_count, read_flags());
}

void do_irq_test(int skip_priv)
{
#if DO_APIC_SW_IRQ
info_event("Triggering user space software interrupts");
asm("int %0\n\t" ::"i"(IRQ_VECTOR):);
asm("int %0\n\t" ::"i"(IRQ_VECTOR):);
printf("\n");
info("Triggering ring3 software interrupts..");
for (int i=0; i < 3; i++)
{
do_irq_sw();
post_irq();
}

#if DO_EXEC_PRIV
if (!skip_priv)
{
printf("\n");
info("Triggering ring0 software interrupts..");
for (int i=0; i < 3; i++)
{
exec_priv(do_irq_sw);
post_irq();
}
}
#endif
#endif

#if DO_APIC_TMR_IRQ
info_event("Triggering user space APIC timer interrupts");
printf("\n");
info("Triggering ring3 APIC timer interrupts..");
apic_timer_oneshot(IRQ_VECTOR);

for (int i=0; i < 3; i++)
{
//apic_send_ipi_self(IRQ_VECTOR);
#if DO_USER_LOOP
do_irq_loop();
#else
exec_priv(do_irq_loop);
#endif
info("returned from timer IRQ: my_cpl=%d; irq_cpl=%d; count=%d; flags=%p", my_cpl, __ss_irq_cpl, __ss_irq_count, read_flags());
do_irq_tmr();
post_irq();
}

#if DO_EXEC_PRIV
if (!skip_priv)
{
printf("\n");
info("Triggering ring0 APIC timer interrupts..");
for (int i=0; i < 3; i++)
{
exec_priv(do_irq_tmr);
post_irq();
}
}
#endif

apic_timer_deadline();
#endif
}

int main( int argc, char **argv )
{
idt_t idt = {0};
ASSERT( !claim_cpu(VICTIM_CPU) );

info_event("Installing and testing ring3 IDT handler");
map_idt(&idt);
install_user_asm_irq_handler(&idt, __ss_irq_handler, IRQ_VECTOR);
do_irq_test(/*skip_priv=*/1);

info_event("Installing and testing ring0 IDT handler");
install_kernel_irq_handler(&idt, __ss_irq_handler, IRQ_VECTOR);
#if DO_EXEC_PRIV
exec_priv(pre_irq);
info("back from exec_priv(pre_irq) with CPL=%d", my_cpl);
#endif
do_irq_test(/*skip_priv=*/0);

info("all is well; irq_count=%d; exiting..", __ss_irq_count);
return 0;
Expand Down
3 changes: 3 additions & 0 deletions libsgxstep/idt.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ void install_user_irq_handler(idt_t *idt, irq_cb_t handler, int vector);
void install_user_asm_irq_handler(idt_t *idt, void* asm_handler, int vector);
void install_kernel_irq_handler(idt_t *idt, void *asm_handler, int vector);

void __ss_irq_handler(void);
extern int volatile __ss_irq_fired, __ss_irq_count, __ss_irq_cpl;

#endif
36 changes: 36 additions & 0 deletions libsgxstep/irq_entry.S
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
/* ********************************************************************** */
.data
.global __ss_irq_fired, __ss_irq_count, __ss_irq_cpl
__ss_irq_fired:
.int 0x0
__ss_irq_count:
.int 0x0
__ss_irq_cpl:
.int 0xff
/* not sure there's a kernel stack we can use(?) */
__ss_irq_rax:
.quad 0x0

.text
.global __ss_irq_handler
__ss_irq_handler:
incl __ss_irq_fired(%rip)
incl __ss_irq_count(%rip)

mov %cs, __ss_irq_cpl(%rip)
and $0x3, __ss_irq_cpl(%rip)

/* apic_write(APIC_EOI, 0x0); */
mov %rax, __ss_irq_rax(%rip)
lea apic_base(%rip), %rax
mov (%rax),%rax
test %rax, %rax
jz 1f
add $0xb0, %rax
movl $0x0, (%rax)
1:
mov __ss_irq_rax(%rip), %rax
iretq

/* ********************************************************************** */

.text
.global sgx_step_irq_entry
.type sgx_step_irq_entry,@function
Expand Down

0 comments on commit 8845a6b

Please sign in to comment.