1/*
2            Copyright Martin Husemann 2013.
3   Distributed under the Boost Software License, Version 1.0.
4      (See accompanying file LICENSE_1_0.txt or copy at
5          http://www.boost.org/LICENSE_1_0.txt)
6*/
7
8/*******************************************************************
9 *                                                                 *
10 *  -------------------------------------------------------------  *
11 *  |  Offset (in 4 or 8 byte units) | Content                  |  *
12 *  -------------------------------------------------------------  *
13 *  | 0                              | %sp                      |  *
14 *  -------------------------------------------------------------  *
15 *  | 1                              | %pc                      |  *
16 *  -------------------------------------------------------------  *
17 *  | 2                              | %i7 (return address)     |  *
18 *  -------------------------------------------------------------  *
19 *  | 3                              | %g1                      |  *
20 *  -------------------------------------------------------------  *
21 *  | 4                              | %g2                      |  *
22 *  -------------------------------------------------------------  *
23 *  | 5                              | %g3                      |  *
24 *  -------------------------------------------------------------  *
25 *  | 6                              | %g6                      |  *
26 *  -------------------------------------------------------------  *
27 *  | 7                              | %g7                      |  *
28 *  -------------------------------------------------------------  *
29 *    The local and in registers are stored on the stack.          *
30 *******************************************************************/
31
32#define	OFF(N)	(8*(N))
33#define	CCFSZ	176		// C Compiler Frame Size
34#define	BIAS	(2048-1)	// Stack offset for 64 bit programs
35#define	FC_SZ	448		// sizeof(fcontext_t)
36#define	FC_STK	384		// offsetof(fcontext_t, fc_stack)
37#define	FC_FPU	0		// offsetof(fcontext_t, fc_fp)
38#define	FC_FSR	264		// offsetof(fcontext_t, fc_fp.fp_fsr)
39#define	FC_FPRS	256		// offsetof(fcontext_t, fc_fp.fp_fprs)
40#define	FC_GREG	320		// offsetof(fcontext_t, fc_greg)
41#define	BLOCK_SIZE	64
42
43	.register %g2,#ignore
44	.register %g3,#ignore
45	.register %g6,#ignore
46
47.text
48.globl make_fcontext
49.align 4
50.type make_fcontext,@function
51// fcontext_t *
52// make_fcontext( void * sp, std::size_t size, void (* fn)( intptr_t) )
53make_fcontext:
54    save %sp, -CCFSZ, %sp
55    // %i0 initial stack pointer
56    // %i1 stack size limit
57    // %i2 function pointer for context start function
58
59    sub %i0, FC_SZ, %i4		// allocate fcontext_t at on the new stack and keep pointer as return value
60    andn %i4, BLOCK_SIZE-1, %i5	// force block ops usable alignement and keep pointer to fcontext in %i5
61
62    stx %i0, [%i5+FC_STK+OFF(0)]	// save fs_stack.sp
63    stx %i1, [%i5+FC_STK+OFF(1)]	// save fs_stack.size
64    sub %i5, CCFSZ+BIAS, %o1	   	// leave space for one register window (and offset stack for 64bit)
65    stx %o1, [%i5+FC_GREG+OFF(0)]	// save new stack pointer
66    stx %i2, [%i5+FC_GREG+OFF(1)]	// save new %pc (function pointer)
67    stx %g1, [%i5+FC_GREG+OFF(3)]
68    stx %g2, [%i5+FC_GREG+OFF(4)]
69    stx %g3, [%i5+FC_GREG+OFF(5)]
70    stx %g6, [%i5+FC_GREG+OFF(6)]
71    stx %g7, [%i5+FC_GREG+OFF(7)]
72
73    // synthesize "return address": jump to finish
741:  rd %pc, %i4
75    add %i4, finish-1b-8, %i4
76    stx %i4, [%i5+FC_GREG+OFF(2)]
77
78    ret
79    restore %g0, %i5, %o0	// return fcontext_t
80
81finish:
82    mov %g0, %o0
83    call _exit
84     nop
85
86.size make_fcontext,.-make_fcontext
87
88/* Mark that we don't need executable stack.  */
89.section .note.GNU-stack,"",%progbits
90