qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* Implement standard file operation with QEMU
@ 2020-07-16  0:51 casmac
  2020-07-16  7:57 ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 4+ messages in thread
From: casmac @ 2020-07-16  0:51 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 2903 bytes --]

Hi all,
  I am trying to implment standard file operation (stdio) with QEMU for DSP architecture. The manufacture (TI) provides a runtime library that support posix standard IO, but it left the device level implmentation as hook function calls, like  in the library source , it contains  add_device() function, and write(),read(),open() are not implemented:



int add_device(char      *name,
               unsigned   flags,
               int      (*dopen)  (const char *path, unsigned flags, int foo),
               int      (*dclose) (int fno),
               int      (*dread)  (int fno, char *buf, unsigned count),
               int      (*dwrite) (int fno, const char *buf, unsigned count),
               fpos_t     (*dlseek) (int fno, fpos_t offset, int origin),
               int      (*dunlink)(const char *path),
               int      (*drename)(const char *old_name, const char *new_name))
{
   _DEVICE *dt;

   strncpy(dt->name,name,8);
   dt->name[8] = '\0';
   dt->flags   = flags;
   dt->OPEN    = dopen;
   dt->CLOSE   = dclose;
   dt->READ    = dread;
   dt->WRITE   = dwrite;
   dt->LSEEK   = dlseek;
   dt->UNLINK  = dunlink;
   dt->RENAME  = drename;
}


int write(int           fildes,
          const char   *bufptr,
          unsigned      cnt)
{
   /*------------------------------------------------------------------------*/
   /* CALL FUNCTION FROM DEVICE TABLE TO PERFORM WRITE FOR THIS DEVICE/FILE  */
   /*------------------------------------------------------------------------*/
   return (*(_stream[fildes]->WRITE)) (fildes,bufptr,cnt);
}



   Then, how can we use this runtime library together with QEMu to implement full-stack file oerations?  I  really appreaciate any advice.


  Thanks.
regards,
xiaolei

