summaryrefslogtreecommitdiff
path: root/system/libucontext/powerpc.patch
blob: e6df4cf2551c3b466413f625859a2b7395a8db1c (plain) (blame)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
From 2610c7faa7b9c4114d53bb659264a6be238cd2a3 Mon Sep 17 00:00:00 2001
From: Bobby Bingham <koorogi@koorogi.info>
Date: Sat, 23 Feb 2019 17:12:37 -0600
Subject: [PATCH] ppc32/64: rewrite get/set/swapcontext in assembly

getcontext cannot be correctly implemented in C.

If this calls another function, as it does to call syscall, it needs to
first spill its return address to the stack.  If, after getcontext returns,
its caller then calls other functions, this saved return address can be
clobbered.  When the context saved by getcontext is later restored, the
(now clobbered) return address will be reloaded from the stack, and the
second return from getcontext will return to the wrong location.

Because the powerpc swapcontext syscall allows either the old context or
new context pointers to be null, it is usable for implementing all of
get/set/swapcontext.

We therefore rewrite swapcontext in assembly, and get/setcontext as simple
assembly function wrappers around swapcontext.

The one piece we keep in C is the code to check the return value of the
system call and to set errno.  This code was actually unnecessary before --
libc does this within syscall.  However, now that the system call is made
directly in assembly, bypassing libc, it is truly necessary.  Because errno
is thread-local and the details of how to set it can vary by libc, this
code remains written in C.
---
 Makefile                                      | 10 +----
 README.md                                     |  2 +-
 arch/ppc/getcontext.S                         | 20 +++++++++
 arch/ppc/{getcontext.c => retfromsyscall.c}   | 30 +++----------
 arch/ppc/setcontext.S                         | 21 +++++++++
 arch/ppc/setcontext.c                         | 45 -------------------
 arch/ppc/swapcontext.S                        | 23 ++++++++++
 arch/ppc/swapcontext.c                        | 45 -------------------
 arch/ppc64/getcontext.S                       | 25 +++++++++++
 arch/ppc64/{getcontext.c => retfromsyscall.c} | 30 +++----------
 arch/ppc64/setcontext.S                       | 26 +++++++++++
 arch/ppc64/setcontext.c                       | 45 -------------------
 arch/ppc64/swapcontext.S                      | 28 ++++++++++++
 arch/ppc64/swapcontext.c                      | 45 -------------------
 14 files changed, 156 insertions(+), 239 deletions(-)
 create mode 100644 arch/ppc/getcontext.S
 rename arch/ppc/{getcontext.c => retfromsyscall.c} (53%)
 create mode 100644 arch/ppc/setcontext.S
 delete mode 100644 arch/ppc/setcontext.c
 create mode 100644 arch/ppc/swapcontext.S
 delete mode 100644 arch/ppc/swapcontext.c
 create mode 100644 arch/ppc64/getcontext.S
 rename arch/ppc64/{getcontext.c => retfromsyscall.c} (53%)
 create mode 100644 arch/ppc64/setcontext.S
 delete mode 100644 arch/ppc64/setcontext.c
 create mode 100644 arch/ppc64/swapcontext.S
 delete mode 100644 arch/ppc64/swapcontext.c

diff --git a/Makefile b/Makefile
index 51365a3..d6ff1b0 100644
--- a/Makefile
+++ b/Makefile
@@ -2,14 +2,8 @@ ARCH := $(shell uname -m)
 
 CFLAGS = -ggdb3 -O2 -Wall -Iarch/${ARCH}
 
