target-mips: add MDI semihosting
cover target-mips: add MDI semihosting
This patch series adds semihosting for bare-metal 32-bit MIPS targets that follows the MDI semihosting model used in MIPS's development product.
The semihosting protocol works as follows:
- All semihosted calls go through _mdi_syscall, which in the absence of simulator support, returns ENOSYS;
- The address of _mdi_syscall is stored in a special section, .sdeosabi;
- When an MDI semihosting-aware simulator is used, a breakpoint is placed at _mdi_syscall;
- When this breakpoint is hit, the simulator performs the requested action and returns control to the program.
Discovering the address of _mdi_syscall is straightforward when the program is passed in via -kernel. We extended the ELF loader interface slightly so that the caller could inspect the sections of the binary and act on the contents of the sections if they so chose. Things are slightly trickier when using QEMU as a GDB remote target, as the program will not be provided via -kernel, but via `load' from within GDB. This second method requires that we implement qSymbol support in the GDB stub so we can directly ask GDB where _mdi_syscall is.
The patches have been built for {mips,mips64}-softmmu targets and tested with the libstdc++ testsuite (which exercises things like read(), write(), and lseek()).
-Nathan
PATCH 1/6 sysemu: add section_callback argument to ELF loader
Some targets indicate properties of the program with special sections in the ELF file--MIPS in particular. This infrastructure is useful for grovelling through those sections.
--- elf_ops.h | 34 +++++++++++++++++++++++++++++++--- loader.c | 19 ++++++++++++++----- sysemu.h | 5 +++++ 3 files changed, 50 insertions(+), 8 deletions(-)
elf_ops.h +31 -3
@@ -98,7 +98,13 @@ static int glue(symcmp, SZ)(const void *s0, const void *s1) : ((sym0->st_value > sym1->st_value) ? 1 : 0); } -static int glue (load_symbols ,SZ )(struct elfhdr *ehdr,int fd,int must_swab) +/* Load function symbols for later groveling. If SECTION_CALLBACK is +non-NULL, it will be called with information about each section in +the binary. This interface enables to the caller to mine the binary +for useful information about the ABI, such as whether the CPU chosen is +compatible with the binary. */ +static int glue (load_symbols ,SZ )(struct elfhdr *ehdr,int fd,int must_swab, +section_callback_t section_callback) {struct elf_shdr *symtab, *strtab, *shdr_table =NULL ;struct elf_sym *syms =NULL ;
@@ -117,6 +123,27 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab) } } +/* Permit machines to grovel through the ELF file looking for +interesting bits of information. */ +if (section_callback && ehdr->e_shstrndx !=SHN_UNDEF ) { +char *shstr =NULL ; +struct elf_shdr *shstrtab = &shdr_table[ehdr->e_shstrndx]; + + shstr =load_at (fd, shstrtab->sh_offset, shstrtab->sh_size); +if (!shstr) + goto fail_callback; + +for (i =0 ; i < ehdr->e_shnum; i++) { +struct elf_shdr *sh = shdr_table + i; + +section_callback (fd, must_swab, sh->sh_size, sh->sh_offset, + &shstr[sh->sh_name]); + } + + fail_callback: +free (shstr); + } + symtab =glue (find_section,SZ )(shdr_table, ehdr->e_shnum,SHT_SYMTAB );if (!symtab) goto fail;
@@ -179,7 +206,8 @@ static int glue(load_symbols, SZ)(struct elfhdr *ehdr, int fd, int must_swab)static int glue (load_elf ,SZ )(int fd,int64_t address_offset,int must_swab,uint64_t *pentry, -uint64_t *lowaddr,uint64_t *highaddr) +uint64_t *lowaddr,uint64_t *highaddr, +section_callback_t section_callback) {struct elfhdr ehdr;struct elf_phdr *phdr =NULL , *ph;
@@ -213,7 +241,7 @@ static int glue(load_elf, SZ)(int fd, int64_t address_offset,if (pentry) *pentry = (uint64_t )(elf_sword )ehdr.e_entry; -glue (load_symbols,SZ )(&ehdr, fd, must_swab); +glue (load_symbols,SZ )(&ehdr, fd, must_swab, section_callback); size = ehdr.e_phnum *sizeof (phdr[0 ]);lseek (fd, ehdr.e_phoff,SEEK_SET );
loader.c +14 -5
@@ -302,9 +302,10 @@ static void *load_at(int fd, int offset, int size)#define SZ 64#include "elf_ops.h" -/* return < 0 if error, otherwise the number of bytes loaded in memory */ -int load_elf (const char *filename,int64_t address_offset, -uint64_t *pentry,uint64_t *lowaddr,uint64_t *highaddr) +int load_elf_introspect (const char *filename,int64_t address_offset, +uint64_t *pentry,uint64_t *lowaddr, +uint64_t *highaddr, +section_callback_t section_callback) {int fd, data_order, host_data_order, must_swab, ret;uint8_t e_ident[EI_NIDENT ];
@@ -339,10 +340,10 @@ int load_elf(const char *filename, int64_t address_offset,lseek (fd,0 ,SEEK_SET );if (e_ident[EI_CLASS ] ==ELFCLASS64 ) { ret =load_elf64 (fd, address_offset, must_swab, pentry, - lowaddr, highaddr); + lowaddr, highaddr, section_callback); }else { ret =load_elf32 (fd, address_offset, must_swab, pentry, - lowaddr, highaddr); + lowaddr, highaddr, section_callback); }close (fd);
@@ -353,6 +354,14 @@ int load_elf(const char *filename, int64_t address_offset,return -1 ; } +/* return < 0 if error, otherwise the number of bytes loaded in memory */ +int load_elf (const char *filename,int64_t address_offset, +uint64_t *pentry,uint64_t *lowaddr,uint64_t *highaddr) +{ +return load_elf_introspect (filename, address_offset, pentry, lowaddr, + highaddr,NULL ); +} +static void bswap_uboot_header (uboot_image_header_t *hdr) {#ifndef WORDS_BIGENDIAN
sysemu.h +5 -0
@@ -245,6 +245,11 @@ int load_image(const char *filename, uint8_t *addr); /* deprecated */int load_image_targphys (const char *filename,target_phys_addr_t ,int max_sz);int load_elf (const char *filename,int64_t address_offset,uint64_t *pentry,uint64_t *lowaddr,uint64_t *highaddr); +typedef void (*section_callback_t )(int fd,int must_swab, +uint64_t size,uint64_t offset,char *name); +int load_elf_introspect (const char *filename,int64_t address_offset, +uint64_t *pentry,uint64_t *lowaddr, +uint64_t *highaddr,section_callback_t callback);int load_aout (const char *filename,target_phys_addr_t addr,int max_sz);int load_uimage (const char *filename,target_ulong *ep,target_ulong *loadaddr,int *is_linux);
Signature · 1 line
1.6.3.2
PATCH 2/6 add softmmu_target_strlen
In reply to Nathan Froyd--- softmmu-semi.h | 16 ++++++++++++++++ 1 files changed, 16 insertions(+), 0 deletions(-)
softmmu-semi.h +16 -0
@@ -60,6 +60,22 @@ static char *softmmu_lock_user_string(CPUState *env, uint32_t addr)return s; }#define lock_user_string (p) softmmu_lock_user_string(env, p) + +static int softmmu_target_strlen (CPUState *env,target_ulong addr) +{ +uint8_t c; +int len; + + len =0 ; +do { +cpu_memory_rw_debug (env, addr + len, &c,1 ,0 ); + len++; + }while (c); + +return len -1 ; +} +#define target_strlen (p) softmmu_target_strlen(env, p) +static void softmmu_unlock_user (CPUState *env,void *p,target_ulong addr,target_ulong len) {
Signature · 1 line
1.6.3.2
PATCH 3/6 add implementation of MIPS semihosting
In reply to Nathan FroydLater patches will call into this file via do_mips_semihosting.
--- Makefile.target | 1 + mips-semi.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++++++++ target-mips/cpu.h | 1 + 3 files changed, 219 insertions(+), 0 deletions(-) create mode 100644 mips-semi.c
Makefile.target +1 -0
@@ -534,6 +534,7 @@ obj-mips-y += piix_pci.o parallel.o cirrus_vga.o pcspk.o $(sound-obj-y) obj-mips-y += mipsnet.o obj-mips-y += pflash_cfi01.o obj-mips-y += vmware_vga.o +obj-mips-y += mips-semi.o ifeq ($(TARGET_BASE_ARCH), mips) CPPFLAGS += -DHAS_AUDIO -DHAS_AUDIO_CHOICE
mips-semi.c new file +217 -0
217 lines not on this page · raw message
target-mips/cpu.h +1 -0
@@ -587,6 +587,7 @@ int cpu_mips_handle_mmu_fault (CPUState *env, target_ulong address, int rw,void do_interrupt (CPUState *env);void r4k_invalidate_tlb (CPUState *env,int idx,int use_extra); +void do_mips_semihosting (CPUState *env);static inline void cpu_pc_from_tb (CPUState *env,TranslationBlock *tb) { env->active_tc.PC = tb->pc;
Signature · 1 line
1.6.3.2
Re: [Qemu-devel] [PATCH 3/6] add implementation of MIPS semihosting
Quoted 6 lines · Nathan Froyd
Later patches will call into this file via
do_mips_semihosting.+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+ * MA 02110-1301, USA.
Please use the latest version: "if not, see <http://www.gnu.org/licenses/>."
Re: [Qemu-devel] [PATCH 3/6] add implementation of MIPS semihosting
Later patches will call into this file via do_mips_semihosting.
--- Makefile.target | 1 + mips-semi.c | 216 +++++++++++++++++++++++++++++++++++++++++++++++++++++ target-mips/cpu.h | 1 + 3 files changed, 218 insertions(+), 0 deletions(-) create mode 100644 mips-semi.c
v2: point at FSF website for copies of GPL
Makefile.target +1 -0
@@ -534,6 +534,7 @@ obj-mips-y += piix_pci.o parallel.o cirrus_vga.o pcspk.o $(sound-obj-y) obj-mips-y += mipsnet.o obj-mips-y += pflash_cfi01.o obj-mips-y += vmware_vga.o +obj-mips-y += mips-semi.o ifeq ($(TARGET_BASE_ARCH), mips) CPPFLAGS += -DHAS_AUDIO -DHAS_AUDIO_CHOICE
mips-semi.c new file +216 -0
216 lines not on this page · raw message
target-mips/cpu.h +1 -0
@@ -587,6 +587,7 @@ int cpu_mips_handle_mmu_fault (CPUState *env, target_ulong address, int rw,void do_interrupt (CPUState *env);void r4k_invalidate_tlb (CPUState *env,int idx,int use_extra); +void do_mips_semihosting (CPUState *env);static inline void cpu_pc_from_tb (CPUState *env,TranslationBlock *tb) { env->active_tc.PC = tb->pc;
Signature · 1 line
1.6.3.2
PATCH 4/6 target-mips: add MDI semihosting support to mipssim machine
In reply to Nathan FroydWe need to grovel through the .sdeosabi section to find out where _mdi_syscall is located. Once we find it, we can set a breakpoint there.
We use BP_CPU breakpoints to implement the semihosting breakpoint. Writing BREAK instructions means that users could unintentionally remove breakpoints, either by means of buggy programs, or mistyped commands. Using BP_CPU breakpoints means that the user cannot access them and therefore that they will not be removed (although BP_CPU breakpoints can be overridden by BP_GDB breakpoints; we assume that the user knows what he or she is doing if a breakpoint is set at _mdi_syscall).
The shuffling of #includes in helper.c is to deal with peculiarities of dyngen-exec.h.
--- hw/mips_mipssim.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- target-mips/cpu.h | 2 + target-mips/helper.c | 50 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 103 insertions(+), 7 deletions(-)
hw/mips_mipssim.c +55 -3
@@ -48,6 +48,50 @@ static struct _loaderparams {const char *initrd_filename; } loaderparams; +static uint32_t mdi_semihost_bkpt; + +static void find_sdeosabi_section (int fd,int must_swab, +uint64_t size,uint64_t offset, +char *name) +{ +/* We don't support semihosting for 64-bit targets */ +#ifndef TARGET_MIPS64 +if (semihosting_enabled && + size >=8 && +strcmp (name,".sdeosabi" ) ==0 ) { +uint64_t section_offset =0 ; + +if (lseek (fd, offset,SEEK_SET ) <0 ) +return ; + +while (section_offset < size) { +/* .sdeosabi is organized into pairs of 4-byte words. The +first word in each pair is a numeric tag; the second word +is interpreted according to the tag. For our purposes, +we're looking for tag 2. The second word will be the +address of the _mdi_syscall function. */ +uint32_t bkpt_info[2 ]; + +if (read (fd, bkpt_info,sizeof (bkpt_info)) ==sizeof (bkpt_info)) { +if (must_swab) { +bswap32s (&bkpt_info[0 ]); +bswap32s (&bkpt_info[1 ]); + } + +if (bkpt_info[0 ] ==2 && bkpt_info[1 ]) { + mdi_semihost_bkpt = bkpt_info[1 ]; +break ; + } + }else { +break ; + } + + section_offset +=sizeof (bkpt_info); + } + } +#endif +} +static void load_kernel (CPUState *env) {int64_t entry, kernel_low, kernel_high;
@@ -55,9 +99,12 @@ static void load_kernel (CPUState *env)long initrd_size;ram_addr_t initrd_offset; - kernel_size =load_elf (loaderparams.kernel_filename,VIRT_TO_PHYS_ADDEND , - (uint64_t *)&entry, (uint64_t *)&kernel_low, - (uint64_t *)&kernel_high); + kernel_size =load_elf_introspect (loaderparams.kernel_filename, +VIRT_TO_PHYS_ADDEND , + (uint64_t *)&entry, + (uint64_t *)&kernel_low, + (uint64_t *)&kernel_high, + find_sdeosabi_section);if (kernel_size >=0 ) {if ((entry & ~0x7fffffffULL ) ==0x80000000 ) entry = (int32_t )entry;
@@ -68,6 +115,11 @@ static void load_kernel (CPUState *env)exit (1 ); } +/* set up semihosting */ +if (semihosting_enabled && mdi_semihost_bkpt) { +install_semihosting_breakpoint (env, mdi_semihost_bkpt); + } +/* load initrd */ initrd_size =0 ; initrd_offset =0 ;
target-mips/cpu.h +2 -0
@@ -588,6 +588,8 @@ void do_interrupt (CPUState *env);void r4k_invalidate_tlb (CPUState *env,int idx,int use_extra);void do_mips_semihosting (CPUState *env); +void install_semihosting_breakpoint (CPUState *env,uint32_t bkpt_address); +static inline void cpu_pc_from_tb (CPUState *env,TranslationBlock *tb) { env->active_tc.PC = tb->pc;
target-mips/helper.c +46 -4
65 lines not on this page · raw message
Signature · 1 line
1.6.3.2
PATCH 5/6 enable --semihosting option for TARGET_MIPS
In reply to Nathan Froyd--- qemu-options.hx | 2 +- vl.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
qemu-options.hx +1 -1
@@ -1653,7 +1653,7 @@ DEF("prom-env", HAS_ARG, QEMU_OPTION_prom_env, "-prom-env variable=value\n" " set OpenBIOS nvram variables\n") #endif -#if defined(TARGET_ARM) || defined(TARGET_M68K) +#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_MIPS) DEF("semihosting", 0, QEMU_OPTION_semihosting, "-semihosting semihosting mode\n") #endif
vl.c +1 -1
@@ -5511,7 +5511,7 @@ int main(int argc, char **argv, char **envp) option_rom[nb_option_roms] = optarg; nb_option_roms++;break ; -#if defined(TARGET_ARM ) || defined(TARGET_M68K ) +#if defined(TARGET_ARM ) || defined(TARGET_M68K ) || defined(TARGET_MIPS )case QEMU_OPTION_semihosting: semihosting_enabled =1 ;break ;
Signature · 1 line
1.6.3.2
PATCH 6/6 gdbstub: add qSymbol handling for TARGET_MIPS
In reply to Nathan FroydQEMU needs to know the address of _mdi_syscall so that breakpoints can be set appropriately. But if QEMU is started from within GDB as:
(gdb) target remote | qemu -M mipssim -s -S ... -kernel /dev/null
...
(gdb) load
then QEMU's ELF loader never gets a chance to grovel through the ELF file to look for the .sdeosabi section.
Therefore, the GDB stub needs to know how to ask GDB for the address of _mdi_syscall so that the necessary breakpoint can be set.
--- gdbstub.c | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-)
gdbstub.c +29 -0
@@ -1937,6 +1937,35 @@ static int gdb_handle_packet(GDBState *s, const char *line_buf)put_packet (s, buf); }break ; + }else if (strncmp (p,"Symbol:" ,7 ) ==0 ) { +#if defined(TARGET_MIPS ) && !defined(TARGET_MIPS64 ) && !defined(CONFIG_USER_ONLY ) +#define MDI_SYSCALL_SYMBOL "_mdi_syscall" +if (strncmp (p+7 ,":" ,1 ) ==0 ) { +/* GDB is telling us we can ask for symbols. Look for +_mdi_syscall. */ +memtohex ((char *)mem_buf, (const uint8_t *)MDI_SYSCALL_SYMBOL , +strlen (MDI_SYSCALL_SYMBOL )); + mem_buf[strlen (MDI_SYSCALL_SYMBOL )*2 ] =0 ; +snprintf (buf,sizeof (buf),"qSymbol:%s" , mem_buf); +put_packet (s, buf); +break ; + }else { +/* A response from a previous query. */ +if (*(p+7 ) !=':' ) { + addr =strtoull (p+7 , (char **)&p,16 ); +hextomem (mem_buf, p+1 ,strlen (MDI_SYSCALL_SYMBOL )*2 ); + +if (memcmp (mem_buf,MDI_SYSCALL_SYMBOL , +strlen (MDI_SYSCALL_SYMBOL )) ==0 ) { +install_semihosting_breakpoint (s->c_cpu, addr); + } + } + } +/* All done, regardless of whether we got the right symbol. */ +put_packet (s,"OK" ); +break ; +#undef MDI_SYSCALL_SYMBOL +#endif } #ifdef CONFIG_USER_ONLY else if (strncmp(p, "Offsets", 7) == 0) {
Signature · 1 line
1.6.3.2