4 * Copyright (c) 2003,2005 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
40 * By Harald Barth <haba@stacken.kth.se> after looking
41 * at Derek Atkins' i386 routines and realizing that
42 * there were some differences and it was not enough
43 * just renaming the registers.
46 #ifdef HAVE_MACHINE_ASM_H
47 #include <machine/asm.h>
65 * savecontext(int (*f)(), struct savearea *area1, char *newsp)
69 * In spite of passing arguments in registers, gcc first copies the content of the
70 * registers onto the stack. I do not know why gcc does this, but for now I mimic
71 * gcc's behaviour. Here are the offsets the arguments are copied to.
77 .globl _C_LABEL(PRE_Block)
78 .globl _C_LABEL(savecontext)
81 pushq %rbp /* The frame setup is just like gcc */
83 subq $32, %rsp /* make room for args on the stack */
84 movq %rdi, f(%rbp) /* (3*8=24 but increments seem to */
85 movq %rsi, area1(%rbp) /* i multiples of 24, so 32 it is) */
86 movq %rdx, newsp(%rbp) /* and copy them there. */
88 movl $1,_C_LABEL(PRE_Block) /* Do not allow any interrupts */
90 pushq %rsp /* Push all registers onto the stack */
91 pushq %rax /* Probably not _all_ are necessary */
92 pushq %rcx /* but better one too much than one */
93 pushq %rdx /* forgotten */
105 pushq %r15 /* Btw, the pusha instruction is no more */
107 movq area1(%rbp),%rax /* rax = base of savearea */
108 movq %rsp,topstack(%rax) /* area->topstack = rsp */
109 movq newsp(%rbp),%rax /* rax = new sp */
111 je L1 /* if new sp is 0 then dont change rsp */
112 movq %rax,%rsp /* Change rsp to the new sp */
114 jmp *f(%rbp) /* jump to function pointer passed in arg */
116 /* Shouldnt be here....*/
120 * returnto(struct savearea *area2)
123 /* Offset where we put arg - se savecontext */
126 .globl _C_LABEL(returnto)
129 pushq %rbp /* New frame, gcc style */
130 movq %rsp, %rbp /* See savecontext above */
131 subq $16, %rsp /* Make room for 2 args */
132 movq %rdi, area2(%rbp) /* use room to copy 1 arg */
133 movq area2(%rbp),%rax /* rax = area2 */
134 movq topstack(%rax),%rsp /* restore rsp from place relative rbp*/
136 popq %r15 /* Restore regs */
151 popq %rsp /* See savecontext */
153 movl $0,_C_LABEL(PRE_Block) /* clear it up... */
154 addq $32, %rsp /* We did rsp-32 above, correct that */
158 /* We never should get here, put in emergency brake as in i386 code */