Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/common/libc/arch/mips/setjmp.S
2096 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* setjmp and longjmp for MIPS.
32
*/
33
34
#include <kern/mips/regdefs.h>
35
36
.text
37
.set noreorder
38
39
/*
40
* int setjmp(jmp_buf jb);
41
*
42
* Save the current state so we can return again from the call later
43
* if/when longjmp is called. (If the function that called setjmp
44
* returns before longjmp is called, the results are undefined. We
45
* only need to save registers, not the whole contents of the stack.)
46
*/
47
48
.globl setjmp
49
.type setjmp,@function
50
.ent setjmp
51
setjmp:
52
/*
53
* jmp_buf is in a0. We need to save s0-s8, sp, and ra in it.
54
* Don't store more registers without adjusting machine/setjmp.h.
55
*/
56
57
sw sp, 0(a0) /* save registers */
58
sw ra, 4(a0)
59
sw s0, 8(a0)
60
sw s1, 12(a0)
61
sw s2, 16(a0)
62
sw s3, 20(a0)
63
sw s4, 24(a0)
64
sw s5, 28(a0)
65
sw s6, 32(a0)
66
sw s7, 36(a0)
67
sw s8, 40(a0)
68
69
j ra /* done */
70
li v0, 0 /* return 0 (in delay slot) */
71
.end setjmp
72
73
74
/*
75
* void longjmp(jmp_buf jb, int code);
76
*/
77
.globl longjmp
78
.type longjmp,@function
79
.ent longjmp
80
longjmp:
81
/*
82
* jmp_buf is in a0. Return code is in a1.
83
* We need to restore s0-s8, sp, and ra from the jmp_buf.
84
* The return code is forced to 1 if 0 is passed in.
85
*/
86
87
sltiu t0, a1, 1 /* set t0 to 1 if return code is 0... otherwise 0 */
88
addu a1, a1, t0 /* update the return code */
89
90
lw sp, 0(a0) /* restore registers */
91
lw ra, 4(a0)
92
lw s0, 8(a0)
93
lw s1, 12(a0)
94
lw s2, 16(a0)
95
lw s3, 20(a0)
96
lw s4, 24(a0)
97
lw s5, 28(a0)
98
lw s6, 32(a0)
99
lw s7, 36(a0)
100
lw s8, 40(a0)
101
102
j ra /* return, to where setjmp was called from */
103
move v0, a1 /* set return value */
104
.end longjmp
105
106