Skip to content

Commit

Permalink
Some kernel text swapping code
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia committed Oct 24, 2019
1 parent 11546e1 commit eb53c8e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ INCLUDES := -include param.h -iquote libutil/include -I$(MTRACESRC)
COMFLAGS := -pthread -Wno-unused-result
LDFLAGS := -pthread
endif
COMFLAGS += -g -MD -MP -O3 -Wall -DHW_$(HW) $(INCLUDES) -fno-stack-protector
COMFLAGS += -g -MD -MP -O3 -Wall -DHW_$(HW) $(INCLUDES) -fno-stack-protector # -mindirect-branch=thunk
CFLAGS := $(COMFLAGS) -std=c99 $(CFLAGS)
CXXFLAGS := $(COMFLAGS) -std=c++17 -Wno-sign-compare -faligned-new $(CXXFLAGS)
ASFLAGS := $(ASFLAGS) -Iinclude -I$(O)/include -m64 -gdwarf-2 -MD -MP -DHW_$(HW) -include param.h
Expand Down
3 changes: 3 additions & 0 deletions include/kernel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ int futexkey(const u64* useraddr, vmap* vmap, futexkey_t* key);
long futexwait(futexkey_t key, u64 val, u64 timer);
long futexwake(futexkey_t key, u64 nwake);

// hotpatch.cc
extern void* qtext;

// hz.c
void microdelay(u64);
void inithz(void);
Expand Down
1 change: 1 addition & 0 deletions kernel/Makefrag
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OBJS = \
futex.o \
idle.o \
ioapic.o \
hotpatch.o \
hwvm.o \
hz.o \
kalloc.o \
Expand Down
64 changes: 64 additions & 0 deletions kernel/hotpatch.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <string.h>
#include "types.h"
#include "kernel.hh"

void* qtext;

// extern u64 __x86_indirect_thunk_rax;
// extern u64 __x86_indirect_thunk_rcx;
// extern u64 __x86_indirect_thunk_rdx;
// extern u64 __x86_indirect_thunk_rbx;
// extern u64 __x86_indirect_thunk_rsi;
// extern u64 __x86_indirect_thunk_r8;
// extern u64 __x86_indirect_thunk_r9;
// extern u64 __x86_indirect_thunk_r10;
// extern u64 __x86_indirect_thunk_r12;
// extern u64 __x86_indirect_thunk_r13;
// extern u64 __x86_indirect_thunk_r14;
// extern u64 __x86_indirect_thunk_r15;

// void* retpoline_thunks[] = {
// (void*)&__x86_indirect_thunk_rax,
// (void*)&__x86_indirect_thunk_rcx,
// (void*)&__x86_indirect_thunk_rdx,
// (void*)&__x86_indirect_thunk_rbx,
// (void*)&__x86_indirect_thunk_rsi,
// (void*)&__x86_indirect_thunk_r8,
// (void*)&__x86_indirect_thunk_r9,
// (void*)&__x86_indirect_thunk_r10,
// (void*)&__x86_indirect_thunk_r12,
// (void*)&__x86_indirect_thunk_r13,
// };

// void* text_to_qtext(void* addr) {
// return (char*)addr - KCODE + (u64)qtext;
// }

// void replace_qtext_u16(void* target, u16 value) {
// *(u16*)text_to_qtext(target) = value;
// }
// void replace_qtext_u32(void* target, u32 value) {
// *(u32*)text_to_qtext(target) = value;
// }

// void remove_retpolines()
// {
// replace_qtext_u16(&__x86_indirect_thunk_rax, 0xE0FF);
// replace_qtext_u16(&__x86_indirect_thunk_rcx, 0xE1FF);
// replace_qtext_u16(&__x86_indirect_thunk_rdx, 0xE2FF);
// replace_qtext_u16(&__x86_indirect_thunk_rbx, 0xE3FF);
// replace_qtext_u16(&__x86_indirect_thunk_rsi, 0xE6FF);
// replace_qtext_u32(&__x86_indirect_thunk_r8, 0xE0FF41);
// replace_qtext_u32(&__x86_indirect_thunk_r9, 0xE1FF41);
// replace_qtext_u32(&__x86_indirect_thunk_r10, 0xE2FF41);
// replace_qtext_u32(&__x86_indirect_thunk_r12, 0xE4FF41);
// replace_qtext_u32(&__x86_indirect_thunk_r13, 0xE5FF41);
// }

void inithotpatch()
{
qtext = kalloc("qtext", 0x200000);
memset(qtext, 0, 0x100000);
memmove(qtext + 0x100000, (void*)KCODE + 0x100000, 0x100000);
// remove_retpolines();
}
2 changes: 1 addition & 1 deletion kernel/hwvm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ struct pgmap {

k = PX(L_PML4, KGLOBAL);
memset(&pair.user->e[0], 0, PGSIZE);
pair.user->uexpose((void*)KCODE, L_2M, true);

return pair;
}
Expand Down Expand Up @@ -698,6 +697,7 @@ namespace mmu_per_core_page_table {
mypml4s.user->uexpose((void*)(mycpu()->ts.ist[1] + i - KSTACKSIZE), pgmap::L_4K); // nmi stack
mypml4s.user->uexpose((void*)(mycpu()->ts.ist[2] + i - KSTACKSIZE), pgmap::L_4K); // double fault stack
}
*(mypml4s.user->find(KCODE, pgmap::L_2M).create(0)) = v2p(qtext) | PTE_PS | PTE_P | PTE_W;
}

// TODO(behrensj): Avoid doing this on every `switch_to` call.
Expand Down
2 changes: 2 additions & 0 deletions kernel/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ void inithpet(void);
void initrtc(void);
void initmfs(void);
void idleloop(void);
void inithotpatch(void);

#define IO_RTC 0x70

Expand Down Expand Up @@ -246,6 +247,7 @@ cmain(u64 mbmagic, u64 mbaddr)
initdisk(); // disk
initinode(); // inode cache
initmfs();
inithotpatch();

if (VERBOSE)
cprintf("ncpu %d %lu MHz\n", ncpu, cpuhz / 1000000);
Expand Down
6 changes: 3 additions & 3 deletions net/Makefrag
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LWIP_CFLAGS = -Wno-attributes \
-Wno-char-subscripts \
-Wno-unused-but-set-variable \
-Wno-format \
-mcmodel=large -O2
-mcmodel=kernel -O2 -mindirect-branch=keep

LWIP_INCLUDES := \
-Ilwip/src/include \
Expand Down Expand Up @@ -62,8 +62,8 @@ LWIP_SRCFILES += \
net/if.c
LWIP_OBJFILES := $(patsubst %.c, $(O)/%.o, $(LWIP_SRCFILES))

$(O)/net/%.o: CXXFLAGS+=-mcmodel=large -DXV6_KERNEL
$(O)/lwip/src/%.o: CFLAGS+=-mcmodel=large $(LWIP_CFLAGS) $(LWIP_INCLUDES)
$(O)/net/%.o: CXXFLAGS+=-mcmodel=kernel -DXV6_KERNEL
$(O)/lwip/src/%.o: CFLAGS+=-mcmodel=kernel $(LWIP_CFLAGS) $(LWIP_INCLUDES)

$(O)/liblwip.a: $(LWIP_OBJFILES)
@echo " AR $@"
Expand Down

0 comments on commit eb53c8e

Please sign in to comment.