50f718a
From 6ae2cc94566550e2e27c791485319bd5791cc861 Mon Sep 17 00:00:00 2001
50f718a
From: Peter Jones <pjones@redhat.com>
50f718a
Date: Tue, 9 Aug 2011 13:46:53 -0400
50f718a
Subject: [PATCH] Add tcc.efi to test our calling convention shananagans.
50f718a
50f718a
Add a test case to actually make sure we've got stack alignment and
50f718a
calling conventions set up correctly.
50f718a
---
50f718a
 apps/Makefile |    3 +-
50f718a
 apps/tcc.c    |  442 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50f718a
 2 files changed, 444 insertions(+), 1 deletions(-)
50f718a
 create mode 100644 apps/tcc.c
50f718a
50f718a
diff --git a/apps/Makefile b/apps/Makefile
50f718a
index 6b50e4f..71e738d 100644
50f718a
--- a/apps/Makefile
50f718a
+++ b/apps/Makefile
50f718a
@@ -32,13 +32,14 @@ TOPDIR = $(SRCDIR)/..
50f718a
 CDIR=$(TOPDIR)/..
50f718a
 LINUX_HEADERS	= /usr/src/sys/build
50f718a
 CPPFLAGS	+= -D__KERNEL__ -I$(LINUX_HEADERS)/include
50f718a
+#CFLAGS		+= -ggdb
50f718a
 CRTOBJS		= ../gnuefi/crt0-efi-$(ARCH).o
50f718a
 LDSCRIPT	= $(TOPDIR)/gnuefi/elf_$(ARCH)_efi.lds
50f718a
 LDFLAGS		+= -T $(LDSCRIPT) -shared -Bsymbolic -L../lib -L../gnuefi $(CRTOBJS)
50f718a
 LOADLIBES	= -lefi -lgnuefi $(shell $(CC) $(ARCH3264) -print-libgcc-file-name)
50f718a
 FORMAT		= efi-app-$(ARCH)
50f718a
 
50f718a
-TARGETS = t.efi t2.efi t3.efi t4.efi t5.efi t6.efi printenv.efi t7.efi route80h.efi modelist.efi
50f718a
+TARGETS = t.efi t2.efi t3.efi t4.efi t5.efi t6.efi printenv.efi t7.efi route80h.efi modelist.efi tcc.efi
50f718a
 
50f718a
 all:	$(TARGETS)
50f718a
 
