File: syswrap-x86-solaris.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (989 lines) | stat: -rw-r--r-- 38,636 bytes parent folder | download | duplicates (3)
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989

/*--------------------------------------------------------------------*/
/*--- Platform-specific syscalls stuff.      syswrap-x86-solaris.c ---*/
/*--------------------------------------------------------------------*/

/*
   This file is part of Valgrind, a dynamic binary instrumentation
   framework.

   Copyright (C) 2011-2017 Petr Pavlu
      setup@dagobah.cz

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.

   The GNU General Public License is contained in the file COPYING.
*/

#if defined(VGP_x86_solaris)

#include "libvex_guest_offsets.h"
#include "pub_core_basics.h"
#include "pub_core_vki.h"
#include "pub_core_threadstate.h"
#include "pub_core_aspacemgr.h"
#include "pub_core_xarray.h"
#include "pub_core_clientstate.h"
#include "pub_core_debuglog.h"
#include "pub_core_libcassert.h"
#include "pub_core_libcbase.h"
#include "pub_core_libcfile.h"
#include "pub_core_libcprint.h"
#include "pub_core_libcsignal.h"
#include "pub_core_machine.h"           // VG_(get_SP)
#include "pub_core_mallocfree.h"
#include "pub_core_options.h"
#include "pub_core_tooliface.h"
#include "pub_core_signals.h"
#include "pub_core_syscall.h"
#include "pub_core_syswrap.h"

#include "priv_types_n_macros.h"
#include "priv_syswrap-generic.h"
#include "priv_syswrap-solaris.h"

/* Call f(arg1), but first switch stacks, using 'stack' as the new stack, and
   use 'retaddr' as f's return-to address.  Also, clear all the integer
   registers before entering f. */
__attribute__((noreturn))
void ML_(call_on_new_stack_0_1)(Addr stack,             /* 4(%esp) */
                                Addr retaddr,           /* 8(%esp) */
                                void (*f)(Word),        /* 12(%esp) */
                                Word arg1);             /* 16(%esp) */
__asm__ (
".text\n"
".globl vgModuleLocal_call_on_new_stack_0_1\n"
"vgModuleLocal_call_on_new_stack_0_1:\n"
"   movl  %esp, %esi\n"         /* remember old stack pointer */
"   movl  4(%esi), %esp\n"      /* set stack */
"   pushl $0\n"                 /* align stack */
"   pushl $0\n"                 /* align stack */
"   pushl $0\n"                 /* align stack */
"   pushl 16(%esi)\n"           /* arg1 to stack */
"   pushl 8(%esi)\n"            /* retaddr to stack */
"   pushl 12(%esi)\n"           /* f to stack */
"   movl  $0, %eax\n"           /* zero all GP regs */
"   movl  $0, %ebx\n"
"   movl  $0, %ecx\n"
"   movl  $0, %edx\n"
"   movl  $0, %esi\n"
"   movl  $0, %edi\n"
"   movl  $0, %ebp\n"
"   ret\n"                      /* jump to f */
"   ud2\n"                      /* should never get here */
".previous\n"
);

/* This function is called to setup a context of a new Valgrind thread (which
   will run the client code). */
void ML_(setup_start_thread_context)(ThreadId tid, vki_ucontext_t *uc)
{
   ThreadState *tst = VG_(get_ThreadState)(tid);
   UWord *stack = (UWord*)tst->os_state.valgrind_stack_init_SP;
   UShort cs, ds, ss, es, fs, gs;

   VG_(memset)(uc, 0, sizeof(*uc));
   uc->uc_flags = VKI_UC_CPU | VKI_UC_SIGMASK;

   /* Start the thread with everything blocked. */
   VG_(sigfillset)(&uc->uc_sigmask);

   /* Set up the stack, it should be always 16-byte aligned before doing
      a function call, i.e. the first parameter is also 16-byte aligned. */
   vg_assert(VG_IS_16_ALIGNED(stack));
   stack -= 1;
   stack[0] = 0; /* bogus return value */
   stack[1] = (UWord)tst; /* the parameter */

   /* Set up the registers. */
   uc->uc_mcontext.gregs[VKI_EIP] = (UWord)ML_(start_thread_NORETURN);
   uc->uc_mcontext.gregs[VKI_UESP] = (UWord)stack;

   /* Copy segment registers. */
   __asm__ __volatile__(
      "movw %%cs, %[cs]\n"
      "movw %%ds, %[ds]\n"
      "movw %%ss, %[ss]\n"
      "movw %%es, %[es]\n"
      "movw %%fs, %[fs]\n"
      "movw %%gs, %[gs]\n"
      : [cs] "=m" (cs), [ds] "=m" (ds), [ss] "=m" (ss), [es] "=m" (es),
        [fs] "=m" (fs), [gs] "=m" (gs));
   uc->uc_mcontext.gregs[VKI_CS] = cs;
   uc->uc_mcontext.gregs[VKI_DS] = ds;
   uc->uc_mcontext.gregs[VKI_SS] = ss;
   uc->uc_mcontext.gregs[VKI_ES] = es;
   uc->uc_mcontext.gregs[VKI_FS] = fs;
   uc->uc_mcontext.gregs[VKI_GS] = gs;
}

