4 * Copyright (c) 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * By Harald Barth <haba@stacken.kth.se> after looking
43 * at Derek Atkins' i386 routines and realizing that
44 * there were some differences and it was not enough
45 * just renaming the registers.
48 #ifdef HAVE_MACHINE_ASM_H
49 #include <machine/asm.h>
67 * savecontext(int (*f)(), struct savearea *area1, char *newsp)
71 * In spite of passing arguments in registers, gcc first copies the content of the
72 * registers onto the stack. I do not know why gcc does this, but for now I mimic
73 * gcc's behaviour. Here are the offsets the arguments are copied to.
79 .globl _C_LABEL(PRE_Block)
80 .globl _C_LABEL(savecontext)
83 pushq %rbp /* The frame setup is just like gcc */
85 subq $32, %rsp /* make room for args on the stack */
86 movq %rdi, f(%rbp) /* (3*8=24 but increments seem to */
87 movq %rsi, area1(%rbp) /* i multiples of 24, so 32 it is) */
88 movq %rdx, newsp(%rbp) /* and copy them there. */
90 movl $1,_C_LABEL(PRE_Block) /* Do not allow any interrupts */
92 pushq %rsp /* Push all registers onto the stack */
93 pushq %rax /* Probably not _all_ are necessary */
94 pushq %rcx /* but better one too much than one */
95 pushq %rdx /* forgotten */
107 pushq %r15 /* Btw, the pusha instruction is no more */
109 movq area1(%rbp),%rax /* rax = base of savearea */
110 movq %rsp,topstack(%rax) /* area->topstack = rsp */
111 movq newsp(%rbp),%rax /* rax = new sp */
113 je L1 /* if new sp is 0 then dont change rsp */
114 movq %rax,%rsp /* Change rsp to the new sp */
116 jmp *f(%rbp) /* jump to function pointer passed in arg */
118 /* Shouldnt be here....*/
122 * returnto(struct savearea *area2)
125 /* Offset where we put arg - se savecontext */
128 .globl _C_LABEL(returnto)
131 pushq %rbp /* New frame, gcc style */
132 movq %rsp, %rbp /* See savecontext above */
133 subq $16, %rsp /* Make room for 2 args */
134 movq %rdi, area2(%rbp) /* use room to copy 1 arg */
135 movq area2(%rbp),%rax /* rax = area2 */
136 movq topstack(%rax),%rsp /* restore rsp from place relative rbp*/
138 popq %r15 /* Restore regs */
153 popq %rsp /* See savecontext */
155 movl $0,_C_LABEL(PRE_Block) /* clear it up... */
156 addq $32, %rsp /* We did rsp-32 above, correct that */
160 /* We never should get here, put in emergency brake as in i386 code */