1/*
2 * QuickThreads -- Threads-building toolkit.
3 * Copyright (c) 1993 by David Keppel
4 *
5 * Permission to use, copy, modify and distribute this software and
6 * its documentation for any purpose and without fee is hereby
7 * granted, provided that the above copyright notice and this notice
8 * appear in all copies.  This software is provided as a
9 * proof-of-concept and for demonstration purposes; there is no
10 * representation about the suitability of this software for any
11 * purpose.
12 */
13
14#ifndef QUICKTHREADS_X86_64_H
15#define QUICKTHREADS_X86_64_H
16
17typedef unsigned long qt_word_t;
18
19/* Thread's initial stack layout on the iX86_64:
20
21   non-varargs:
22
23   +---
24   | arg[2]	=== `userf' on startup
25   | arg[1]	=== `pt' on startup
26   | arg[0]	=== `pu' on startup
27   +---
28   | ret pc === qt_error
29   +---
30   | ret pc	=== `only' on startup
31   +---
32   | %ebp
33   | %esi
34   | %edi
35   | %ebx				<--- qt_t.sp
36   +---
37
38   When a non-varargs thread is started, it ``returns'' directly to
39   the client's `only' function.
40
41   varargs:
42
43   +---
44   | arg[n-1]
45   | ..
46   | arg[0]
47   +---
48   | ret pc	=== `qt_vstart'
49   +---
50   | %ebp	=== `startup'
51   | %esi	=== `cleanup'
52   | %edi	=== `pt'
53   | %ebx	=== `vuserf'		<--- qt_t.sp
54   +---
55
56   When a varargs thread is started, it ``returns'' to the `qt_vstart'
57   startup code.  The startup code calls the appropriate functions. */
58
59
60/* What to do to start a varargs thread running. */
61extern void qt_vstart (void);
62
63
64/* Hold four return pcs (qt_error, qt_start and twice qt_align)
65   plus ten args and two qt_word_t's for correct alignment. */
66#define QUICKTHREADS_STKBASE	(16 * sizeof(long))
67
68/* Hold 4 saved regs plus one return pc (qt_vstart). */
69#define QUICKTHREADS_VSTKBASE	(5 * sizeof(long))
70
71
72/* Stack must be 16-byte aligned at function call instr. (SSE data support) */
73#define QUICKTHREADS_STKALIGN	(16)
74
75
76/* Where to place various arguments. */
77#define QUICKTHREADS_ONLY_INDEX	(QUICKTHREADS_PC)
78#define QUICKTHREADS_USER_INDEX	(QUICKTHREADS_ARG2)
79#define QUICKTHREADS_ARGT_INDEX	(QUICKTHREADS_ARG1)
80#define QUICKTHREADS_ARGU_INDEX	(QUICKTHREADS_ARG0)
81
82#define QUICKTHREADS_VSTARTUP_INDEX	(QUICKTHREADS_EBP)
83#define QUICKTHREADS_VUSERF_INDEX	(QUICKTHREADS_EBX)
84#define QUICKTHREADS_VCLEANUP_INDEX	(QUICKTHREADS_ESI)
85#define QUICKTHREADS_VARGT_INDEX	(QUICKTHREADS_EDI)
86
87
88/* Stack layout offsets relative to stack at initial stack setup. */
89
90/* Stack alignment         15 */
91#define QUICKTHREADS_RPC   14
92#define QUICKTHREADS_POP0  13
93#define QUICKTHREADS_PC    12
94#define QUICKTHREADS_POP1  11
95#define QUICKTHREADS_RBP   10
96/* Stack alignment          9 */
97#define QUICKTHREADS_R12    8
98#define QUICKTHREADS_R13    7
99#define QUICKTHREADS_R14    6
100#define QUICKTHREADS_R15    5
101#define QUICKTHREADS_RBX    4
102#define QUICKTHREADS_RCX    3
103#define QUICKTHREADS_RDX    2
104#define QUICKTHREADS_RDI    1
105#define QUICKTHREADS_RSI    0
106
107
108/* Arguments to save stack function. */
109
110#define QUICKTHREADS_ARG0 QUICKTHREADS_RDI
111#define QUICKTHREADS_ARG1 QUICKTHREADS_RSI
112#define QUICKTHREADS_ARG2 QUICKTHREADS_RDX
113
114
115/* Stack grows down.  The top of the stack is the first thing to
116   pop off (preincrement, postdecrement). */
117#define QUICKTHREADS_GROW_DOWN
118
119extern void qt_error (void);
120extern void qt_align (void); /* For correct stack alignment */
121
122/* Push on the error return address, force Frame Pointer to 0 and
123   push stack alignment trampoline function.  */
124#define QUICKTHREADS_ARGS_MD(sto) \
125  (QUICKTHREADS_SPUT (sto, QUICKTHREADS_RBP, 0), \
126   QUICKTHREADS_SPUT (sto, QUICKTHREADS_POP0, qt_align), \
127   QUICKTHREADS_SPUT (sto, QUICKTHREADS_POP1, qt_align), \
128   QUICKTHREADS_SPUT (sto, QUICKTHREADS_RPC, qt_error))
129
130
131/* When varargs are pushed, allocate space for all the args. */
132#define QUICKTHREADS_VARGS_MD0(sto, nbytes) \
133  ((qt_t *)(((char *)(sto)) - QUICKTHREADS_STKROUNDUP(nbytes)))
134
135#define QUICKTHREADS_VARGS_MD1(sto) \
136  (QUICKTHREADS_SPUT (sto, QUICKTHREADS_PC, qt_vstart))
137
138#define QUICKTHREADS_VARGS_DEFAULT
139
140#endif /* QUICKTHREADS_X86_64_H */
141