/* Architecture-specific part of VG_(save_context). */
void ML_(save_machine_context)(ThreadId tid, vki_ucontext_t *uc,
                               CorePart part)
{
   ThreadState *tst = VG_(get_ThreadState)(tid);
   struct vki_fpchip_state *fs
      = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state;
   SizeT i;

   /* CPU */
   /* Common registers */
   uc->uc_mcontext.gregs[VKI_EIP] = tst->arch.vex.guest_EIP;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_EIP,
            (Addr)&uc->uc_mcontext.gregs[VKI_EIP], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_EAX] = tst->arch.vex.guest_EAX;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_EAX,
            (Addr)&uc->uc_mcontext.gregs[VKI_EAX], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_EBX] = tst->arch.vex.guest_EBX;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_EBX,
            (Addr)&uc->uc_mcontext.gregs[VKI_EBX], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_ECX] = tst->arch.vex.guest_ECX;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_ECX,
            (Addr)&uc->uc_mcontext.gregs[VKI_ECX], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_EDX] = tst->arch.vex.guest_EDX;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_EDX,
            (Addr)&uc->uc_mcontext.gregs[VKI_EDX], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_EBP] = tst->arch.vex.guest_EBP;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_EBP,
            (Addr)&uc->uc_mcontext.gregs[VKI_EBP], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_ESI] = tst->arch.vex.guest_ESI;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_ESI,
            (Addr)&uc->uc_mcontext.gregs[VKI_ESI], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_EDI] = tst->arch.vex.guest_EDI;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_EDI,
            (Addr)&uc->uc_mcontext.gregs[VKI_EDI], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_UESP] = tst->arch.vex.guest_ESP;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_ESP,
            (Addr)&uc->uc_mcontext.gregs[VKI_UESP], sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_ESP] = 0;
   VG_TRACK(post_mem_write, part, tid, (Addr)&uc->uc_mcontext.gregs[VKI_ESP],
            sizeof(UWord));

   /* ERR and TRAPNO */
   uc->uc_mcontext.gregs[VKI_ERR] = 0;
   VG_TRACK(post_mem_write, part, tid, (Addr)&uc->uc_mcontext.gregs[VKI_ERR],
            sizeof(UWord));
   uc->uc_mcontext.gregs[VKI_TRAPNO] = 0;
   VG_TRACK(post_mem_write, part, tid, (Addr)&uc->uc_mcontext.gregs[VKI_TRAPNO],
            sizeof(UWord));

   /* Segment registers */
   /* Note that segment registers are 16b in VEX, but 32b in mcontext.  Thus
      we tell a tool that the lower 16 bits were copied and that the higher 16
      bits were set (to zero).  (This assumes a little-endian
      architecture.) */
   uc->uc_mcontext.gregs[VKI_CS] = tst->arch.vex.guest_CS;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_CS,
            (Addr)&uc->uc_mcontext.gregs[VKI_CS], sizeof(UShort));
   VG_TRACK(post_mem_write, part, tid,
            (Addr)(&uc->uc_mcontext.gregs[VKI_CS]) + 2, sizeof(UShort));
   uc->uc_mcontext.gregs[VKI_DS] = tst->arch.vex.guest_DS;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_DS,
            (Addr)&uc->uc_mcontext.gregs[VKI_DS], sizeof(UShort));
   VG_TRACK(post_mem_write, part, tid,
            (Addr)(&uc->uc_mcontext.gregs[VKI_DS]) + 2, sizeof(UShort));
   uc->uc_mcontext.gregs[VKI_SS] = tst->arch.vex.guest_SS;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_SS,
            (Addr)&uc->uc_mcontext.gregs[VKI_SS], sizeof(UShort));
   VG_TRACK(post_mem_write, part, tid,
            (Addr)(&uc->uc_mcontext.gregs[VKI_SS]) + 2, sizeof(UShort));
   uc->uc_mcontext.gregs[VKI_ES] = tst->arch.vex.guest_ES;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_ES,
            (Addr)&uc->uc_mcontext.gregs[VKI_ES], sizeof(UShort));
   VG_TRACK(post_mem_write, part, tid,
            (Addr)(&uc->uc_mcontext.gregs[VKI_ES]) + 2, sizeof(UShort));
   uc->uc_mcontext.gregs[VKI_FS] = tst->arch.vex.guest_FS;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_FS,
            (Addr)&uc->uc_mcontext.gregs[VKI_FS], sizeof(UShort));
   VG_TRACK(post_mem_write, part, tid,
            (Addr)(&uc->uc_mcontext.gregs[VKI_FS]) + 2, sizeof(UShort));
   uc->uc_mcontext.gregs[VKI_GS] = tst->arch.vex.guest_GS;
   VG_TRACK(copy_reg_to_mem, part, tid, OFFSET_x86_GS,
            (Addr)&uc->uc_mcontext.gregs[VKI_GS], sizeof(UShort));
   VG_TRACK(post_mem_write, part, tid,
            (Addr)(&uc->uc_mcontext.gregs[VKI_GS]) + 2, sizeof(UShort));

   /* Handle eflags (optimistically make all flags defined). */
   uc->uc_mcontext.gregs[VKI_EFL] =
      LibVEX_GuestX86_get_eflags(&tst->arch.vex);
   VG_TRACK(post_mem_write, part, tid, (Addr)&uc->uc_mcontext.gregs[VKI_EFL],
         sizeof(UWord));
   /* The LibVEX_GuestX86_get_eflags() call calculates eflags value from the
      CC_OP, CC_DEP1, CC_DEP2, CC_NDEP, DFLAG, IDFLAG and ACFLAG guest state
      values.  The *FLAG values represent one-bit information and are saved
      without loss of precision into eflags.  However when CC_* values are
      converted into eflags then precision is lost.  What we do here is to
      save unmodified CC_* values into unused ucontext members (the 'long
      uc_filler[5] and 'int fs->__pad[2]' arrays) so we can then restore the
      context in ML_(restore_machine_context)() without the loss of precision.
      This imposes a requirement on client programs to not use these two
      members. Luckily this is never a case in Solaris-gate programs and
      libraries. */
   /* CC_OP and CC_NDEP are always defined, but we don't want to tell a tool
      that we just defined uc_filler[0,1].  This helps if someone uses an
      uninitialized ucontext and tries to read (use) uc_filler[0,1].  Memcheck
      in such a case should detect this error. */
   VKI_UC_GUEST_CC_OP(uc) = tst->arch.vex.guest_CC_OP;
   VKI_UC_GUEST_CC_NDEP(uc) = tst->arch.vex.guest_CC_NDEP;
   /* We want to copy shadow values of CC_DEP1 and CC_DEP2 so we have to tell
      a tool about this copy. */
   VKI_UC_GUEST_CC_DEP1(uc) = tst->arch.vex.guest_CC_DEP1;
   VG_TRACK(copy_reg_to_mem, part, tid,
            offsetof(VexGuestX86State, guest_CC_DEP1),
            (Addr)&VKI_UC_GUEST_CC_DEP1(uc), sizeof(UWord));
   VKI_UC_GUEST_CC_DEP2(uc) = tst->arch.vex.guest_CC_DEP2;
   VG_TRACK(copy_reg_to_mem, part, tid,
            offsetof(VexGuestX86State, guest_CC_DEP2),
            (Addr)&VKI_UC_GUEST_CC_DEP2(uc), sizeof(UWord));
   /* Make another copy of eflags. */
   VKI_UC_GUEST_EFLAGS_NEG(uc) = ~uc->uc_mcontext.gregs[VKI_EFL];
   /* Calculate a checksum. */
   {
      UInt buf[5];
      UInt checksum;

      buf[0] = VKI_UC_GUEST_CC_OP(uc);
      buf[1] = VKI_UC_GUEST_CC_NDEP(uc);
      buf[2] = VKI_UC_GUEST_CC_DEP1(uc);
      buf[3] = VKI_UC_GUEST_CC_DEP2(uc);
      buf[4] = uc->uc_mcontext.gregs[VKI_EFL];
      checksum = ML_(fletcher32)((UShort*)&buf, sizeof(buf) / sizeof(UShort));
      /* Store the checksum. */
      VKI_UC_GUEST_EFLAGS_CHECKSUM(uc) = checksum;
   }

   /* FPU */
   /* x87 */
   vg_assert(sizeof(fs->state) == 108);
   LibVEX_GuestX86_get_x87(&tst->arch.vex, (UChar*)&fs->state);

   /* Flags and control words */
   VG_TRACK(post_mem_write, part, tid, (Addr)&fs->state, 28);
   /* ST registers */
   for (i = 0; i < 8; i++) {
      Addr addr = (Addr)&fs->state + 28 + i * 10;
      /* x87 uses 80b FP registers but VEX uses only 64b registers, thus we
         have to lie here. :< */
      VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
               guest_FPREG[i]), addr, sizeof(ULong));
      VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
               guest_FPREG[i]), addr + 8, sizeof(UShort));
      }

   /* Status word (sw) at exception */
   fs->status = 0;
   VG_TRACK(post_mem_write, part, tid, (Addr)&fs->status, sizeof(fs->status));

   /* SSE */
   fs->mxcsr = LibVEX_GuestX86_get_mxcsr(&tst->arch.vex);
   VG_TRACK(post_mem_write, part, tid, (Addr)&fs->mxcsr, sizeof(fs->mxcsr));

   /* MXCSR at exception */
   fs->xstatus = 0;
   VG_TRACK(post_mem_write, part, tid, (Addr)&fs->xstatus,
            sizeof(fs->xstatus));

   /* XMM registers */
