Skip to content

Commit

Permalink
arch/x86: Add function to check for unmodified ectx
Browse files Browse the repository at this point in the history
This can be used in situtations where the executed code must not touch
the extended registers. For example, interrupt handlers in Unikraft do
save these and therefore also cannot modify them. Doing so is usually
breaking the interrupted code.

Signed-off-by: Marco Schlumpp <marco@unikraft.io>
  • Loading branch information
mschlumpp committed May 17, 2023
1 parent b0da6bf commit d3ed251
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
26 changes: 26 additions & 0 deletions arch/x86/ectx.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <uk/essentials.h>
#include <uk/assert.h>
#include <uk/print.h>
#include <uk/hexdump.h>
#include <string.h> /* memset */

enum x86_save_method {
Expand Down Expand Up @@ -173,3 +174,28 @@ void ukarch_ectx_load(struct ukarch_ectx *state)
break;
}
}

#ifdef CONFIG_ARCH_X86_64
void ukarch_ectx_assert_equal(struct ukarch_ectx *state)
{
__u8 ectxbuf[ectx_size + ectx_align];
struct ukarch_ectx *current;

/* Store the current state */
current = (struct ukarch_ectx *)ALIGN_UP((__uptr)ectxbuf, ectx_align);
ukarch_ectx_init(current);

if (memcmp(current, state, ectx_size) != 0) {
uk_pr_crit("Modified ECTX detected!\n");
uk_pr_crit("Current:\n");
uk_hexdumpk(KLVL_CRIT, current, ectx_size,
UK_HXDF_ADDR | UK_HXDF_GRPQWORD | UK_HXDF_COMPRESS,
2);

uk_pr_crit("Expected:\n");
uk_hexdumpk(KLVL_CRIT, state, ectx_size,
UK_HXDF_ADDR | UK_HXDF_GRPQWORD | UK_HXDF_COMPRESS,
2);
}
}
#endif
11 changes: 11 additions & 0 deletions include/uk/arch/ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,16 @@ void ukarch_ectx_store(struct ukarch_ectx *state);
*/
void ukarch_ectx_load(struct ukarch_ectx *state);

#ifdef CONFIG_ARCH_X86_64
/**
* Compare the given extended context with the state of the currently executing
* CPU. If the state is different, crash the kernel.
*
* @param state
* Reference to extended context to compare to
*/
void ukarch_ectx_assert_equal(struct ukarch_ectx *state);
#endif

#endif /* !__ASSEMBLY__ */
#endif /* __UKARCH_CTX_H__ */

0 comments on commit d3ed251

Please sign in to comment.