forked from checkpoint-restore/criu
/
crtools.c
814 lines (680 loc) · 20.9 KB
/
crtools.c
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#include <string.h>
#include <unistd.h>
#include <elf.h>
#include <sys/user.h>
#include <asm/unistd.h>
#include <sys/uio.h>
#include "asm/types.h"
#include "asm/fpu.h"
#include "asm/restorer.h"
#include "asm/dump.h"
#include "cr_options.h"
#include "compiler.h"
#include "ptrace.h"
#include "parasite-syscall.h"
#include "log.h"
#include "util.h"
#include "cpu.h"
#include "errno.h"
#include "protobuf.h"
#include "images/core.pb-c.h"
#include "images/creds.pb-c.h"
#define MSR_TMA (1UL<<34) /* bit 29 Trans Mem state: Transactional */
#define MSR_TMS (1UL<<33) /* bit 30 Trans Mem state: Suspended */
#define MSR_TM (1UL<<32) /* bit 31 Trans Mem Available */
#define MSR_VEC (1UL<<25)
#define MSR_VSX (1UL<<23)
#define MSR_TM_ACTIVE(x) ((((x) & MSR_TM) && ((x)&(MSR_TMA|MSR_TMS))) != 0)
#ifndef NT_PPC_TM_SPR
#define NT_PPC_TM_CGPR 0x108 /* TM checkpointed GPR Registers */
#define NT_PPC_TM_CFPR 0x109 /* TM checkpointed FPR Registers */
#define NT_PPC_TM_CVMX 0x10a /* TM checkpointed VMX Registers */
#define NT_PPC_TM_CVSX 0x10b /* TM checkpointed VSX Registers */
#define NT_PPC_TM_SPR 0x10c /* TM Special Purpose Registers */
#endif
/*
* Injected syscall instruction
*/
const u32 code_syscall[] = {
0x44000002, /* sc */
0x0fe00000 /* twi 31,0,0 */
};
static inline void __check_code_syscall(void)
{
BUILD_BUG_ON(sizeof(code_syscall) != BUILTIN_SYSCALL_SIZE);
BUILD_BUG_ON(!is_log2(sizeof(code_syscall)));
}
void parasite_setup_regs(unsigned long new_ip, void *stack, user_regs_struct_t *regs)
{
/*
* OpenPOWER ABI requires that r12 is set to the calling function addressi
* to compute the TOC pointer.
*/
regs->gpr[12] = new_ip;
regs->nip = new_ip;
if (stack)
regs->gpr[1] = (unsigned long) stack;
regs->trap = 0;
}
bool arch_can_dump_task(pid_t pid)
{
/*
* TODO: We should detect 32bit task when BE support is done.
*/
return true;
}
int syscall_seized(struct parasite_ctl *ctl, int nr, unsigned long *ret,
unsigned long arg1,
unsigned long arg2,
unsigned long arg3,
unsigned long arg4,
unsigned long arg5,
unsigned long arg6)
{
user_regs_struct_t regs = ctl->orig.regs;
int err;
regs.gpr[0] = (unsigned long)nr;
regs.gpr[3] = arg1;
regs.gpr[4] = arg2;
regs.gpr[5] = arg3;
regs.gpr[6] = arg4;
regs.gpr[7] = arg5;
regs.gpr[8] = arg6;
err = __parasite_execute_syscall(ctl, ®s, (char*)code_syscall);
*ret = regs.gpr[3];
return err;
}
static UserPpc64FpstateEntry *copy_fp_regs(uint64_t *fpregs)
{
UserPpc64FpstateEntry *fpe;
int i;
fpe = xmalloc(sizeof(UserPpc64FpstateEntry));
if (!fpe)
return NULL;
user_ppc64_fpstate_entry__init(fpe);
fpe->n_fpregs = NFPREG;
fpe->fpregs = xmalloc(fpe->n_fpregs * sizeof(fpe->fpregs[0]));
if (!fpe->fpregs) {
xfree(fpe);
return NULL;
}
/* FPSRC is the last (33th) register in the set */
for (i = 0; i < NFPREG; i++)
fpe->fpregs[i] = fpregs[i];
return fpe;
}
/* This is the layout of the POWER7 VSX registers and the way they
* overlap with the existing FPR and VMX registers.
*
* VSR doubleword 0 VSR doubleword 1
* ----------------------------------------------------------------
* VSR[0] | FPR[0] | |
* ----------------------------------------------------------------
* VSR[1] | FPR[1] | |
* ----------------------------------------------------------------
* | ... | |
* ----------------------------------------------------------------
* VSR[30] | FPR[30] | |
* ----------------------------------------------------------------
* VSR[31] | FPR[31] | |
* ----------------------------------------------------------------
* VSR[32] | VR[0] |
* ----------------------------------------------------------------
* VSR[33] | VR[1] |
* ----------------------------------------------------------------
* | ... |
* ----------------------------------------------------------------
* VSR[62] | VR[30] |
* ----------------------------------------------------------------
* VSR[63] | VR[31] |
* ----------------------------------------------------------------
*
* PTRACE_GETFPREGS returns FPR[0..31] + FPSCR
* PTRACE_GETVRREGS returns VR[0..31] + VSCR + VRSAVE
* PTRACE_GETVSRREGS returns VSR[0..31]
*
* PTRACE_GETVSRREGS and PTRACE_GETFPREGS are required since we need
* to save FPSCR too.
*
* There 32 VSX double word registers to save since the 32 first VSX double
* word registers are saved through FPR[0..32] and the remaining registers
* are saved when saving the Altivec registers VR[0..32].
*/
static int get_fpu_regs(pid_t pid, user_fpregs_struct_t *fp)
{
if (ptrace(PTRACE_GETFPREGS, pid, 0, (void *)&fp->fpregs) < 0) {
pr_perror("Couldn't get floating-point registers");
return -1;
}
return 0;
}
static void put_fpu_regs(mcontext_t *mc, UserPpc64FpstateEntry *fpe)
{
int i;
uint64_t *mcfp = (uint64_t *)mc->fp_regs;
for (i = 0; i < fpe->n_fpregs; i++)
mcfp[i] = fpe->fpregs[i];
}
static UserPpc64VrstateEntry *copy_altivec_regs(unsigned char *vrregs)
{
UserPpc64VrstateEntry *vse;
uint64_t *p64;
uint32_t *p32;
int i;
vse = xmalloc(sizeof(*vse));
if (!vse)
return NULL;
user_ppc64_vrstate_entry__init(vse);
/* protocol buffer store only 64bit entries and we need 128bit */
vse->n_vrregs = (NVRREG-1) * 2;
vse->vrregs = xmalloc(vse->n_vrregs * sizeof(vse->vrregs[0]));
if (!vse->vrregs) {
xfree(vse);
return NULL;
}
/* Vectors are 2*64bits entries */
for (i = 0; i < (NVRREG-1); i++) {
p64 = (uint64_t*) &vrregs[i * 2 * sizeof(uint64_t)];
vse->vrregs[i*2] = p64[0];
vse->vrregs[i*2 + 1] = p64[1];
}
p32 = (uint32_t*) &vrregs[(NVRREG-1) * 2 * sizeof(uint64_t)];
vse->vrsave = *p32;
return vse;
}
static int get_altivec_regs(pid_t pid, user_fpregs_struct_t *fp)
{
if (ptrace(PTRACE_GETVRREGS, pid, 0, (void*)&fp->vrregs) < 0) {
/* PTRACE_GETVRREGS returns EIO if Altivec is not supported.
* This should not happen if msr_vec is set. */
if (errno != EIO) {
pr_perror("Couldn't get Altivec registers");
return -1;
}
pr_debug("Altivec not supported\n");
return 0;
}
pr_debug("Dumping Altivec registers\n");
return 1;
}
static int put_altivec_regs(mcontext_t *mc, UserPpc64VrstateEntry *vse)
{
vrregset_t *v_regs = (vrregset_t *)(((unsigned long)mc->vmx_reserve + 15) & ~0xful);
pr_debug("Restoring Altivec registers\n");
if (vse->n_vrregs != (NVRREG-1)*2) {
pr_err("Corrupted Altivec dump data\n");
return -1;
}
/* Note that this should only be done in the case MSR_VEC is set but
* this is not a big deal to do that in all cases.
*/
memcpy(&v_regs->vrregs[0][0], vse->vrregs,
sizeof(uint64_t) * 2 * (NVRREG-1));
/* vscr has been restored with the previous memcpy which copied 32
* 128bits registers + a 128bits field containing the vscr value in
* the low part.
*/
v_regs->vrsave = vse->vrsave;
mc->v_regs = v_regs;
return 0;
}
static UserPpc64VsxstateEntry* copy_vsx_regs(uint64_t *vsregs)
{
UserPpc64VsxstateEntry *vse;
int i;
vse = xmalloc(sizeof(*vse));
if (!vse)
return NULL;
user_ppc64_vsxstate_entry__init(vse);
vse->n_vsxregs = NVSXREG;
vse->vsxregs = xmalloc(vse->n_vsxregs*sizeof(vse->vsxregs[0]));
if (!vse->vsxregs) {
xfree(vse);
return NULL;
}
for (i = 0; i < vse->n_vsxregs; i++)
vse->vsxregs[i] = vsregs[i];
return vse;
}
/*
* Since the FPR[0-31] is stored in the first double word of VSR[0-31] and
* FPR are saved through the FP state, there is no need to save the upper part
* of the first 32 VSX registers.
* Furthermore, the 32 last VSX registers are also the 32 Altivec registers
* already saved, so no need to save them.
* As a consequence, only the doubleword 1 of the 32 first VSX registers have
* to be saved (the ones are returned by PTRACE_GETVSRREGS).
*/
static int get_vsx_regs(pid_t pid, user_fpregs_struct_t *fp)
{
if (ptrace(PTRACE_GETVSRREGS, pid, 0, (void*)&fp->vsregs) < 0) {
/*
* EIO is returned in the case PTRACE_GETVRREGS is not
* supported.
*/
if (errno == EIO) {
pr_debug("VSX register's dump not supported.\n");
return 0;
}
pr_perror("Couldn't get VSX registers");
return -1;
}
return 1;
}
static int put_vsx_regs(mcontext_t *mc, UserPpc64VsxstateEntry *vse)
{
uint64_t *buf;
int i;
pr_debug("Restoring VSX registers\n");
if (!mc->v_regs) {
/* VSX implies Altivec so v_regs should be set */
pr_err("Internal error\n");
return -1;
}
/* point after the Altivec registers */
buf = (uint64_t*) (mc->v_regs + 1);
/* Copy the value saved by get_vsx_regs in the sigframe */
for (i=0; i < vse->n_vsxregs; i++)
buf[i] = vse->vsxregs[i];
return 0;
}
static void copy_gp_regs(UserPpc64RegsEntry *dst, user_regs_struct_t *src)
{
int i;
#define assign_reg(e) do { \
dst->e = (__typeof__(dst->e))src->e; \
} while (0)
for (i=0; i<32; i++)
assign_reg(gpr[i]);
assign_reg(nip);
assign_reg(msr);
assign_reg(orig_gpr3);
assign_reg(ctr);
assign_reg(link);
assign_reg(xer);
assign_reg(ccr);
assign_reg(trap);
#undef assign_reg
}
static void restore_gp_regs(mcontext_t *dst, UserPpc64RegsEntry *src)
{
int i;
/* r0 to r31 */
for (i=0; i<32; i++)
dst->gp_regs[i] = src->gpr[i];
dst->gp_regs[PT_NIP] = src->nip;
dst->gp_regs[PT_MSR] = src->msr;
dst->gp_regs[PT_ORIG_R3] = src->orig_gpr3;
dst->gp_regs[PT_CTR] = src->ctr;
dst->gp_regs[PT_LNK] = src->link;
dst->gp_regs[PT_XER] = src->xer;
dst->gp_regs[PT_CCR] = src->ccr;
dst->gp_regs[PT_TRAP] = src->trap;
}
static UserPpc64RegsEntry *allocate_gp_regs(void)
{
UserPpc64RegsEntry *gpregs;
gpregs = xmalloc(sizeof(*gpregs));
if (!gpregs)
return NULL;
user_ppc64_regs_entry__init(gpregs);
gpregs->n_gpr = 32;
gpregs->gpr = xmalloc(32 * sizeof(uint64_t));
if (!gpregs->gpr) {
xfree(gpregs);
return NULL;
}
return gpregs;
}
/****************************************************************************
* TRANSACTIONAL MEMORY SUPPORT
*/
static void xfree_tm_state(UserPpc64TmRegsEntry *tme)
{
if (tme) {
if (tme->fpstate) {
xfree(tme->fpstate->fpregs);
xfree(tme->fpstate);
}
if (tme->vrstate) {
xfree(tme->vrstate->vrregs);
xfree(tme->vrstate);
}
if (tme->vsxstate) {
xfree(tme->vsxstate->vsxregs);
xfree(tme->vsxstate);
}
if (tme->gpregs) {
if (tme->gpregs->gpr)
xfree(tme->gpregs->gpr);
xfree(tme->gpregs);
}
xfree(tme);
}
}
static int get_tm_regs(pid_t pid, user_fpregs_struct_t *fpregs)
{
struct iovec iov;
pr_debug("Dumping TM registers\n");
#define TM_REQUIRED 0
#define TM_OPTIONAL 1
#define PTRACE_GET_TM(s,n,c,u) do { \
iov.iov_base = &s; \
iov.iov_len = sizeof(s); \
if (ptrace(PTRACE_GETREGSET, pid, c, &iov)) { \
if (!u || errno != EIO) { \
pr_perror("Couldn't get TM "n); \
pr_err("Your kernel seems to not support the " \
"new TM ptrace API (>= 4.8)\n"); \
goto out_free; \
} \
pr_debug("TM "n" not supported.\n"); \
iov.iov_base = NULL; \
} \
} while(0)
/* Get special registers */
PTRACE_GET_TM(fpregs->tm.tm_spr_regs, "SPR", NT_PPC_TM_SPR, TM_REQUIRED);
/* Get checkpointed regular registers */
PTRACE_GET_TM(fpregs->tm.regs, "GPR", NT_PPC_TM_CGPR, TM_REQUIRED);
/* Get checkpointed FP registers */
PTRACE_GET_TM(fpregs->tm.fpregs, "FPR", NT_PPC_TM_CFPR, TM_OPTIONAL);
if (iov.iov_base)
fpregs->flags |= 0x2;
/* Get checkpointed VMX (Altivec) registers */
PTRACE_GET_TM(fpregs->tm.vmxregs, "VMX", NT_PPC_TM_CVMX, TM_OPTIONAL);
if (iov.iov_base)
fpregs->flags |= 0x4;
/* Get checkpointed VSX registers */
PTRACE_GET_TM(fpregs->tm.vsxregs, "VSX", NT_PPC_TM_CVSX, TM_OPTIONAL);
if (iov.iov_base)
fpregs->flags |= 0x8;
return 0;
out_free:
return -1; /* still failing the checkpoint */
}
static int put_tm_regs(struct rt_sigframe *f, UserPpc64TmRegsEntry *tme)
{
/*
* WARNING: As stated in kernel's restore_tm_sigcontexts, TEXASR has to be
* restored by the process itself :
* TEXASR was set by the signal delivery reclaim, as was TFIAR.
* Users doing anything abhorrent like thread-switching w/ signals for
* TM-Suspended code will have to back TEXASR/TFIAR up themselves.
* For the case of getting a signal and simply returning from it,
* we don't need to re-copy them here.
*/
struct ucontext *tm_uc = &f->uc_transact;
pr_debug("Restoring TM registers FP:%d VR:%d VSX:%d\n",
!!(tme->fpstate), !!(tme->vrstate), !!(tme->vsxstate));
restore_gp_regs(&tm_uc->uc_mcontext, tme->gpregs);
if (tme->fpstate)
put_fpu_regs(&tm_uc->uc_mcontext, tme->fpstate);
if (tme->vrstate && put_altivec_regs(&tm_uc->uc_mcontext,
tme->vrstate))
return -1;
if (tme->vsxstate && put_vsx_regs(&tm_uc->uc_mcontext,
tme->vsxstate))
return -1;
f->uc.uc_link = tm_uc;
return 0;
}
/****************************************************************************/
int get_task_regs(pid_t pid, user_regs_struct_t regs, save_regs_t save, void *arg)
{
user_fpregs_struct_t fpregs;
int ret;
pr_info("Dumping GP/FPU registers for %d\n", pid);
/*
* This is inspired by kernel function check_syscall_restart in
* arch/powerpc/kernel/signal.c
*/
#ifndef TRAP
#define TRAP(r) ((r).trap & ~0xF)
#endif
if (TRAP(regs) == 0x0C00 && regs.ccr & 0x10000000) {
/* Restart the system call */
switch (regs.gpr[3]) {
case ERESTARTNOHAND:
case ERESTARTSYS:
case ERESTARTNOINTR:
regs.gpr[3] = regs.orig_gpr3;
regs.nip -= 4;
break;
case ERESTART_RESTARTBLOCK:
regs.gpr[0] = __NR_restart_syscall;
regs.nip -= 4;
break;
}
}
/* Resetting trap since we are now coming from user space. */
regs.trap = 0;
/*
* Check for Transactional Memory operation in progress.
* Until we have support of TM register's state through the ptrace API,
* we can't checkpoint process with TM operation in progress (almost
* impossible) or suspended (easy to get).
*/
if (MSR_TM_ACTIVE(regs.msr)) {
pr_debug("Task %d has %s TM operation at 0x%lx\n",
pid,
(regs.msr & MSR_TMS) ? "a suspended" : "an active",
regs.nip);
fpregs.flags = 1;
if (get_tm_regs(pid, &fpregs))
return -1;
} else
fpregs.flags = 0;
if (get_fpu_regs(pid, &fpregs))
return -1;
ret = get_altivec_regs(pid, &fpregs);
if (ret < 0)
return -1;
if (ret == 1) {
/*
* Force the MSR_VEC bit of the restored MSR otherwise the
* kernel will not restore them from the signal frame.
*/
fpregs.flags |= 0x16;
/*
* Save the VSX registers if Altivec registers are supported
*/
ret = get_vsx_regs(pid, &fpregs);
if (ret < 0)
return -1;
if (ret == 0) {
/*
* Force the MSR_VSX bit of the restored MSR otherwise
* the kernel will not restore them from the signal
* frame.
*/
fpregs.flags |= 0x32;
}
}
return save(arg, ®s, &fpregs);
}
int save_task_regs(void *x, user_regs_struct_t *regs, user_fpregs_struct_t *fpregs)
{
CoreEntry *core = x;
UserPpc64RegsEntry *gpregs;
UserPpc64FpstateEntry **fpstate;
UserPpc64VrstateEntry **vrstate;
UserPpc64VsxstateEntry **vsxstate;
if (fpregs->flags) {
UserPpc64TmRegsEntry *tme;
tme = xmalloc(sizeof(*tme));
if (!tme)
return -1;
user_ppc64_tm_regs_entry__init(tme);
tme->gpregs = allocate_gp_regs();
if (!tme->gpregs) {
xfree(tme);
return -1;
}
core->ti_ppc64->tmstate = tme;
gpregs = core->ti_ppc64->gpregs;
gpregs->has_tfhar = true;
gpregs->tfhar = fpregs->tm.tm_spr_regs.tfhar;
gpregs->has_texasr = true;
gpregs->texasr = fpregs->tm.tm_spr_regs.texasr;
gpregs->has_tfiar = true;
gpregs->tfiar = fpregs->tm.tm_spr_regs.tfiar;
copy_gp_regs(gpregs, &fpregs->tm.regs);
if (fpregs->flags & 0x2) {
core->ti_ppc64->fpstate = copy_fp_regs(fpregs->tm.fpregs);
if (!core->ti_ppc64->fpstate)
return -1;
}
if (fpregs->flags & 0x4) {
core->ti_ppc64->vrstate = copy_altivec_regs((unsigned char *)fpregs->tm.vmxregs);
if (!core->ti_ppc64->vrstate)
return -1;
}
if (fpregs->flags & 0x8) {
core->ti_ppc64->vsxstate = copy_vsx_regs(fpregs->tm.vsxregs);
if (!core->ti_ppc64->vsxstate)
return -1;
}
gpregs = core->ti_ppc64->tmstate->gpregs;
fpstate = &(core->ti_ppc64->tmstate->fpstate);
vrstate = &(core->ti_ppc64->tmstate->vrstate);
vsxstate = &(core->ti_ppc64->tmstate->vsxstate);
} else {
gpregs = core->ti_ppc64->gpregs;
fpstate = &(core->ti_ppc64->fpstate);
vrstate = &(core->ti_ppc64->vrstate);
vsxstate = &(core->ti_ppc64->vsxstate);
}
copy_gp_regs(gpregs, regs);
*fpstate = copy_fp_regs(fpregs->fpregs);
if (!*fpstate)
return -1;
if (fpregs->flags & 0x16) {
gpregs->msr |= MSR_VEC;
*vrstate = copy_altivec_regs(fpregs->vrregs);
if (!*vrstate)
return -1;
if (fpregs->flags & 0x32) {
gpregs->msr |= MSR_VSX;
*vsxstate = copy_vsx_regs(fpregs->vsregs);
if (!*vsxstate)
return -1;
}
}
return 0;
}
int arch_alloc_thread_info(CoreEntry *core)
{
ThreadInfoPpc64 *ti_ppc64;
ti_ppc64 = xmalloc(sizeof(*ti_ppc64));
if(!ti_ppc64)
return -1;
thread_info_ppc64__init(ti_ppc64);
ti_ppc64->gpregs = allocate_gp_regs();
if (!ti_ppc64->gpregs) {
xfree(ti_ppc64);
return -1;
}
CORE_THREAD_ARCH_INFO(core) = ti_ppc64;
return 0;
}
void arch_free_thread_info(CoreEntry *core)
{
if (CORE_THREAD_ARCH_INFO(core)) {
if (CORE_THREAD_ARCH_INFO(core)->fpstate) {
xfree(CORE_THREAD_ARCH_INFO(core)->fpstate->fpregs);
xfree(CORE_THREAD_ARCH_INFO(core)->fpstate);
}
if (CORE_THREAD_ARCH_INFO(core)->vrstate) {
xfree(CORE_THREAD_ARCH_INFO(core)->vrstate->vrregs);
xfree(CORE_THREAD_ARCH_INFO(core)->vrstate);
}
if (CORE_THREAD_ARCH_INFO(core)->vsxstate) {
xfree(CORE_THREAD_ARCH_INFO(core)->vsxstate->vsxregs);
xfree(CORE_THREAD_ARCH_INFO(core)->vsxstate);
}
xfree_tm_state(CORE_THREAD_ARCH_INFO(core)->tmstate);
xfree(CORE_THREAD_ARCH_INFO(core)->gpregs->gpr);
xfree(CORE_THREAD_ARCH_INFO(core)->gpregs);
xfree(CORE_THREAD_ARCH_INFO(core));
CORE_THREAD_ARCH_INFO(core) = NULL;
}
}
int restore_fpu(struct rt_sigframe *sigframe, CoreEntry *core)
{
int ret = 0;
if (CORE_THREAD_ARCH_INFO(core)->fpstate)
put_fpu_regs(&sigframe->uc.uc_mcontext,
CORE_THREAD_ARCH_INFO(core)->fpstate);
if (CORE_THREAD_ARCH_INFO(core)->vrstate)
ret = put_altivec_regs(&sigframe->uc.uc_mcontext,
CORE_THREAD_ARCH_INFO(core)->vrstate);
else if (core->ti_ppc64->gpregs->msr & MSR_VEC) {
pr_err("Register's data mismatch, corrupted image ?\n");
ret = -1;
}
if (!ret && CORE_THREAD_ARCH_INFO(core)->vsxstate)
ret = put_vsx_regs(&sigframe->uc.uc_mcontext,
CORE_THREAD_ARCH_INFO(core)->vsxstate);
else if (core->ti_ppc64->gpregs->msr & MSR_VSX) {
pr_err("VSX register's data mismatch, corrupted image ?\n");
ret = -1;
}
if (!ret && CORE_THREAD_ARCH_INFO(core)->tmstate)
ret = put_tm_regs(sigframe,
CORE_THREAD_ARCH_INFO(core)->tmstate);
else if (MSR_TM_ACTIVE(core->ti_ppc64->gpregs->msr)) {
pr_err("TM register's data mismatch, corrupted image ?\n");
ret = -1;
}
return ret;
}
/*
* The signal frame has been built using local addresses. Since it has to be
* used in the context of the checkpointed process, the v_regs pointer in the
* signal frame must be updated to match the address in the remote stack.
*/
static inline void update_vregs(mcontext_t *lcontext, mcontext_t *rcontext)
{
if (lcontext->v_regs) {
uint64_t offset = (uint64_t)(lcontext->v_regs) - (uint64_t)lcontext;
lcontext->v_regs = (vrregset_t *)((uint64_t)rcontext + offset);
pr_debug("Updated v_regs:%llx (rcontext:%llx)\n",
(unsigned long long) lcontext->v_regs,
(unsigned long long) rcontext);
}
}
int sigreturn_prep_fpu_frame(struct rt_sigframe *frame,
struct rt_sigframe *rframe)
{
uint64_t msr = frame->uc.uc_mcontext.gp_regs[PT_MSR];
update_vregs(&frame->uc.uc_mcontext, &rframe->uc.uc_mcontext);
/* Sanity check: If TM so uc_link should be set, otherwise not */
if (MSR_TM_ACTIVE(msr) ^ (!!(frame->uc.uc_link))) {
BUG();
return 1;
}
/* Updating the transactional state address if any */
if (frame->uc.uc_link) {
update_vregs(&frame->uc_transact.uc_mcontext,
&rframe->uc_transact.uc_mcontext);
frame->uc.uc_link = &rframe->uc_transact;
}
return 0;
}
int restore_gpregs(struct rt_sigframe *f, UserPpc64RegsEntry *r)
{
restore_gp_regs(&f->uc.uc_mcontext, r);
return 0;
}
void *mmap_seized(struct parasite_ctl *ctl,
void *addr, size_t length, int prot,
int flags, int fd, off_t offset)
{
unsigned long map = 0;
int err;
err = syscall_seized(ctl, __NR_mmap, &map,
(unsigned long)addr, length, prot, flags, fd, offset);
if (err < 0 || (long)map < 0)
map = 0;
return (void *)map;
}