#define COPY_OUT_XMM(dest, src) \
   do {                         \
      dest._l[0] = src[0];      \
      dest._l[1] = src[1];      \
      dest._l[2] = src[2];      \
      dest._l[3] = src[3];      \
   } while (0)
   COPY_OUT_XMM(fs->xmm[0], tst->arch.vex.guest_XMM0);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM0), (Addr)&fs->xmm[0], sizeof(U128));
   COPY_OUT_XMM(fs->xmm[1], tst->arch.vex.guest_XMM1);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM1), (Addr)&fs->xmm[1], sizeof(U128));
   COPY_OUT_XMM(fs->xmm[2], tst->arch.vex.guest_XMM2);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM2), (Addr)&fs->xmm[2], sizeof(U128));
   COPY_OUT_XMM(fs->xmm[3], tst->arch.vex.guest_XMM3);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM3), (Addr)&fs->xmm[3], sizeof(U128));
   COPY_OUT_XMM(fs->xmm[4], tst->arch.vex.guest_XMM4);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM4), (Addr)&fs->xmm[4], sizeof(U128));
   COPY_OUT_XMM(fs->xmm[5], tst->arch.vex.guest_XMM5);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM5), (Addr)&fs->xmm[5], sizeof(U128));
   COPY_OUT_XMM(fs->xmm[6], tst->arch.vex.guest_XMM6);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM6), (Addr)&fs->xmm[6], sizeof(U128));
   COPY_OUT_XMM(fs->xmm[7], tst->arch.vex.guest_XMM7);
   VG_TRACK(copy_reg_to_mem, part, tid, offsetof(VexGuestX86State,
            guest_XMM7), (Addr)&fs->xmm[7], sizeof(U128));
#undef COPY_OUT_XMM
}

