aboutsummaryrefslogtreecommitdiffstats
path: root/mps/code
diff options
context:
space:
mode:
authorDavid Lovemore2012-08-09 16:05:08 +0100
committerDavid Lovemore2012-08-09 16:05:08 +0100
commitbe8a839d8344942f7e4ba8d22d2e1bdc63d3e002 (patch)
tree334e3dcf773847bf1e4ef839ba77eeb24bb18742 /mps/code
parent3e9f8108a414297a2932dd5973db1e3902e53d20 (diff)
downloademacs-be8a839d8344942f7e4ba8d22d2e1bdc63d3e002.tar.gz
emacs-be8a839d8344942f7e4ba8d22d2e1bdc63d3e002.zip
Made 64 bit linux port lii6gc. i've renamed thlii4.c to thli.c.
Copied from Perforce Change: 178876 ServerID: perforce.ravenbrook.com
Diffstat (limited to 'mps/code')
-rw-r--r--mps/code/lii4gc.gmk2
-rw-r--r--mps/code/lii6gc.gmk71
-rw-r--r--mps/code/mpstd.h17
-rw-r--r--mps/code/prmci6li.c123
-rw-r--r--mps/code/protlii6.c180
-rw-r--r--mps/code/ssixi6.c19
-rw-r--r--mps/code/thli.c350
7 files changed, 751 insertions, 11 deletions
diff --git a/mps/code/lii4gc.gmk b/mps/code/lii4gc.gmk
index 2eceecb9827..6ed66a21f85 100644
--- a/mps/code/lii4gc.gmk
+++ b/mps/code/lii4gc.gmk
@@ -5,7 +5,7 @@
5 5
6PFM = lii4gc 6PFM = lii4gc
7 7
8THREADSRC = lockli.c thlii4.c pthrdext.c 8THREADSRC = lockli.c thli.c pthrdext.c
9THREADLIB = -lpthread 9THREADLIB = -lpthread
10 10
11# _XOPEN_SOURCE is to get the modern POSIX signal handling 11# _XOPEN_SOURCE is to get the modern POSIX signal handling
diff --git a/mps/code/lii6gc.gmk b/mps/code/lii6gc.gmk
new file mode 100644
index 00000000000..d072f5f62fd
--- /dev/null
+++ b/mps/code/lii6gc.gmk
@@ -0,0 +1,71 @@
1# lii6gc.gmk: BUILD FOR LINUX/x64/GCC PLATFORM
2#
3# $Id$
4# Copyright (c) 2001 Ravenbrook Limited. See end of file for license.
5
6PFM = lii6gc
7
8THREADSRC = lockli.c thli.c pthrdext.c
9THREADLIB = -lpthread
10
11# _XOPEN_SOURCE is to get the modern POSIX signal handling
12# _GNU_SOURCE is to get register numbers in prmci3li.c
13PFMDEFS = -D_REENTRANT -D_XOPEN_SOURCE=500 -D_GNU_SOURCE
14
15MPMPF = ${THREADSRC} vmix.c \
16 protix.c protlii6.c proti6.c prmci6li.c ssixi6.c span.c
17SWPF = than.c vmli.c protsw.c prmcan.c ssan.c
18
19LIBS = -lm ${THREADLIB}
20
21include gc.gmk
22
23CC = cc
24
25# Suppress some warnings (SuSE).
26# .void: -Wpointer-arith cannot be used because the string.h header does
27# arithmetic on void*.
28CFLAGSCOMPILER := -pthread $(subst -Wpointer-arith,,$(CFLAGSCOMPILER))
29
30include comm.gmk
31
32
33# C. COPYRIGHT AND LICENSE
34#
35# Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>.
36# All rights reserved. This is an open source license. Contact
37# Ravenbrook for commercial licensing options.
38#
39# Redistribution and use in source and binary forms, with or without
40# modification, are permitted provided that the following conditions are
41# met:
42#
43# 1. Redistributions of source code must retain the above copyright
44# notice, this list of conditions and the following disclaimer.
45#
46# 2. Redistributions in binary form must reproduce the above copyright
47# notice, this list of conditions and the following disclaimer in the
48# documentation and/or other materials provided with the distribution.
49#
50# 3. Redistributions in any form must be accompanied by information on how
51# to obtain complete source code for this software and any accompanying
52# software that uses this software. The source code must either be
53# included in the distribution or be available for no more than the cost
54# of distribution plus a nominal fee, and must be freely redistributable
55# under reasonable conditions. For an executable file, complete source
56# code means the source code for all modules it contains. It does not
57# include source code for modules or files that typically accompany the
58# major components of the operating system on which the executable file
59# runs.
60#
61# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
62# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
64# PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
65# COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
66# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
67# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
68# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
69# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
71# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/mps/code/mpstd.h b/mps/code/mpstd.h
index 4a8a0875ca3..86b89c836f5 100644
--- a/mps/code/mpstd.h
+++ b/mps/code/mpstd.h
@@ -470,6 +470,23 @@
470#define MPS_WORD_SHIFT 5 470#define MPS_WORD_SHIFT 5
471#define MPS_PF_ALIGN 4 471#define MPS_PF_ALIGN 4
472 472
473/* GCC 4.6.3, gcc -E -dM */
474
475#elif defined(__linux__) && defined(__x86_64) && defined(__GNUC__)
476#if defined(CONFIG_PF_STRING) && ! defined(CONFIG_PF_LII6GC)
477#error "specified CONFIG_PF_... inconsistent with detected lii6gc"
478#endif
479#define MPS_PF_LII6GC
480#define MPS_PF_STRING "lii6gc"
481#define MPS_OS_LI
482#define MPS_ARCH_I6
483#define MPS_BUILD_GC
484#define MPS_T_WORD unsigned long
485#define MPS_T_ULONGEST unsigned long
486#define MPS_WORD_WIDTH 64
487#define MPS_WORD_SHIFT 6
488#define MPS_PF_ALIGN 8
489
473/* GCC 2.7.2, gcc -E -dM */ 490/* GCC 2.7.2, gcc -E -dM */
474 491
475#elif defined(__linux__) && defined(__PPC__) && defined(__GNUC__) 492#elif defined(__linux__) && defined(__PPC__) && defined(__GNUC__)
diff --git a/mps/code/prmci6li.c b/mps/code/prmci6li.c
new file mode 100644
index 00000000000..e81a7cfd750
--- /dev/null
+++ b/mps/code/prmci6li.c
@@ -0,0 +1,123 @@
1/* prmci6li.c: PROTECTION MUTATOR CONTEXT x64 (LINUX)
2 *
3 * $Id$
4 * Copyright (c) 2001 Ravenbrook Limited. See end of file for license.
5 *
6 * .purpose: This module implements the part of the protection module
7 * that decodes the MutatorFaultContext.
8 *
9 *
10 * SOURCES
11 *
12 * .source.linux.kernel: Linux kernel source files.
13 *
14 *
15 * ASSUMPTIONS
16 *
17 * .assume.regref: The resisters in the context can be modified by
18 * storing into an MRef pointer.
19 */
20
21#include "prmcix.h"
22#include "prmci6.h"
23
24SRCID(prmci6li, "$Id$");
25
26
27/* Prmci6AddressHoldingReg -- return an address of a register in a context */
28
29MRef Prmci6AddressHoldingReg(MutatorFaultContext mfc, unsigned int regnum)
30{
31 Word *gregs;
32
33 AVER(regnum <= 15);
34 AVER(regnum >= 0);
35
36 gregs = (Word *)&mfc->ucontext->uc_mcontext.gregs;
37
38 /* .assume.regref */
39 /* The REG_EAX etc. symbols are only present if _GNU_SOURCE is defined.
40 * Currently this is in lii6gc.gmk in PFMDEFS. */
41 switch (regnum) {
42 case 0: return &gregs[REG_RAX];
43 case 1: return &gregs[REG_RCX];
44 case 2: return &gregs[REG_RDX];
45 case 3: return &gregs[REG_RBX];
46 case 4: return &gregs[REG_RSP];
47 case 5: return &gregs[REG_RBP];
48 case 6: return &gregs[REG_RSI];
49 case 7: return &gregs[REG_RDI];
50 case 8: return &gregs[REG_R8];
51 case 9: return &gregs[REG_R9];
52 case 10: return &gregs[REG_R10];
53 case 11: return &gregs[REG_R11];
54 case 12: return &gregs[REG_R12];
55 case 13: return &gregs[REG_R13];
56 case 14: return &gregs[REG_R14];
57 case 15: return &gregs[REG_R15];
58 }
59 NOTREACHED;
60 return (MRef)NULL; /* Avoids compiler warning. */
61}
62
63
64/* Prmci3DecodeFaultContext -- decode fault to find faulting address and IP */
65
66void Prmci6DecodeFaultContext(MRef *faultmemReturn,
67 Byte **insvecReturn,
68 MutatorFaultContext mfc)
69{
70 /* .source.linux.kernel (linux/arch/x86/mm/fault.c). */
71 *faultmemReturn = (MRef)mfc->info->si_addr;
72 *insvecReturn = (Byte*)mfc->ucontext->uc_mcontext.gregs[REG_RIP];
73}
74
75
76/* Prmci3StepOverIns -- modify context to step over instruction */
77
78void Prmci6StepOverIns(MutatorFaultContext mfc, Size inslen)
79{
80 mfc->ucontext->uc_mcontext.gregs[REG_RIP] += (Word)inslen;
81}
82
83
84/* C. COPYRIGHT AND LICENSE
85 *
86 * Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>.
87 * All rights reserved. This is an open source license. Contact
88 * Ravenbrook for commercial licensing options.
89 *
90 * Redistribution and use in source and binary forms, with or without
91 * modification, are permitted provided that the following conditions are
92 * met:
93 *
94 * 1. Redistributions of source code must retain the above copyright
95 * notice, this list of conditions and the following disclaimer.
96 *
97 * 2. Redistributions in binary form must reproduce the above copyright
98 * notice, this list of conditions and the following disclaimer in the
99 * documentation and/or other materials provided with the distribution.
100 *
101 * 3. Redistributions in any form must be accompanied by information on how
102 * to obtain complete source code for this software and any accompanying
103 * software that uses this software. The source code must either be
104 * included in the distribution or be available for no more than the cost
105 * of distribution plus a nominal fee, and must be freely redistributable
106 * under reasonable conditions. For an executable file, complete source
107 * code means the source code for all modules it contains. It does not
108 * include source code for modules or files that typically accompany the
109 * major components of the operating system on which the executable file
110 * runs.
111 *
112 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
113 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
114 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
115 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
116 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
117 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
118 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
119 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
120 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
121 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
122 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123 */
diff --git a/mps/code/protlii6.c b/mps/code/protlii6.c
new file mode 100644
index 00000000000..e149d5269ed
--- /dev/null
+++ b/mps/code/protlii6.c
@@ -0,0 +1,180 @@
1/* protlii6.c: PROTECTION FOR LINUX (x64)
2 *
3 * $Id$
4 * Copyright (c) 2001 Ravenbrook Limited. See end of file for license.
5 *
6 * SOURCES
7 *
8 * .source.linux.kernel: Linux kernel source files.
9 */
10
11#include "prmcix.h"
12
13#ifndef MPS_OS_LI
14#error "protlii6.c is Linux-specific, but MPS_OS_LI is not set"
15#endif
16#if !defined(MPS_ARCH_I6)
17#error "protlii6.c is x64, but MPS_ARCH_I6 is not set"
18#endif
19#ifndef PROTECTION
20#error "protlii6.c implements protection, but PROTECTION is not set"
21#endif
22
23#include <limits.h>
24#include <stddef.h>
25#include <stdlib.h>
26#include <signal.h>
27#include <unistd.h>
28
29SRCID(protlii6, "$Id$");
30
31
32
33/* The previously-installed signal action, as returned by */
34/* sigaction(3). See ProtSetup. */
35
36static struct sigaction sigNext;
37
38
39/* sigHandle -- protection signal handler
40 *
41 * This is the signal handler installed by ProtSetup to deal with
42 * protection faults. It is installed on the SIGSEGV signal.
43 * It decodes the protection fault details from the signal context
44 * and passes them to ArenaAccess, which attempts to handle the
45 * fault and remove its cause. If the fault is handled, then
46 * the handler returns and execution resumes. If it isn't handled,
47 * then sigHandle does its best to pass the signal on to the
48 * previously installed signal handler (sigNext).
49 *
50 * .sigh.context: We check si_code for being a memory access
51 * si_addr gives the fault address. See
52 * .source.linux.kernel (linux/arch/x86/mm/fault.c).
53 *
54 * .sigh.addr: We assume that the OS decodes the address to something
55 * sensible
56 */
57/* This is defined here to keep the sources closer to those in protsgix.c
58 * They can't be merged yet because protsgix doesn't pass the context to
59 * ArenaAccess */
60
61#define PROT_SIGNAL SIGSEGV
62
63static void sigHandle(int sig, siginfo_t *info, void *context) /* .sigh.args */
64{
65 int e;
66 /* sigset renamed to asigset due to clash with global on Darwin. */
67 sigset_t asigset, oldset;
68 struct sigaction sa;
69
70 AVER(sig == PROT_SIGNAL);
71
72 if(info->si_code == SEGV_ACCERR) { /* .sigh.context */
73 AccessSet mode;
74 Addr base;
75 ucontext_t *ucontext;
76 MutatorFaultContextStruct mfContext;
77
78 ucontext = (ucontext_t *)context;
79 mfContext.ucontext = ucontext;
80 mfContext.info = info;
81
82 /* on linux we used to be able to tell whether this was a read or a write */
83 mode = AccessREAD | AccessWRITE;
84
85 /* We assume that the access is for one word at the address. */
86 base = (Addr)info->si_addr; /* .sigh.addr */
87 /* limit = AddrAdd(base, (Size)sizeof(Addr)); */
88
89 /* Offer each protection structure the opportunity to handle the */
90 /* exception. If it succeeds, then allow the mutator to continue. */
91
92 if(ArenaAccess(base, mode, &mfContext))
93 return;
94 }
95
96 /* The exception was not handled by any known protection structure, */
97 /* so throw it to the previously installed handler. That handler won't */
98 /* get an accurate context (the MPS would fail if it were the second in */
99 /* line) but it's the best we can do. */
100
101 e = sigaction(PROT_SIGNAL, &sigNext, &sa);
102 AVER(e == 0);
103 sigemptyset(&asigset);
104 sigaddset(&asigset, PROT_SIGNAL);
105 e = sigprocmask(SIG_UNBLOCK, &asigset, &oldset);
106 AVER(e == 0);
107 kill(getpid(), PROT_SIGNAL);
108 e = sigprocmask(SIG_SETMASK, &oldset, NULL);
109 AVER(e == 0);
110 e = sigaction(PROT_SIGNAL, &sa, NULL);
111 AVER(e == 0);
112}
113
114
115/* ProtSetup -- global protection setup
116 *
117 * Under Linux, the global setup involves installing a signal handler
118 * on SIGSEGV to catch and handle page faults (see sigHandle).
119 * The previous handler is recorded so that it can be reached from
120 * sigHandle if it fails to handle the fault.
121 *
122 * NOTE: There are problems with this approach:
123 * 1. we can't honor the sa_flags for the previous handler,
124 * 2. what if this thread is suspended just after calling signal(3)?
125 * The sigNext variable will never be initialized!
126 */
127
128void ProtSetup(void)
129{
130 struct sigaction sa;
131 int result;
132
133 sa.sa_sigaction = sigHandle;
134 sigemptyset(&sa.sa_mask);
135 sa.sa_flags = SA_SIGINFO;
136
137 result = sigaction(PROT_SIGNAL, &sa, &sigNext);
138 AVER(result == 0);
139}
140
141/* C. COPYRIGHT AND LICENSE
142 *
143 * Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>.
144 * All rights reserved. This is an open source license. Contact
145 * Ravenbrook for commercial licensing options.
146 *
147 * Redistribution and use in source and binary forms, with or without
148 * modification, are permitted provided that the following conditions are
149 * met:
150 *
151 * 1. Redistributions of source code must retain the above copyright
152 * notice, this list of conditions and the following disclaimer.
153 *
154 * 2. Redistributions in binary form must reproduce the above copyright
155 * notice, this list of conditions and the following disclaimer in the
156 * documentation and/or other materials provided with the distribution.
157 *
158 * 3. Redistributions in any form must be accompanied by information on how
159 * to obtain complete source code for this software and any accompanying
160 * software that uses this software. The source code must either be
161 * included in the distribution or be available for no more than the cost
162 * of distribution plus a nominal fee, and must be freely redistributable
163 * under reasonable conditions. For an executable file, complete source
164 * code means the source code for all modules it contains. It does not
165 * include source code for modules or files that typically accompany the
166 * major components of the operating system on which the executable file
167 * runs.
168 *
169 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
170 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
171 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
172 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
173 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
174 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
175 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
176 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
177 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
178 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
179 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
180 */
diff --git a/mps/code/ssixi6.c b/mps/code/ssixi6.c
index 6732f850b3b..6b425bed804 100644
--- a/mps/code/ssixi6.c
+++ b/mps/code/ssixi6.c
@@ -1,4 +1,4 @@
1/* ssixi6.c: UNIX/INTEL STACK SCANNING 1/* ssixi6.c: UNIX/x64 STACK SCANNING
2 * 2 *
3 * $Id$ 3 * $Id$
4 * Copyright (c) 2001 Ravenbrook Limited. See end of file for license. 4 * Copyright (c) 2001 Ravenbrook Limited. See end of file for license.
@@ -49,22 +49,21 @@ SRCID(ssixi6, "$Id$");
49 49
50Res StackScan(ScanState ss, Addr *stackBot) 50Res StackScan(ScanState ss, Addr *stackBot)
51{ 51{
52 Addr calleeSaveRegs[6];
52 Addr *stackTop; 53 Addr *stackTop;
53 Res res; 54 Res res;
54 55
55 ASMV("push %rbp"); 56 ASMV("mov %%rbp, %0" : "=m" (calleeSaveRegs[0]));
56 ASMV("push %rbx"); 57 ASMV("mov %%rbx, %0" : "=m" (calleeSaveRegs[1]));
57 ASMV("push %r12"); 58 ASMV("mov %%r12, %0" : "=m" (calleeSaveRegs[2]));
58 ASMV("push %r13"); 59 ASMV("mov %%r13, %0" : "=m" (calleeSaveRegs[3]));
59 ASMV("push %r14"); 60 ASMV("mov %%r14, %0" : "=m" (calleeSaveRegs[4]));
60 ASMV("push %r15"); 61 ASMV("mov %%r15, %0" : "=m" (calleeSaveRegs[5]));
61 ASMV("mov %%rsp, %0" : "=r" (stackTop) :); /* stackTop = esp */ 62 ASMV("mov %%rsp, %0" : "=r" (stackTop) :); /* stackTop = rsp */
62 63
63 AVER(AddrIsAligned((Addr)stackTop, sizeof(Addr))); /* .assume.align */ 64 AVER(AddrIsAligned((Addr)stackTop, sizeof(Addr))); /* .assume.align */
64 res = TraceScanArea(ss, stackTop, stackBot); 65 res = TraceScanArea(ss, stackTop, stackBot);
65 66
66 ASMV("add $48, %rsp"); /* pop 6 regs to restore the stack pointer */
67
68 return res; 67 return res;
69} 68}
70 69
diff --git a/mps/code/thli.c b/mps/code/thli.c
new file mode 100644
index 00000000000..a486e9ab9b6
--- /dev/null
+++ b/mps/code/thli.c
@@ -0,0 +1,350 @@
1/* thli.c: Threads Manager for LinuxThreads
2 *
3 * $Id$
4 * Copyright (c) 2001 Ravenbrook Limited. See end of file for license.
5 *
6 * .purpose: This is a pthreads implementation of the threads manager.
7 * This implements <code/th.h>.
8 *
9 * .design: See <design/thread-manager/>.
10 *
11 * .thread.id: The thread id is used to identify the current thread.
12 *
13 * ASSUMPTIONS
14 *
15 * .error.resume: PThreadextResume is assumed to succeed unless the thread
16 * has been destroyed.
17 * .error.suspend: PThreadextSuspend is assumed to succeed unless the thread
18 * has been destroyed. In this case, the suspend context is set to NULL;
19 *
20 * .stack.full-descend: assumes full descending stack.
21 * i.e. stack pointer points to the last allocated location;
22 * stack grows downwards.
23 *
24 * .stack.below-bottom: it's legal for the stack pointer to be at a
25 * higher address than the registered bottom of stack. This might
26 * happen if the stack of another thread doesn't contain any frames
27 * belonging to the client language. In this case, the stack should
28 * not be scanned.
29 *
30 * .stack.align: assume roots on the stack are always word-aligned,
31 * but don't assume that the stack pointer is necessarily
32 * word-aligned at the time of reading the context of another thread.
33 *
34 * .sp: The stack pointer in the context is uc_stack.ss_sp.
35 * .context.regroots: The root regs are
36 * assumed to be recorded in the context at pointer-aligned boundaries.
37 */
38
39#include "prmcix.h"
40#include "mpm.h"
41
42#if !defined(MPS_OS_LI)
43#error "Compiling thli when MPS_OS_LI is not defined."
44#endif
45
46#if !(defined(MPS_ARCH_I4) || defined(MPS_ARCH_I6))
47#error "Need to check assumptions for thli.c are true for this architecture"
48#endif
49
50#include <pthread.h>
51#include "pthrdext.h"
52
53SRCID(thli, "$Id$");
54
55
56/* ThreadStruct -- thread desriptor */
57
58typedef struct ThreadStruct { /* PThreads thread structure */
59 Sig sig; /* <design/sig/> */
60 Serial serial; /* from arena->threadSerial */
61 Arena arena; /* owning arena */
62 RingStruct arenaRing; /* threads attached to arena */
63 PThreadextStruct thrextStruct; /* PThreads extension */
64 pthread_t id; /* Pthread object of thread */
65 MutatorFaultContext mfc; /* Context if thread is suspended */
66} ThreadStruct;
67
68
69/* ThreadCheck -- check a thread */
70
71Bool ThreadCheck(Thread thread)
72{
73 CHECKS(Thread, thread);
74 CHECKU(Arena, thread->arena);
75 CHECKL(thread->serial < thread->arena->threadSerial);
76 CHECKL(RingCheck(&thread->arenaRing));
77 CHECKD(PThreadext, &thread->thrextStruct);
78 return TRUE;
79}
80
81Bool ThreadCheckSimple(Thread thread)
82{
83 CHECKS(Thread, thread);
84 return TRUE;
85}
86
87
88/* ThreadRegister -- register a thread with an arena */
89
90Res ThreadRegister(Thread *threadReturn, Arena arena)
91{
92 Res res;
93 Thread thread;
94 void *p;
95
96 AVER(threadReturn != NULL);
97 AVERT(Arena, arena);
98
99 res = ControlAlloc(&p, arena, sizeof(ThreadStruct),
100 /* withReservoirPermit */ FALSE);
101 if(res != ResOK)
102 return res;
103 thread = (Thread)p;
104
105 thread->id = pthread_self();
106
107 RingInit(&thread->arenaRing);
108
109 thread->sig = ThreadSig;
110 thread->serial = arena->threadSerial;
111 ++arena->threadSerial;
112 thread->arena = arena;
113 thread->mfc = NULL;
114
115 PThreadextInit(&thread->thrextStruct, thread->id);
116
117 AVERT(Thread, thread);
118
119 RingAppend(ArenaThreadRing(arena), &thread->arenaRing);
120
121 *threadReturn = thread;
122 return ResOK;
123}
124
125
126/* ThreadDeregister -- deregister a thread from an arena */
127
128void ThreadDeregister(Thread thread, Arena arena)
129{
130 AVERT(Thread, thread);
131 AVERT(Arena, arena);
132
133 RingRemove(&thread->arenaRing);
134
135 thread->sig = SigInvalid;
136
137 RingFinish(&thread->arenaRing);
138
139 PThreadextFinish(&thread->thrextStruct);
140
141 ControlFree(arena, thread, sizeof(ThreadStruct));
142}
143
144
145/* mapThreadRing -- map over threads on ring calling a function on each one
146 * except the current thread
147 */
148
149static void mapThreadRing(Ring threadRing, void (*func)(Thread))
150{
151 Ring node, next;
152 pthread_t self;
153
154 AVERT(Ring, threadRing);
155
156 self = pthread_self();
157 RING_FOR(node, threadRing, next) {
158 Thread thread = RING_ELT(Thread, arenaRing, node);
159 AVERT(Thread, thread);
160 if(! pthread_equal(self, thread->id)) /* .thread.id */
161 (*func)(thread);
162 }
163}
164
165
166/* ThreadRingSuspend -- suspend all threads on a ring, expect the current one */
167
168
169static void threadSuspend(Thread thread)
170{
171 /* .error.suspend */
172 /* In the error case (PThreadextSuspend returning ResFAIL), we */
173 /* assume the thread has been destroyed. */
174 /* In which case we simply continue. */
175 Res res;
176 res = PThreadextSuspend(&thread->thrextStruct, &thread->mfc);
177 if(res != ResOK)
178 thread->mfc = NULL;
179}
180
181
182
183void ThreadRingSuspend(Ring threadRing)
184{
185 mapThreadRing(threadRing, threadSuspend);
186}
187
188
189/* ThreadRingResume -- resume all threads on a ring (expect the current one) */
190
191
192static void threadResume(Thread thread)
193{
194 /* .error.resume */
195 /* If the previous suspend failed (thread->mfc == NULL), */
196 /* or in the error case (PThreadextResume returning ResFAIL), */
197 /* assume the thread has been destroyed. */
198 /* In which case we simply continue. */
199 if(thread->mfc != NULL) {
200 (void)PThreadextResume(&thread->thrextStruct);
201 thread->mfc = NULL;
202 }
203}
204
205void ThreadRingResume(Ring threadRing)
206{
207 mapThreadRing(threadRing, threadResume);
208}
209
210
211/* ThreadRingThread -- return the thread at the given ring element */
212
213Thread ThreadRingThread(Ring threadRing)
214{
215 Thread thread;
216 AVERT(Ring, threadRing);
217 thread = RING_ELT(Thread, arenaRing, threadRing);
218 AVERT(Thread, thread);
219 return thread;
220}
221
222
223/* ThreadArena -- get the arena of a thread
224 *
225 * Must be thread-safe. See <design/interface-c/#thread-safety>.
226 */
227
228Arena ThreadArena(Thread thread)
229{
230 /* Can't check thread as that would not be thread-safe. */
231 return thread->arena;
232}
233
234
235/* ThreadScan -- scan the state of a thread (stack and regs) */
236
237Res ThreadScan(ScanState ss, Thread thread, void *stackBot)
238{
239 pthread_t self;
240 Res res;
241
242 AVERT(Thread, thread);
243 self = pthread_self();
244 if(pthread_equal(self, thread->id)) {
245 /* scan this thread's stack */
246 res = StackScan(ss, stackBot);
247 if(res != ResOK)
248 return res;
249 } else {
250 MutatorFaultContext mfc;
251 Addr *stackBase, *stackLimit, stackPtr;
252 mcontext_t *mc;
253 mfc = thread->mfc;
254 if(mfc == NULL) {
255 /* .error.suspend */
256 /* We assume that the thread must have been destroyed. */
257 /* We ignore the situation by returning immediately. */
258 return ResOK;
259 }
260
261 stackPtr = (Addr)mfc->ucontext->uc_stack.ss_sp; /* .sp */
262 /* .stack.align */
263 stackBase = (Addr *)AddrAlignUp(stackPtr, sizeof(Addr));
264 stackLimit = (Addr *)stackBot;
265 if (stackBase >= stackLimit)
266 return ResOK; /* .stack.below-bottom */
267
268 /* scan stack inclusive of current sp and exclusive of
269 * stackBot (.stack.full-descend)
270 */
271 res = TraceScanAreaTagged(ss, stackBase, stackLimit);
272 if(res != ResOK)
273 return res;
274
275 /* (.context.regroots)
276 * This scans the root registers (.context.regroots). It also
277 * unecessarily scans the rest of the context. The optimisation
278 * to scan only relevent parts would be machine dependent.
279 */
280 mc = &mfc->ucontext->uc_mcontext;
281 res = TraceScanAreaTagged(ss, (Addr *)mc,
282 (Addr *)((char *)mc + sizeof(*mc)));
283 if(res != ResOK)
284 return res;
285 }
286
287 return ResOK;
288}
289
290
291/* ThreadDescribe -- describe a thread */
292
293Res ThreadDescribe(Thread thread, mps_lib_FILE *stream)
294{
295 Res res;
296
297 res = WriteF(stream,
298 "Thread $P ($U) {\n", (WriteFP)thread, (WriteFU)thread->serial,
299 " arena $P ($U)\n",
300 (WriteFP)thread->arena, (WriteFU)thread->arena->serial,
301 " id $U\n", (WriteFU)thread->id,
302 "} Thread $P ($U)\n", (WriteFP)thread, (WriteFU)thread->serial,
303 NULL);
304 if(res != ResOK)
305 return res;
306
307 return ResOK;
308}
309
310
311/* C. COPYRIGHT AND LICENSE
312 *
313 * Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>.
314 * All rights reserved. This is an open source license. Contact
315 * Ravenbrook for commercial licensing options.
316 *
317 * Redistribution and use in source and binary forms, with or without
318 * modification, are permitted provided that the following conditions are
319 * met:
320 *
321 * 1. Redistributions of source code must retain the above copyright
322 * notice, this list of conditions and the following disclaimer.
323 *
324 * 2. Redistributions in binary form must reproduce the above copyright
325 * notice, this list of conditions and the following disclaimer in the
326 * documentation and/or other materials provided with the distribution.
327 *
328 * 3. Redistributions in any form must be accompanied by information on how
329 * to obtain complete source code for this software and any accompanying
330 * software that uses this software. The source code must either be
331 * included in the distribution or be available for no more than the cost
332 * of distribution plus a nominal fee, and must be freely redistributable
333 * under reasonable conditions. For an executable file, complete source
334 * code means the source code for all modules it contains. It does not
335 * include source code for modules or files that typically accompany the
336 * major components of the operating system on which the executable file
337 * runs.
338 *
339 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
340 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
341 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
342 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
343 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
344 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
345 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
346 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
347 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
348 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
349 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
350 */