c_support.c
4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/*****************************************************************
* c_support.c
*
* Routines that are linked in with an application. These routines
* include those used by IRIX and some C-routines that are to be
* emulated by solo mipsy. Backdoor solo routines are found in
* solo_anl.c.
*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <unistd.h>
#include "solo_interface.h"
#include "sys/prctl.h"
static CommArea *comm;
static pid_t firstPID;
static pid_t currentTID;
unsigned int PIDLock;
/* Prototypes for Irix routines */
static void IrixCreate( void (*func)() );
static unsigned long long IrixCurrentTime(void);
static void IrixExit(void);
static int IrixGetCPUNum(void);
static void IrixIOMap(unsigned, long long);
static void IrixWaitForEnd(int numProcs);
static void IrixPlaceRange(unsigned start, unsigned stop, int node);
static void IrixNull(void);
static int IrixNullIntPtr(void *);
static int IrixNullInt(void);
/*****************************************************************
* IrixSetupCommArea
*
* This sets up the pointers will be called when an application is
* running under IRIX.
*****************************************************************/
void
IrixSetupCommArea(void)
{
char *addr;
int fd, length;
/*
* Allocate a file to be mapped and used as a shared memory region
* for communication between solo mipsy and the application
*/
fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
perror("Mapping /dev/zero open (for comm area mapping)");
exit(-1);
}
length = COMM_LENGTH;
addr = (char *) mmap((void *) COMM_START, length,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED, fd, 0);
if (addr != (char *) COMM_START) {
perror("Mapping communication area.\n");
(void) close(fd);
}
comm = (CommArea *)addr;
comm->WaitForEnd = IrixWaitForEnd;
comm->Create = IrixCreate;
comm->CurrentTime = IrixCurrentTime;
comm->Exit = IrixExit;
comm->GetCPUNum = IrixGetCPUNum;
comm->PlaceRange = IrixPlaceRange;
/* Calls that are not supported in IRIX */
comm->TurnOnCaches = IrixNull;
comm->TurnOffCaches = IrixNull;
comm->ResetStats = IrixNull;
comm->ActiveProcs = IrixNullInt;
comm->Flush = IrixNullIntPtr;
comm->IOMap = IrixIOMap;
/* For GetCPUNum, we need to have the original pid of the process. */
firstPID= getpid();
((struct prda *) PRDA)->usr_prda.fill[0] = currentTID = 0;
}
static void
IrixNull(void)
{
}
static int
IrixNullIntPtr(void *foo)
{
}
static int
IrixNullInt(void)
{
}
static void
IrixCreate(void (*func)())
{
int retval;
if ((retval = sproc(func, PR_SADDR, NULL)) == -1) {
fprintf(stderr,"WHOA! Sproc returned %d: \n",retval);
perror("Sproc problem");
exit (-1);
}
_LockEnter(&PIDLock);
((struct prda *) PRDA)->usr_prda.fill[0] = ++currentTID;
printf("[Sproc() of task #%d, PID %d)]\n",
(struct prda *) PRDA->usr_prda.fill[0],
(int)getpid());
_Unlock(&PIDLock);
}
static unsigned long long
IrixCurrentTime(void)
{
struct timeval time;
if (gettimeofday (&time, (struct timezone *)0)) {
perror ("CLOCK macro");
exit (-1);
}
return (unsigned long long) (time.tv_sec & 0x7ff)*1000000 + time.tv_usec;
}
static void
IrixExit(void)
{
exit(0);
}
static int
IrixGetCPUNum(void)
{
return (int)(((struct prda *) PRDA)->usr_prda.fill[0]);
}
static void
IrixIOMap(unsigned a, long long b)
{
fprintf(stderr, "IO Mapping is not currently supported\n");
}
static void
IrixPlaceRange(unsigned start, unsigned stop, int node)
{
}
static void
IrixWaitForEnd(int numProcs)
{
int i;
for (i=1; i<=numProcs; i++)
wait(0);
}
void
initMCSlock(MCSlock L)
{
L->next = 0;
L->locked = 0;
}
void
acquireMCSlock(volatile MCSlock L, volatile MCSlock I)
{
volatile MCSlock pred;
/* printf("before L = %x *L: %x I= %x\n",L,*L,I); */
I->next = NULL;
pred = (MCSlock) _FetchAndStore((void *) L, (int) I);
/* printf("after L = %x *L: %x I= %x\n",L,*L,I); */
if (pred) {
I->locked = 1;
pred->next = I;
while (I->locked) {
}
}
}
void
releaseMCSlock(volatile MCSlock L, volatile MCSlock I)
{
if (I->next == NULL) {
if (_CompareAndSwap((void *) L, (int) I, (int) NULL)) {
return;
}
while ((I->next) == NULL) {
}
}
I->next->locked = NULL;
}
#ifdef FLASHPOINT
void * non_instrumented_malloc(size_t size) {
void * tmp = malloc(size);
return tmp;
}
#endif