/* Architecture-specific part of VG_(restore_context). */
void ML_(restore_machine_context)(ThreadId tid, vki_ucontext_t *uc,
                                  CorePart part, Bool esp_is_thrptr)
{
   ThreadState *tst = VG_(get_ThreadState)(tid);
   struct vki_fpchip_state *fs
      = &uc->uc_mcontext.fpregs.fp_reg_set.fpchip_state;

   /* CPU */
   if (uc->uc_flags & VKI_UC_CPU) {
      /* Common registers */
      tst->arch.vex.guest_EIP = uc->uc_mcontext.gregs[VKI_EIP];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_EIP], OFFSET_x86_EIP,
               sizeof(UWord));
      tst->arch.vex.guest_EAX = uc->uc_mcontext.gregs[VKI_EAX];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_EAX], OFFSET_x86_EAX,
               sizeof(UWord));
      tst->arch.vex.guest_EBX = uc->uc_mcontext.gregs[VKI_EBX];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_EBX], OFFSET_x86_EBX,
               sizeof(UWord));
      tst->arch.vex.guest_ECX = uc->uc_mcontext.gregs[VKI_ECX];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_ECX], OFFSET_x86_ECX,
               sizeof(UWord));
      tst->arch.vex.guest_EDX = uc->uc_mcontext.gregs[VKI_EDX];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_EDX], OFFSET_x86_EDX,
               sizeof(UWord));
      tst->arch.vex.guest_EBP = uc->uc_mcontext.gregs[VKI_EBP];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_EBP], OFFSET_x86_EBP,
               sizeof(UWord));
      tst->arch.vex.guest_ESI = uc->uc_mcontext.gregs[VKI_ESI];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_ESI], OFFSET_x86_ESI,
               sizeof(UWord));
      tst->arch.vex.guest_EDI = uc->uc_mcontext.gregs[VKI_EDI];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_EDI], OFFSET_x86_EDI,
               sizeof(UWord));
      tst->arch.vex.guest_ESP = uc->uc_mcontext.gregs[VKI_UESP];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_UESP], OFFSET_x86_ESP,
               sizeof(UWord));

      if (esp_is_thrptr) {
         /* The thrptr value is passed by libc to the kernel in the otherwise
            unused ESP field.  This is used when a new thread is created. */
         VG_TRACK(pre_mem_read, part, tid,
                  "restore_machine_context(uc->uc_mcontext.gregs[VKI_ESP])",
                  (Addr)&uc->uc_mcontext.gregs[VKI_ESP], sizeof(UWord));
         if (uc->uc_mcontext.gregs[VKI_ESP]) {
            tst->os_state.thrptr = uc->uc_mcontext.gregs[VKI_ESP];
            ML_(update_gdt_lwpgs)(tid);
         }
      }

      /* Ignore ERR and TRAPNO. */

      /* Segment registers */
      tst->arch.vex.guest_CS = uc->uc_mcontext.gregs[VKI_CS];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_CS], OFFSET_x86_CS,
               sizeof(UShort));
      tst->arch.vex.guest_DS = uc->uc_mcontext.gregs[VKI_DS];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_DS], OFFSET_x86_DS,
               sizeof(UShort));
      tst->arch.vex.guest_SS = uc->uc_mcontext.gregs[VKI_SS];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_SS], OFFSET_x86_SS,
               sizeof(UShort));
      tst->arch.vex.guest_ES = uc->uc_mcontext.gregs[VKI_ES];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_ES], OFFSET_x86_ES,
               sizeof(UShort));
      tst->arch.vex.guest_FS = uc->uc_mcontext.gregs[VKI_FS];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_FS], OFFSET_x86_FS,
               sizeof(UShort));
      tst->arch.vex.guest_GS = uc->uc_mcontext.gregs[VKI_GS];
      VG_TRACK(copy_mem_to_reg, part, tid,
               (Addr)&uc->uc_mcontext.gregs[VKI_GS], OFFSET_x86_GS,
               sizeof(UShort));

      /* Eflags */
      {
         UInt eflags;
         UInt orig_eflags;
         UInt new_eflags;
         Bool ok_restore = False;

         VG_TRACK(pre_mem_read, part, tid,
                  "restore_machine_context(uc->uc_mcontext.gregs[VKI_EFL])",
                  (Addr)&uc->uc_mcontext.gregs[VKI_EFL], sizeof(UWord));
         eflags = uc->uc_mcontext.gregs[VKI_EFL];
         orig_eflags = LibVEX_GuestX86_get_eflags(&tst->arch.vex);
         new_eflags = eflags;
         /* The kernel disallows the ID flag to be changed via the setcontext
            call, thus do the same. */
         if (orig_eflags & VKI_EFLAGS_ID_BIT)
            new_eflags |= VKI_EFLAGS_ID_BIT;
         else
            new_eflags &= ~VKI_EFLAGS_ID_BIT;
         LibVEX_GuestX86_put_eflags(new_eflags, &tst->arch.vex);
         VG_TRACK(post_reg_write, part, tid,
                  offsetof(VexGuestX86State, guest_CC_DEP1), sizeof(UWord));
         VG_TRACK(post_reg_write, part, tid,
                  offsetof(VexGuestX86State, guest_CC_DEP2), sizeof(UWord));

         /* Check if this context was created by us in VG_(save_context). In
            that case, try to restore the CC_OP, CC_DEP1, CC_DEP2 and CC_NDEP
            values which we previously stashed into unused members of the
            context. */
         if (eflags != ~VKI_UC_GUEST_EFLAGS_NEG(uc)) {
            VG_(debugLog)(1, "syswrap-solaris",
                             "The eflags value was restored from an "
                             "explicitly set value in thread %u.\n", tid);
            ok_restore = True;
         }
         else {
            UInt buf[5];
            UInt checksum;

            buf[0] = VKI_UC_GUEST_CC_OP(uc);
            buf[1] = VKI_UC_GUEST_CC_NDEP(uc);
            buf[2] = VKI_UC_GUEST_CC_DEP1(uc);
            buf[3] = VKI_UC_GUEST_CC_DEP2(uc);
            buf[4] = eflags;
            checksum = ML_(fletcher32)((UShort*)&buf,
                                       sizeof(buf) / sizeof(UShort));
            if (checksum == VKI_UC_GUEST_EFLAGS_CHECKSUM(uc)) {
               /* Check ok, the full restoration is possible. */
               VG_(debugLog)(1, "syswrap-solaris",
                                "The CC_* guest state values were fully "
                                "restored in thread %u.\n", tid);
               ok_restore = True;

               tst->arch.vex.guest_CC_OP = VKI_UC_GUEST_CC_OP(uc);
               tst->arch.vex.guest_CC_NDEP = VKI_UC_GUEST_CC_NDEP(uc);
               tst->arch.vex.guest_CC_DEP1 = VKI_UC_GUEST_CC_DEP1(uc);
               VG_TRACK(copy_mem_to_reg, part, tid,
                        (Addr)&VKI_UC_GUEST_CC_DEP1(uc),
                        offsetof(VexGuestX86State, guest_CC_DEP1),
                        sizeof(UWord));
               tst->arch.vex.guest_CC_DEP2 = VKI_UC_GUEST_CC_DEP2(uc);
               VG_TRACK(copy_mem_to_reg, part, tid,
                        (Addr)&VKI_UC_GUEST_CC_DEP2(uc),
                        offsetof(VexGuestX86State, guest_CC_DEP2),
                        sizeof(UWord));
            }
         }

         if (!ok_restore)
            VG_(debugLog)(1, "syswrap-solaris",
                             "Cannot fully restore the CC_* guest state "
                             "values, using approximate eflags in thread "
                             "%u.\n", tid);
      }
   }

   if (uc->uc_flags & VKI_UC_FPU) {
      /* FPU */
      VexEmNote note;
      SizeT i;

      /* x87 */
      /* Flags and control words */
      VG_TRACK(pre_mem_read, part, tid,
               "restore_machine_context(uc->uc_mcontext.fpregs..x87_state)",
               (Addr)&fs->state, 28);
      /* ST registers */
      for (i = 0; i < 8; i++) {
         Addr addr = (Addr)&fs->state + 28 + i * 10;
         VG_TRACK(copy_mem_to_reg, part, tid, addr,
                  offsetof(VexGuestX86State, guest_FPREG[i]), sizeof(ULong));
      }
      note = LibVEX_GuestX86_put_x87((UChar*)&fs->state, &tst->arch.vex);
      if (note != EmNote_NONE)
         VG_(message)(Vg_UserMsg,
                      "Error restoring x87 state in thread %u: %s.\n",
                      tid, LibVEX_EmNote_string(note));

      /* SSE */
      VG_TRACK(pre_mem_read, part, tid,
               "restore_machine_context(uc->uc_mcontext.fpregs..mxcsr)",
               (Addr)&fs->mxcsr, sizeof(fs->mxcsr));
      note = LibVEX_GuestX86_put_mxcsr(fs->mxcsr, &tst->arch.vex);
      if (note != EmNote_NONE)
         VG_(message)(Vg_UserMsg,
                      "Error restoring mxcsr state in thread %u: %s.\n",
                      tid, LibVEX_EmNote_string(note));
      /* XMM registers */
#define COPY_IN_XMM(src, dest) \
      do {                     \
         dest[0] = src._l[0];  \
         dest[1] = src._l[1];  \
         dest[2] = src._l[2];  \
         dest[3] = src._l[3];  \
      } while (0)
      COPY_IN_XMM(fs->xmm[0], tst->arch.vex.guest_XMM0);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[0],
               offsetof(VexGuestX86State, guest_XMM0), sizeof(U128));
      COPY_IN_XMM(fs->xmm[1], tst->arch.vex.guest_XMM1);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[1],
               offsetof(VexGuestX86State, guest_XMM1), sizeof(U128));
      COPY_IN_XMM(fs->xmm[2], tst->arch.vex.guest_XMM2);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[2],
               offsetof(VexGuestX86State, guest_XMM2), sizeof(U128));
      COPY_IN_XMM(fs->xmm[3], tst->arch.vex.guest_XMM3);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[3],
               offsetof(VexGuestX86State, guest_XMM3), sizeof(U128));
      COPY_IN_XMM(fs->xmm[4], tst->arch.vex.guest_XMM4);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[4],
               offsetof(VexGuestX86State, guest_XMM4), sizeof(U128));
      COPY_IN_XMM(fs->xmm[5], tst->arch.vex.guest_XMM5);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[5],
               offsetof(VexGuestX86State, guest_XMM5), sizeof(U128));
      COPY_IN_XMM(fs->xmm[6], tst->arch.vex.guest_XMM6);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[6],
               offsetof(VexGuestX86State, guest_XMM6), sizeof(U128));
      COPY_IN_XMM(fs->xmm[7], tst->arch.vex.guest_XMM7);
      VG_TRACK(copy_mem_to_reg, part, tid, (Addr)&fs->xmm[7],
               offsetof(VexGuestX86State, guest_XMM7), sizeof(U128));
