/
syscall.c
2059 lines (1798 loc) · 53 KB
/
syscall.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
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
990
991
992
993
994
995
996
997
998
999
1000
/*
* BSD syscalls
*
* Copyright (c) 2003 - 2008 Fabrice Bellard
*
* 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/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/event.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <utime.h>
#include "qemu.h"
#include "qemu-common.h"
//#define DEBUG
static abi_ulong target_brk;
static abi_ulong target_original_brk;
static inline abi_long get_errno(abi_long ret)
{
if (ret == -1)
/* XXX need to translate host -> target errnos here */
return -(errno);
else
return ret;
}
#define target_to_host_bitmask(x, tbl) (x)
static inline int is_error(abi_long ret)
{
return (abi_ulong)ret >= (abi_ulong)(-4096);
}
void target_set_brk(abi_ulong new_brk)
{
target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
}
/* do_obreak() must return target errnos. */
static abi_long do_obreak(abi_ulong new_brk)
{
abi_ulong brk_page;
abi_long mapped_addr;
int new_alloc_size;
if (!new_brk)
return 0;
if (new_brk < target_original_brk)
return -TARGET_EINVAL;
brk_page = HOST_PAGE_ALIGN(target_brk);
/* If the new brk is less than this, set it and we're done... */
if (new_brk < brk_page) {
target_brk = new_brk;
return 0;
}
/* We need to allocate more memory after the brk... */
new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0));
if (!is_error(mapped_addr))
target_brk = new_brk;
else
return mapped_addr;
return 0;
}
abi_long do_brk(abi_ulong new_brk)
{
return do_obreak(new_brk);
}
#if defined(TARGET_I386)
static abi_long do_freebsd_sysarch(CPUX86State *env, int op, abi_ulong parms)
{
abi_long ret = 0;
abi_ulong val;
int idx;
switch(op) {
#ifdef TARGET_ABI32
case TARGET_FREEBSD_I386_SET_GSBASE:
case TARGET_FREEBSD_I386_SET_FSBASE:
if (op == TARGET_FREEBSD_I386_SET_GSBASE)
#else
case TARGET_FREEBSD_AMD64_SET_GSBASE:
case TARGET_FREEBSD_AMD64_SET_FSBASE:
if (op == TARGET_FREEBSD_AMD64_SET_GSBASE)
#endif
idx = R_GS;
else
idx = R_FS;
if (get_user(val, parms, abi_ulong))
return -TARGET_EFAULT;
cpu_x86_load_seg(env, idx, 0);
env->segs[idx].base = val;
break;
#ifdef TARGET_ABI32
case TARGET_FREEBSD_I386_GET_GSBASE:
case TARGET_FREEBSD_I386_GET_FSBASE:
if (op == TARGET_FREEBSD_I386_GET_GSBASE)
#else
case TARGET_FREEBSD_AMD64_GET_GSBASE:
case TARGET_FREEBSD_AMD64_GET_FSBASE:
if (op == TARGET_FREEBSD_AMD64_GET_GSBASE)
#endif
idx = R_GS;
else
idx = R_FS;
val = env->segs[idx].base;
if (put_user(val, parms, abi_ulong))
return -TARGET_EFAULT;
break;
/* XXX handle the others... */
default:
ret = -TARGET_EINVAL;
break;
}
return ret;
}
#endif
#ifdef TARGET_SPARC
static abi_long do_freebsd_sysarch(void *env, int op, abi_ulong parms)
{
/* XXX handle
* TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
* TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
*/
return -TARGET_EINVAL;
}
#endif
#ifdef TARGET_ARM
static abi_long do_freebsd_sysarch(void *env, int op, abi_ulong parms)
{
return -TARGET_EINVAL;
}
#endif
#ifdef TARGET_MIPS
static abi_long do_freebsd_sysarch(void *env, int op, abi_ulong parms)
{
return -TARGET_EINVAL;
}
#endif
#ifdef __FreeBSD__
/*
* XXX this uses the undocumented oidfmt interface to find the kind of
* a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
* (this is mostly copied from src/sbin/sysctl/sysctl.c)
*/
static int
oidfmt(int *oid, int len, char *fmt, uint32_t *kind)
{
int qoid[CTL_MAXNAME+2];
uint8_t buf[BUFSIZ];
int i;
size_t j;
qoid[0] = 0;
qoid[1] = 4;
memcpy(qoid + 2, oid, len * sizeof(int));
j = sizeof(buf);
i = sysctl(qoid, len + 2, buf, &j, 0, 0);
if (i)
return i;
if (kind)
*kind = *(uint32_t *)buf;
if (fmt)
strcpy(fmt, (char *)(buf + sizeof(uint32_t)));
return (0);
}
/*
* try and convert sysctl return data for the target.
* XXX doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
*/
static int sysctl_oldcvt(void *holdp, size_t holdlen, uint32_t kind)
{
switch (kind & CTLTYPE) {
case CTLTYPE_INT:
case CTLTYPE_UINT:
*(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
break;
#ifdef TARGET_ABI32
case CTLTYPE_LONG:
case CTLTYPE_ULONG:
*(uint32_t *)holdp = tswap32(*(long *)holdp);
break;
#else
case CTLTYPE_LONG:
*(uint64_t *)holdp = tswap64(*(long *)holdp);
case CTLTYPE_ULONG:
*(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
break;
#endif
#if !defined(__FreeBSD_version) || __FreeBSD_version < 900031
case CTLTYPE_QUAD:
#else
case CTLTYPE_U64:
case CTLTYPE_S64:
#endif
*(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
break;
case CTLTYPE_STRING:
break;
default:
/* XXX unhandled */
return -1;
}
return 0;
}
/* XXX this needs to be emulated on non-FreeBSD hosts... */
static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong oldp,
abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
{
abi_long ret;
void *hnamep, *holdp, *hnewp = NULL;
size_t holdlen;
abi_ulong oldlen = 0;
int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
uint32_t kind = 0;
if (oldlenp)
get_user_ual(oldlen, oldlenp);
if (!(hnamep = lock_user(VERIFY_READ, namep, namelen, 1)))
return -TARGET_EFAULT;
if (newp && !(hnewp = lock_user(VERIFY_READ, newp, newlen, 1)))
return -TARGET_EFAULT;
if (!(holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0)))
return -TARGET_EFAULT;
holdlen = oldlen;
for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
*q++ = tswap32(*p);
oidfmt(snamep, namelen, NULL, &kind);
/* XXX swap hnewp */
ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
if (!ret)
sysctl_oldcvt(holdp, holdlen, kind);
put_user_ual(holdlen, oldlenp);
unlock_user(hnamep, namep, 0);
unlock_user(holdp, oldp, holdlen);
if (hnewp)
unlock_user(hnewp, newp, 0);
g_free(snamep);
return ret;
}
#endif
/* FIXME
* lock_iovec()/unlock_iovec() have a return code of 0 for success where
* other lock functions have a return code of 0 for failure.
*/
static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
int count, int copy)
{
struct target_iovec *target_vec;
abi_ulong base;
int i;
target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
if (!target_vec)
return -TARGET_EFAULT;
for(i = 0;i < count; i++) {
base = tswapl(target_vec[i].iov_base);
vec[i].iov_len = tswapl(target_vec[i].iov_len);
if (vec[i].iov_len != 0) {
vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
/* Don't check lock_user return value. We must call writev even
if a element has invalid base address. */
} else {
/* zero length pointer is ignored */
vec[i].iov_base = NULL;
}
}
unlock_user (target_vec, target_addr, 0);
return 0;
}
static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
int count, int copy)
{
struct target_iovec *target_vec;
abi_ulong base;
int i;
target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
if (!target_vec)
return -TARGET_EFAULT;
for(i = 0;i < count; i++) {
if (target_vec[i].iov_base) {
base = tswapl(target_vec[i].iov_base);
unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
}
}
unlock_user (target_vec, target_addr, 0);
return 0;
}
static inline rlim_t
target_to_host_rlim(abi_ulong target_rlim)
{
abi_ulong target_rlim_swap;
rlim_t result;
target_rlim_swap = tswapal(target_rlim);
if (target_rlim_swap == TARGET_RLIM_INFINITY)
return (RLIM_INFINITY);
result = target_rlim_swap;
if (target_rlim_swap != (rlim_t)result)
return (RLIM_INFINITY);
return (result);
}
static inline abi_ulong
host_to_target_rlim(rlim_t rlim)
{
abi_ulong target_rlim_swap;
abi_ulong result;
if (rlim == RLIM_INFINITY || rlim != (abi_long)rlim)
target_rlim_swap = TARGET_RLIM_INFINITY;
else
target_rlim_swap = rlim;
result = tswapal(target_rlim_swap);
return (result);
}
static inline int
target_to_host_resource(int code)
{
switch (code) {
case TARGET_RLIMIT_AS:
return RLIMIT_AS;
case TARGET_RLIMIT_CORE:
return RLIMIT_CORE;
case TARGET_RLIMIT_CPU:
return RLIMIT_CPU;
case TARGET_RLIMIT_DATA:
return RLIMIT_DATA;
case TARGET_RLIMIT_FSIZE:
return RLIMIT_FSIZE;
case TARGET_RLIMIT_MEMLOCK:
return RLIMIT_MEMLOCK;
case TARGET_RLIMIT_NOFILE:
return RLIMIT_NOFILE;
case TARGET_RLIMIT_NPROC:
return RLIMIT_NPROC;
case TARGET_RLIMIT_RSS:
return RLIMIT_RSS;
case TARGET_RLIMIT_SBSIZE:
return RLIMIT_SBSIZE;
case TARGET_RLIMIT_STACK:
return RLIMIT_STACK;
case TARGET_RLIMIT_SWAP:
return RLIMIT_SWAP;
case TARGET_RLIMIT_NPTS:
return RLIMIT_NPTS;
default:
return (code);
}
}
static int
target_to_host_fcntl_cmd(int cmd)
{
switch(cmd) {
case TARGET_F_DUPFD:
return F_DUPFD;
case TARGET_F_DUP2FD:
return F_DUP2FD;
case TARGET_F_GETFD:
return F_GETFD;
case TARGET_F_SETFD:
return F_SETFD;
case TARGET_F_GETFL:
return F_GETFL;
case TARGET_F_SETFL:
return F_SETFL;
case TARGET_F_GETOWN:
return F_GETOWN;
case TARGET_F_SETOWN:
return F_SETOWN;
case TARGET_F_GETLK:
return F_GETLK;
case TARGET_F_SETLK:
return F_SETLK;
case TARGET_F_SETLKW:
return F_SETLKW;
case TARGET_F_READAHEAD:
return F_READAHEAD;
case TARGET_F_RDAHEAD:
return F_RDAHEAD;
default:
return (cmd);
}
}
static inline abi_long
host_to_target_rusage(abi_ulong target_addr, const struct rusage *rusage)
{
struct target_rusage *target_rusage;
if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0))
return (-TARGET_EFAULT);
target_rusage->ru_utime.tv_sec = tswapal(rusage->ru_utime.tv_sec);
target_rusage->ru_utime.tv_usec = tswapal(rusage->ru_utime.tv_usec);
target_rusage->ru_stime.tv_sec = tswapal(rusage->ru_stime.tv_sec);
target_rusage->ru_stime.tv_usec = tswapal(rusage->ru_stime.tv_usec);
target_rusage->ru_maxrss = tswapal(rusage->ru_maxrss);
target_rusage->ru_ixrss = tswapal(rusage->ru_ixrss);
target_rusage->ru_idrss = tswapal(rusage->ru_idrss);
target_rusage->ru_isrss = tswapal(rusage->ru_isrss);
target_rusage->ru_minflt = tswapal(rusage->ru_minflt);
target_rusage->ru_majflt = tswapal(rusage->ru_majflt);
target_rusage->ru_nswap = tswapal(rusage->ru_nswap);
target_rusage->ru_inblock = tswapal(rusage->ru_inblock);
target_rusage->ru_oublock = tswapal(rusage->ru_oublock);
target_rusage->ru_msgsnd = tswapal(rusage->ru_msgsnd);
target_rusage->ru_msgrcv = tswapal(rusage->ru_msgrcv);
target_rusage->ru_nsignals = tswapal(rusage->ru_nsignals);
target_rusage->ru_nvcsw = tswapal(rusage->ru_nvcsw);
target_rusage->ru_nivcsw = tswapal(rusage->ru_nivcsw);
unlock_user_struct(target_rusage, target_addr, 1);
return (0);
}
/*
* Map host to target signal numbers for the wait family of syscalls.
* Assume all other status bits are the same.
*/
static int
host_to_target_waitstatus(int status)
{
if (WIFSIGNALED(status)) {
return (host_to_target_signal(WTERMSIG(status)) |
(status & ~0x7f));
}
if (WIFSTOPPED(status)) {
return (host_to_target_signal(WSTOPSIG(status)) << 8) |
(status & 0xff);
}
return (status);
}
static inline abi_long
fbsd_copy_from_user_timeval(struct timeval *tv, abi_ulong target_tv_addr)
{
struct target_freebsd_timeval *target_tv;
if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 0))
return -TARGET_EFAULT;
__get_user(tv->tv_sec, &target_tv->tv_sec);
__get_user(tv->tv_usec, &target_tv->tv_usec);
unlock_user_struct(target_tv, target_tv_addr, 1);
return (0);
}
static inline abi_long
fbsd_copy_from_user_timespec(struct timespec *ts, abi_ulong target_ts_addr)
{
struct target_freebsd_timespec *target_ts;
if (!lock_user_struct(VERIFY_READ, target_ts, target_ts_addr, 0))
return -TARGET_EFAULT;
__get_user(ts->tv_sec, &target_ts->tv_sec);
__get_user(ts->tv_nsec, &target_ts->tv_nsec);
unlock_user_struct(target_ts, target_ts_addr, 1);
return (0);
}
static inline abi_long
fbsd_copy_to_user_timeval(struct timeval *tv, abi_ulong target_tv_addr)
{
struct target_freebsd_timeval *target_tv;
if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0))
return -TARGET_EFAULT;
__put_user(tv->tv_sec, &target_tv->tv_sec);
__put_user(tv->tv_usec, &target_tv->tv_usec);
unlock_user_struct(target_tv, target_tv_addr, 1);
return (0);
}
static inline abi_long
fbsd_copy_to_user_timespec(struct timespec *ts, abi_ulong target_ts_addr)
{
struct target_freebsd_timespec *target_ts;
if (!lock_user_struct(VERIFY_WRITE, target_ts, target_ts_addr, 0))
return -TARGET_EFAULT;
__put_user(ts->tv_sec, &target_ts->tv_sec);
__put_user(ts->tv_nsec, &target_ts->tv_nsec);
unlock_user_struct(target_ts, target_ts_addr, 1);
return (0);
}
static inline abi_ulong
fbsd_copy_from_user_fdset(fd_set *fds, abi_ulong target_fds_addr, int n)
{
int i, nw, j, k;
abi_ulong b, *target_fds;
nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
if (!(target_fds = lock_user(VERIFY_READ, target_fds_addr,
sizeof(abi_ulong) * nw, 1)))
return (-TARGET_EFAULT);
FD_ZERO(fds);
k = 0;
for (i = 0; i < nw; i++) {
/* grab the abi_ulong */
__get_user(b, &target_fds[i]);
for (j = 0; j < TARGET_ABI_BITS; j++) {
/* check the bit inside the abi_ulong */
if ((b >> j) & 1)
FD_SET(k, fds);
k++;
}
}
unlock_user(target_fds, target_fds_addr, 0);
return (0);
}
static inline abi_ulong
fbsd_copy_from_user_fdset_ptr(fd_set *fds, fd_set **fds_ptr,
abi_ulong target_fds_addr, int n)
{
if (target_fds_addr) {
if (fbsd_copy_from_user_fdset(fds, target_fds_addr, n))
return (-TARGET_EFAULT);
*fds_ptr = fds;
} else {
*fds_ptr = NULL;
}
return (0);
}
static inline abi_long
fbsd_copy_to_user_fdset(abi_ulong target_fds_addr, const fd_set *fds, int n)
{
int i, nw, j, k;
abi_long v;
abi_ulong *target_fds;
nw = (n + TARGET_ABI_BITS - 1) / TARGET_ABI_BITS;
if (!(target_fds = lock_user(VERIFY_WRITE, target_fds_addr,
sizeof(abi_ulong) * nw, 0)))
return (-TARGET_EFAULT);
k = 0;
for (i = 0; i < nw; i++) {
v = 0;
for (j = 0; j < TARGET_ABI_BITS; j++) {
v |= ((FD_ISSET(k, fds) != 0) << j);
k++;
}
__put_user(v, &target_fds[i]);
}
unlock_user(target_fds, target_fds_addr, sizeof(abi_ulong) * nw);
return (0);
}
#if TARGET_ABI_BITS == 32
static inline uint64_t
target_offset64(uint32_t word0, uint32_t word1)
{
#ifdef TARGET_WORDS_BIGENDIAN
return ((uint64_t)word0 << 32) | word1;
#else
return ((uint64_t)word1 << 32) | word0;
#endif
}
#else /* TARGET_ABI_BITS != 32 */
static inline uint64_t
target_offset64(uint64_t word0, uint64_t word1)
{
return (word0);
}
#endif /* TARGET_ABI_BITS != 32 */
/* ARM EABI and MIPS expect 64bit types aligned even on pairs of registers */
#ifdef TARGET_ARM
static inline int
regpairs_aligned(void *cpu_env) {
return ((((CPUARMState *)cpu_env)->eabi) == 1);
}
#elif defined(TARGET_MIPS)
static inline int
regpairs_aligned(void *cpu_env) { return 1; }
#else
static inline int
regpairs_aligned(void *cpu_env) { return 0; }
#endif
static inline abi_long
unimplemented(int num)
{
qemu_log("qemu: Unsupported syscall: %d\n", num);
return (-TARGET_ENOSYS);
}
/* do_freebsd_select() must return target values and target errnos. */
static abi_long
do_freebsd_select(int n, abi_ulong rfd_addr, abi_ulong wfd_addr,
abi_ulong efd_addr, abi_ulong target_tv_addr)
{
fd_set rfds, wfds, efds;
fd_set *rfds_ptr, *wfds_ptr, *efds_ptr;
struct timeval tv, *tv_ptr;
abi_long ret;
if ((ret = fbsd_copy_from_user_fdset_ptr(&rfds, &rfds_ptr, rfd_addr, n))
!= 0)
return (ret);
if ((ret = fbsd_copy_from_user_fdset_ptr(&wfds, &wfds_ptr, wfd_addr, n))
!= 0)
return (ret);
if ((ret = fbsd_copy_from_user_fdset_ptr(&efds, &efds_ptr, efd_addr, n))
!= 0)
return (ret);
if (target_tv_addr) {
if (fbsd_copy_from_user_timeval(&tv, target_tv_addr))
return (-TARGET_EFAULT);
tv_ptr = &tv;
} else {
tv_ptr = NULL;
}
ret = get_errno(select(n, rfds_ptr, wfds_ptr, efds_ptr, tv_ptr));
if (!is_error(ret)) {
if (rfd_addr && fbsd_copy_to_user_fdset(rfd_addr, &rfds, n))
return (-TARGET_EFAULT);
if (wfd_addr && fbsd_copy_to_user_fdset(wfd_addr, &wfds, n))
return (-TARGET_EFAULT);
if (efd_addr && fbsd_copy_to_user_fdset(efd_addr, &efds, n))
return (-TARGET_EFAULT);
if (target_tv_addr &&
fbsd_copy_to_user_timeval(&tv, target_tv_addr))
return (-TARGET_EFAULT);
}
return (ret);
}
/* do_syscall() should always have a single exit point at the end so
that actions, such as logging of syscall results, can be performed.
All errnos that do_syscall() returns must be -TARGET_<errcode>. */
abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
abi_long arg2, abi_long arg3, abi_long arg4,
abi_long arg5, abi_long arg6, abi_long arg7,
abi_long arg8)
{
abi_long ret;
void *p;
struct stat st;
#ifdef DEBUG
gemu_log("freebsd syscall %d\n", num);
#endif
if(do_strace)
print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
switch(num) {
case TARGET_FREEBSD_NR_exit:
#ifdef TARGET_GPROF
_mcleanup();
#endif
gdb_exit(cpu_env, arg1);
/* XXX: should free thread stack and CPU env */
_exit(arg1);
ret = 0; /* avoid warning */
break;
case TARGET_FREEBSD_NR_read:
if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
goto efault;
ret = get_errno(read(arg1, p, arg3));
unlock_user(p, arg2, ret);
break;
case TARGET_FREEBSD_NR_readv:
{
int count = arg3;
struct iovec *vec;
vec = alloca(count * sizeof(struct iovec));
if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
goto efault;
ret = get_errno(readv(arg1, vec, count));
unlock_iovec(vec, arg2, count, 1);
}
break;
case TARGET_FREEBSD_NR_pread:
if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
goto efault;
ret = get_errno(pread(arg1, p, arg3, target_offset64(arg4, arg5)));
unlock_user(p, arg2, ret);
break;
case TARGET_FREEBSD_NR_preadv:
{
int count = arg3;
struct iovec *vec;
vec = alloca(count * sizeof(struct iovec));
if (lock_iovec(VERIFY_WRITE, vec, arg2, count, 0) < 0)
goto efault;
ret = get_errno(preadv(arg1, vec, count,
target_offset64(arg4, arg5)));
unlock_iovec(vec, arg2, count, 1);
}
break;
case TARGET_FREEBSD_NR_write:
if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
goto efault;
ret = get_errno(write(arg1, p, arg3));
unlock_user(p, arg2, 0);
break;
case TARGET_FREEBSD_NR_writev:
{
int count = arg3;
struct iovec *vec;
vec = alloca(count * sizeof(struct iovec));
if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
goto efault;
ret = get_errno(writev(arg1, vec, count));
unlock_iovec(vec, arg2, count, 0);
}
break;
case TARGET_FREEBSD_NR_pwrite:
if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
goto efault;
ret = get_errno(pwrite(arg1, p, arg3, target_offset64(arg4, arg5)));
unlock_user(p, arg2, 0);
break;
case TARGET_FREEBSD_NR_pwritev:
{
int count = arg3;
struct iovec *vec;
vec = alloca(count * sizeof(struct iovec));
if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
goto efault;
ret = get_errno(pwritev(arg1, vec, count,
target_offset64(arg4, arg5)));
unlock_iovec(vec, arg2, count, 0);
}
break;
case TARGET_FREEBSD_NR_open:
if (!(p = lock_user_string(arg1)))
goto efault;
ret = get_errno(open(path(p),
target_to_host_bitmask(arg2, fcntl_flags_tbl),
arg3));
unlock_user(p, arg1, 0);
break;
case TARGET_FREEBSD_NR_mmap:
ret = get_errno(target_mmap(arg1, arg2, arg3,
target_to_host_bitmask(arg4, mmap_flags_tbl),
arg5,
arg6));
break;
case TARGET_FREEBSD_NR_munmap:
ret = get_errno(target_munmap(arg1, arg2));
break;
case TARGET_FREEBSD_NR_mprotect:
ret = get_errno(target_mprotect(arg1, arg2, arg3));
break;
case TARGET_FREEBSD_NR_msync:
ret = get_errno(msync(g2h(arg1), arg2, arg3));
break;
case TARGET_FREEBSD_NR_mlock:
ret = get_errno(mlock(g2h(arg1), arg2));
break;
case TARGET_FREEBSD_NR_munlock:
ret = get_errno(munlock(g2h(arg1), arg2));
break;
case TARGET_FREEBSD_NR_mlockall:
ret = get_errno(mlockall(arg1));
break;
case TARGET_FREEBSD_NR_munlockall:
ret = get_errno(munlockall());
break;
case TARGET_FREEBSD_NR_madvise:
/*
* A straight passthrough may not be safe because qemu sometimes
* turns private file-backed mapping into anonymous mappings. This
* will break MADV_DONTNEED. This is a hint, so ignoring and returing
* success is ok.
*/
ret = get_errno(0);
break;
case TARGET_FREEBSD_NR_break:
ret = do_obreak(arg1);
break;
#ifdef __FreeBSD__
case TARGET_FREEBSD_NR___sysctl:
ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
break;
#endif
case TARGET_FREEBSD_NR_sysarch:
ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
break;
case TARGET_FREEBSD_NR_syscall:
case TARGET_FREEBSD_NR___syscall:
ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
break;
case TARGET_FREEBSD_NR_stat:
if (!(p = lock_user_string(arg1)))
goto efault;
ret = get_errno(stat(path(p), &st));
unlock_user(p, arg1, 0);
goto do_stat;
case TARGET_FREEBSD_NR_lstat:
if (!(p = lock_user_string(arg1)))
goto efault;
ret = get_errno(lstat(path(p), &st));
unlock_user(p, arg1, 0);
goto do_stat;
case TARGET_FREEBSD_NR_fstat:
{
ret = get_errno(fstat(arg1, &st));
do_stat:
if (!is_error(ret)) {
struct target_freebsd_stat *target_st;
if (!lock_user_struct(VERIFY_WRITE, target_st, arg2, 0))
goto efault;
memset(target_st, 0, sizeof(*target_st));
__put_user(st.st_dev, &target_st->st_dev);
__put_user(st.st_ino, &target_st->st_ino);
__put_user(st.st_mode, &target_st->st_mode);
__put_user(st.st_nlink, &target_st->st_nlink);
__put_user(st.st_uid, &target_st->st_uid);
__put_user(st.st_gid, &target_st->st_gid);
__put_user(st.st_rdev, &target_st->st_rdev);
__put_user(st.st_atim.tv_sec, &target_st->st_atim.tv_sec);
__put_user(st.st_atim.tv_nsec, &target_st->st_atim.tv_nsec);
__put_user(st.st_mtim.tv_sec, &target_st->st_mtim.tv_sec);
__put_user(st.st_mtim.tv_nsec, &target_st->st_mtim.tv_nsec);
__put_user(st.st_ctim.tv_sec, &target_st->st_ctim.tv_sec);
__put_user(st.st_ctim.tv_nsec, &target_st->st_ctim.tv_nsec);
__put_user(st.st_size, &target_st->st_size);
__put_user(st.st_blocks, &target_st->st_blocks);
__put_user(st.st_blksize, &target_st->st_blksize);
__put_user(st.st_flags, &target_st->st_flags);
__put_user(st.st_gen, &target_st->st_gen);
/* st_lspare not used */
__put_user(st.st_birthtim.tv_sec,
&target_st->st_birthtim.tv_sec);
__put_user(st.st_birthtim.tv_nsec,
&target_st->st_birthtim.tv_nsec);
unlock_user_struct(target_st, arg2, 1);
}
}
break;
case TARGET_FREEBSD_NR_clock_gettime:
{
struct timespec ts;
ret = get_errno(clock_gettime(arg1, &ts));
if (!is_error(ret)) {
if (fbsd_copy_to_user_timespec(&ts, arg2))
goto efault;
}
}
break;
case TARGET_FREEBSD_NR_clock_getres:
{
struct timespec ts;
ret = get_errno(clock_getres(arg1, &ts));
if (!is_error(ret)) {
if (fbsd_copy_to_user_timespec(&ts, arg2))
goto efault;
}
}
break;
case TARGET_FREEBSD_NR_clock_settime:
{
struct timespec ts;
if (fbsd_copy_from_user_timespec(&ts, arg2) != 0)
goto efault;
ret = get_errno(clock_settime(arg1, &ts));
}
break;
case TARGET_FREEBSD_NR_gettimeofday:
{
struct timeval tv;
struct timezone tz, *target_tz;
if (arg2 != 0) {
if (!lock_user_struct(VERIFY_READ, target_tz, arg2, 0))
goto efault;
__get_user(tz.tz_minuteswest,
&target_tz->tz_minuteswest);
__get_user(tz.tz_dsttime, &target_tz->tz_dsttime);