File: run.c

package info (click to toggle)
libspe2 2.2.80-95-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 940 kB
  • ctags: 1,214
  • sloc: ansic: 9,316; makefile: 551; ada: 448; sh: 24
file content (366 lines) | stat: -rw-r--r-- 10,419 bytes parent folder | download
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * libspe2 - A wrapper library to adapt the JSRE SPU usage model to SPUFS
 * Copyright (C) 2005 IBM Corp.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <errno.h>
#define GNU_SOURCE 1

#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <syscall.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/mman.h>

#include <sys/spu.h>

#include "elf_loader.h"
#include "lib_builtin.h"
#include "spebase.h"

/*Thread-local variable for use by the debugger*/
__thread struct spe_context_info {
	int spe_id;
	unsigned int npc;
	unsigned int status;
	struct spe_context_info *prev;
}*__spe_current_active_context;


static void cleanupspeinfo(struct spe_context_info *ctxinfo)
{
	struct spe_context_info *tmp = ctxinfo->prev;
	__spe_current_active_context = tmp;
}

static int set_regs(struct spe_context *spe, void *regs)
{
	int fd_regs, rc;

	fd_regs = openat(spe->base_private->fd_spe_dir, "regs", O_RDWR);
	if (fd_regs < 0) {
		DEBUG_PRINTF("Could not open SPE regs file.\n");
		errno = EFAULT;
		return -1;
	}

	rc = write(fd_regs, regs, 2048);

	close(fd_regs);

	if (rc < 0)
		return -1;

	return 0;
}

static int issue_isolated_exit(struct spe_context *spe)
{
	struct spe_spu_control_area *cntl_area =
		spe->base_private->cntl_mmap_base;

	if (cntl_area == MAP_FAILED) {
		DEBUG_PRINTF("%s: could not access SPE control area\n",
				__FUNCTION__);
		return -1;
	}

	/* 0x2 is an isolated exit request */
	cntl_area->SPU_RunCntl = 0x2;

	return 0;
}

static inline void freespeinfo()
{
	/*Clean up the debug variable*/
	struct spe_context_info *tmp = __spe_current_active_context->prev;
	__spe_current_active_context = tmp;
}