#undef COPY_IN_XMM
   }
}

/* Allocate GDT for a given thread. */
void ML_(setup_gdt)(VexGuestX86State *vex)
{
   Addr gdt = (Addr)VG_(calloc)("syswrap-solaris-x86.gdt",
                                VEX_GUEST_X86_GDT_NENT,
                                sizeof(VexGuestX86SegDescr));
   vex->guest_GDT = gdt;
}

/* Deallocate GDT for a given thread. */
void ML_(cleanup_gdt)(VexGuestX86State *vex)
{
   if (!vex->guest_GDT)
      return;
   VG_(free)((void *) (HWord) vex->guest_GDT);
   vex->guest_GDT = 0;
}

/* For a given thread, update the LWPGS descriptor in the thread's GDT
   according to the thread pointer. */
void ML_(update_gdt_lwpgs)(ThreadId tid)
{
   ThreadState *tst = VG_(get_ThreadState)(tid);
   Addr base = tst->os_state.thrptr;
   VexGuestX86SegDescr *gdt
      = (VexGuestX86SegDescr *) (HWord) tst->arch.vex.guest_GDT;
   VexGuestX86SegDescr desc;

   vg_assert(gdt);

   VG_(memset)(&desc, 0, sizeof(desc));
   if (base) {
      desc.LdtEnt.Bits.LimitLow = -1;
      desc.LdtEnt.Bits.LimitHi = -1;
      desc.LdtEnt.Bits.BaseLow = base & 0xffff;
      desc.LdtEnt.Bits.BaseMid = (base >> 16) & 0xff;
      desc.LdtEnt.Bits.BaseHi = (base >> 24) & 0xff;
      desc.LdtEnt.Bits.Pres = 1;
      desc.LdtEnt.Bits.Dpl = 3; /* SEL_UPL */
      desc.LdtEnt.Bits.Type = 19; /* SDT_MEMRWA */
      desc.LdtEnt.Bits.Granularity = 1; /* SDP_PAGES */
      desc.LdtEnt.Bits.Default_Big = 1; /* SDP_OP32 */
   }

   gdt[VKI_GDT_LWPGS] = desc;

   /* Write %gs. */
   tst->arch.vex.guest_GS = VKI_LWPGS_SEL;
   VG_TRACK(post_reg_write, Vg_CoreSysCall, tid, OFFSET_x86_GS,
            sizeof(UShort));
}