-LIBUCONTEXT_C_SRC = \
-	arch/${ARCH}/makecontext.c
-
-LIBUCONTEXT_S_SRC = \
-	arch/${ARCH}/getcontext.S \
-	arch/${ARCH}/setcontext.S \
-	arch/${ARCH}/swapcontext.S \
-	arch/${ARCH}/startcontext.S
+LIBUCONTEXT_C_SRC = $(wildcard arch/${ARCH}/*.c)
+LIBUCONTEXT_S_SRC = $(wildcard arch/${ARCH}/*.S)
 
 LIBUCONTEXT_OBJ = ${LIBUCONTEXT_C_SRC:.c=.o} ${LIBUCONTEXT_S_SRC:.S=.o}
 LIBUCONTEXT_SOVERSION = 0
diff --git a/README.md b/README.md
index 6bd4493..27d3d0b 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,7 @@ Right now these archs are supported and should work on bare metal:
  * aarch64
  * s390x
 
-These archs require kernel assistance and use a syscall (the only assembly is the trampoline):
+These archs require kernel assistance and use a syscall:
 
  * ppc
  * ppc64 (ELFv2 ABI spec only, ELFv1 not supported)
diff --git a/arch/ppc/getcontext.S b/arch/ppc/getcontext.S
new file mode 100644
index 0000000..beffaf5
--- /dev/null
+++ b/arch/ppc/getcontext.S
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * This software is provided 'as is' and without any warranty, express or
+ * implied.  In no event shall the authors be liable for any damages arising
+ * from the use of this software.
+ */
+
+.global __getcontext
+.hidden __swapcontext
+__getcontext:
+	li 4, 0
+	b __swapcontext@local
+
+.weak getcontext
+getcontext = __getcontext
diff --git a/arch/ppc/getcontext.c b/arch/ppc/retfromsyscall.c
similarity index 53%
rename from arch/ppc/getcontext.c
rename to arch/ppc/retfromsyscall.c
index 5da9dfb..3ad43c0 100644
--- a/arch/ppc/getcontext.c
+++ b/arch/ppc/retfromsyscall.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -10,36 +11,15 @@
  * from the use of this software.
  */
 
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
 #include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
 
-
-int
-__getcontext(ucontext_t *ucp)
+__attribute__ ((visibility ("hidden")))
+int __retfromsyscall(long retval)
 {
-#ifdef SYS_swapcontext
-	int r;
-
-	r = syscall(SYS_swapcontext, ucp, NULL, sizeof(ucontext_t));
-	if (r < 0)
-	{
-		errno = -r;
+	if (retval < 0) {
+		errno = -retval;
 		return -1;
 	}
-
 	return 0;
-#else
-	errno = ENOSYS;
-	return -1;
-#endif
 }
 
-
-extern __typeof(__getcontext) getcontext __attribute__((weak, __alias__("__getcontext")));
diff --git a/arch/ppc/setcontext.S b/arch/ppc/setcontext.S
new file mode 100644
index 0000000..89e6de0
--- /dev/null
+++ b/arch/ppc/setcontext.S
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * This software is provided 'as is' and without any warranty, express or
+ * implied.  In no event shall the authors be liable for any damages arising
+ * from the use of this software.
+ */
+
+.global __setcontext
+.hidden __swapcontext
+__setcontext:
+	mr 4, 3
+	li 3, 0
+	b __swapcontext@local
+
+.weak setcontext
+setcontext = __setcontext
diff --git a/arch/ppc/setcontext.c b/arch/ppc/setcontext.c
deleted file mode 100644
index 59c65b4..0000000
--- a/arch/ppc/setcontext.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * This software is provided 'as is' and without any warranty, express or
- * implied.  In no event shall the authors be liable for any damages arising
- * from the use of this software.
- */
-
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-
-int
-__setcontext(const ucontext_t *ucp)
-{
-#ifdef SYS_swapcontext
-	int r;
-
-	r = syscall(SYS_swapcontext, NULL, (void *) ucp, sizeof(ucontext_t));
-	if (r < 0)
-	{
-		errno = -r;
-		return -1;
-	}
-
-	return r;
-#else
-	errno = ENOSYS;
-	return -1;
-#endif
-}
-
-
-extern __typeof(__setcontext) setcontext __attribute__((weak, __alias__("__setcontext")));
diff --git a/arch/ppc/swapcontext.S b/arch/ppc/swapcontext.S
new file mode 100644
index 0000000..24b6655
--- /dev/null
+++ b/arch/ppc/swapcontext.S
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * This software is provided 'as is' and without any warranty, express or
+ * implied.  In no event shall the authors be liable for any damages arising
+ * from the use of this software.
+ */
+
+.global __swapcontext
+__swapcontext:
+	li 0, 249     # SYS_swapcontext
+	li 5, 1184    # sizeof(ucontext_t)
+	sc
+
+.hidden __retfromsyscall
+	b __retfromsyscall@local
+
+.weak swapcontext
+swapcontext = __swapcontext
diff --git a/arch/ppc/swapcontext.c b/arch/ppc/swapcontext.c
deleted file mode 100644
index af14bc2..0000000
--- a/arch/ppc/swapcontext.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * This software is provided 'as is' and without any warranty, express or
- * implied.  In no event shall the authors be liable for any damages arising
- * from the use of this software.
- */
-
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-
-int
-__swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
-{
-#ifdef SYS_swapcontext
-	int r;
-
-	r = syscall(SYS_swapcontext, oucp, ucp, sizeof(ucontext_t));
-	if (r < 0)
-	{
-		errno = -r;
-		return -1;
-	}
-
-	return r;
-#else
-	errno = ENOSYS;
-	return -1;
-#endif
-}
-
-
-extern __typeof(__swapcontext) swapcontext __attribute__((weak, __alias__("__swapcontext")));
diff --git a/arch/ppc64/getcontext.S b/arch/ppc64/getcontext.S
new file mode 100644
index 0000000..935edd2
--- /dev/null
+++ b/arch/ppc64/getcontext.S
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * This software is provided 'as is' and without any warranty, express or
+ * implied.  In no event shall the authors be liable for any damages arising
+ * from the use of this software.
+ */
+
+.global __getcontext
+.hidden __swapcontext
+__getcontext:
+	addis 2, 12, .TOC.-__getcontext@ha
+	addi  2, 12, .TOC.-__getcontext@l
+
+	.localentry __getcontext,.-__getcontext
+
+	li 4, 0
+	b __swapcontext
+
+.weak getcontext
+getcontext = __getcontext
diff --git a/arch/ppc64/getcontext.c b/arch/ppc64/retfromsyscall.c
similarity index 53%
rename from arch/ppc64/getcontext.c
rename to arch/ppc64/retfromsyscall.c
index 5da9dfb..3ad43c0 100644
--- a/arch/ppc64/getcontext.c
+++ b/arch/ppc64/retfromsyscall.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -10,36 +11,15 @@
  * from the use of this software.
  */
 
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
 #include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
 
-
-int
-__getcontext(ucontext_t *ucp)
+__attribute__ ((visibility ("hidden")))
+int __retfromsyscall(long retval)
 {
-#ifdef SYS_swapcontext
-	int r;
-
-	r = syscall(SYS_swapcontext, ucp, NULL, sizeof(ucontext_t));
-	if (r < 0)
-	{
-		errno = -r;
+	if (retval < 0) {
+		errno = -retval;
 		return -1;
 	}
-
 	return 0;
-#else
-	errno = ENOSYS;
-	return -1;
-#endif
 }
 
-
-extern __typeof(__getcontext) getcontext __attribute__((weak, __alias__("__getcontext")));
diff --git a/arch/ppc64/setcontext.S b/arch/ppc64/setcontext.S
new file mode 100644
index 0000000..5a0cde3
--- /dev/null
+++ b/arch/ppc64/setcontext.S
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * This software is provided 'as is' and without any warranty, express or
+ * implied.  In no event shall the authors be liable for any damages arising
+ * from the use of this software.
+ */
+
+.global __setcontext
+.hidden __swapcontext
+__setcontext:
+	addis 2, 12, .TOC.-__setcontext@ha
+	addi  2, 12, .TOC.-__setcontext@l
+
+	.localentry __setcontext,.-__setcontext
+
+	mr 4, 3
+	li 3, 0
+	b __swapcontext
+
+.weak setcontext
+setcontext = __setcontext
diff --git a/arch/ppc64/setcontext.c b/arch/ppc64/setcontext.c
deleted file mode 100644
index 59c65b4..0000000
--- a/arch/ppc64/setcontext.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * This software is provided 'as is' and without any warranty, express or
- * implied.  In no event shall the authors be liable for any damages arising
- * from the use of this software.
- */
-
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-
-int
-__setcontext(const ucontext_t *ucp)
-{
-#ifdef SYS_swapcontext
-	int r;
-
-	r = syscall(SYS_swapcontext, NULL, (void *) ucp, sizeof(ucontext_t));
-	if (r < 0)
-	{
-		errno = -r;
-		return -1;
-	}
-
-	return r;
-#else
-	errno = ENOSYS;
-	return -1;
-#endif
-}
-
-
-extern __typeof(__setcontext) setcontext __attribute__((weak, __alias__("__setcontext")));
diff --git a/arch/ppc64/swapcontext.S b/arch/ppc64/swapcontext.S
new file mode 100644
index 0000000..982537a
--- /dev/null
+++ b/arch/ppc64/swapcontext.S
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2019 Bobby Bingham <koorogi@koorogi.info>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * This software is provided 'as is' and without any warranty, express or
+ * implied.  In no event shall the authors be liable for any damages arising
+ * from the use of this software.
+ */
+
+.global __swapcontext
+__swapcontext:
+	addis 2, 12, .TOC.-__swapcontext@ha
+	addi  2, 12, .TOC.-__swapcontext@l
+
+	.localentry __swapcontext,.-__swapcontext
+
+	li 0, 249     # SYS_swapcontext
+	li 5, 1696    # sizeof(ucontext_t)
+	sc
+
+.hidden __retfromsyscall
+	b __retfromsyscall
+
+.weak swapcontext
+swapcontext = __swapcontext
diff --git a/arch/ppc64/swapcontext.c b/arch/ppc64/swapcontext.c
deleted file mode 100644
index af14bc2..0000000
--- a/arch/ppc64/swapcontext.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2018 William Pitcock <nenolod@dereferenced.org>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * This software is provided 'as is' and without any warranty, express or
- * implied.  In no event shall the authors be liable for any damages arising
- * from the use of this software.
- */
-
-#define _GNU_SOURCE
-#include <stddef.h>
-#include <stdarg.h>
-#include <signal.h>
-#include <string.h>
-#include <stdint.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/syscall.h>
-
-
-int
-__swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
-{
-#ifdef SYS_swapcontext
-	int r;
-
-	r = syscall(SYS_swapcontext, oucp, ucp, sizeof(ucontext_t));
-	if (r < 0)
-	{
-		errno = -r;
-		return -1;
-	}
-
-	return r;
-#else
-	errno = ENOSYS;
-	return -1;
-#endif
-}
-
-
-extern __typeof(__swapcontext) swapcontext __attribute__((weak, __alias__("__swapcontext")));