[-- Attachment #2: Type: text/html, Size: 3143 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Implement standard file operation with QEMU
  2020-07-16  0:51 Implement standard file operation with QEMU casmac
@ 2020-07-16  7:57 ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2020-07-16  7:57 UTC (permalink / raw)
  To: casmac, qemu-devel, Alex Bennée, Laurent Vivier,
	Marc-André Lureau

Hi Xiaolei,

+Laurent (user-mode)
+Alex (semihosting)
+Marc-André (chardev)

On 7/16/20 2:51 AM, casmac wrote:
> Hi all,
>   I am trying to implment standard file operation (stdio) with QEMU for
> DSP architecture. The manufacture (TI) provides a runtime library that
> support posix standard IO, but it left the device level implmentation as
> hook function calls, like in the library source , it contains 
> add_device() function, and write(),read(),open() are not implemented:
> 
> int add_device(char      *name,
>                unsigned   flags,
>                int      (*dopen)  (const char *path, unsigned flags, int
> foo),
>                int      (*dclose) (int fno),
>                int      (*dread)  (int fno, char *buf, unsigned count),
>                int      (*dwrite) (int fno, const char *buf, unsigned
> count),
>                fpos_t     (*dlseek) (int fno, fpos_t offset, int origin),
>                int      (*dunlink)(const char *path),
>                int      (*drename)(const char *old_name, const char
> *new_name))
> {
>    _DEVICE *dt;
> 
>    strncpy(dt->name,name,8);
>    dt->name[8] = '\0';
>    dt->flags   = flags;
>    dt->OPEN    = dopen;
>    dt->CLOSE   = dclose;
>    dt->READ    = dread;
>    dt->WRITE   = dwrite;
>    dt->LSEEK   = dlseek;
>    dt->UNLINK  = dunlink;
>    dt->RENAME  = drename;
> }
> 
> int write(int           fildes,
>           const char   *bufptr,
>           unsigned      cnt)
> {
>   
> /*------------------------------------------------------------------------*/
>    /* CALL FUNCTION FROM DEVICE TABLE TO PERFORM WRITE FOR THIS
> DEVICE/FILE  */
>   
> /*------------------------------------------------------------------------*/
>    return (*(_stream[fildes]->WRITE)) (fildes,bufptr,cnt);
> }
> 
>    Then, how can we use this runtime library together with QEMu to
> implement full-stack file oerations?  I really appreaciate any advice.

Trying to understand...

Are you trying to ask "how to implement semihosting for my
qemu-user-tidsp fork"?

Have a look at "hw/semihosting/console.h" and the implementation
(so far only ARM) of qemu_semihosting_console_[in/out].
This might help to plug read/write. Using other stream than
stdin/stdout is not supported (but you can add support) so
open/lseek/close/rename/unlink are not considered.

(for QEMU 'console' is the stdin/stdout subset of stdio).

You can redirect semihosted files with any host chardev,
this is done in qemu_semihosting_connect_chardevs().

You might also have a look at the functions declared in
"hw/semihosting/semihost.h" and how the different TCG helpers
use them.

Regards,

Phil.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Implement standard file operation with QEMU
@ 2020-07-17 15:18 casmac
  2020-07-17 15:43 ` Peter Maydell
  0 siblings, 1 reply; 4+ messages in thread
From: casmac @ 2020-07-17 15:18 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Laurent Vivier, Marc-André Lureau, Alex Bennée,
	qemu-devel

[-- Attachment #1: Type: text/plain, Size: 5221 bytes --]

Hello Phil,
   What I want to realize is to be able to call standard file operations (open, read, write etc) in the application program, and execute such programs in QEMU. But I am building under system mode.
   TI provide copilation toolchain and a library that provide partial functionality from libc. I am hoping to use TI's toolkit to generate object code which contains calls to hook functions, and then use QEMU's host I/O implementation to realize low-level file operation. For example:

&nbsp; &nbsp; _stream[fildes]-&gt;WRITE&nbsp; <---hook to ---&gt;&nbsp;qemu_semihosting_console_outs
&nbsp; &nbsp;Are you suggesting that this is done by TCG helpers?


best regards,
xiaolei
&nbsp;&nbsp; 





------------------ Original ------------------
From:                                                                                                                        "Philippe Mathieu-Daudé"                                                                                    <f4bug@amsat.org&gt;;
Date:&nbsp;Thu, Jul 16, 2020 03:57 PM
To:&nbsp;"casmac"<climber.cui@qq.com&gt;;"qemu-devel"<qemu-devel@nongnu.org&gt;;"Alex Bennée"<alex.bennee@linaro.org&gt;;"Laurent Vivier"<lvivier@redhat.com&gt;;"Marc-André Lureau"<marcandre.lureau@redhat.com&gt;;

Subject:&nbsp;Re: Implement standard file operation with QEMU



Hi Xiaolei,

+Laurent (user-mode)
+Alex (semihosting)
+Marc-André (chardev)

On 7/16/20 2:51 AM, casmac wrote:
&gt; Hi all,
&gt; &nbsp; I am trying to implment standard file operation (stdio) with QEMU for
&gt; DSP architecture. The manufacture (TI) provides a runtime library that
&gt; support posix standard IO, but it left the device level implmentation as
&gt; hook function calls, like in the library source , it contains 
&gt; add_device() function, and write(),read(),open() are not implemented:
&gt; 
&gt; int add_device(char&nbsp; &nbsp; &nbsp; *name,
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; unsigned &nbsp; flags,
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; int&nbsp; &nbsp; &nbsp; (*dopen)&nbsp; (const char *path, unsigned flags, int
&gt; foo),
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; int&nbsp; &nbsp; &nbsp; (*dclose) (int fno),
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; int&nbsp; &nbsp; &nbsp; (*dread)&nbsp; (int fno, char *buf, unsigned count),
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; int&nbsp; &nbsp; &nbsp; (*dwrite) (int fno, const char *buf, unsigned
&gt; count),
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; fpos_t &nbsp; &nbsp; (*dlseek) (int fno, fpos_t offset, int origin),
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; int&nbsp; &nbsp; &nbsp; (*dunlink)(const char *path),
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; int&nbsp; &nbsp; &nbsp; (*drename)(const char *old_name, const char
&gt; *new_name))
&gt; {
&gt; &nbsp;&nbsp; _DEVICE *dt;
&gt; 
&gt; &nbsp;&nbsp; strncpy(dt-&gt;name,name,8);
&gt; &nbsp;&nbsp; dt-&gt;name[8] = '\0';
&gt; &nbsp;&nbsp; dt-&gt;flags &nbsp; = flags;
&gt; &nbsp;&nbsp; dt-&gt;OPEN&nbsp; &nbsp; = dopen;
&gt; &nbsp;&nbsp; dt-&gt;CLOSE &nbsp; = dclose;
&gt; &nbsp;&nbsp; dt-&gt;READ&nbsp; &nbsp; = dread;
&gt; &nbsp;&nbsp; dt-&gt;WRITE &nbsp; = dwrite;
&gt; &nbsp;&nbsp; dt-&gt;LSEEK &nbsp; = dlseek;
&gt; &nbsp;&nbsp; dt-&gt;UNLINK&nbsp; = dunlink;
&gt; &nbsp;&nbsp; dt-&gt;RENAME&nbsp; = drename;
&gt; }
&gt; 
&gt; int write(int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; fildes,
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; const char &nbsp; *bufptr,
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; unsigned&nbsp; &nbsp; &nbsp; cnt)
&gt; {
&gt; &nbsp; 
&gt; /*------------------------------------------------------------------------*/
&gt; &nbsp;&nbsp; /* CALL FUNCTION FROM DEVICE TABLE TO PERFORM WRITE FOR THIS
&gt; DEVICE/FILE&nbsp; */
&gt; &nbsp; 
&gt; /*------------------------------------------------------------------------*/
&gt; &nbsp;&nbsp; return (*(_stream[fildes]-&gt;WRITE)) (fildes,bufptr,cnt);
&gt; }
&gt; 
&gt; &nbsp;&nbsp; Then, how can we use this runtime library together with QEMu to
&gt; implement full-stack file oerations?&nbsp; I really appreaciate any advice.

Trying to understand...

Are you trying to ask "how to implement semihosting for my
qemu-user-tidsp fork"?

Have a look at "hw/semihosting/console.h" and the implementation
(so far only ARM) of qemu_semihosting_console_[in/out].
This might help to plug read/write. Using other stream than
stdin/stdout is not supported (but you can add support) so
open/lseek/close/rename/unlink are not considered.

(for QEMU 'console' is the stdin/stdout subset of stdio).

You can redirect semihosted files with any host chardev,
this is done in qemu_semihosting_connect_chardevs().

You might also have a look at the functions declared in
"hw/semihosting/semihost.h" and how the different TCG helpers
use them.

Regards,

Phil.

[-- Attachment #2: Type: text/html, Size: 5827 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Implement standard file operation with QEMU
  2020-07-17 15:18 casmac
@ 2020-07-17 15:43 ` Peter Maydell
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2020-07-17 15:43 UTC (permalink / raw)
  To: casmac
  Cc: Laurent Vivier, Marc-André Lureau, Alex Bennée,
	Philippe Mathieu-Daudé, qemu-devel

On Fri, 17 Jul 2020 at 16:28, casmac <climber.cui@qq.com> wrote:
>    What I want to realize is to be able to call standard file operations (open, read, write etc) in the application program, and execute such programs in QEMU. But I am building under system mode.
>    TI provide copilation toolchain and a library that provide partial functionality from libc. I am hoping to use TI's toolkit to generate object code which contains calls to hook functions, and then use QEMU's host I/O implementation to realize low-level file operation. For example:
>     _stream[fildes]->WRITE  <---hook to ---> qemu_semihosting_console_outs

If the QEMU guest architecture you're using supports
semihosting (eg arm, mips, lm32, m68k, niso2, xtensa), then
you can use it to implement that kind of guest-libc
functionality. What you need to do is entirely in the guest
code: the implementation of the hook function for write
would need to invoke the correct semihosting call.
(For instance on Arm this is "put the arguments into the
correct guest registers/in-memory structures, then invoke
the right SVC instruction".)

If the guest architecture you're using does not have a
semihosting ABI, then you would need to define one, which
is a moderate amount of work and also requires some agreement
about what that ABI definition should look like (eg for
risc-v we asked the risc-v folks to write up a basic
spec document so that everybody implementing semihosting
was working to the same set of requirements).

The other usual way to implement the low-level hook
functions would be for them to talk to emulated devices:
eg a write-to-console function could be implemented to send
the string to a UART device.

thanks
-- PMM


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-07-17 15:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-16  0:51 Implement standard file operation with QEMU casmac
2020-07-16  7:57 ` Philippe Mathieu-Daudé
  -- strict thread matches above, loose matches on Subject: below --
2020-07-17 15:18 casmac
2020-07-17 15:43 ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).