50f718a
diff --git a/apps/tcc.c b/apps/tcc.c
50f718a
new file mode 100644
50f718a
index 0000000..546df92
50f718a
--- /dev/null
50f718a
+++ b/apps/tcc.c
50f718a
@@ -0,0 +1,442 @@
50f718a
+/*
50f718a
+ * Test if our calling convention gymnastics actually work
50f718a
+ */
50f718a
+
50f718a
+#include <efi.h>
50f718a
+#include <efilib.h>
50f718a
+
50f718a
+#ifdef __x86_64__
50f718a
+#include <x86_64/pe.h>
50f718a
+#include <x86_64/efibind.h>
50f718a
+#endif
50f718a
+
50f718a
+#if 0
50f718a
+	asm volatile("out %0,%1" : : "a" ((uint8_t)a), "dN" (0x80));
50f718a
+
50f718a
+extern void dump_stack(void);
50f718a
+asm(	".globl	dump_stack\n"
50f718a
+	"dump_stack:\n"
50f718a
+	"	movq %rsp, %rdi\n"
50f718a
+	"	jmp *dump_stack_helper@GOTPCREL(%rip)\n"
50f718a
+	".size	dump_stack, .-dump_stack");
50f718a
+
c968658
+void dump_stack_helper(unsigned long rsp_val)
50f718a
+{
c968658
+	uint64_t *rsp = (unsigned long *)rsp_val;
50f718a
+	int x;
50f718a
+
50f718a
+	Print(L"%%rsp: 0x%08x%08x stack:\r\n",
50f718a
+					(rsp_val & 0xffffffff00000000) >>32,
50f718a
+					 rsp_val & 0xffffffff);
50f718a
+	for (x = 0; x < 8; x++) {
50f718a
+		Print(L"%08x: ", ((uint64_t)rsp) & 0xffffffff);
50f718a
+		Print(L"%016x ", *rsp++);
50f718a
+		Print(L"%016x ", *rsp++);
50f718a
+		Print(L"%016x ", *rsp++);
50f718a
+		Print(L"%016x\r\n", *rsp++);
50f718a
+	}
50f718a
+}
f338ca5
+#endif
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_failure_callback(void)
50f718a
+{
50f718a
+	return EFI_UNSUPPORTED;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_failure(void)
50f718a
+{
9f8dca1
+	return uefi_call_wrapper(test_failure_callback, 0);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call0_callback(void)
50f718a
+{
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call0(void)
50f718a
+{
9f8dca1
+	return uefi_call_wrapper(test_call0_callback, 0);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call1_callback(UINT32 a)
50f718a
+{
50f718a
+	if (a != 0x12345678) {
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	}
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call1(void)
50f718a
+{
9f8dca1
+	return uefi_call_wrapper(test_call1_callback, 1,0x12345678);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call2_callback(UINT32 a, UINT32 b)
50f718a
+{
50f718a
+	if (a != 0x12345678) {
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	}
50f718a
+	if (b != 0x23456789) {
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	}
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call2(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call2_callback, 2,
9f8dca1
+		0x12345678, 0x23456789);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call3_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call3(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call3_callback, 3,
50f718a
+		0x12345678, 0x23456789, 0x3456789a);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call4_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c, UINT32 d)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	if (d != 0x456789ab)
50f718a
+		return EFI_BAD_BUFFER_SIZE;
50f718a
+
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call4(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call4_callback, 4,
9f8dca1
+		0x12345678, 0x23456789, 0x3456789a, 0x456789ab);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call5_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c, UINT32 d, UINT32 e)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	if (d != 0x456789ab)
50f718a
+		return EFI_BAD_BUFFER_SIZE;
50f718a
+	if (e != 0x56789abc)
50f718a
+		return EFI_BUFFER_TOO_SMALL;
50f718a
+
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call5(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call5_callback, 5,
9f8dca1
+		0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call6_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c, UINT32 d, UINT32 e, UINT32 f)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	if (d != 0x456789ab)
50f718a
+		return EFI_BAD_BUFFER_SIZE;
50f718a
+	if (e != 0x56789abc)
50f718a
+		return EFI_BUFFER_TOO_SMALL;
50f718a
+	if (f != 0x6789abcd)
50f718a
+		return EFI_NOT_READY;
9f8dca1
+
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call6(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call6_callback, 6,
50f718a
+		0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc,
9f8dca1
+		0x6789abcd);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call7_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	if (d != 0x456789ab)
50f718a
+		return EFI_BAD_BUFFER_SIZE;
50f718a
+	if (e != 0x56789abc)
50f718a
+		return EFI_BUFFER_TOO_SMALL;
50f718a
+	if (f != 0x6789abcd)
50f718a
+		return EFI_NOT_READY;
50f718a
+	if (g != 0x789abcde)
50f718a
+		return EFI_DEVICE_ERROR;
9f8dca1
+
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call7(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call7_callback, 7,
50f718a
+		0x12345678, 0x23456789, 0x3456789a, 0x456789ab,
9f8dca1
+		0x56789abc, 0x6789abcd, 0x789abcde);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call8_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	if (d != 0x456789ab)
50f718a
+		return EFI_BAD_BUFFER_SIZE;
50f718a
+	if (e != 0x56789abc)
50f718a
+		return EFI_BUFFER_TOO_SMALL;
50f718a
+	if (f != 0x6789abcd)
50f718a
+		return EFI_NOT_READY;
50f718a
+	if (g != 0x789abcde)
50f718a
+		return EFI_DEVICE_ERROR;
50f718a
+	if (h != 0x89abcdef)
50f718a
+		return EFI_WRITE_PROTECTED;
50f718a
+
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call8(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call8_callback, 8,
50f718a
+		0x12345678,
50f718a
+		0x23456789,
50f718a
+		0x3456789a,
50f718a
+		0x456789ab,
50f718a
+		0x56789abc,
50f718a
+		0x6789abcd,
50f718a
+		0x789abcde,
9f8dca1
+		0x89abcdef);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS EFI_FUNCTION test_call9_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h, UINT32 i)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	if (d != 0x456789ab)
50f718a
+		return EFI_BAD_BUFFER_SIZE;
50f718a
+	if (e != 0x56789abc)
50f718a
+		return EFI_BUFFER_TOO_SMALL;
50f718a
+	if (f != 0x6789abcd)
50f718a
+		return EFI_NOT_READY;
50f718a
+	if (g != 0x789abcde)
50f718a
+		return EFI_DEVICE_ERROR;
50f718a
+	if (h != 0x89abcdef)
50f718a
+		return EFI_WRITE_PROTECTED;
50f718a
+	if (i != 0x9abcdef0)
50f718a
+		return EFI_OUT_OF_RESOURCES;
50f718a
+
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call9(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call9_callback, 9,
50f718a
+		0x12345678,
50f718a
+		0x23456789,
50f718a
+		0x3456789a,
50f718a
+		0x456789ab,
50f718a
+		0x56789abc,
50f718a
+		0x6789abcd,
50f718a
+		0x789abcde,
50f718a
+		0x89abcdef,
9f8dca1
+		0x9abcdef0);
50f718a
+}
50f718a
+
50f718a
+extern EFI_STATUS test_call10(void);
50f718a
+EFI_STATUS EFI_FUNCTION test_call10_callback(UINT32 a, UINT32 b,
50f718a
+	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h, UINT32 i,
50f718a
+	UINT32 j)
50f718a
+{
50f718a
+	if (a != 0x12345678)
50f718a
+		return EFI_LOAD_ERROR;
50f718a
+	if (b != 0x23456789)
50f718a
+		return EFI_INVALID_PARAMETER;
50f718a
+	if (c != 0x3456789a)
50f718a
+		return EFI_UNSUPPORTED;
50f718a
+	if (d != 0x456789ab)
50f718a
+		return EFI_BAD_BUFFER_SIZE;
50f718a
+	if (e != 0x56789abc)
50f718a
+		return EFI_BUFFER_TOO_SMALL;
50f718a
+	if (f != 0x6789abcd)
50f718a
+		return EFI_NOT_READY;
50f718a
+	if (g != 0x789abcde)
50f718a
+		return EFI_DEVICE_ERROR;
50f718a
+	if (h != 0x89abcdef)
50f718a
+		return EFI_WRITE_PROTECTED;
50f718a
+	if (i != 0x9abcdef0)
50f718a
+		return EFI_OUT_OF_RESOURCES;
50f718a
+	if (j != 0xabcdef01)
50f718a
+		return EFI_VOLUME_CORRUPTED;
50f718a
+
50f718a
+	return EFI_SUCCESS;
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS test_call10(void)
50f718a
+{
50f718a
+	return uefi_call_wrapper(test_call10_callback, 10,
50f718a
+		0x12345678,
50f718a
+		0x23456789,
50f718a
+		0x3456789a,
50f718a
+		0x456789ab,
50f718a
+		0x56789abc,
50f718a
+		0x6789abcd,
50f718a
+		0x789abcde,
50f718a
+		0x89abcdef,
50f718a
+		0x9abcdef0,
9f8dca1
+		0xabcdef01);
50f718a
+}
50f718a
+
50f718a
+EFI_STATUS
50f718a
+efi_main (EFI_HANDLE *image, EFI_SYSTEM_TABLE *systab)
50f718a
+{
50f718a
+	EFI_STATUS rc = EFI_SUCCESS;
50f718a
+
50f718a
+	InitializeLib(image, systab);
50f718a
+	PoolAllocationType = 2; /* klooj */
50f718a
+
50f718a
+#ifndef __x86_64__
50f718a
+	uefi_call_wrapper(systab->ConOut->OutputString, 2, systab->ConOut,
50f718a
+		L"This test is only valid on x86_64\n");
50f718a
+	return EFI_UNSUPPORTED;
50f718a
+#endif
50f718a
+
50f718a
+	asm volatile("out %0,%1" : : "a" ((uint8_t)0x14), "dN" (0x80));
50f718a
+
50f718a
+	Print(L"Hello\r\n");
50f718a
+	rc = test_failure();
50f718a
+	if (EFI_ERROR(rc)) {
50f718a
+		Print(L"Returning Failure works\n");
50f718a
+	} else {
50f718a
+		Print(L"Returning failure doesn't work.\r\n");
50f718a
+		Print(L"%%rax was 0x%016x, should have been 0x%016x\n",
50f718a
+			rc, EFI_UNSUPPORTED);
50f718a
+		return EFI_INVALID_PARAMETER;
9f8dca1
+	}
50f718a
+
50f718a
+	rc = test_call0();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"0 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"0 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call1();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"1 arg works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"1 arg failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call2();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"2 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"2 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call3();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"3 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"3 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call4();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"4 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"4 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call5();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"5 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"5 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call6();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"6 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"6 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call7();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"7 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"7 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call8();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"8 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"8 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call9();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"9 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"9 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	rc = test_call10();
50f718a
+	if (!EFI_ERROR(rc)) {
50f718a
+		Print(L"10 args works just fine here.\r\n");
50f718a
+	} else {
50f718a
+		Print(L"10 args failed: 0x%016x\n", rc);
50f718a
+		return rc;
50f718a
+	}
50f718a
+
50f718a
+	return rc;
50f718a
+}
50f718a
-- 
50f718a
1.7.6
50f718a