/* ---------------------------------------------------------------------
   PRE/POST wrappers for x86/Solaris-specific syscalls
   ------------------------------------------------------------------ */

#define PRE(name)       DEFN_PRE_TEMPLATE(x86_solaris, name)
#define POST(name)      DEFN_POST_TEMPLATE(x86_solaris, name)

/* implementation */

PRE(sys_fstatat64)
{
   /* int fstatat64(int fildes, const char *path, struct stat64 *buf,
                    int flag); */
   PRINT("sys_fstatat64 ( %ld, %#lx(%s), %#lx, %ld )", SARG1, ARG2,
         (HChar*)ARG2, ARG3, SARG4);
   PRE_REG_READ4(long, "fstatat64", int, fildes, const char *, path,
                 struct stat64 *, buf, int, flag);
   if (ARG2)
      PRE_MEM_RASCIIZ("fstatat64(path)", ARG2);
   PRE_MEM_WRITE("fstatat64(buf)", ARG3, sizeof(struct vki_stat64));

   /* Be strict. */
   if (ARG1 != VKI_AT_FDCWD &&
       !ML_(fd_allowed)(ARG1, "fstatat64", tid, False))
      SET_STATUS_Failure(VKI_EBADF);
}

POST(sys_fstatat64)
{
   POST_MEM_WRITE(ARG3, sizeof(struct vki_stat64));
}

PRE(sys_openat64)
{
   /* int openat64(int fildes, const char *filename, int flags);
      int openat64(int fildes, const char *filename, int flags, mode_t mode);
    */
   *flags |= SfMayBlock;

   if (ARG3 & VKI_O_CREAT) {
      /* 4-arg version */
      PRINT("sys_openat64 ( %ld, %#lx(%s), %ld, %ld )", SARG1, ARG2,
            (HChar*)ARG2, SARG3, SARG4);
      PRE_REG_READ4(long, "openat64", int, fildes, const char *, filename,
                    int, flags, vki_mode_t, mode);
   }
   else {
      /* 3-arg version */
      PRINT("sys_openat64 ( %ld, %#lx(%s), %ld )", SARG1, ARG2, (HChar*)ARG2,
            SARG3);
      PRE_REG_READ3(long, "openat64", int, fildes, const char *, filename,
                    int, flags);
   }

   PRE_MEM_RASCIIZ("openat64(filename)", ARG2);

   /* Be strict. */
   if (ARG1 != VKI_AT_FDCWD && !ML_(fd_allowed)(ARG1, "openat64", tid, False))
      SET_STATUS_Failure(VKI_EBADF);
}

POST(sys_openat64)
{
   if (!ML_(fd_allowed)(RES, "openat64", tid, True)) {
      VG_(close)(RES);
      SET_STATUS_Failure(VKI_EMFILE);
   }
   else if (VG_(clo_track_fds))
      ML_(record_fd_open_with_given_name)(tid, RES, (HChar*)ARG2);
}

PRE(sys_llseek32)
{
   /* offset_t llseek(int fildes, offset_t offset, int whence); */
   PRINT("sys_llseek32 ( %ld, %#lx, %#lx, %ld )", SARG1, ARG2, ARG3, SARG4);
   PRE_REG_READ4(long, "llseek", int, fildes, vki_u32, offset_low,
                 vki_u32, offset_high, int, whence);

   /* Stay sane. */
   if (!ML_(fd_allowed)(ARG1, "llseek", tid, False))
      SET_STATUS_Failure(VKI_EBADF);
}

PRE(sys_mmap64)
{
   /* void *mmap64(void *addr, size_t len, int prot, int flags,
                   int fildes, uint32_t offlo, uint32_t offhi); */
   /* Note this wrapper assumes a little-endian architecture, offlo and offhi
      have to be swapped if a big-endian architecture is present. */
#if !defined(VG_LITTLEENDIAN)
#error "Unexpected endianness."
#endif /* !VG_LITTLEENDIAN */

   SysRes r;
   ULong u;
   Off64T offset;

   /* Stay sane. */
   vg_assert(VKI_PAGE_SIZE == 4096);
   vg_assert(sizeof(u) == sizeof(offset));

   PRINT("sys_mmap ( %#lx, %#lx, %#lx, %#lx, %ld, %#lx, %#lx )",
         ARG1, ARG2, ARG3, ARG4, SARG5, ARG6, ARG7);
   PRE_REG_READ7(long, "mmap", void *, start, vki_size_t, length,
                 int, prot, int, flags, int, fd, uint32_t, offlo,
                 uint32_t, offhi);

   /* The offlo and offhi values can actually represent a negative value.
      Make sure it's passed correctly to the generic mmap wrapper. */
   u = ((ULong)ARG7 << 32) + ARG6;
   offset = *(Off64T*)&u;

   r = ML_(generic_PRE_sys_mmap)(tid, ARG1, ARG2, ARG3, ARG4, ARG5, offset);
   SET_STATUS_from_SysRes(r);
}

