Skip to content

Commit

Permalink
KernelTestFramework#6 support exception tables
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ahmed <[email protected]>
  • Loading branch information
82marbag committed Sep 6, 2021
1 parent c46f6da commit 64e8fe6
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 3 deletions.
8 changes: 8 additions & 0 deletions arch/x86/link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ SECTIONS
__end_data = .;
} :kernel

.extables BLOCK(4K) : ALIGN(4K)
{
__start_extables = .;
*(.extables)
. = ALIGN(4K);
__end_extables = .;
} :kernel

.bss BLOCK(4K) : ALIGN(4K)
{
. = ALIGN(4K);
Expand Down
20 changes: 20 additions & 0 deletions arch/x86/traps.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <drivers/keyboard.h>
#include <drivers/pit.h>
#include <drivers/serial.h>
#include <extables.h>
#include <mm/regions.h>
#include <ktf.h>
#include <lib.h>
#include <percpu.h>
Expand Down Expand Up @@ -289,6 +291,24 @@ void print_callstack(const void *sp, const void *ip) {

void do_exception(struct cpu_regs *regs) {
static char ec_str[32], panic_str[128];
for (extable_entry_t *cur = __start_extables; cur < __end_extables; ++cur) {
if (regs->_ASM_IP == cur->fault_addr) {
printk("found exception entry\n");
if (cur->fixup) {
printk("using fixup\n");
regs->_ASM_IP = cur->fixup;
}
if (cur->cb) {
printk("using cb\n");
cur->cb(regs);
}
if (cur->fixup || cur->cb) {
return;
} else {
panic("fixup or cb must be set in the extable_entry\n");
}
}
}

dump_regs(regs);
print_callstack(_ptr(regs->_ASM_SP), _ptr(regs->_ASM_IP));
Expand Down
46 changes: 46 additions & 0 deletions include/extables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright © 2021 Amazon.com, Inc. or its affiliates.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KTF_EXTABLES_H
#define KTF_EXTABLES_H

#include <arch/x86/processor.h>

typedef void(extable_entry_callback_t)(cpu_regs_t *regs);

struct extable_entry {
uint64_t fault_addr;

uint64_t fixup;
extable_entry_callback_t *cb;
};
typedef struct extable_entry extable_entry_t;

#define ASM_EXTABLE_HANDLER(fault_address, fixup, handler) \
".pushsection .extables, \"a\";\n" \
".quad " STR(fault_address) ", " STR(fixup) ", " STR(handler) ";\n" \
".popsection;\n"

#endif

22 changes: 21 additions & 1 deletion include/lib.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2020 Amazon.com, Inc. or its affiliates.
* Copyright © 2021 Amazon.com, Inc. or its affiliates.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -28,6 +28,7 @@
#include <asm-macros.h>
#include <console.h>
#include <ktf.h>
#include <extables.h>
#include <processor.h>
#include <segment.h>

Expand Down Expand Up @@ -144,6 +145,25 @@ static inline uint64_t rdmsr(uint32_t msr_idx) {
return (((uint64_t) high) << 32) | low;
}

/*
* read the msr_idx msr; the result is valid iff *read is true
*/
static inline uint64_t rdmsr_safe(uint32_t msr_idx, bool *read) {
uint32_t low, high;
low = high = 0;

*read = false;

//asm("1: rdmsr; 2: ret;"
asm volatile("1: ret; 2: ret;"
ASM_EXTABLE_HANDLER(1b, 2b, 0)
//: "=a"(low), "=d"(high) : "c"(msr_idx)
);

*read = true;
return (((uint64_t) high) << 32) | low;
}

static inline void wrmsr(uint32_t msr_idx, uint64_t value) {
asm volatile("wrmsr" ::"c"(msr_idx), "a"((uint32_t) value),
"d"((uint32_t)(value >> 32)));
Expand Down
2 changes: 2 additions & 0 deletions include/mm/regions.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#ifndef __ASSEMBLY__
#include <cmdline.h>
#include <extables.h>
#include <page.h>
#include <string.h>

Expand All @@ -56,6 +57,7 @@ extern unsigned long __start_bss_init[], __end_bss_init[];

extern unsigned long __start_text_rmode[], __end_text_rmode[];
extern unsigned long __start_data_rmode[], __end_data_rmode[];
extern struct extable_entry __start_extables[], __end_extables[];
extern unsigned long __start_bss_rmode[], __end_bss_rmode[];

extern struct ktf_param __start_cmdline[], __end_cmdline[];
Expand Down
1 change: 1 addition & 0 deletions mm/regions.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ addr_range_t addr_ranges[] = {

KERNEL_RANGE( ".text", L1_PROT_RO, __start_text, __end_text ),
KERNEL_RANGE( ".data", L1_PROT, __start_data, __end_data ),
KERNEL_RANGE( ".extables", L1_PROT_RO, __start_extables, __end_extables ),
KERNEL_RANGE( ".bss", L1_PROT, __start_bss, __end_bss ),
KERNEL_RANGE( ".rodata", L1_PROT_RO, __start_rodata, __end_rodata ),
KERNEL_RANGE( ".symbols", L1_PROT_RO, __start_symbols, __end_symbols ),
Expand Down
7 changes: 5 additions & 2 deletions tests/test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Amazon.com, Inc. or its affiliates.
* Copyright (c) 2021 Amazon.com, Inc. or its affiliates.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -64,8 +64,11 @@ void test_main(void) {
test_fn *fn = NULL;
unsigned n = 0;

printk("\nRunning tests\n");
bool read = false;
rdmsr_safe(0, &read);
printk("\nread? %x\n", read);

printk("\nRunning tests\n");
while (get_next_test(&fn, &name) == TESTS_FOUND) {
int rc;

Expand Down

0 comments on commit 64e8fe6

Please sign in to comment.