1/* This is file is a merger of SETJMP.S and LONGJMP.S */
2/*
3 *  This file was modified to use the __USER_LABEL_PREFIX__ and
4 *  __REGISTER_PREFIX__ macros defined by later versions of GNU cpp by
5 *  Joel Sherrill (joel@OARcorp.com)
6 *  Slight change: now includes i386mach.h for this (Werner Almesberger)
7 *
8 * Copyright (C) 1991 DJ Delorie
9 * All rights reserved.
10 *
11 * Redistribution, modification, and use in source and binary forms is permitted
12 * provided that the above copyright notice and following paragraph are
13 * duplicated in all such forms.
14 *
15 * This file is distributed WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 */
18
19 /*
20 **	jmp_buf:
21 **	 eax ebx ecx edx esi edi ebp esp eip
22 **	 0   4   8   12  16  20  24  28  32
23 **
24 **	Intel MCU jmp_buf:
25 **	 ebx esi edi ebp esp eip
26 **	 0   4   8   12  16  20
27 */
28
29#include <picolibc.h>
30
31       #include "i386mach.h"
32
33        .global SYM (setjmp)
34        .global SYM (longjmp)
35       SOTYPE_FUNCTION(setjmp)
36       SOTYPE_FUNCTION(longjmp)
37
38SYM (setjmp):
39
40#ifdef __iamcu__
41	/* Store EIP.  */
42	movl	0(esp),ecx
43	movl	ecx,20(eax)
44
45	movl	ebx,0 (eax)
46	movl	esi,4 (eax)
47	movl	edi,8 (eax)
48	movl	ebp,12(eax)
49
50	/* Skip return address, which will be pushed onto stack in
51	   longjmp, and store SP.  */
52	leal	4(esp),ecx
53	movl	ecx,16(eax)
54
55	xorl	eax,eax
56#else
57	pushl	ebp
58	movl	esp,ebp
59
60	pushl	edi
61	movl	8 (ebp),edi
62
63	movl	eax,0 (edi)
64	movl	ebx,4 (edi)
65	movl	ecx,8 (edi)
66	movl	edx,12 (edi)
67	movl	esi,16 (edi)
68
69	movl	-4 (ebp),eax
70	movl	eax,20 (edi)
71
72	movl	0 (ebp),eax
73	movl	eax,24 (edi)
74
75	movl	esp,eax
76	addl	$12,eax
77	movl	eax,28 (edi)
78
79	movl	4 (ebp),eax
80	movl	eax,32 (edi)
81
82	popl	edi
83	movl	$0,eax
84	leave
85#endif
86	ret
87
88SYM (longjmp):
89#ifdef __iamcu__
90	/* Check retval.  */
91	testl	edx,edx
92	jne	0f
93	incl	edx
940:
95	/* Restore stack first.  */
96	movl	16(eax),esp
97
98	/* Put return address on stack.  */
99	pushl	20(eax)
100
101	movl	0(eax),ebx
102	movl	4(eax),esi
103	movl	8(eax),edi
104	movl	12(eax),ebp
105	movl	edx,eax
106#else
107	pushl	ebp
108	movl	esp,ebp
109
110	movl	8(ebp),edi	/* get jmp_buf */
111	movl	12(ebp),eax	/* store retval in j->eax */
112	testl	eax,eax
113	jne	0f
114	incl	eax
1150:
116	movl	eax,0(edi)
117
118	movl	24(edi),ebp
119
120       __CLI
121	movl	28(edi),esp
122
123	pushl	32(edi)
124
125	movl	0(edi),eax
126	movl	4(edi),ebx
127	movl	8(edi),ecx
128	movl	12(edi),edx
129	movl	16(edi),esi
130	movl	20(edi),edi
131       __STI
132#endif
133
134	ret
135
136#if defined(__linux__) && defined(__ELF__)
137.section .note.GNU-stack,"",%progbits
138#endif
139