2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * arch.c: architecture-dependant process context code
30 * Known contributors to this file:
32 * Steve McClure, 1994-2000
35 #include "prototypes.h"
37 #if defined(_EMPTH_LWP)
47 static struct lwpProc *tempcontext;
48 struct lwpProc *initcontext = NULL;
56 startpoint = (void *)&x;
57 if (!setjmp(initcontext->context))
58 longjmp(tempcontext->context, 1);
60 if (!setjmp(tempcontext->context))
61 longjmp(LwpCurrent->context, 1);
67 lwpInitContext(newp, sp)
71 struct lwpProc holder;
74 if (initcontext == NULL) {
75 initcontext = (struct lwpProc *)malloc(sizeof(struct lwpProc));
76 tempcontext = &holder;
77 if (!setjmp(tempcontext->context))
83 if (endpoint < startpoint) {
84 if (!setjmp(LwpCurrent->context))
85 longjmp(initcontext->context, 1);
87 LwpCurrent->size = endpoint - startpoint;
88 LwpCurrent->sbtm = realloc(LwpCurrent->sbtm, LwpCurrent->size);
89 memcpy(LwpCurrent->sbtm, startpoint, LwpCurrent->size);
90 if (!setjmp(LwpCurrent->context))
91 longjmp(initcontext->context, 1);
92 memcpy(startpoint, LwpCurrent->sbtm, LwpCurrent->size);
99 lwpInitContext(newp, sp)
100 volatile struct lwpProc *volatile newp;
104 extern struct lwpProc *LwpCurrent;
106 if (!lwpSave(LwpCurrent->context)) {
107 cpp = (jmp_buf *) & newp->context;
108 asm volatile ("ldw %0, %%sp"::"o" (sp));
110 lwpRestore(LwpCurrent->context);
119 /* save stack pointer and return program counter */
120 asm("stw %sp, 4(%arg0)");
121 asm("stw %rp, 8(%arg0)");
123 /* save "callee save" registers */
124 asm("stw %r3, 12(%arg0)");
125 asm("stw %r4, 16(%arg0)");
126 asm("stw %r5, 20(%arg0)");
127 asm("stw %r6, 24(%arg0)");
128 asm("stw %r7, 28(%arg0)");
129 asm("stw %r8, 32(%arg0)");
130 asm("stw %r9, 36(%arg0)");
131 asm("stw %r10, 40(%arg0)");
132 asm("stw %r11, 44(%arg0)");
133 asm("stw %r12, 48(%arg0)");
134 asm("stw %r13, 52(%arg0)");
135 asm("stw %r14, 56(%arg0)");
136 asm("stw %r15, 60(%arg0)");
137 asm("stw %r16, 64(%arg0)");
138 asm("stw %r17, 68(%arg0)");
139 asm("stw %r18, 72(%arg0)");
141 /* save "callee save" space register */
142 asm volatile ("mfsp %sr3, %r1");
143 asm("stw %r1, 0(%arg0)");
145 /* indicate "true return" from saved() */
148 asm(".LABEL _comefrom_");
155 /* restore stack pointer and program counter */
156 asm volatile ("ldw 4(%arg0), %sp");
157 asm volatile ("ldw 8(%arg0), %rp");
159 /* restore "callee save" space register */
160 asm volatile ("ldw 0(%arg0), %r1");
161 asm volatile ("mtsp %r1, %sr3");
163 /* restore "callee save" registers */
164 asm volatile ("ldw 12(%arg0), %r3");
165 asm volatile ("ldw 16(%arg0), %r4");
166 asm volatile ("ldw 20(%arg0), %r5");
167 asm volatile ("ldw 24(%arg0), %r6");
168 asm volatile ("ldw 28(%arg0), %r7");
169 asm volatile ("ldw 32(%arg0), %r8");
170 asm volatile ("ldw 36(%arg0), %r9");
171 asm volatile ("ldw 40(%arg0), %r10");
172 asm volatile ("ldw 44(%arg0), %r11");
173 asm volatile ("ldw 48(%arg0), %r12");
174 asm volatile ("ldw 52(%arg0), %r13");
175 asm volatile ("ldw 56(%arg0), %r14");
176 asm volatile ("ldw 60(%arg0), %r15");
177 asm volatile ("ldw 64(%arg0), %r16");
178 asm volatile ("ldw 68(%arg0), %r17");
179 asm volatile ("ldw 72(%arg0), %r18");
181 /* warp to saved() to unwind the frame correctly */
182 asm volatile ("bl _comefrom_, %r0");
183 asm volatile ("ldi 1, %ret0");
186 #elif defined(BSD386)
188 lwpInitContext(newp, sp)
189 struct lwpProc *newp;
192 newp->context[2] = (int)sp;
193 newp->context[0] = (int)lwpEntryPoint;
199 lwpInitContext(newp, sp)
200 struct lwpProc *newp;
203 setjmp(newp->context);
204 newp->context->_jb[2] = (int)sp;
205 newp->context->_jb[3] = (int)sp;
206 newp->context->_jb[0] = (int)lwpEntryPoint;
209 #elif defined(__linux__)
212 lwpInitContext(newp, sp)
213 struct lwpProc *newp;
216 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
218 newp->context->__jmpbuf[JB_GPR1] = (int)sp;
219 newp->context->__jmpbuf[JB_LR] = (int)lwpEntryPoint;
221 newp->context->__jmpbuf[JB_SP] = (int)sp;
222 newp->context->__jmpbuf[JB_BP] = (int)sp;
223 newp->context->__jmpbuf[JB_PC] = (int)lwpEntryPoint;
226 newp->context->__sp = sp;
227 newp->context->__bp = sp;
228 newp->context->__pc = (void *)lwpEntryPoint;
235 lwpInitContext(newp, sp)
236 struct lwpProc *newp;
239 newp->context[2] = (int)sp;
240 newp->context[3] = (int)lwpEntryPoint;
248 lwpInitContext(newp, stack)
249 struct lwpProc *newp;
252 int *sp = (int *)stack;
255 /* Build root frame on new stack for lwpEntryPoint */
257 *--sp = (int)fp; /* fp */
260 *--sp = 0; /* condition handler */
263 /* Build stack frame to return from. */
264 *--sp = (int)lwpEntryPoint + 2; /* pc */
265 *--sp = (int)fp; /* fp */
268 *--sp = 0; /* condition handler */
271 /* Fill in the context */
272 /* Note: This is *not* how libc fills out jump buffers. */
273 newp->context[0] = 0; /* r6 */
274 newp->context[1] = 0;
275 newp->context[2] = 0;
276 newp->context[3] = 0;
277 newp->context[4] = 0;
278 newp->context[5] = 0; /* r11 */
279 newp->context[6] = 0; /* ap */
280 newp->context[7] = (int)fp; /* fp */
288 asm("movl 4(ap), r0"); /* r0 = &jb */
289 asm("movl r6, (r0)"); /* jb[0] = r6 */
290 asm("movl r7, 4(r0)");
291 asm("movl r8, 8(r0)");
292 asm("movl r9, 12(r0)");
293 asm("movl r10, 16(r0)");
294 asm("movl r11, 20(r0)");
295 asm("movl ap, 24(r0)");
296 asm("movl fp, 28(r0)"); /* jb[7] = fp */
304 asm("movl 4(ap), r0"); /* r0 = &jb */
305 asm("movl (r0), r6"); /* r6 = jb[0] */
306 asm("movl 4(r0), r7");
307 asm("movl 8(r0), r8");
308 asm("movl 12(r0), r9");
309 asm("movl 16(r0), r10");
310 asm("movl 20(r0), r11");
311 asm("movl 24(r0), ap");
312 asm("movl 28(r0), fp"); /* fp = jb[7] */
313 asm("movl $1, r0"); /* faked return 1 from lwpSave() */
322 lwpInitContext(newp, sp)
323 struct lwpProc *newp;
327 extern struct lwpProc *LwpCurrent;
329 bzero(newp->context, sizeof(newp->context));
330 newp->context[0] = (int)sp;
331 /* preserve cpp for new context */
332 cpp = (jmp_buf *) & newp->context;
333 if (!_setjmp(LwpCurrent->context)) {
334 /* create new context */
335 /* flush registers */
338 asm("ld [%fp+0x44], %o0");
339 /* %o1 <- newp->context[0] */
340 asm("ld [%o0], %o1");
341 /* create min frame on new stack */
342 asm("save %o1,-96, %sp");
344 _longjmp(LwpCurrent->context, 1);
349 #elif defined(__USLC__) && defined(i386)
351 /* USL/Unixware on an Intel 386/486/... processor.
352 * Tested on Unixware v1.1.2, based on SYSV R4.2
355 /* As per normal empire documentation, there is none.
357 * But, what we are attempting to do here is set up a longjump
358 * context buffer so that the lwpEntryPoint is called when
361 * I.E., what a setjmp/longjmp call set would do.
363 * How to figure this out? Well, without the setjmp code, you
364 * need to reverse engineer it by printing out the context buffer
365 * and the processor registers, and mapping which ones need
368 * Alternatively, you can single instruction step through the longjmp
369 * function, and figure out the offsets that it uses.
371 * Using offsets in bytes,
372 * context + 0x04 [1] -> esi (general purpose reg)
373 * context + 0x08 [2] -> edi (general purpose reg)
374 * context + 0x0C [3] -> ebp (general purpose or parameter passing)
375 * context + 0x10 [4] -> esp (stack)
376 * context + 0x14 [5] -> jump location for return
380 lwpInitContext(newp, sp)
381 struct lwpProc *newp;
384 newp->context[4] = (int)sp;
385 newp->context[5] = (int)lwpEntryPoint;
388 #elif defined UCONTEXT
391 * Alternate aproach using setcontext en getcontext in stead of setjmp and
392 * longjump. This should work on any SVr4 machine independant of
393 * architecture. Unfortunaltely some changes are still nessesary in lwp.c.
398 lwpInitContext(newp, spp)
399 struct lwpProc *newp;
402 getcontext(&(newp->context));
403 newp->context.uc_stack.ss_sp = spp->ss_sp;
404 newp->context.uc_stack.ss_size = spp->ss_size;
405 makecontext(&(newp->context), lwpEntryPoint, 0);
413 lwpInitContext(newp, sp)
414 struct lwpProc *newp;
419 /* register values obtained from setjmp.h */
420 _setjmp(newp->context);
421 newp->context[2] = (long)lwpEntryPoint; /* program counter */
422 newp->context[30] = (long)lwpEntryPoint; /* return address */
423 newp->context[31] = (long)lwpEntryPoint; /* fake program value (!) */
424 newp->context[34] = (long)sp; /* stack pointer */
438 /* resume, but get the pv from the jmp_buf */
439 asm("ldq %pv, 248(%a0)");
440 asm("stq %a0, 16(%sp)");
441 /* generates a warning, but functions just fine */
442 asm("bsr %ra, __longjump_resume");