1/*	$NetBSD: tcc.c,v 1.1.1.2 2018/08/16 18:17:47 jmcneill Exp $	*/
2
3/*
4 * Test if our calling convention gymnastics actually work
5 */
6
7#include <efi.h>
8#include <efilib.h>
9
10#if 0
11extern void dump_stack(void);
12asm(	".globl	dump_stack\n"
13	"dump_stack:\n"
14	"	movq %rsp, %rdi\n"
15	"	jmp *dump_stack_helper@GOTPCREL(%rip)\n"
16	".size	dump_stack, .-dump_stack");
17
18void dump_stack_helper(uint64_t rsp_val)
19{
20	uint64_t *rsp = (uint64_t *)rsp_val;
21	int x;
22
23	Print(L"%%rsp: 0x%08x%08x stack:\r\n",
24					(rsp_val & 0xffffffff00000000) >>32,
25					 rsp_val & 0xffffffff);
26	for (x = 0; x < 8; x++) {
27		Print(L"%08x: ", ((uint64_t)rsp) & 0xffffffff);
28		Print(L"%016x ", *rsp++);
29		Print(L"%016x ", *rsp++);
30		Print(L"%016x ", *rsp++);
31		Print(L"%016x\r\n", *rsp++);
32	}
33}
34#endif
35
36EFI_STATUS EFI_FUNCTION test_failure_callback(void)
37{
38	return EFI_UNSUPPORTED;
39}
40
41EFI_STATUS test_failure(void)
42{
43	return uefi_call_wrapper(test_failure_callback, 0);
44}
45
46EFI_STATUS EFI_FUNCTION test_call0_callback(void)
47{
48	return EFI_SUCCESS;
49}
50
51EFI_STATUS test_call0(void)
52{
53	return uefi_call_wrapper(test_call0_callback, 0);
54}
55
56EFI_STATUS EFI_FUNCTION test_call1_callback(UINT32 a)
57{
58	if (a != 0x12345678) {
59		return EFI_LOAD_ERROR;
60	}
61	return EFI_SUCCESS;
62}
63
64EFI_STATUS test_call1(void)
65{
66	return uefi_call_wrapper(test_call1_callback, 1,0x12345678);
67}
68
69EFI_STATUS EFI_FUNCTION test_call2_callback(UINT32 a, UINT32 b)
70{
71	if (a != 0x12345678) {
72		return EFI_LOAD_ERROR;
73	}
74	if (b != 0x23456789) {
75		return EFI_INVALID_PARAMETER;
76	}
77	return EFI_SUCCESS;
78}
79
80EFI_STATUS test_call2(void)
81{
82	return uefi_call_wrapper(test_call2_callback, 2,
83		0x12345678, 0x23456789);
84}
85
86EFI_STATUS EFI_FUNCTION test_call3_callback(UINT32 a, UINT32 b,
87	UINT32 c)
88{
89	if (a != 0x12345678)
90		return EFI_LOAD_ERROR;
91	if (b != 0x23456789)
92		return EFI_INVALID_PARAMETER;
93	if (c != 0x3456789a)
94		return EFI_UNSUPPORTED;
95	return EFI_SUCCESS;
96}
97
98EFI_STATUS test_call3(void)
99{
100	return uefi_call_wrapper(test_call3_callback, 3,
101		0x12345678, 0x23456789, 0x3456789a);
102}
103
104EFI_STATUS EFI_FUNCTION test_call4_callback(UINT32 a, UINT32 b,
105	UINT32 c, UINT32 d)
106{
107	if (a != 0x12345678)
108		return EFI_LOAD_ERROR;
109	if (b != 0x23456789)
110		return EFI_INVALID_PARAMETER;
111	if (c != 0x3456789a)
112		return EFI_UNSUPPORTED;
113	if (d != 0x456789ab)
114		return EFI_BAD_BUFFER_SIZE;
115
116	return EFI_SUCCESS;
117}
118
119EFI_STATUS test_call4(void)
120{
121	return uefi_call_wrapper(test_call4_callback, 4,
122		0x12345678, 0x23456789, 0x3456789a, 0x456789ab);
123}
124
125EFI_STATUS EFI_FUNCTION test_call5_callback(UINT32 a, UINT32 b,
126	UINT32 c, UINT32 d, UINT32 e)
127{
128	if (a != 0x12345678)
129		return EFI_LOAD_ERROR;
130	if (b != 0x23456789)
131		return EFI_INVALID_PARAMETER;
132	if (c != 0x3456789a)
133		return EFI_UNSUPPORTED;
134	if (d != 0x456789ab)
135		return EFI_BAD_BUFFER_SIZE;
136	if (e != 0x56789abc)
137		return EFI_BUFFER_TOO_SMALL;
138
139	return EFI_SUCCESS;
140}
141
142EFI_STATUS test_call5(void)
143{
144	return uefi_call_wrapper(test_call5_callback, 5,
145		0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc);
146}
147
148EFI_STATUS EFI_FUNCTION test_call6_callback(UINT32 a, UINT32 b,
149	UINT32 c, UINT32 d, UINT32 e, UINT32 f)
150{
151	if (a != 0x12345678)
152		return EFI_LOAD_ERROR;
153	if (b != 0x23456789)
154		return EFI_INVALID_PARAMETER;
155	if (c != 0x3456789a)
156		return EFI_UNSUPPORTED;
157	if (d != 0x456789ab)
158		return EFI_BAD_BUFFER_SIZE;
159	if (e != 0x56789abc)
160		return EFI_BUFFER_TOO_SMALL;
161	if (f != 0x6789abcd)
162		return EFI_NOT_READY;
163
164	return EFI_SUCCESS;
165}
166
167EFI_STATUS test_call6(void)
168{
169	return uefi_call_wrapper(test_call6_callback, 6,
170		0x12345678, 0x23456789, 0x3456789a, 0x456789ab, 0x56789abc,
171		0x6789abcd);
172}
173
174EFI_STATUS EFI_FUNCTION test_call7_callback(UINT32 a, UINT32 b,
175	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g)
176{
177	if (a != 0x12345678)
178		return EFI_LOAD_ERROR;
179	if (b != 0x23456789)
180		return EFI_INVALID_PARAMETER;
181	if (c != 0x3456789a)
182		return EFI_UNSUPPORTED;
183	if (d != 0x456789ab)
184		return EFI_BAD_BUFFER_SIZE;
185	if (e != 0x56789abc)
186		return EFI_BUFFER_TOO_SMALL;
187	if (f != 0x6789abcd)
188		return EFI_NOT_READY;
189	if (g != 0x789abcde)
190		return EFI_DEVICE_ERROR;
191
192	return EFI_SUCCESS;
193}
194
195EFI_STATUS test_call7(void)
196{
197	return uefi_call_wrapper(test_call7_callback, 7,
198		0x12345678, 0x23456789, 0x3456789a, 0x456789ab,
199		0x56789abc, 0x6789abcd, 0x789abcde);
200}
201
202EFI_STATUS EFI_FUNCTION test_call8_callback(UINT32 a, UINT32 b,
203	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h)
204{
205	if (a != 0x12345678)
206		return EFI_LOAD_ERROR;
207	if (b != 0x23456789)
208		return EFI_INVALID_PARAMETER;
209	if (c != 0x3456789a)
210		return EFI_UNSUPPORTED;
211	if (d != 0x456789ab)
212		return EFI_BAD_BUFFER_SIZE;
213	if (e != 0x56789abc)
214		return EFI_BUFFER_TOO_SMALL;
215	if (f != 0x6789abcd)
216		return EFI_NOT_READY;
217	if (g != 0x789abcde)
218		return EFI_DEVICE_ERROR;
219	if (h != 0x89abcdef)
220		return EFI_WRITE_PROTECTED;
221
222	return EFI_SUCCESS;
223}
224
225EFI_STATUS test_call8(void)
226{
227	return uefi_call_wrapper(test_call8_callback, 8,
228		0x12345678,
229		0x23456789,
230		0x3456789a,
231		0x456789ab,
232		0x56789abc,
233		0x6789abcd,
234		0x789abcde,
235		0x89abcdef);
236}
237
238EFI_STATUS EFI_FUNCTION test_call9_callback(UINT32 a, UINT32 b,
239	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h, UINT32 i)
240{
241	if (a != 0x12345678)
242		return EFI_LOAD_ERROR;
243	if (b != 0x23456789)
244		return EFI_INVALID_PARAMETER;
245	if (c != 0x3456789a)
246		return EFI_UNSUPPORTED;
247	if (d != 0x456789ab)
248		return EFI_BAD_BUFFER_SIZE;
249	if (e != 0x56789abc)
250		return EFI_BUFFER_TOO_SMALL;
251	if (f != 0x6789abcd)
252		return EFI_NOT_READY;
253	if (g != 0x789abcde)
254		return EFI_DEVICE_ERROR;
255	if (h != 0x89abcdef)
256		return EFI_WRITE_PROTECTED;
257	if (i != 0x9abcdef0)
258		return EFI_OUT_OF_RESOURCES;
259
260	return EFI_SUCCESS;
261}
262
263EFI_STATUS test_call9(void)
264{
265	return uefi_call_wrapper(test_call9_callback, 9,
266		0x12345678,
267		0x23456789,
268		0x3456789a,
269		0x456789ab,
270		0x56789abc,
271		0x6789abcd,
272		0x789abcde,
273		0x89abcdef,
274		0x9abcdef0);
275}
276
277extern EFI_STATUS test_call10(void);
278EFI_STATUS EFI_FUNCTION test_call10_callback(UINT32 a, UINT32 b,
279	UINT32 c, UINT32 d, UINT32 e, UINT32 f, UINT32 g, UINT32 h, UINT32 i,
280	UINT32 j)
281{
282	if (a != 0x12345678)
283		return EFI_LOAD_ERROR;
284	if (b != 0x23456789)
285		return EFI_INVALID_PARAMETER;
286	if (c != 0x3456789a)
287		return EFI_UNSUPPORTED;
288	if (d != 0x456789ab)
289		return EFI_BAD_BUFFER_SIZE;
290	if (e != 0x56789abc)
291		return EFI_BUFFER_TOO_SMALL;
292	if (f != 0x6789abcd)
293		return EFI_NOT_READY;
294	if (g != 0x789abcde)
295		return EFI_DEVICE_ERROR;
296	if (h != 0x89abcdef)
297		return EFI_WRITE_PROTECTED;
298	if (i != 0x9abcdef0)
299		return EFI_OUT_OF_RESOURCES;
300	if (j != 0xabcdef01)
301		return EFI_VOLUME_CORRUPTED;
302
303	return EFI_SUCCESS;
304}
305
306EFI_STATUS test_call10(void)
307{
308	return uefi_call_wrapper(test_call10_callback, 10,
309		0x12345678,
310		0x23456789,
311		0x3456789a,
312		0x456789ab,
313		0x56789abc,
314		0x6789abcd,
315		0x789abcde,
316		0x89abcdef,
317		0x9abcdef0,
318		0xabcdef01);
319}
320
321EFI_STATUS
322efi_main (EFI_HANDLE *image, EFI_SYSTEM_TABLE *systab)
323{
324	EFI_STATUS rc = EFI_SUCCESS;
325
326	InitializeLib(image, systab);
327	PoolAllocationType = 2; /* klooj */
328
329#ifdef __x86_64__
330	__asm__ volatile("out %0,%1" : : "a" ((uint8_t)0x14), "dN" (0x80));
331#endif
332
333	Print(L"Hello\r\n");
334	rc = test_failure();
335	if (EFI_ERROR(rc)) {
336		Print(L"Returning Failure works\n");
337	} else {
338		Print(L"Returning failure doesn't work.\r\n");
339		Print(L"%%rax was 0x%016x, should have been 0x%016x\n",
340			rc, EFI_UNSUPPORTED);
341		return EFI_INVALID_PARAMETER;
342	}
343
344	rc = test_call0();
345	if (!EFI_ERROR(rc)) {
346		Print(L"0 args works just fine here.\r\n");
347	} else {
348		Print(L"0 args failed: 0x%016x\n", rc);
349		return rc;
350	}
351
352	rc = test_call1();
353	if (!EFI_ERROR(rc)) {
354		Print(L"1 arg works just fine here.\r\n");
355	} else {
356		Print(L"1 arg failed: 0x%016x\n", rc);
357		return rc;
358	}
359
360	rc = test_call2();
361	if (!EFI_ERROR(rc)) {
362		Print(L"2 args works just fine here.\r\n");
363	} else {
364		Print(L"2 args failed: 0x%016x\n", rc);
365		return rc;
366	}
367
368	rc = test_call3();
369	if (!EFI_ERROR(rc)) {
370		Print(L"3 args works just fine here.\r\n");
371	} else {
372		Print(L"3 args failed: 0x%016x\n", rc);
373		return rc;
374	}
375
376	rc = test_call4();
377	if (!EFI_ERROR(rc)) {
378		Print(L"4 args works just fine here.\r\n");
379	} else {
380		Print(L"4 args failed: 0x%016x\n", rc);
381		return rc;
382	}
383
384	rc = test_call5();
385	if (!EFI_ERROR(rc)) {
386		Print(L"5 args works just fine here.\r\n");
387	} else {
388		Print(L"5 args failed: 0x%016x\n", rc);
389		return rc;
390	}
391
392	rc = test_call6();
393	if (!EFI_ERROR(rc)) {
394		Print(L"6 args works just fine here.\r\n");
395	} else {
396		Print(L"6 args failed: 0x%016x\n", rc);
397		return rc;
398	}
399
400	rc = test_call7();
401	if (!EFI_ERROR(rc)) {
402		Print(L"7 args works just fine here.\r\n");
403	} else {
404		Print(L"7 args failed: 0x%016x\n", rc);
405		return rc;
406	}
407
408	rc = test_call8();
409	if (!EFI_ERROR(rc)) {
410		Print(L"8 args works just fine here.\r\n");
411	} else {
412		Print(L"8 args failed: 0x%016x\n", rc);
413		return rc;
414	}
415
416	rc = test_call9();
417	if (!EFI_ERROR(rc)) {
418		Print(L"9 args works just fine here.\r\n");
419	} else {
420		Print(L"9 args failed: 0x%016x\n", rc);
421		return rc;
422	}
423
424	rc = test_call10();
425	if (!EFI_ERROR(rc)) {
426		Print(L"10 args works just fine here.\r\n");
427	} else {
428		Print(L"10 args failed: 0x%016x\n", rc);
429		return rc;
430	}
431
432	return rc;
433}
434