int _base_spe_context_run(spe_context_ptr_t spe, unsigned int *entry,
		unsigned int runflags, void *argp, void *envp,
		spe_stop_info_t *stopinfo)
{
	int retval = 0, run_rc;
	unsigned int run_status, tmp_entry;
	spe_stop_info_t	stopinfo_buf;
	struct spe_context_info this_context_info __attribute__((cleanup(cleanupspeinfo)));

	/* If the caller hasn't set a stopinfo buffer, provide a buffer on the
	 * stack instead. */
	if (!stopinfo)
		stopinfo = &stopinfo_buf;


	/* In emulated isolated mode, the npc will always return as zero.
	 * use our private entry point instead */
	if (spe->base_private->flags & SPE_ISOLATE_EMULATE)
		tmp_entry = spe->base_private->emulated_entry;

	else if (*entry == SPE_DEFAULT_ENTRY)
		tmp_entry = spe->base_private->entry;
	else 
		tmp_entry = *entry;

	/* If we're starting the SPE binary from its original entry point,
	 * setup the arguments to main() */
	if (tmp_entry == spe->base_private->entry &&
			!(spe->base_private->flags &
				(SPE_ISOLATE | SPE_ISOLATE_EMULATE))) {

		addr64 argp64, envp64, tid64, ls64;
		unsigned int regs[128][4];

		/* setup parameters */
		argp64.ull = (uint64_t)(unsigned long)argp;
		envp64.ull = (uint64_t)(unsigned long)envp;
		tid64.ull = (uint64_t)(unsigned long)spe;

		/* make sure the register values are 0 */
		memset(regs, 0, sizeof(regs));

		/* set sensible values for stack_ptr and stack_size */
		regs[1][0] = (unsigned int) LS_SIZE - 16; 	/* stack_ptr */
		regs[2][0] = 0; 							/* stack_size ( 0 = default ) */

		if (runflags & SPE_RUN_USER_REGS) {
			/* When SPE_USER_REGS is set, argp points to an array
			 * of 3x128b registers to be passed directly to the SPE
			 * program.
			 */
			memcpy(regs[3], argp, sizeof(unsigned int) * 12);
		} else {
			regs[3][0] = tid64.ui[0];
			regs[3][1] = tid64.ui[1];

			regs[4][0] = argp64.ui[0];
			regs[4][1] = argp64.ui[1];

			regs[5][0] = envp64.ui[0];
			regs[5][1] = envp64.ui[1];
		}
		
		/* Store the LS base address in R6 */
		ls64.ull = (uint64_t)(unsigned long)spe->base_private->mem_mmap_base;
		regs[6][0] = ls64.ui[0];
		regs[6][1] = ls64.ui[1];

		if (set_regs(spe, regs))
			return -1;
	}

	/*Leave a trail of breadcrumbs for the debugger to follow */
	if (!__spe_current_active_context) {
		__spe_current_active_context = &this_context_info;
		if (!__spe_current_active_context)
			return -1;
		__spe_current_active_context->prev = NULL;
	} else {
		struct spe_context_info *newinfo;
		newinfo = &this_context_info;
		if (!newinfo)
			return -1;
		newinfo->prev = __spe_current_active_context;
		__spe_current_active_context = newinfo;
	}
	/*remember the ls-addr*/
	__spe_current_active_context->spe_id = spe->base_private->fd_spe_dir;

do_run:
	/*Remember the npc value*/
	__spe_current_active_context->npc = tmp_entry;

	/* run SPE context */
	run_rc = spu_run(spe->base_private->fd_spe_dir,
			&tmp_entry, &run_status);

	/*Remember the npc value*/
	__spe_current_active_context->npc = tmp_entry;
	__spe_current_active_context->status = run_status;

	DEBUG_PRINTF("spu_run returned run_rc=0x%08x, entry=0x%04x, "
			"ext_status=0x%04x.\n", run_rc, tmp_entry, run_status);

	/* set up return values and stopinfo according to spu_run exit
	 * conditions. This is overwritten on error.
	 */
	stopinfo->spu_status = run_rc;

	if (spe->base_private->flags & SPE_ISOLATE_EMULATE) {
		/* save the entry point, and pretend that the npc is zero */
		spe->base_private->emulated_entry = tmp_entry;
		*entry = 0;
	} else {
		*entry = tmp_entry;
	}

	/* Return with stopinfo set on syscall error paths */
	if (run_rc == -1) {
		DEBUG_PRINTF("spu_run returned error %d, errno=%d\n",
				run_rc, errno);
		stopinfo->stop_reason = SPE_RUNTIME_FATAL;
		stopinfo->result.spe_runtime_fatal = errno;
		retval = -1;

		/* For isolated contexts, pass EPERM up to the
		 * caller.
		 */
		if (!(spe->base_private->flags & SPE_ISOLATE
				&& errno == EPERM))
			errno = EFAULT;

	} else if (run_rc & SPE_SPU_INVALID_INSTR) {
		DEBUG_PRINTF("SPU has tried to execute an invalid "
				"instruction. %d\n", run_rc);
		stopinfo->stop_reason = SPE_RUNTIME_ERROR;
		stopinfo->result.spe_runtime_error = SPE_SPU_INVALID_INSTR;
		errno = EFAULT;
		retval = -1;

	} else if ((spe->base_private->flags & SPE_EVENTS_ENABLE) && run_status) {
		/* Report asynchronous error if return val are set and
		 * SPU events are enabled.
		 */
		stopinfo->stop_reason = SPE_RUNTIME_EXCEPTION;
		stopinfo->result.spe_runtime_exception = run_status;
		stopinfo->spu_status = -1;
		errno = EIO;
		retval = -1;

	} else if (run_rc & SPE_SPU_STOPPED_BY_STOP) {
		/* Stop & signals are broken down into three groups
		 *  1. SPE library call
		 *  2. SPE user defined stop & signal
		 *  3. SPE program end.
		 *
		 * These groups are signified by the 14-bit stop code:
		 */
		int stopcode = (run_rc >> 16) & 0x3fff;

		/* Check if this is a library callback, and callbacks are
		 * allowed (ie, running without SPE_NO_CALLBACKS)
		 */
		if ((stopcode & 0xff00) == SPE_PROGRAM_LIBRARY_CALL
				&& !(runflags & SPE_NO_CALLBACKS)) {

			int callback_rc, callback_number = stopcode & 0xff;

			/* execute library callback */
			DEBUG_PRINTF("SPE library call: %d\n", callback_number);
			callback_rc = _base_spe_handle_library_callback(spe,
									callback_number, *entry);

			if (callback_rc) {
				/* library callback failed; set errno and
				 * return immediately */
				DEBUG_PRINTF("SPE library call failed: %d\n",
						callback_rc);
				stopinfo->stop_reason = SPE_CALLBACK_ERROR;
				stopinfo->result.spe_callback_error =
					callback_rc;
				errno = EFAULT;
				retval = -1;
			} else {
				/* successful library callback - restart the SPE
				 * program at the next instruction */
				tmp_entry += 4;
				goto do_run;
			}

		} else if ((stopcode & 0xff00) == SPE_PROGRAM_NORMAL_END) {
			/* The SPE program has exited by exit(X) */
			stopinfo->stop_reason = SPE_EXIT;
			stopinfo->result.spe_exit_code = stopcode & 0xff;

			if (spe->base_private->flags & SPE_ISOLATE) {
				/* Issue an isolated exit, and re-run the SPE.
				 * We should see a return value without the
				 * 0x80 bit set. */
				if (!issue_isolated_exit(spe))
					goto do_run;
				retval = -1;
			}

		} else if ((stopcode & 0xfff0) == SPE_PROGRAM_ISOLATED_STOP) {

			/* 0x2206: isolated app has been loaded by loader;
			 * provide a hook for the debugger to catch this,
			 * and restart
			 */
			if (stopcode == SPE_PROGRAM_ISO_LOAD_COMPLETE) {
				_base_spe_program_load_complete(spe);
				goto do_run;
			} else {
				stopinfo->stop_reason = SPE_ISOLATION_ERROR;
				stopinfo->result.spe_isolation_error =
					stopcode & 0xf;
			}

		} else if (spe->base_private->flags & SPE_ISOLATE &&
				!(run_rc & 0x80)) {
			/* We've successfully exited isolated mode */
			retval = 0;

		} else {
			/* User defined stop & signal, including
			 * callbacks when disabled */
			stopinfo->stop_reason = SPE_STOP_AND_SIGNAL;
			stopinfo->result.spe_signal_code = stopcode;
			retval = stopcode;
		}

	} else if (run_rc & SPE_SPU_HALT) {
		DEBUG_PRINTF("SPU was stopped by halt. %d\n", run_rc);
		stopinfo->stop_reason = SPE_RUNTIME_ERROR;
		stopinfo->result.spe_runtime_error = SPE_SPU_HALT;
		errno = EFAULT;
		retval = -1;

	} else if (run_rc & SPE_SPU_WAITING_ON_CHANNEL) {
		DEBUG_PRINTF("SPU is waiting on channel. %d\n", run_rc);
		stopinfo->stop_reason = SPE_RUNTIME_EXCEPTION;
		stopinfo->result.spe_runtime_exception = run_status;
		stopinfo->spu_status = -1;
		errno = EIO;
		retval = -1;

	} else if (run_rc & SPE_SPU_INVALID_CHANNEL) {
		DEBUG_PRINTF("SPU has tried to access an invalid "
				"channel. %d\n", run_rc);
		stopinfo->stop_reason = SPE_RUNTIME_ERROR;
		stopinfo->result.spe_runtime_error = SPE_SPU_INVALID_CHANNEL;
		errno = EFAULT;
		retval = -1;

	} else {
		DEBUG_PRINTF("spu_run returned invalid data: 0x%04x\n", run_rc);
		stopinfo->stop_reason = SPE_RUNTIME_FATAL;
		stopinfo->result.spe_runtime_fatal = -1;
		stopinfo->spu_status = -1;
		errno = EFAULT;
		retval = -1;

	}

	freespeinfo();
	return retval;
}