diff options
| author | Richard Brooksby | 2012-08-14 17:30:22 +0100 |
|---|---|---|
| committer | Richard Brooksby | 2012-08-14 17:30:22 +0100 |
| commit | a801df50f4d0c2e32ee308fd332b5e29975041e7 (patch) | |
| tree | 5a0d2a01e20c6f3bffbc1695e736e5692070dd8f /mps/code | |
| parent | e9765e20d4a10142e68cb44b1929bdb73c265816 (diff) | |
| download | emacs-a801df50f4d0c2e32ee308fd332b5e29975041e7.tar.gz emacs-a801df50f4d0c2e32ee308fd332b5e29975041e7.zip | |
Adding fri6gc (freebsd on amd64/x86_64) platform.
Unifying thfr.c with generalised thix.c for Posix threads.
Copied from Perforce
Change: 178928
ServerID: perforce.ravenbrook.com
Diffstat (limited to 'mps/code')
| -rw-r--r-- | mps/code/fri4gc.gmk | 6 | ||||
| -rw-r--r-- | mps/code/fri6gc.gmk | 70 | ||||
| -rw-r--r-- | mps/code/mpstd.h | 18 | ||||
| -rw-r--r-- | mps/code/prmci3fr.c | 93 | ||||
| -rw-r--r-- | mps/code/prmci6fr.c | 87 | ||||
| -rw-r--r-- | mps/code/thfri4.c | 345 |
6 files changed, 269 insertions, 350 deletions
diff --git a/mps/code/fri4gc.gmk b/mps/code/fri4gc.gmk index 1184a0bc4f4..3bc48a35a79 100644 --- a/mps/code/fri4gc.gmk +++ b/mps/code/fri4gc.gmk | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # fri4gc.gmk: BUILD FOR FreeBSD/INTEL/GCC PLATFORM | 1 | # fri4gc.gmk: BUILD FOR FreeBSD/i386/GCC PLATFORM |
| 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. |
| @@ -7,8 +7,8 @@ PFM = fri4gc | |||
| 7 | 7 | ||
| 8 | PFMDEFS = -D_REENTRANT | 8 | PFMDEFS = -D_REENTRANT |
| 9 | 9 | ||
| 10 | MPMPF = lockix.c thfri4.c pthrdext.c vmix.c \ | 10 | MPMPF = lockix.c thix.c pthrdext.c vmix.c \ |
| 11 | protix.c protsgix.c prmcan.c ssixi3.c span.c | 11 | protix.c protsgix.c prmcan.c prmci3fr.c ssixi3.c span.c |
| 12 | SWPF = than.c vmfr.c protsw.c prmcan.c ssan.c | 12 | SWPF = than.c vmfr.c protsw.c prmcan.c ssan.c |
| 13 | 13 | ||
| 14 | LIBS = -lm -pthread | 14 | LIBS = -lm -pthread |
diff --git a/mps/code/fri6gc.gmk b/mps/code/fri6gc.gmk new file mode 100644 index 00000000000..291c2e7706b --- /dev/null +++ b/mps/code/fri6gc.gmk | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | # fri6gc.gmk: BUILD FOR FreeBSD/amd64/GCC PLATFORM | ||
| 2 | # | ||
| 3 | # $Id$ | ||
| 4 | # Copyright (c) 2001 Ravenbrook Limited. See end of file for license. | ||
| 5 | |||
| 6 | PFM = fri6gc | ||
| 7 | |||
| 8 | PFMDEFS = -D_REENTRANT | ||
| 9 | |||
| 10 | MPMPF = lockix.c thix.c pthrdext.c vmix.c \ | ||
| 11 | protix.c protsgix.c prmcan.c prmci6fr.c ssixi6.c span.c | ||
| 12 | SWPF = than.c vmfr.c protsw.c prmcan.c ssan.c | ||
| 13 | |||
| 14 | LIBS = -lm -pthread | ||
| 15 | |||
| 16 | include gc.gmk | ||
| 17 | |||
| 18 | CFLAGSCOMPILER := $(subst -pedantic,,$(CFLAGSCOMPILER)) | ||
| 19 | CFLAGSDEBUG = -g3 | ||
| 20 | CFLAGSOPT = -O -g3 | ||
| 21 | |||
| 22 | CC = cc | ||
| 23 | |||
| 24 | # Suppress some warnings (SuSE). | ||
| 25 | # .void: -Wpointer-arith cannot be used because the string.h header does | ||
| 26 | # arithmetic on void*. | ||
| 27 | #CFLAGSCOMPILER := $(subst -Wpointer-arith,,$(CFLAGSCOMPILER)) | ||
| 28 | |||
| 29 | include comm.gmk | ||
| 30 | |||
| 31 | |||
| 32 | # C. COPYRIGHT AND LICENSE | ||
| 33 | # | ||
| 34 | # Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>. | ||
| 35 | # All rights reserved. This is an open source license. Contact | ||
| 36 | # Ravenbrook for commercial licensing options. | ||
| 37 | # | ||
| 38 | # Redistribution and use in source and binary forms, with or without | ||
| 39 | # modification, are permitted provided that the following conditions are | ||
| 40 | # met: | ||
| 41 | # | ||
| 42 | # 1. Redistributions of source code must retain the above copyright | ||
| 43 | # notice, this list of conditions and the following disclaimer. | ||
| 44 | # | ||
| 45 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 46 | # notice, this list of conditions and the following disclaimer in the | ||
| 47 | # documentation and/or other materials provided with the distribution. | ||
| 48 | # | ||
| 49 | # 3. Redistributions in any form must be accompanied by information on how | ||
| 50 | # to obtain complete source code for this software and any accompanying | ||
| 51 | # software that uses this software. The source code must either be | ||
| 52 | # included in the distribution or be available for no more than the cost | ||
| 53 | # of distribution plus a nominal fee, and must be freely redistributable | ||
| 54 | # under reasonable conditions. For an executable file, complete source | ||
| 55 | # code means the source code for all modules it contains. It does not | ||
| 56 | # include source code for modules or files that typically accompany the | ||
| 57 | # major components of the operating system on which the executable file | ||
| 58 | # runs. | ||
| 59 | # | ||
| 60 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 61 | # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 62 | # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 63 | # PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
| 64 | # COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 65 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 66 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
| 67 | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| 68 | # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 69 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
| 70 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
diff --git a/mps/code/mpstd.h b/mps/code/mpstd.h index c6343b7db59..3977e63a5af 100644 --- a/mps/code/mpstd.h +++ b/mps/code/mpstd.h | |||
| @@ -217,8 +217,7 @@ | |||
| 217 | #define MPS_WORD_SHIFT 5 | 217 | #define MPS_WORD_SHIFT 5 |
| 218 | #define MPS_PF_ALIGN 8 /* @@@@ not tested */ | 218 | #define MPS_PF_ALIGN 8 /* @@@@ not tested */ |
| 219 | 219 | ||
| 220 | /* GCC 2.95.3, gcc -E -dM | 220 | /* GCC 2.95.3, gcc -E -dM */ |
| 221 | */ | ||
| 222 | 221 | ||
| 223 | #elif defined(__FreeBSD__) && defined (__i386__) && defined (__GNUC__) | 222 | #elif defined(__FreeBSD__) && defined (__i386__) && defined (__GNUC__) |
| 224 | #if defined(CONFIG_PF_STRING) && ! defined(CONFIG_PF_FRI4GC) | 223 | #if defined(CONFIG_PF_STRING) && ! defined(CONFIG_PF_FRI4GC) |
| @@ -235,6 +234,21 @@ | |||
| 235 | #define MPS_WORD_SHIFT 5 | 234 | #define MPS_WORD_SHIFT 5 |
| 236 | #define MPS_PF_ALIGN 4 | 235 | #define MPS_PF_ALIGN 4 |
| 237 | 236 | ||
| 237 | #elif defined(__FreeBSD__) && defined (__x86_64__) && defined (__GNUC__) | ||
| 238 | #if defined(CONFIG_PF_STRING) && ! defined(CONFIG_PF_FRI6GC) | ||
| 239 | #error "specified CONFIG_PF_... inconsistent with detected fri6gc" | ||
| 240 | #endif | ||
| 241 | #define MPS_PF_FRI6GC | ||
| 242 | #define MPS_PF_STRING "fri6gc" | ||
| 243 | #define MPS_OS_FR | ||
| 244 | #define MPS_ARCH_I6 | ||
| 245 | #define MPS_BUILD_GC | ||
| 246 | #define MPS_T_WORD unsigned long | ||
| 247 | #define MPS_T_ULONGEST unsigned long | ||
| 248 | #define MPS_WORD_WIDTH 64 | ||
| 249 | #define MPS_WORD_SHIFT 6 | ||
| 250 | #define MPS_PF_ALIGN 8 | ||
| 251 | |||
| 238 | #else | 252 | #else |
| 239 | #error "Unable to detect target platform" | 253 | #error "Unable to detect target platform" |
| 240 | #endif | 254 | #endif |
diff --git a/mps/code/prmci3fr.c b/mps/code/prmci3fr.c new file mode 100644 index 00000000000..6dbe5de09ae --- /dev/null +++ b/mps/code/prmci3fr.c | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | /* prmci3fr.c: PROTECTION MUTATOR CONTEXT INTEL 386 (FREEBSD) | ||
| 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.i486: Intel486 Microprocessor Family Programmer's | ||
| 13 | * Reference Manual | ||
| 14 | * | ||
| 15 | * | ||
| 16 | * ASSUMPTIONS | ||
| 17 | * | ||
| 18 | * .sp: The stack pointer in the context is ESP (x86) or RSP (x86_64). | ||
| 19 | * | ||
| 20 | * .context.regroots: The root regs are EDI, ESI, EBX, EDX, ECX, EAX (or | ||
| 21 | * their x86_64 equivalents) are assumed to be recorded in the context at | ||
| 22 | * pointer-aligned boundaries. | ||
| 23 | */ | ||
| 24 | |||
| 25 | #include "prmcix.h" | ||
| 26 | #include "prmci3.h" | ||
| 27 | |||
| 28 | SRCID(prmci3fr, "$Id$"); | ||
| 29 | |||
| 30 | |||
| 31 | Addr MutatorFaultContextSP(MutatorFaultContext mfc) | ||
| 32 | { | ||
| 33 | return (Addr)mfc->ucontext->uc_mcontext.mc_esp; /* .sp */ | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | Res MutatorFaultContextScan(ScanState ss, MutatorFaultContext mfc) | ||
| 38 | { | ||
| 39 | Res res; | ||
| 40 | |||
| 41 | /* This scans the root registers (.context.regroots). It also unnecessarily | ||
| 42 | scans the rest of the context. The optimisation to scan only relevant | ||
| 43 | parts would be machine dependent. */ | ||
| 44 | res = TraceScanAreaTagged( | ||
| 45 | ss, | ||
| 46 | (Addr *)mfc->ucontext, | ||
| 47 | (Addr *)((char *)mfc->ucontext + sizeof(*(mfc->ucontext))) | ||
| 48 | ); | ||
| 49 | |||
| 50 | return res; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | /* C. COPYRIGHT AND LICENSE | ||
| 55 | * | ||
| 56 | * Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>. | ||
| 57 | * All rights reserved. This is an open source license. Contact | ||
| 58 | * Ravenbrook for commercial licensing options. | ||
| 59 | * | ||
| 60 | * Redistribution and use in source and binary forms, with or without | ||
| 61 | * modification, are permitted provided that the following conditions are | ||
| 62 | * met: | ||
| 63 | * | ||
| 64 | * 1. Redistributions of source code must retain the above copyright | ||
| 65 | * notice, this list of conditions and the following disclaimer. | ||
| 66 | * | ||
| 67 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 68 | * notice, this list of conditions and the following disclaimer in the | ||
| 69 | * documentation and/or other materials provided with the distribution. | ||
| 70 | * | ||
| 71 | * 3. Redistributions in any form must be accompanied by information on how | ||
| 72 | * to obtain complete source code for this software and any accompanying | ||
| 73 | * software that uses this software. The source code must either be | ||
| 74 | * included in the distribution or be available for no more than the cost | ||
| 75 | * of distribution plus a nominal fee, and must be freely redistributable | ||
| 76 | * under reasonable conditions. For an executable file, complete source | ||
| 77 | * code means the source code for all modules it contains. It does not | ||
| 78 | * include source code for modules or files that typically accompany the | ||
| 79 | * major components of the operating system on which the executable file | ||
| 80 | * runs. | ||
| 81 | * | ||
| 82 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 83 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 84 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 85 | * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
| 86 | * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 87 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 88 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
| 89 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| 90 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 91 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
| 92 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 93 | */ | ||
diff --git a/mps/code/prmci6fr.c b/mps/code/prmci6fr.c new file mode 100644 index 00000000000..811d2c8f99d --- /dev/null +++ b/mps/code/prmci6fr.c | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | /* prmci6li.c: PROTECTION MUTATOR CONTEXT x64 (FREEBSD) | ||
| 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 | * ASSUMPTIONS | ||
| 11 | * | ||
| 12 | * .sp: The stack pointer in the context is ESP (x86) or RSP (x86_64). | ||
| 13 | * | ||
| 14 | * .context.regroots: The root regs are EDI, ESI, EBX, EDX, ECX, EAX (or | ||
| 15 | * their x86_64 equivalents) are assumed to be recorded in the context at | ||
| 16 | * pointer-aligned boundaries. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include "prmcix.h" | ||
| 20 | #include "prmci6.h" | ||
| 21 | |||
| 22 | SRCID(prmci6fr, "$Id$"); | ||
| 23 | |||
| 24 | |||
| 25 | Addr MutatorFaultContextSP(MutatorFaultContext mfc) | ||
| 26 | { | ||
| 27 | return (Addr)mfc->ucontext->uc_mcontext.mc_rsp; /* .sp */ | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | Res MutatorFaultContextScan(ScanState ss, MutatorFaultContext mfc) | ||
| 32 | { | ||
| 33 | Res res; | ||
| 34 | |||
| 35 | /* This scans the root registers (.context.regroots). It also unnecessarily | ||
| 36 | scans the rest of the context. The optimisation to scan only relevant | ||
| 37 | parts would be machine dependent. */ | ||
| 38 | res = TraceScanAreaTagged( | ||
| 39 | ss, | ||
| 40 | (Addr *)mfc->ucontext, | ||
| 41 | (Addr *)((char *)mfc->ucontext + sizeof(*(mfc->ucontext))) | ||
| 42 | ); | ||
| 43 | |||
| 44 | return res; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | /* C. COPYRIGHT AND LICENSE | ||
| 49 | * | ||
| 50 | * Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>. | ||
| 51 | * All rights reserved. This is an open source license. Contact | ||
| 52 | * Ravenbrook for commercial licensing options. | ||
| 53 | * | ||
| 54 | * Redistribution and use in source and binary forms, with or without | ||
| 55 | * modification, are permitted provided that the following conditions are | ||
| 56 | * met: | ||
| 57 | * | ||
| 58 | * 1. Redistributions of source code must retain the above copyright | ||
| 59 | * notice, this list of conditions and the following disclaimer. | ||
| 60 | * | ||
| 61 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 62 | * notice, this list of conditions and the following disclaimer in the | ||
| 63 | * documentation and/or other materials provided with the distribution. | ||
| 64 | * | ||
| 65 | * 3. Redistributions in any form must be accompanied by information on how | ||
| 66 | * to obtain complete source code for this software and any accompanying | ||
| 67 | * software that uses this software. The source code must either be | ||
| 68 | * included in the distribution or be available for no more than the cost | ||
| 69 | * of distribution plus a nominal fee, and must be freely redistributable | ||
| 70 | * under reasonable conditions. For an executable file, complete source | ||
| 71 | * code means the source code for all modules it contains. It does not | ||
| 72 | * include source code for modules or files that typically accompany the | ||
| 73 | * major components of the operating system on which the executable file | ||
| 74 | * runs. | ||
| 75 | * | ||
| 76 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 77 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 78 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 79 | * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
| 80 | * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 81 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 82 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
| 83 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| 84 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 85 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
| 86 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 87 | */ | ||
diff --git a/mps/code/thfri4.c b/mps/code/thfri4.c deleted file mode 100644 index 8e109d2b788..00000000000 --- a/mps/code/thfri4.c +++ /dev/null | |||
| @@ -1,345 +0,0 @@ | |||
| 1 | /* thfri3.c: Threads Manager for Intel x86 systems on FreeBSD | ||
| 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 ESP. | ||
| 35 | * .context.regroots: The root regs are EDI, ESI, EBX, EDX, ECX, EAX 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_FR) || !defined(MPS_ARCH_I4) | ||
| 43 | #error "Compiling thfri4 when MPS_OS_FR or MPS_ARCH_I4 not defined." | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #include <pthread.h> | ||
| 47 | #include "pthrdext.h" | ||
| 48 | |||
| 49 | SRCID(thfri4, "$Id$"); | ||
| 50 | |||
| 51 | |||
| 52 | /* ThreadStruct -- thread desriptor */ | ||
| 53 | |||
| 54 | typedef struct ThreadStruct { /* PThreads thread structure */ | ||
| 55 | Sig sig; /* <design/sig/> */ | ||
| 56 | Serial serial; /* from arena->threadSerial */ | ||
| 57 | Arena arena; /* owning arena */ | ||
| 58 | RingStruct arenaRing; /* threads attached to arena */ | ||
| 59 | PThreadextStruct thrextStruct; /* PThreads extension */ | ||
| 60 | pthread_t id; /* Pthread object of thread */ | ||
| 61 | MutatorFaultContext mfc; /* Context if thread is suspended */ | ||
| 62 | } ThreadStruct; | ||
| 63 | |||
| 64 | |||
| 65 | /* ThreadCheck -- check a thread */ | ||
| 66 | |||
| 67 | Bool ThreadCheck(Thread thread) | ||
| 68 | { | ||
| 69 | CHECKS(Thread, thread); | ||
| 70 | CHECKU(Arena, thread->arena); | ||
| 71 | CHECKL(thread->serial < thread->arena->threadSerial); | ||
| 72 | CHECKL(RingCheck(&thread->arenaRing)); | ||
| 73 | CHECKD(PThreadext, &thread->thrextStruct); | ||
| 74 | return TRUE; | ||
| 75 | } | ||
| 76 | |||
| 77 | Bool ThreadCheckSimple(Thread thread) | ||
| 78 | { | ||
| 79 | CHECKS(Thread, thread); | ||
| 80 | return TRUE; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | /* ThreadRegister -- register a thread with an arena */ | ||
| 85 | |||
| 86 | Res ThreadRegister(Thread *threadReturn, Arena arena) | ||
| 87 | { | ||
| 88 | Res res; | ||
| 89 | Thread thread; | ||
| 90 | void *p; | ||
| 91 | |||
| 92 | AVER(threadReturn != NULL); | ||
| 93 | AVERT(Arena, arena); | ||
| 94 | |||
| 95 | res = ControlAlloc(&p, arena, sizeof(ThreadStruct), | ||
| 96 | /* withReservoirPermit */ FALSE); | ||
| 97 | if(res != ResOK) | ||
| 98 | return res; | ||
| 99 | thread = (Thread)p; | ||
| 100 | |||
| 101 | thread->id = pthread_self(); | ||
| 102 | |||
| 103 | RingInit(&thread->arenaRing); | ||
| 104 | |||
| 105 | thread->sig = ThreadSig; | ||
| 106 | thread->serial = arena->threadSerial; | ||
| 107 | ++arena->threadSerial; | ||
| 108 | thread->arena = arena; | ||
| 109 | thread->mfc = NULL; | ||
| 110 | |||
| 111 | PThreadextInit(&thread->thrextStruct, thread->id); | ||
| 112 | |||
| 113 | AVERT(Thread, thread); | ||
| 114 | |||
| 115 | RingAppend(ArenaThreadRing(arena), &thread->arenaRing); | ||
| 116 | |||
| 117 | *threadReturn = thread; | ||
| 118 | return ResOK; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | /* ThreadDeregister -- deregister a thread from an arena */ | ||
| 123 | |||
| 124 | void ThreadDeregister(Thread thread, Arena arena) | ||
| 125 | { | ||
| 126 | AVERT(Thread, thread); | ||
| 127 | AVERT(Arena, arena); | ||
| 128 | |||
| 129 | RingRemove(&thread->arenaRing); | ||
| 130 | |||
| 131 | thread->sig = SigInvalid; | ||
| 132 | |||
| 133 | RingFinish(&thread->arenaRing); | ||
| 134 | |||
| 135 | PThreadextFinish(&thread->thrextStruct); | ||
| 136 | |||
| 137 | ControlFree(arena, thread, sizeof(ThreadStruct)); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | /* mapThreadRing -- map over threads on ring calling a function on each one | ||
| 142 | * except the current thread | ||
| 143 | */ | ||
| 144 | |||
| 145 | static void mapThreadRing(Ring threadRing, void (*func)(Thread)) | ||
| 146 | { | ||
| 147 | Ring node, next; | ||
| 148 | pthread_t self; | ||
| 149 | |||
| 150 | AVERT(Ring, threadRing); | ||
| 151 | |||
| 152 | self = pthread_self(); | ||
| 153 | RING_FOR(node, threadRing, next) { | ||
| 154 | Thread thread = RING_ELT(Thread, arenaRing, node); | ||
| 155 | AVERT(Thread, thread); | ||
| 156 | if(! pthread_equal(self, thread->id)) /* .thread.id */ | ||
| 157 | (*func)(thread); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | /* ThreadRingSuspend -- suspend all threads on a ring, expect the current one */ | ||
| 163 | |||
| 164 | |||
| 165 | static void threadSuspend(Thread thread) | ||
| 166 | { | ||
| 167 | /* .error.suspend */ | ||
| 168 | /* In the error case (PThreadextSuspend returning ResFAIL), we */ | ||
| 169 | /* assume the thread has been destroyed. */ | ||
| 170 | /* In which case we simply continue. */ | ||
| 171 | Res res; | ||
| 172 | res = PThreadextSuspend(&thread->thrextStruct, &thread->mfc); | ||
| 173 | if(res != ResOK) | ||
| 174 | thread->mfc = NULL; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | void ThreadRingSuspend(Ring threadRing) | ||
| 180 | { | ||
| 181 | mapThreadRing(threadRing, threadSuspend); | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | /* ThreadRingResume -- resume all threads on a ring (expect the current one) */ | ||
| 186 | |||
| 187 | |||
| 188 | static void threadResume(Thread thread) | ||
| 189 | { | ||
| 190 | /* .error.resume */ | ||
| 191 | /* If the previous suspend failed (thread->mfc == NULL), */ | ||
| 192 | /* or in the error case (PThreadextResume returning ResFAIL), */ | ||
| 193 | /* assume the thread has been destroyed. */ | ||
| 194 | /* In which case we simply continue. */ | ||
| 195 | if(thread->mfc != NULL) { | ||
| 196 | (void)PThreadextResume(&thread->thrextStruct); | ||
| 197 | thread->mfc = NULL; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | void ThreadRingResume(Ring threadRing) | ||
| 202 | { | ||
| 203 | mapThreadRing(threadRing, threadResume); | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | /* ThreadRingThread -- return the thread at the given ring element */ | ||
| 208 | |||
| 209 | Thread ThreadRingThread(Ring threadRing) | ||
| 210 | { | ||
| 211 | Thread thread; | ||
| 212 | AVERT(Ring, threadRing); | ||
| 213 | thread = RING_ELT(Thread, arenaRing, threadRing); | ||
| 214 | AVERT(Thread, thread); | ||
| 215 | return thread; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | /* ThreadArena -- get the arena of a thread | ||
| 220 | * | ||
| 221 | * Must be thread-safe. See <design/interface-c/#thread-safety>. | ||
| 222 | */ | ||
| 223 | |||
| 224 | Arena ThreadArena(Thread thread) | ||
| 225 | { | ||
| 226 | /* Can't check thread as that would not be thread-safe. */ | ||
| 227 | return thread->arena; | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | /* ThreadScan -- scan the state of a thread (stack and regs) */ | ||
| 232 | |||
| 233 | Res ThreadScan(ScanState ss, Thread thread, void *stackBot) | ||
| 234 | { | ||
| 235 | pthread_t self; | ||
| 236 | Res res; | ||
| 237 | |||
| 238 | AVERT(Thread, thread); | ||
| 239 | self = pthread_self(); | ||
| 240 | if(pthread_equal(self, thread->id)) { | ||
| 241 | /* scan this thread's stack */ | ||
| 242 | res = StackScan(ss, stackBot); | ||
| 243 | if(res != ResOK) | ||
| 244 | return res; | ||
| 245 | } else { | ||
| 246 | MutatorFaultContext mfc; | ||
| 247 | Addr *stackBase, *stackLimit, stackPtr; | ||
| 248 | |||
| 249 | mfc = thread->mfc; | ||
| 250 | if(mfc == NULL) { | ||
| 251 | /* .error.suspend */ | ||
| 252 | /* We assume that the thread must have been destroyed. */ | ||
| 253 | /* We ignore the situation by returning immediately. */ | ||
| 254 | return ResOK; | ||
| 255 | } | ||
| 256 | |||
| 257 | stackPtr = (Addr)mfc->ucontext->uc_mcontext.mc_esp; /* .i3.sp */ | ||
| 258 | /* .stack.align */ | ||
| 259 | stackBase = (Addr *)AddrAlignUp(stackPtr, sizeof(Addr)); | ||
| 260 | stackLimit = (Addr *)stackBot; | ||
| 261 | if (stackBase >= stackLimit) | ||
| 262 | return ResOK; /* .stack.below-bottom */ | ||
| 263 | |||
| 264 | /* scan stack inclusive of current sp and exclusive of | ||
| 265 | * stackBot (.stack.full-descend) | ||
| 266 | */ | ||
| 267 | res = TraceScanAreaTagged(ss, stackBase, stackLimit); | ||
| 268 | if(res != ResOK) | ||
| 269 | return res; | ||
| 270 | |||
| 271 | /* (.context.regroots) | ||
| 272 | * This scans the root registers (.context.regroots). It also | ||
| 273 | * unecessarily scans the rest of the context. The optimisation | ||
| 274 | * to scan only relevent parts would be machine dependent. | ||
| 275 | */ | ||
| 276 | res = TraceScanAreaTagged(ss, (Addr *)mfc->ucontext, | ||
| 277 | (Addr *)((char *)mfc->ucontext + sizeof(*(mfc->ucontext)))); | ||
| 278 | if(res != ResOK) | ||
| 279 | return res; | ||
| 280 | } | ||
| 281 | |||
| 282 | return ResOK; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | /* ThreadDescribe -- describe a thread */ | ||
| 287 | |||
| 288 | Res ThreadDescribe(Thread thread, mps_lib_FILE *stream) | ||
| 289 | { | ||
| 290 | Res res; | ||
| 291 | |||
| 292 | res = WriteF(stream, | ||
| 293 | "Thread $P ($U) {\n", (WriteFP)thread, (WriteFU)thread->serial, | ||
| 294 | " arena $P ($U)\n", | ||
| 295 | (WriteFP)thread->arena, (WriteFU)thread->arena->serial, | ||
| 296 | " id $U\n", (WriteFU)thread->id, | ||
| 297 | "} Thread $P ($U)\n", (WriteFP)thread, (WriteFU)thread->serial, | ||
| 298 | NULL); | ||
| 299 | if(res != ResOK) | ||
| 300 | return res; | ||
| 301 | |||
| 302 | return ResOK; | ||
| 303 | } | ||
| 304 | |||
| 305 | |||
| 306 | /* C. COPYRIGHT AND LICENSE | ||
| 307 | * | ||
| 308 | * Copyright (C) 2001-2002 Ravenbrook Limited <http://www.ravenbrook.com/>. | ||
| 309 | * All rights reserved. This is an open source license. Contact | ||
| 310 | * Ravenbrook for commercial licensing options. | ||
| 311 | * | ||
| 312 | * Redistribution and use in source and binary forms, with or without | ||
| 313 | * modification, are permitted provided that the following conditions are | ||
| 314 | * met: | ||
| 315 | * | ||
| 316 | * 1. Redistributions of source code must retain the above copyright | ||
| 317 | * notice, this list of conditions and the following disclaimer. | ||
| 318 | * | ||
| 319 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 320 | * notice, this list of conditions and the following disclaimer in the | ||
| 321 | * documentation and/or other materials provided with the distribution. | ||
| 322 | * | ||
| 323 | * 3. Redistributions in any form must be accompanied by information on how | ||
| 324 | * to obtain complete source code for this software and any accompanying | ||
| 325 | * software that uses this software. The source code must either be | ||
| 326 | * included in the distribution or be available for no more than the cost | ||
| 327 | * of distribution plus a nominal fee, and must be freely redistributable | ||
| 328 | * under reasonable conditions. For an executable file, complete source | ||
| 329 | * code means the source code for all modules it contains. It does not | ||
| 330 | * include source code for modules or files that typically accompany the | ||
| 331 | * major components of the operating system on which the executable file | ||
| 332 | * runs. | ||
| 333 | * | ||
| 334 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 335 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| 336 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 337 | * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
| 338 | * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 339 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 340 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
| 341 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
| 342 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 343 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
| 344 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 345 | */ | ||