PRE(sys_stat64)
{
   /* int stat64(const char *path, struct stat64 *buf); */
   PRINT("sys_stat64 ( %#lx(%s), %#lx )", ARG1, (HChar*)ARG1, ARG2);
   PRE_REG_READ2(long, "stat64", const char *, path, struct stat64 *, buf);

   PRE_MEM_RASCIIZ("stat64(path)", ARG1);
   PRE_MEM_WRITE("stat64(buf)", ARG2, sizeof(struct vki_stat64));
}

POST(sys_stat64)
{
   POST_MEM_WRITE(ARG2, sizeof(struct vki_stat64));
}

PRE(sys_lstat64)
{
   /* int lstat64(const char *path, struct stat64 *buf); */
   PRINT("sys_lstat64 ( %#lx(%s), %#lx )", ARG1, (HChar*)ARG1, ARG2);
   PRE_REG_READ2(long, "lstat64", const char *, path, struct stat64 *, buf);

   PRE_MEM_RASCIIZ("lstat64(path)", ARG1);
   PRE_MEM_WRITE("lstat64(buf)", ARG2, sizeof(struct vki_stat64));
}

POST(sys_lstat64)
{
   POST_MEM_WRITE(ARG2, sizeof(struct vki_stat64));
}

PRE(sys_fstat64)
{
   /* int fstat64(int fildes, struct stat64 *buf); */
   PRINT("sys_fstat64 ( %ld, %#lx )", SARG1, ARG2);
   PRE_REG_READ2(long, "fstat64", int, fildes, struct stat64 *, buf);
   PRE_MEM_WRITE("fstat64(buf)", ARG2, sizeof(struct vki_stat64));

   /* Be strict. */
   if (!ML_(fd_allowed)(ARG1, "fstat64", tid, False))
      SET_STATUS_Failure(VKI_EBADF);
}

POST(sys_fstat64)
{
   POST_MEM_WRITE(ARG2, sizeof(struct vki_stat64));
}

static void do_statvfs64_post(struct vki_statvfs64 *stats, ThreadId tid)
{
   POST_FIELD_WRITE(stats->f_bsize);
   POST_FIELD_WRITE(stats->f_frsize);
   POST_FIELD_WRITE(stats->f_blocks);
   POST_FIELD_WRITE(stats->f_bfree);
   POST_FIELD_WRITE(stats->f_bavail);
   POST_FIELD_WRITE(stats->f_files);
   POST_FIELD_WRITE(stats->f_ffree);
   POST_FIELD_WRITE(stats->f_favail);
   POST_FIELD_WRITE(stats->f_fsid);
   POST_MEM_WRITE((Addr) stats->f_basetype, VG_(strlen)(stats->f_basetype) + 1);
   POST_FIELD_WRITE(stats->f_flag);
   POST_FIELD_WRITE(stats->f_namemax);
   POST_MEM_WRITE((Addr) stats->f_fstr, VG_(strlen)(stats->f_fstr) + 1);
}

PRE(sys_statvfs64)
{
   /* int statvfs64(const char *path, struct statvfs64 *buf); */
   *flags |= SfMayBlock;
   PRINT("sys_statvfs64 ( %#lx(%s), %#lx )", ARG1, (HChar *) ARG1, ARG2);
   PRE_REG_READ2(long, "statvfs64", const char *, path,
                 struct vki_statvfs64 *, buf);
   PRE_MEM_RASCIIZ("statvfs64(path)", ARG1);
   PRE_MEM_WRITE("statvfs64(buf)", ARG2, sizeof(struct vki_statvfs64));
}

POST(sys_statvfs64)
{
   do_statvfs64_post((struct vki_statvfs64 *) ARG2, tid);
}

PRE(sys_fstatvfs64)
{
   /* int fstatvfs64(int fd, struct statvfs64 *buf); */
   *flags |= SfMayBlock;
   PRINT("sys_fstatvfs64 ( %ld, %#lx )", SARG1, ARG2);
   PRE_REG_READ2(long, "fstatvfs64", int, fd, struct vki_statvfs64 *, buf);
   PRE_MEM_WRITE("fstatvfs64(buf)", ARG2, sizeof(struct vki_statvfs64));

   /* Be strict. */
   if (!ML_(fd_allowed)(ARG1, "fstatvfs64", tid, False))
      SET_STATUS_Failure(VKI_EBADF);
}

POST(sys_fstatvfs64)
{
   do_statvfs64_post((struct vki_statvfs64 *) ARG2, tid);
}

