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#include "qemu/osdep.h"
35#include "qemu/timer.h"
36#include "exec/gdbstub.h"
37#include "gdbstub/syscalls.h"
38#include "semihosting/semihost.h"
39#include "semihosting/console.h"
40#include "semihosting/common-semi.h"
41#include "semihosting/guestfd.h"
42#include "semihosting/syscalls.h"
43
44#ifdef CONFIG_USER_ONLY
45#include "qemu.h"
46
47#define COMMON_SEMI_HEAP_SIZE (128 * 1024 * 1024)
48#else
49#include "qemu/cutils.h"
50#include "hw/loader.h"
51#include "hw/boards.h"
52#endif
53
54#define TARGET_SYS_OPEN 0x01
55#define TARGET_SYS_CLOSE 0x02
56#define TARGET_SYS_WRITEC 0x03
57#define TARGET_SYS_WRITE0 0x04
58#define TARGET_SYS_WRITE 0x05
59#define TARGET_SYS_READ 0x06
60#define TARGET_SYS_READC 0x07
61#define TARGET_SYS_ISERROR 0x08
62#define TARGET_SYS_ISTTY 0x09
63#define TARGET_SYS_SEEK 0x0a
64#define TARGET_SYS_FLEN 0x0c
65#define TARGET_SYS_TMPNAM 0x0d
66#define TARGET_SYS_REMOVE 0x0e
67#define TARGET_SYS_RENAME 0x0f
68#define TARGET_SYS_CLOCK 0x10
69#define TARGET_SYS_TIME 0x11
70#define TARGET_SYS_SYSTEM 0x12
71#define TARGET_SYS_ERRNO 0x13
72#define TARGET_SYS_GET_CMDLINE 0x15
73#define TARGET_SYS_HEAPINFO 0x16
74#define TARGET_SYS_EXIT 0x18
75#define TARGET_SYS_SYNCCACHE 0x19
76#define TARGET_SYS_EXIT_EXTENDED 0x20
77#define TARGET_SYS_ELAPSED 0x30
78#define TARGET_SYS_TICKFREQ 0x31
79
80
81
82#define ADP_Stopped_ApplicationExit (0x20026)
83
84#ifndef O_BINARY
85#define O_BINARY 0
86#endif
87
88static int gdb_open_modeflags[12] = {
89 GDB_O_RDONLY,
90 GDB_O_RDONLY,
91 GDB_O_RDWR,
92 GDB_O_RDWR,
93 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
94 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
95 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
96 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
97 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
98 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
99 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
100 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
101};
102
103#ifndef CONFIG_USER_ONLY
104
105
106
107
108
109
110
111
112
113
114
115
116
117typedef struct LayoutInfo {
118 target_ulong rambase;
119 size_t ramsize;
120 hwaddr heapbase;
121 hwaddr heaplimit;
122} LayoutInfo;
123
124static bool find_ram_cb(Int128 start, Int128 len, const MemoryRegion *mr,
125 hwaddr offset_in_region, void *opaque)
126{
127 LayoutInfo *info = (LayoutInfo *) opaque;
128 uint64_t size = int128_get64(len);
129
130 if (!mr->ram || mr->readonly) {
131 return false;
132 }
133
134 if (size > info->ramsize) {
135 info->rambase = int128_get64(start);
136 info->ramsize = size;
137 }
138
139
140 return false;
141}
142
143static LayoutInfo common_semi_find_bases(CPUState *cs)
144{
145 FlatView *fv;
146 LayoutInfo info = { 0, 0, 0, 0 };
147
148 RCU_READ_LOCK_GUARD();
149
150 fv = address_space_to_flatview(cs->as);
151 flatview_for_each_range(fv, find_ram_cb, &info);
152
153
154
155
156
157
158 if (info.rambase || info.ramsize > 0) {
159 RomGap gap = rom_find_largest_gap_between(info.rambase, info.ramsize);
160 info.heapbase = gap.base;
161 info.heaplimit = gap.base + gap.size;
162 }
163
164 return info;
165}
166
167#endif
168
169#include "common-semi-target.h"
170
171
172
173
174
175
176
177
178
179
180
181
182
183#define GET_ARG(n) do { \
184 if (is_64bit_semihosting(env)) { \
185 if (get_user_u64(arg ## n, args + (n) * 8)) { \
186 goto do_fault; \
187 } \
188 } else { \
189 if (get_user_u32(arg ## n, args + (n) * 4)) { \
190 goto do_fault; \
191 } \
192 } \
193} while (0)
194
195#define SET_ARG(n, val) \
196 (is_64bit_semihosting(env) ? \
197 put_user_u64(val, args + (n) * 8) : \
198 put_user_u32(val, args + (n) * 4))
199
200
201
202
203
204
205
206
207
208#ifndef CONFIG_USER_ONLY
209static target_ulong syscall_err;
210
211#include "semihosting/softmmu-uaccess.h"
212#endif
213
214static inline uint32_t get_swi_errno(CPUState *cs)
215{
216#ifdef CONFIG_USER_ONLY
217 TaskState *ts = cs->opaque;
218
219 return ts->swi_errno;
220#else
221 return syscall_err;
222#endif
223}
224
225static void common_semi_cb(CPUState *cs, uint64_t ret, int err)
226{
227 if (err) {
228#ifdef CONFIG_USER_ONLY
229 TaskState *ts = cs->opaque;
230 ts->swi_errno = err;
231#else
232 syscall_err = err;
233#endif
234 }
235 common_semi_set_ret(cs, ret);
236}
237
238
239
240
241
242static void common_semi_dead_cb(CPUState *cs, uint64_t ret, int err)
243{
244 common_semi_set_ret(cs, 0xdeadbeef);
245}
246
247
248
249
250
251static void common_semi_rw_cb(CPUState *cs, uint64_t ret, int err)
252{
253
254 CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
255 target_ulong args = common_semi_arg(cs, 1);
256 target_ulong arg2;
257 GET_ARG(2);
258
259 if (err) {
260 do_fault:
261 ret = 0;
262 }
263 common_semi_set_ret(cs, arg2 - ret);
264}
265
266
267
268
269
270static void common_semi_istty_cb(CPUState *cs, uint64_t ret, int err)
271{
272 if (err) {
273 ret = (err == ENOTTY ? 0 : -1);
274 }
275 common_semi_cb(cs, ret, err);
276}
277
278
279
280
281static void common_semi_seek_cb(CPUState *cs, uint64_t ret, int err)
282{
283 if (!err) {
284 ret = 0;
285 }
286 common_semi_cb(cs, ret, err);
287}
288
289
290
291
292
293
294
295static target_ulong common_semi_flen_buf(CPUState *cs)
296{
297 target_ulong sp = common_semi_stack_bottom(cs);
298 return sp - 64;
299}
300
301static void
302common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
303{
304 if (!err) {
305
306 uint64_t size;
307 if (cpu_memory_rw_debug(cs, common_semi_flen_buf(cs) +
308 offsetof(struct gdb_stat, gdb_st_size),
309 &size, 8, 0)) {
310 ret = -1, err = EFAULT;
311 } else {
312 size = be64_to_cpu(size);
313 if (ret != size) {
314 ret = -1, err = EOVERFLOW;
315 }
316 }
317 }
318 common_semi_cb(cs, ret, err);
319}
320
321static void
322common_semi_readc_cb(CPUState *cs, uint64_t ret, int err)
323{
324 if (!err) {
325 CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
326 uint8_t ch;
327
328 if (get_user_u8(ch, common_semi_stack_bottom(cs) - 1)) {
329 ret = -1, err = EFAULT;
330 } else {
331 ret = ch;
332 }
333 }
334 common_semi_cb(cs, ret, err);
335}
336
337#define SHFB_MAGIC_0 0x53
338#define SHFB_MAGIC_1 0x48
339#define SHFB_MAGIC_2 0x46
340#define SHFB_MAGIC_3 0x42
341
342
343#define SH_EXT_EXIT_EXTENDED (1 << 0)
344#define SH_EXT_STDOUT_STDERR (1 << 1)
345
346static const uint8_t featurefile_data[] = {
347 SHFB_MAGIC_0,
348 SHFB_MAGIC_1,
349 SHFB_MAGIC_2,
350 SHFB_MAGIC_3,
351 SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR,
352};
353
354
355
356
357
358
359
360
361
362void do_common_semihosting(CPUState *cs)
363{
364 CPUArchState *env = cs->env_ptr;
365 target_ulong args;
366 target_ulong arg0, arg1, arg2, arg3;
367 target_ulong ul_ret;
368 char * s;
369 int nr;
370 uint32_t ret;
371 int64_t elapsed;
372
373 nr = common_semi_arg(cs, 0) & 0xffffffffU;
374 args = common_semi_arg(cs, 1);
375
376 switch (nr) {
377 case TARGET_SYS_OPEN:
378 {
379 int ret, err = 0;
380 int hostfd;
381
382 GET_ARG(0);
383 GET_ARG(1);
384 GET_ARG(2);
385 s = lock_user_string(arg0);
386 if (!s) {
387 goto do_fault;
388 }
389 if (arg1 >= 12) {
390 unlock_user(s, arg0, 0);
391 common_semi_cb(cs, -1, EINVAL);
392 break;
393 }
394
395 if (strcmp(s, ":tt") == 0) {
396
397
398
399
400
401
402 if (arg1 < 4) {
403 hostfd = STDIN_FILENO;
404 } else if (arg1 < 8) {
405 hostfd = STDOUT_FILENO;
406 } else {
407 hostfd = STDERR_FILENO;
408 }
409 ret = alloc_guestfd();
410 associate_guestfd(ret, hostfd);
411 } else if (strcmp(s, ":semihosting-features") == 0) {
412
413 if (arg1 != 0 && arg1 != 1) {
414 ret = -1;
415 err = EACCES;
416 } else {
417 ret = alloc_guestfd();
418 staticfile_guestfd(ret, featurefile_data,
419 sizeof(featurefile_data));
420 }
421 } else {
422 unlock_user(s, arg0, 0);
423 semihost_sys_open(cs, common_semi_cb, arg0, arg2 + 1,
424 gdb_open_modeflags[arg1], 0644);
425 break;
426 }
427 unlock_user(s, arg0, 0);
428 common_semi_cb(cs, ret, err);
429 break;
430 }
431
432 case TARGET_SYS_CLOSE:
433 GET_ARG(0);
434 semihost_sys_close(cs, common_semi_cb, arg0);
435 break;
436
437 case TARGET_SYS_WRITEC:
438
439
440
441
442 semihost_sys_write_gf(cs, common_semi_dead_cb,
443 &console_out_gf, args, 1);
444 break;
445
446 case TARGET_SYS_WRITE0:
447 {
448 ssize_t len = target_strlen(args);
449 if (len < 0) {
450 common_semi_dead_cb(cs, -1, EFAULT);
451 } else {
452 semihost_sys_write_gf(cs, common_semi_dead_cb,
453 &console_out_gf, args, len);
454 }
455 }
456 break;
457
458 case TARGET_SYS_WRITE:
459 GET_ARG(0);
460 GET_ARG(1);
461 GET_ARG(2);
462 semihost_sys_write(cs, common_semi_rw_cb, arg0, arg1, arg2);
463 break;
464
465 case TARGET_SYS_READ:
466 GET_ARG(0);
467 GET_ARG(1);
468 GET_ARG(2);
469 semihost_sys_read(cs, common_semi_rw_cb, arg0, arg1, arg2);
470 break;
471
472 case TARGET_SYS_READC:
473 semihost_sys_read_gf(cs, common_semi_readc_cb, &console_in_gf,
474 common_semi_stack_bottom(cs) - 1, 1);
475 break;
476
477 case TARGET_SYS_ISERROR:
478 GET_ARG(0);
479 common_semi_set_ret(cs, (target_long)arg0 < 0);
480 break;
481
482 case TARGET_SYS_ISTTY:
483 GET_ARG(0);
484 semihost_sys_isatty(cs, common_semi_istty_cb, arg0);
485 break;
486
487 case TARGET_SYS_SEEK:
488 GET_ARG(0);
489 GET_ARG(1);
490 semihost_sys_lseek(cs, common_semi_seek_cb, arg0, arg1, GDB_SEEK_SET);
491 break;
492
493 case TARGET_SYS_FLEN:
494 GET_ARG(0);
495 semihost_sys_flen(cs, common_semi_flen_fstat_cb, common_semi_cb,
496 arg0, common_semi_flen_buf(cs));
497 break;
498
499 case TARGET_SYS_TMPNAM:
500 {
501 int len;
502 char *p;
503
504 GET_ARG(0);
505 GET_ARG(1);
506 GET_ARG(2);
507 len = asprintf(&s, "%s/qemu-%x%02x", g_get_tmp_dir(),
508 getpid(), (int)arg1 & 0xff);
509 if (len < 0) {
510 common_semi_set_ret(cs, -1);
511 break;
512 }
513
514
515 len++;
516
517 if (len > arg2) {
518 free(s);
519 common_semi_set_ret(cs, -1);
520 break;
521 }
522 p = lock_user(VERIFY_WRITE, arg0, len, 0);
523 if (!p) {
524 free(s);
525 goto do_fault;
526 }
527 memcpy(p, s, len);
528 unlock_user(p, arg0, len);
529 free(s);
530 common_semi_set_ret(cs, 0);
531 break;
532 }
533
534 case TARGET_SYS_REMOVE:
535 GET_ARG(0);
536 GET_ARG(1);
537 semihost_sys_remove(cs, common_semi_cb, arg0, arg1 + 1);
538 break;
539
540 case TARGET_SYS_RENAME:
541 GET_ARG(0);
542 GET_ARG(1);
543 GET_ARG(2);
544 GET_ARG(3);
545 semihost_sys_rename(cs, common_semi_cb, arg0, arg1 + 1, arg2, arg3 + 1);
546 break;
547
548 case TARGET_SYS_CLOCK:
549 common_semi_set_ret(cs, clock() / (CLOCKS_PER_SEC / 100));
550 break;
551
552 case TARGET_SYS_TIME:
553 ul_ret = time(NULL);
554 common_semi_cb(cs, ul_ret, ul_ret == -1 ? errno : 0);
555 break;
556
557 case TARGET_SYS_SYSTEM:
558 GET_ARG(0);
559 GET_ARG(1);
560 semihost_sys_system(cs, common_semi_cb, arg0, arg1 + 1);
561 break;
562
563 case TARGET_SYS_ERRNO:
564 common_semi_set_ret(cs, get_swi_errno(cs));
565 break;
566
567 case TARGET_SYS_GET_CMDLINE:
568 {
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583 char *output_buffer;
584 size_t input_size;
585 size_t output_size;
586 int status = 0;
587#if !defined(CONFIG_USER_ONLY)
588 const char *cmdline;
589#else
590 TaskState *ts = cs->opaque;
591#endif
592 GET_ARG(0);
593 GET_ARG(1);
594 input_size = arg1;
595
596#if !defined(CONFIG_USER_ONLY)
597 cmdline = semihosting_get_cmdline();
598 if (cmdline == NULL) {
599 cmdline = "";
600 }
601 output_size = strlen(cmdline) + 1;
602#else
603 unsigned int i;
604
605 output_size = ts->info->env_strings - ts->info->arg_strings;
606 if (!output_size) {
607
608
609
610
611 output_size = 1;
612 }
613#endif
614
615 if (output_size > input_size) {
616
617 common_semi_cb(cs, -1, E2BIG);
618 break;
619 }
620
621
622 if (SET_ARG(1, output_size - 1)) {
623
624 goto do_fault;
625 }
626
627
628 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
629 if (!output_buffer) {
630 goto do_fault;
631 }
632
633
634#if !defined(CONFIG_USER_ONLY)
635 pstrcpy(output_buffer, output_size, cmdline);
636#else
637 if (output_size == 1) {
638
639 output_buffer[0] = '\0';
640 goto out;
641 }
642
643 if (copy_from_user(output_buffer, ts->info->arg_strings,
644 output_size)) {
645 unlock_user(output_buffer, arg0, 0);
646 goto do_fault;
647 }
648
649
650 for (i = 0; i < output_size - 1; i++) {
651 if (output_buffer[i] == 0) {
652 output_buffer[i] = ' ';
653 }
654 }
655 out:
656#endif
657
658 unlock_user(output_buffer, arg0, output_size);
659 common_semi_cb(cs, status, 0);
660 }
661 break;
662
663 case TARGET_SYS_HEAPINFO:
664 {
665 target_ulong retvals[4];
666 int i;
667#ifdef CONFIG_USER_ONLY
668 TaskState *ts = cs->opaque;
669 target_ulong limit;
670#else
671 LayoutInfo info = common_semi_find_bases(cs);
672#endif
673
674 GET_ARG(0);
675
676#ifdef CONFIG_USER_ONLY
677
678
679
680
681 if (!ts->heap_limit) {
682 abi_ulong ret;
683
684 ts->heap_base = do_brk(0);
685 limit = ts->heap_base + COMMON_SEMI_HEAP_SIZE;
686
687 for (;;) {
688 ret = do_brk(limit);
689 if (ret >= limit) {
690 break;
691 }
692 limit = (ts->heap_base >> 1) + (limit >> 1);
693 }
694 ts->heap_limit = limit;
695 }
696
697 retvals[0] = ts->heap_base;
698 retvals[1] = ts->heap_limit;
699 retvals[2] = ts->stack_base;
700 retvals[3] = 0;
701#else
702 retvals[0] = info.heapbase;
703 retvals[1] = info.heaplimit;
704 retvals[2] = info.heaplimit;
705 retvals[3] = info.heapbase;
706#endif
707
708 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
709 bool fail;
710
711 if (is_64bit_semihosting(env)) {
712 fail = put_user_u64(retvals[i], arg0 + i * 8);
713 } else {
714 fail = put_user_u32(retvals[i], arg0 + i * 4);
715 }
716
717 if (fail) {
718
719 goto do_fault;
720 }
721 }
722 common_semi_set_ret(cs, 0);
723 }
724 break;
725
726 case TARGET_SYS_EXIT:
727 case TARGET_SYS_EXIT_EXTENDED:
728 if (common_semi_sys_exit_extended(cs, nr)) {
729
730
731
732
733
734
735
736 GET_ARG(0);
737 GET_ARG(1);
738
739 if (arg0 == ADP_Stopped_ApplicationExit) {
740 ret = arg1;
741 } else {
742 ret = 1;
743 }
744 } else {
745
746
747
748
749
750
751 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
752 }
753 gdb_exit(ret);
754 exit(ret);
755
756 case TARGET_SYS_ELAPSED:
757 elapsed = get_clock() - clock_start;
758 if (sizeof(target_ulong) == 8) {
759 if (SET_ARG(0, elapsed)) {
760 goto do_fault;
761 }
762 } else {
763 if (SET_ARG(0, (uint32_t) elapsed) ||
764 SET_ARG(1, (uint32_t) (elapsed >> 32))) {
765 goto do_fault;
766 }
767 }
768 common_semi_set_ret(cs, 0);
769 break;
770
771 case TARGET_SYS_TICKFREQ:
772
773 common_semi_set_ret(cs, 1000000000);
774 break;
775
776 case TARGET_SYS_SYNCCACHE:
777
778
779
780
781
782 if (common_semi_has_synccache(env)) {
783 common_semi_set_ret(cs, 0);
784 break;
785 }
786
787 default:
788 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
789 cpu_dump_state(cs, stderr, 0);
790 abort();
791
792 do_fault:
793 common_semi_cb(cs, -1, EFAULT);
794 break;
795 }
796}
797