PRE(sys_setrlimit64)
{
   /* int setrlimit64(int resource, struct rlimit64 *rlim); */
   struct vki_rlimit64 *limit = (struct vki_rlimit64 *)ARG2;
   PRINT("sys_setrlimit64 ( %ld, %#lx )", SARG1, ARG2);
   PRE_REG_READ2(long, "setrlimit64", int, resource, struct rlimit64 *, rlim);
   PRE_MEM_READ("setrlimit64(rlim)", ARG2, sizeof(struct vki_rlimit64));

   if (limit && limit->rlim_cur > limit->rlim_max)
      SET_STATUS_Failure(VKI_EINVAL);
   else if (ARG1 == VKI_RLIMIT_NOFILE) {
      if (limit->rlim_cur > VG_(fd_hard_limit) ||
          limit->rlim_max != VG_(fd_hard_limit)) {
         SET_STATUS_Failure(VKI_EPERM);
      }
      else {
         VG_(fd_soft_limit) = limit->rlim_cur;
         SET_STATUS_Success(0);
      }
   }
   else if (ARG1 == VKI_RLIMIT_DATA) {
      if (limit->rlim_cur > VG_(client_rlimit_data).rlim_max ||
          limit->rlim_max > VG_(client_rlimit_data).rlim_max) {
         SET_STATUS_Failure(VKI_EPERM);
      }
      else {
         VG_(client_rlimit_data).rlim_max = limit->rlim_max;
         VG_(client_rlimit_data).rlim_cur = limit->rlim_cur;
         SET_STATUS_Success(0);
      }
   }
   else if (ARG1 == VKI_RLIMIT_STACK && tid == 1) {
      if (limit->rlim_cur > VG_(client_rlimit_stack).rlim_max ||
          limit->rlim_max > VG_(client_rlimit_stack).rlim_max) {
         SET_STATUS_Failure(VKI_EPERM);
      }
      else {
         /* Change the value of client_stack_szB to the rlim_cur value but
            only if it is smaller than the size of the allocated stack for the
            client. */
         if (limit->rlim_cur <= VG_(clstk_max_size))
            VG_(threads)[tid].client_stack_szB = limit->rlim_cur;

         VG_(client_rlimit_stack).rlim_max = limit->rlim_max;
         VG_(client_rlimit_stack).rlim_cur = limit->rlim_cur;
         SET_STATUS_Success(0);
      }
   }
}

PRE(sys_getrlimit64)
{
   /* int getrlimit64(int resource, struct rlimit64 *rlim); */
   PRINT("sys_getrlimit64 ( %ld, %#lx )", SARG1, ARG2);
   PRE_REG_READ2(long, "getrlimit64",
                 int, resource, struct rlimit64 *, rlim);
   PRE_MEM_WRITE("getrlimit64(rlim)", ARG2, sizeof(struct vki_rlimit64));
}

POST(sys_getrlimit64)
{
   /* Based on common_post_getrlimit() from syswrap-generic.c. */
   struct vki_rlimit64 *rlim = (struct vki_rlimit64*)ARG2;

   POST_MEM_WRITE(ARG2, sizeof(struct vki_rlimit64));

   switch (ARG1 /*resource*/) {
   case VKI_RLIMIT_NOFILE:
      rlim->rlim_cur = VG_(fd_soft_limit);
      rlim->rlim_max = VG_(fd_hard_limit);
      break;
   case VKI_RLIMIT_DATA:
      rlim->rlim_cur = VG_(client_rlimit_data).rlim_cur;
      rlim->rlim_max = VG_(client_rlimit_data).rlim_max;
      break;
   case VKI_RLIMIT_STACK:
      rlim->rlim_cur = VG_(client_rlimit_stack).rlim_cur;
      rlim->rlim_max = VG_(client_rlimit_stack).rlim_max;
      break;
   }
}

PRE(sys_pread64)
{
   /* ssize32_t pread64(int fd, void *buf, size32_t count,
                        uint32_t offset_1, uint32_t offset_2);
    */
   *flags |= SfMayBlock;
   PRINT("sys_pread64 ( %ld, %#lx, %lu, %#lx, %#lx )",
         SARG1, ARG2, ARG3, ARG4, ARG5);
   PRE_REG_READ5(long, "pread64", int, fd, void *, buf, vki_size32_t, count,
                 vki_uint32_t, offset_1, vki_uint32_t, offset_2);
   PRE_MEM_WRITE("pread64(buf)", ARG2, ARG3);

   /* Be strict. */
   if (!ML_(fd_allowed)(ARG1, "pread64", tid, False))
      SET_STATUS_Failure(VKI_EBADF);
}

POST(sys_pread64)
{
   POST_MEM_WRITE(ARG2, RES);
}

PRE(sys_pwrite64)
{
   /* ssize32_t pwrite64(int fd, void *buf, size32_t count,
                         uint32_t offset_1, uint32_t offset_2);
    */
   *flags |= SfMayBlock;
   PRINT("sys_pwrite64 ( %ld, %#lx, %lu, %#lx, %#lx )",
         SARG1, ARG2, ARG3, ARG4, ARG5);
   PRE_REG_READ5(long, "pwrite64", int, fd, void *, buf, vki_size32_t, count,
                 vki_uint32_t, offset_1, vki_uint32_t, offset_2);
   PRE_MEM_READ("pwrite64(buf)", ARG2, ARG3);

   /* Be strict. */
   if (!ML_(fd_allowed)(ARG1, "pwrite64", tid, False))
      SET_STATUS_Failure(VKI_EBADF);
}

PRE(sys_open64)
{
   /* int open64(const char *filename, int flags);
      int open64(const char *filename, int flags, mode_t mode); */
   *flags |= SfMayBlock;

   if (ARG2 & VKI_O_CREAT) {
      /* 3-arg version */
      PRINT("sys_open64 ( %#lx(%s), %#lx, %ld )", ARG1, (HChar*)ARG1, ARG2,
            SARG3);
      PRE_REG_READ3(long, "open64", const char *, filename, int, flags,
                    vki_mode_t, mode);
   }
   else {
      /* 2-arg version */
      PRINT("sys_open64 ( %#lx(%s), %#lx )", ARG1, (HChar*)ARG1, ARG2);
      PRE_REG_READ2(long, "open64", const char *, filename, int, flags);
   }
   PRE_MEM_RASCIIZ("open(filename)", ARG1);
}

POST(sys_open64)
{
   if (!ML_(fd_allowed)(RES, "open64", tid, True)) {
      VG_(close)(RES);
      SET_STATUS_Failure(VKI_EMFILE);
   }
   else if (VG_(clo_track_fds))
      ML_(record_fd_open_with_given_name)(tid, RES, (HChar*)ARG1);
}

#undef PRE
#undef POST

#endif // defined(VGP_x86_solaris)

/*--------------------------------------------------------------------*/
/*--- end                                                          ---*/
/*--------------------------------------------------------------------*/