mini-os: remove device specific struct file members
Subsystems (2)
v2 cover mini-os: remove device specific struct file members
This small series is a followup to the series sent recently:
https://lists.xen.org/archives/html/xen-devel/2022-01/threads.html#00347
It contains the last cleanups related to struct file and can only be applied after the Xen libraries have stopped using the related union members:
https://lists.xen.org/archives/html/xen-devel/2022-01/threads.html#00355
The three series applied have been tested to work with:
- xenstorepvh-stubdom
- pv-grub
- ioemu-stubdom
Changes in V2:
- add many patches for using alloc_file_type() and struct file_ops
Juergen Gross (12):
mini-os: remove event channel specific struct file definitions
mini-os: remove gnttab specific member from struct file
mini-os: use alloc_file_type() and get_file_from_fd() in xs
mini-os: use alloc_file_type() and get_file_from_fd() in tpm_tis
mini-os: use alloc_file_type() and get_file_from_fd() in tpmfront
mini-os: use alloc_file_type() and get_file_from_fd() in blkfront
mini-os: use get_file_from_fd() in netfront
mini-os: use alloc_file_type() and get_file_from_fd() in fbfront
mini-os: use file_ops and get_file_from_fd() for console
mini-os: add struct file_ops for file type socket
mini-os: add struct file_ops for FTYPE_FILE
mini-os: make files array private to sys.c
Config.mk | 2 - arch/x86/testbuild/all-no | 2 - arch/x86/testbuild/all-yes | 2 - arch/x86/testbuild/newxen-yes | 2 - blkfront.c | 92 ++++++-- console/xenbus.c | 125 ++++++++++ console/xencons_ring.c | 6 +- fbfront.c | 125 ++++++++-- gntmap.c | 2 +- include/blkfront.h | 5 - include/console.h | 5 + include/lib.h | 27 +-- include/tpm_tis.h | 6 - include/tpmfront.h | 5 - lib/sys.c | 429 ++++++++-------------------------- lib/xs.c | 64 +++-- netfront.c | 64 ++++- tpm_tis.c | 120 ++++++---- tpmfront.c | 100 +++++--- xenbus/xenbus.c | 1 + 20 files changed, 664 insertions(+), 520 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/12 mini-os: remove event channel specific struct file definitions
The event channel specific union member in struct file is no longer needed, so remove it together with the associated structure definitions.
The event channel file type and its associated handling can be removed, too, as libxenevtchn is now supplying a struct file_ops via a call of alloc_file_type().
This removes all contents of CONFIG_LIBXENEVTCHN guarded sections, so this config option can be removed.
--- Config.mk | 1 - arch/x86/testbuild/all-no | 1 - arch/x86/testbuild/all-yes | 1 - arch/x86/testbuild/newxen-yes | 1 - include/lib.h | 15 +-------------- lib/sys.c | 7 ------- 6 files changed, 1 insertion(+), 25 deletions(-)
Config.mk +0 -1
@@ -197,7 +197,6 @@ CONFIG-n += CONFIG_PARAVIRT endif # Support legacy CONFIG_XC value CONFIG_XC ?= $(libc) -CONFIG-$(CONFIG_XC) += CONFIG_LIBXENEVTCHN CONFIG-$(CONFIG_XC) += CONFIG_LIBXENGNTTAB CONFIG-$(lwip) += CONFIG_LWIP
arch/x86/testbuild/all-no +0 -1
@@ -13,7 +13,6 @@ CONFIG_FBFRONT = n CONFIG_KBDFRONT = n CONFIG_CONSFRONT = n CONFIG_XENBUS = n -CONFIG_LIBXENEVTCHN = n CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n CONFIG_BALLOON = n
arch/x86/testbuild/all-yes +0 -1
@@ -16,6 +16,5 @@ CONFIG_XENBUS = y CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y # The following are special: they need support from outside -CONFIG_LIBXENEVTCHN = n CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n
arch/x86/testbuild/newxen-yes +0 -1
@@ -17,6 +17,5 @@ CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y XEN_INTERFACE_VERSION=__XEN_LATEST_INTERFACE_VERSION__ # The following are special: they need support from outside -CONFIG_LIBXENEVTCHN = n CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n
include/lib.h +1 -14
@@ -169,8 +169,7 @@ void sanity_check(void);#define FTYPE_TPM_TIS 11#define FTYPE_XENBUS 12#define FTYPE_GNTMAP 13 -#define FTYPE_EVTCHN 14 -#define FTYPE_N 15 +#define FTYPE_N 14#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
@@ -199,15 +198,6 @@ off_t lseek_default(int fd, off_t offset, int whence);bool select_yes (int fd);bool select_read_flag (int fd); -LIST_HEAD (evtchn_port_list, evtchn_port_info); - -struct evtchn_port_info { -LIST_ENTRY (evtchn_port_info) list; -evtchn_port_t port; -unsigned long pending; -int bound; -}; -struct file {unsigned int type;bool read;/* maybe available for read */
@@ -215,9 +205,6 @@ struct file {union {int fd;/* Any fd from an upper layer. */ void *dev; -struct { -struct evtchn_port_list ports; - } evtchn;struct gntmap gntmap; }; };
lib/sys.c +0 -7
@@ -505,11 +505,6 @@ int close(int fd) res =lwip_close (files[fd].fd);break ;#endif -#ifdef CONFIG_LIBXENEVTCHN -case FTYPE_EVTCHN : -minios_evtchn_close_fd (fd); -break ; -#endif #ifdef CONFIG_LIBXENGNTTAB case FTYPE_GNTMAP :minios_gnttab_close_fd (fd);
@@ -723,7 +718,6 @@ static const char *file_types[] = { [FTYPE_NONE] = "none", [FTYPE_CONSOLE] = "console", [FTYPE_XENBUS] = "xenbus", - [FTYPE_EVTCHN] = "evtchn", [FTYPE_SOCKET] = "socket", [FTYPE_TAP] = "net", [FTYPE_BLK] = "blk",
@@ -915,7 +909,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceFD_CLR (i, exceptfds);break ;#endif -case FTYPE_EVTCHN :case FTYPE_TAP :case FTYPE_BLK :case FTYPE_KBD :
Signature · 1 line
2.26.2
Re: [PATCH v2 01/12] mini-os: remove event channel specific struct file definitions
Juergen Gross, le mar. 11 janv. 2022 16:12:04 +0100, a ecrit:
Quoted 4 lines
The event channel specific union member in struct file is no longer needed, so remove it together with the associated structure definitions.
Quoted 8 lines
The event channel file type and its associated handling can be removed, too, as libxenevtchn is now supplying a struct
file_opsvia a call ofalloc_file_type().This removes all contents of
CONFIG_LIBXENEVTCHNguarded sections, so this config option can be removed.Signed-off-by Juergen Gross <[email protected]>
Quoted 130 lines
--- Config.mk | 1 - arch/x86/testbuild/all-no | 1 - arch/x86/testbuild/all-yes | 1 - arch/x86/testbuild/newxen-yes | 1 - include/lib.h | 15 +-------------- lib/sys.c | 7 ------- 6 files changed, 1 insertion(+), 25 deletions(-)Config.mk +0
-1@@ -197,7 +197,6 @@ CONFIG-n += CONFIG_PARAVIRT endif # Support legacy CONFIG_XC value CONFIG_XC ?= $(libc) -CONFIG-$(CONFIG_XC) += CONFIG_LIBXENEVTCHN CONFIG-$(CONFIG_XC) += CONFIG_LIBXENGNTTAB CONFIG-$(lwip) += CONFIG_LWIParch/x86/testbuild/all-no +0
-1@@ -13,7 +13,6 @@ CONFIG_FBFRONT = n CONFIG_KBDFRONT = n CONFIG_CONSFRONT = n CONFIG_XENBUS = n -CONFIG_LIBXENEVTCHN = n CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n CONFIG_BALLOON = narch/x86/testbuild/all-yes +0
-1@@ -16,6 +16,5 @@ CONFIG_XENBUS = y CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y # The following are special: they need support from outside -CONFIG_LIBXENEVTCHN = n CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = narch/x86/testbuild/newxen-yes +0
-1@@ -17,6 +17,5 @@ CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y XEN_INTERFACE_VERSION=__XEN_LATEST_INTERFACE_VERSION__ # The following are special: they need support from outside -CONFIG_LIBXENEVTCHN = n CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = ninclude/lib.h +1
-14@@ -169,8 +169,7 @@ void sanity_check(void);#define FTYPE_TPM_TIS 11#define FTYPE_XENBUS 12#define FTYPE_GNTMAP 13 -#define FTYPE_EVTCHN 14 -#define FTYPE_N 15 +#define FTYPE_N 14#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);@@ -199,15 +198,6 @@ off_t lseek_default(int fd, off_t offset, int whence);bool select_yes (int fd);bool select_read_flag (int fd); -LIST_HEAD (evtchn_port_list, evtchn_port_info); - -struct evtchn_port_info { -LIST_ENTRY (evtchn_port_info) list; -evtchn_port_t port; -unsigned long pending; -int bound; -}; -struct file {unsigned int type;bool read;/* maybe available for read */ @@ -215,9 +205,6 @@ struct file {union {int fd;/* Any fd from an upper layer. */ void *dev; -struct { -struct evtchn_port_list ports; - } evtchn;struct gntmap gntmap; }; };lib/sys.c +0
-7@@ -505,11 +505,6 @@ int close(int fd) res =lwip_close (files[fd].fd);break ;#endif -#ifdef CONFIG_LIBXENEVTCHN -case FTYPE_EVTCHN : -minios_evtchn_close_fd (fd); -break ; -#endif #ifdef CONFIG_LIBXENGNTTAB case FTYPE_GNTMAP :minios_gnttab_close_fd (fd);@@ -723,7 +718,6 @@ static const char *file_types[] = { [FTYPE_NONE] = "none", [FTYPE_CONSOLE] = "console", [FTYPE_XENBUS] = "xenbus", - [FTYPE_EVTCHN] = "evtchn", [FTYPE_SOCKET] = "socket", [FTYPE_TAP] = "net", [FTYPE_BLK] = "blk",@@ -915,7 +909,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceFD_CLR (i, exceptfds);break ;#endif -case FTYPE_EVTCHN :case FTYPE_TAP :case FTYPE_BLK :case FTYPE_KBD :Signature · 1 line
2.26.2
Signature · 2 lines
Samuel <h> t: bah c'est tendre le pattern pour se faire matcher, hein
PATCH v2 2/12 mini-os: remove gnttab specific member from struct file
In reply to Juergen GrossThe event channel specific union member in struct file is no longer needed, so remove it.
The gnttab file type and its associated handling can be removed, too, as libxengnttab is now supplying a struct file_ops via a call of alloc_file_type().
This removes all contents of CONFIG_LIBXENGNTTAB guarded sections, so this config option can be removed.
--- Config.mk | 1 - arch/x86/testbuild/all-no | 1 - arch/x86/testbuild/all-yes | 1 - arch/x86/testbuild/newxen-yes | 1 - gntmap.c | 2 +- include/lib.h | 4 +--- lib/sys.c | 5 ----- 7 files changed, 2 insertions(+), 13 deletions(-)
Config.mk +0 -1
@@ -197,7 +197,6 @@ CONFIG-n += CONFIG_PARAVIRT endif # Support legacy CONFIG_XC value CONFIG_XC ?= $(libc) -CONFIG-$(CONFIG_XC) += CONFIG_LIBXENGNTTAB CONFIG-$(lwip) += CONFIG_LWIP
arch/x86/testbuild/all-no +0 -1
@@ -13,7 +13,6 @@ CONFIG_FBFRONT = n CONFIG_KBDFRONT = n CONFIG_CONSFRONT = n CONFIG_XENBUS = n -CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n CONFIG_BALLOON = n CONFIG_USE_XEN_CONSOLE = n
arch/x86/testbuild/all-yes +0 -1
@@ -16,5 +16,4 @@ CONFIG_XENBUS = y CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y # The following are special: they need support from outside -CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n
arch/x86/testbuild/newxen-yes +0 -1
@@ -17,5 +17,4 @@ CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y XEN_INTERFACE_VERSION=__XEN_LATEST_INTERFACE_VERSION__ # The following are special: they need support from outside -CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n
gntmap.c +1 -1
@@ -3,7 +3,7 @@* * Diego Ongaro <[email protected]>, July 2008 * -* Files of type FTYPE_GNTMAP contain a gntmap, which is an array of +* Files of libxengnttab contain a gntmap, which is an array of * (host address, grant handle) pairs. Grant handles come from a hypervisor map * operation and are needed for the corresponding unmap. *
include/lib.h +1 -3
@@ -168,8 +168,7 @@ void sanity_check(void);#define FTYPE_TPMFRONT 10#define FTYPE_TPM_TIS 11#define FTYPE_XENBUS 12 -#define FTYPE_GNTMAP 13 -#define FTYPE_N 14 +#define FTYPE_N 13#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
@@ -205,7 +204,6 @@ struct file {union {int fd;/* Any fd from an upper layer. */ void *dev; -struct gntmap gntmap; }; };
lib/sys.c +0 -5
@@ -505,11 +505,6 @@ int close(int fd) res =lwip_close (files[fd].fd);break ;#endif -#ifdef CONFIG_LIBXENGNTTAB -case FTYPE_GNTMAP : -minios_gnttab_close_fd (fd); -break ; -#endif #ifdef CONFIG_NETFRONT case FTYPE_TAP :shutdown_netfront (files[fd].dev);
Signature · 1 line
2.26.2
Re: [PATCH v2 02/12] mini-os: remove gnttab specific member from struct file
Juergen Gross, le mar. 11 janv. 2022 16:12:05 +0100, a ecrit:
Quoted 11 lines
The event channel specific union member in struct file is no longer needed, so remove it.
The gnttab file type and its associated handling can be removed, too, as libxengnttab is now supplying a struct
file_opsvia a call ofalloc_file_type().This removes all contents of
CONFIG_LIBXENGNTTABguarded sections, so this config option can be removed.Signed-off-by Juergen Gross <[email protected]>
Quoted 108 lines
--- Config.mk | 1 - arch/x86/testbuild/all-no | 1 - arch/x86/testbuild/all-yes | 1 - arch/x86/testbuild/newxen-yes | 1 - gntmap.c | 2 +- include/lib.h | 4 +--- lib/sys.c | 5 ----- 7 files changed, 2 insertions(+), 13 deletions(-)Config.mk +0
-1@@ -197,7 +197,6 @@ CONFIG-n += CONFIG_PARAVIRT endif # Support legacy CONFIG_XC value CONFIG_XC ?= $(libc) -CONFIG-$(CONFIG_XC) += CONFIG_LIBXENGNTTAB CONFIG-$(lwip) += CONFIG_LWIParch/x86/testbuild/all-no +0
-1@@ -13,7 +13,6 @@ CONFIG_FBFRONT = n CONFIG_KBDFRONT = n CONFIG_CONSFRONT = n CONFIG_XENBUS = n -CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = n CONFIG_BALLOON = n CONFIG_USE_XEN_CONSOLE = narch/x86/testbuild/all-yes +0
-1@@ -16,5 +16,4 @@ CONFIG_XENBUS = y CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y # The following are special: they need support from outside -CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = narch/x86/testbuild/newxen-yes +0
-1@@ -17,5 +17,4 @@ CONFIG_BALLOON = y CONFIG_USE_XEN_CONSOLE = y XEN_INTERFACE_VERSION=__XEN_LATEST_INTERFACE_VERSION__ # The following are special: they need support from outside -CONFIG_LIBXENGNTTAB = n CONFIG_LWIP = ngntmap.c +1
-1@@ -3,7 +3,7 @@* * Diego Ongaro <[email protected]>, July 2008 * -* Files of type FTYPE_GNTMAP contain a gntmap, which is an array of +* Files of libxengnttab contain a gntmap, which is an array of * (host address, grant handle) pairs. Grant handles come from a hypervisor map * operation and are needed for the corresponding unmap. * include/lib.h +1
-3@@ -168,8 +168,7 @@ void sanity_check(void);#define FTYPE_TPMFRONT 10#define FTYPE_TPM_TIS 11#define FTYPE_XENBUS 12 -#define FTYPE_GNTMAP 13 -#define FTYPE_N 14 +#define FTYPE_N 13#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);@@ -205,7 +204,6 @@ struct file {union {int fd;/* Any fd from an upper layer. */ void *dev; -struct gntmap gntmap; }; };lib/sys.c +0
-5@@ -505,11 +505,6 @@ int close(int fd) res =lwip_close (files[fd].fd);break ;#endif -#ifdef CONFIG_LIBXENGNTTAB -case FTYPE_GNTMAP : -minios_gnttab_close_fd (fd); -break ; -#endif #ifdef CONFIG_NETFRONT case FTYPE_TAP :shutdown_netfront (files[fd].dev);Signature · 1 line
2.26.2
Signature · 5 lines
Samuel Tu as lu les docs. Tu es devenu un informaticien. Que tu le veuilles ou non. Lire la doc, c'est le Premier et Unique Commandement de l'informaticien. -+- TP in: Guide du Linuxien pervers - "L'évangile selon St Thomas"
Re: [PATCH v2 02/12] mini-os: remove gnttab specific member from struct file
In reply to Juergen GrossQuoted 1 line · Juergen Gross
The event channel specific union member in struct file is no longer
Too much copy&paste from the previous patch?
The actual contents look fine.
~Andrew
Quoted 10 lines
needed, so remove it.
The gnttab file type and its associated handling can be removed, too, as libxengnttab is now supplying a struct
file_opsvia a call ofalloc_file_type().This removes all contents of
CONFIG_LIBXENGNTTABguarded sections, so this config option can be removed.Signed-off-by Juergen Gross <[email protected]>
Re: [PATCH v2 02/12] mini-os: remove gnttab specific member from struct file
Quoted 4 lines · Andrew Cooper
Quoted 1 line · Juergen Gross
The event channel specific union member in struct file is no longer
Too much copy&paste from the previous patch?
Seems so, yes. :-)
Juergen
PATCH v2 3/12 mini-os: use alloc_file_type() and get_file_from_fd() in xs
In reply to Juergen GrossAllocate the needed file type via alloc_file_type().
Instead of directly accessing the files[] array use get_file_from_fd().
--- include/lib.h | 3 +-- lib/sys.c | 18 -------------- lib/xs.c | 64 +++++++++++++++++++++++++++++++++++++------------ xenbus/xenbus.c | 1 + 4 files changed, 51 insertions(+), 35 deletions(-)
include/lib.h +1 -2
@@ -167,8 +167,7 @@ void sanity_check(void);#define FTYPE_BLK 9#define FTYPE_TPMFRONT 10#define FTYPE_TPM_TIS 11 -#define FTYPE_XENBUS 12 -#define FTYPE_N 13 +#define FTYPE_N 12#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
lib/sys.c +0 -18
@@ -495,11 +495,6 @@ int close(int fd)switch (files[fd].type) {default :break ; -#ifdef CONFIG_XENBUS -case FTYPE_XENBUS : -xs_daemon_close ((void *)(intptr_t ) fd); -break ; -#endif #ifdef HAVE_LWIP case FTYPE_SOCKET : res =lwip_close (files[fd].fd);
@@ -712,7 +707,6 @@ int closedir(DIR *dir)static const char *file_types[] = { [FTYPE_NONE ] ="none" , [FTYPE_CONSOLE ] ="console" , - [FTYPE_XENBUS ] ="xenbus" , [FTYPE_SOCKET ] ="socket" , [FTYPE_TAP ] ="net" , [FTYPE_BLK ] ="blk" ,
@@ -892,18 +886,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exce n++;FD_CLR (i, exceptfds);break ; -#ifdef CONFIG_XENBUS -case FTYPE_XENBUS : -if (FD_ISSET (i, readfds)) { -if (files[i].dev) - n++; -else -FD_CLR (i, readfds); - } -FD_CLR (i, writefds); -FD_CLR (i, exceptfds); -break ; -#endif case FTYPE_TAP :case FTYPE_BLK :case FTYPE_KBD :
lib/xs.c +49 -15
90 lines not on this page · raw message
xenbus/xenbus.c +1 -0
@@ -393,6 +393,7 @@ static int allocate_xenbus_id(void)void init_xenbus (void ) {int err; +DEBUG ("init_xenbus called.\n" );create_thread ("xenstore" , xenbus_thread_func,NULL );DEBUG ("buf at %p.\n" , xenstore_buf);
Signature · 1 line
2.26.2
Re: [PATCH v2 03/12] mini-os: use alloc_file_type() and get_file_from_fd() in xs
Juergen Gross, le mar. 11 janv. 2022 16:12:06 +0100, a ecrit:
Quoted 5 lines
Allocate the needed file type via
alloc_file_type().Instead of directly accessing the files[] array use
get_file_from_fd().Signed-off-by Juergen Gross <[email protected]>
Quoted 175 lines
--- include/lib.h | 3 +-- lib/sys.c | 18 -------------- lib/xs.c | 64 +++++++++++++++++++++++++++++++++++++------------ xenbus/xenbus.c | 1 + 4 files changed, 51 insertions(+), 35 deletions(-)include/lib.h +1
-2@@ -167,8 +167,7 @@ void sanity_check(void);#define FTYPE_BLK 9#define FTYPE_TPMFRONT 10#define FTYPE_TPM_TIS 11 -#define FTYPE_XENBUS 12 -#define FTYPE_N 13 +#define FTYPE_N 12#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);lib/sys.c +0
-18@@ -495,11 +495,6 @@ int close(int fd)switch (files[fd].type) {default :break ; -#ifdef CONFIG_XENBUS -case FTYPE_XENBUS : -xs_daemon_close ((void *)(intptr_t ) fd); -break ; -#endif #ifdef HAVE_LWIP case FTYPE_SOCKET : res =lwip_close (files[fd].fd);@@ -712,7 +707,6 @@ int closedir(DIR *dir)static const char *file_types[] = { [FTYPE_NONE ] ="none" , [FTYPE_CONSOLE ] ="console" , - [FTYPE_XENBUS ] ="xenbus" , [FTYPE_SOCKET ] ="socket" , [FTYPE_TAP ] ="net" , [FTYPE_BLK ] ="blk" ,@@ -892,18 +886,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exce n++;FD_CLR (i, exceptfds);break ; -#ifdef CONFIG_XENBUS -case FTYPE_XENBUS : -if (FD_ISSET (i, readfds)) { -if (files[i].dev) - n++; -else -FD_CLR (i, readfds); - } -FD_CLR (i, writefds); -FD_CLR (i, exceptfds); -break ; -#endif case FTYPE_TAP :case FTYPE_BLK :case FTYPE_KBD :lib/xs.c +49
-15@@ -18,23 +18,55 @@ static inline int _xs_fileno(struct xs_handle *h) {return (intptr_t ) h; } +static int xs_close_fd (int fd) +{ +struct xenbus_event *event, *next; +struct file *file =get_file_from_fd (fd); + +for (event = file->dev; event; event = next) + { + next = event->next; +free (event); + } + +return 0 ; +} + +static bool xs_can_read (int fd) +{ +struct file *file =get_file_from_fd (fd); + +return file && file->dev; +} + +static struct file_ops xenbus_ops = { + .name ="xenbus" , + .close = xs_close_fd, + .select_rd = xs_can_read, +}; +struct xs_handle *xs_daemon_open () { -int fd =alloc_fd (FTYPE_XENBUS ); - files[fd].dev =NULL ; -printk ("xs_daemon_open -> %d, %p\n" , fd, &files[fd].dev); +int fd; +struct file *file; +static unsigned int ftype_xenbus; + +if ( !ftype_xenbus ) + ftype_xenbus =alloc_file_type (&xenbus_ops); + + fd =alloc_fd (ftype_xenbus); + file =get_file_from_fd (fd); +if ( !file ) +return NULL ; + + file->dev =NULL ; +printk ("xs_daemon_open -> %d, %p\n" , fd, &file->dev);return (void *)(intptr_t ) fd; }void xs_daemon_close (struct xs_handle *h) { -int fd =_xs_fileno (h); -struct xenbus_event *event, *next; -for (event = files[fd].dev; event; event = next) - { - next = event->next; -free (event); - } +close (_xs_fileno (h)); }int xs_fileno (struct xs_handle *h)@@ -169,18 +201,20 @@ char **xs_directory(struct xs_handle *h, xs_transaction_t t,bool xs_watch (struct xs_handle *h,const char *path,const char *token) { -int fd =_xs_fileno (h); +struct file *file =get_file_from_fd (_xs_fileno (h)); +printk ("xs_watch(%s, %s)\n" , path, token);return xs_bool (xenbus_watch_path_token (XBT_NULL , path, token, - (xenbus_event_queue *)&files[fd].dev)); + (xenbus_event_queue *)&file->dev)); }char **xs_read_watch (struct xs_handle *h,unsigned int *num) { -int fd =_xs_fileno (h);struct xenbus_event *event; - event = files[fd].dev; - files[fd].dev = event->next; +struct file *file =get_file_from_fd (_xs_fileno (h)); + + event = file->dev; + file->dev = event->next;printk ("xs_read_watch() -> %s %s\n" , event->path, event->token); *num =2 ;return (char **) &event->path;xenbus/xenbus.c +1
-0@@ -393,6 +393,7 @@ static int allocate_xenbus_id(void)void init_xenbus (void ) {int err; +DEBUG ("init_xenbus called.\n" );create_thread ("xenstore" , xenbus_thread_func,NULL );DEBUG ("buf at %p.\n" , xenstore_buf);Signature · 1 line
2.26.2
Signature · 8 lines
Samuel <y> muhahaha... <y> ya un train qui part de Perrache à 14h57 <y> qui passe à Part-Dieu à 15h10 <y> si je le prends à Perrache, je suis en zone bleue <y> si je le prends à Part-Dieu, je suis en zone blanche <y> donc je vais le prendre à Perrache *mais* à Part-Dieu ;-) -+- #ens-mim - vive la SNCF -+-
Re: [PATCH v2 03/12] mini-os: use alloc_file_type() and get_file_from_fd() in xs
Samuel Thibault, le mar. 11 janv. 2022 21:06:15 +0100, a ecrit:
Quoted 81 lines
Juergen Gross, le mar. 11 janv. 2022 16:12:06 +0100, a ecrit:
Quoted 5 lines
Allocate the needed file type via
alloc_file_type().Instead of directly accessing the files[] array use
get_file_from_fd().Signed-off-by Juergen Gross <[email protected]>Reviewed-by Samuel Thibault <[email protected]>Quoted 72 lines
--- include/lib.h | 3 +-- lib/sys.c | 18 -------------- lib/xs.c | 64 +++++++++++++++++++++++++++++++++++++------------ xenbus/xenbus.c | 1 + 4 files changed, 51 insertions(+), 35 deletions(-)include/lib.h +1
-2@@ -167,8 +167,7 @@ void sanity_check(void);#define FTYPE_BLK 9#define FTYPE_TPMFRONT 10#define FTYPE_TPM_TIS 11 -#define FTYPE_XENBUS 12 -#define FTYPE_N 13 +#define FTYPE_N 12#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);lib/sys.c +0
-18@@ -495,11 +495,6 @@ int close(int fd)switch (files[fd].type) {default :break ; -#ifdef CONFIG_XENBUS -case FTYPE_XENBUS : -xs_daemon_close ((void *)(intptr_t ) fd); -break ; -#endif #ifdef HAVE_LWIP case FTYPE_SOCKET : res =lwip_close (files[fd].fd);@@ -712,7 +707,6 @@ int closedir(DIR *dir)static const char *file_types[] = { [FTYPE_NONE ] ="none" , [FTYPE_CONSOLE ] ="console" , - [FTYPE_XENBUS ] ="xenbus" , [FTYPE_SOCKET ] ="socket" , [FTYPE_TAP ] ="net" , [FTYPE_BLK ] ="blk" ,@@ -892,18 +886,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exce n++;FD_CLR (i, exceptfds);break ; -#ifdef CONFIG_XENBUS -case FTYPE_XENBUS : -if (FD_ISSET (i, readfds)) { -if (files[i].dev) - n++; -else -FD_CLR (i, readfds); - } -FD_CLR (i, writefds); -FD_CLR (i, exceptfds); -break ; -#endif case FTYPE_TAP :case FTYPE_BLK :case FTYPE_KBD :lib/xs.c +0
-0@@ -18,23 +18,55 @@ static inline int _xs_fileno(struct xs_handle *h) {return (intptr_t ) h; }
Quoted 4 lines
lib/xs.c static inline int _xs_fileno(struct xs_handle *h) { · line 24
Quoted 4 lines
lib/xs.c +4
-0+static int xs_close_fd (int fd) +{ +struct xenbus_event *event, *next; +struct file *file =get_file_from_fd (fd);
This...
Quoted 4 lines
Quoted 4 lines
lib/xs.c +4
-0+for (event = file->dev; event; event = next) + { + next = event->next; +free (event);
Quoted 8 lines
lib/xs.c static inline int _xs_fileno(struct xs_handle *h) { · line 37
Quoted 8 lines
lib/xs.c +8
-0+ } + +return 0 ; +} + +static bool xs_can_read (int fd) +{ +struct file *file =get_file_from_fd (fd);
... and that would be simplified by directly getting the file* from the parameters.
Quoted 95 lines
Quoted 85 lines
lib/xs.c +31
-15+return file && file->dev; +} + +static struct file_ops xenbus_ops = { + .name ="xenbus" , + .close = xs_close_fd, + .select_rd = xs_can_read, +}; +struct xs_handle *xs_daemon_open () { -int fd =alloc_fd (FTYPE_XENBUS ); - files[fd].dev =NULL ; -printk ("xs_daemon_open -> %d, %p\n" , fd, &files[fd].dev); +int fd; +struct file *file; +static unsigned int ftype_xenbus; + +if ( !ftype_xenbus ) + ftype_xenbus =alloc_file_type (&xenbus_ops); + + fd =alloc_fd (ftype_xenbus); + file =get_file_from_fd (fd); +if ( !file ) +return NULL ; + + file->dev =NULL ; +printk ("xs_daemon_open -> %d, %p\n" , fd, &file->dev);return (void *)(intptr_t ) fd; }void xs_daemon_close (struct xs_handle *h) { -int fd =_xs_fileno (h); -struct xenbus_event *event, *next; -for (event = files[fd].dev; event; event = next) - { - next = event->next; -free (event); - } +close (_xs_fileno (h)); }int xs_fileno (struct xs_handle *h)@@ -169,18 +201,20 @@ char **xs_directory(struct xs_handle *h, xs_transaction_t t,bool xs_watch (struct xs_handle *h,const char *path,const char *token) { -int fd =_xs_fileno (h); +struct file *file =get_file_from_fd (_xs_fileno (h)); +printk ("xs_watch(%s, %s)\n" , path, token);return xs_bool (xenbus_watch_path_token (XBT_NULL , path, token, - (xenbus_event_queue *)&files[fd].dev)); + (xenbus_event_queue *)&file->dev)); }char **xs_read_watch (struct xs_handle *h,unsigned int *num) { -int fd =_xs_fileno (h);struct xenbus_event *event; - event = files[fd].dev; - files[fd].dev = event->next; +struct file *file =get_file_from_fd (_xs_fileno (h)); + + event = file->dev; + file->dev = event->next;printk ("xs_read_watch() -> %s %s\n" , event->path, event->token); *num =2 ;return (char **) &event->path;xenbus/xenbus.c +1
-0@@ -393,6 +393,7 @@ static int allocate_xenbus_id(void)void init_xenbus (void ) {int err; +DEBUG ("init_xenbus called.\n" );create_thread ("xenstore" , xenbus_thread_func,NULL );DEBUG ("buf at %p.\n" , xenstore_buf);Signature · 1 line
2.26.2Signature · 8 lines
Samuel <y> muhahaha... <y> ya un train qui part de Perrache à 14h57 <y> qui passe à Part-Dieu à 15h10 <y> si je le prends à Perrache, je suis en zone bleue <y> si je le prends à Part-Dieu, je suis en zone blanche <y> donc je vais le prendre à Perrache *mais* à Part-Dieu ;-) -+- #ens-mim - vive la SNCF -+-
Signature · 4 lines
Samuel <c> hiri, le cri ici, c des marrants <c> j'ai un rep ".uglyhackdirectorywithoutacls" ds mon home -+- #ens-mim en stage -+-
Re: [PATCH v2 03/12] mini-os: use alloc_file_type() and get_file_from_fd() in xs
Quoted 16 lines · Samuel Thibault
Samuel Thibault, le mar. 11 janv. 2022 21:06:15 +0100, a ecrit:
Quoted 9 lines
Juergen Gross, le mar. 11 janv. 2022 16:12:06 +0100, a ecrit:
Quoted 8 lines
lib/xs.c +0
-0@@ -18,23 +18,55 @@ static inline int _xs_fileno(struct xs_handle *h) {return (intptr_t ) h; }Quoted 4 lines
lib/xs.c static inline int _xs_fileno(struct xs_handle *h) { · line 24
Quoted 4 lines
lib/xs.c +4
-0+static int xs_close_fd (int fd) +{ +struct xenbus_event *event, *next; +struct file *file =get_file_from_fd (fd);This...
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
lib/xs.c +4
-0+for (event = file->dev; event; event = next) + { + next = event->next; +free (event);Quoted 8 lines
lib/xs.c static inline int _xs_fileno(struct xs_handle *h) { · line 37
Quoted 8 lines
lib/xs.c +8
-0+ } + +return 0 ; +} + +static bool xs_can_read (int fd) +{ +struct file *file =get_file_from_fd (fd);... and that would be simplified by directly getting the file* from the parameters.
I actually make exactly the same suggestion for close() in libxenevtchn. The callback hooks don't need the same API as the libc functions.
~Andrew
Re: [PATCH v2 03/12] mini-os: use alloc_file_type() and get_file_from_fd() in xs
In reply to Juergen Grosslib/xs.c static inline int _xs_fileno(struct xs_handle *h) { · line 70
Quoted 11 lines · Juergen Gross
void
xs_daemon_close(struct xs_handle *h)
{lib/xs.c +1
-7-int fd =_xs_fileno (h); -struct xenbus_event *event, *next; -for (event = files[fd].dev; event; event = next) - { - next = event->next; -free (event); - } +close (_xs_fileno (h)); }
You've deleted the sole caller of xs_daemon_close() from the main close() function.
That said, I'm very confused, because nothing in minios declares it. The declaration appears to come from xenstore.h, which is clearly included unconditionally (when it ought not to be), but libxenstore also defines the function too...
~Andrew
Re: [PATCH v2 03/12] mini-os: use alloc_file_type() and get_file_from_fd() in xs
Quoted 13 lines · Andrew Cooper
lib/xs.c static inline int _xs_fileno(struct xs_handle *h) { · line 70
Quoted 11 lines · Juergen Gross
void xs_daemon_close(struct xs_handle *h) {lib/xs.c +1
-7-int fd =_xs_fileno (h); -struct xenbus_event *event, *next; -for (event = files[fd].dev; event; event = next) - { - next = event->next; -free (event); - } +close (_xs_fileno (h)); }
Quoted 7 lines
You've deleted the sole caller of
xs_daemon_close()from the mainclose()function.That said, I'm very confused, because nothing in minios declares it. The declaration appears to come from
xenstore.h, which is clearly included unconditionally (when it ought not to be), but libxenstore also defines the function too...
I already thought of restructuring this mess.
lib/xs.c is the Mini-OS variant of libxenstore, and as such it shares quite some code with libxenstore.
So the correct thing to do would be to split libxenstore into a common part and a posix/Mini-OS part, drop lib/xs.c, and use the library in Mini-OS instead.
But this should be done in a separate series.
Juergen
Re: [PATCH v2 03/12] mini-os: use alloc_file_type() and get_file_from_fd() in xs
Quoted 23 lines · Juergen Gross
Quoted 13 lines · Andrew Cooper
Quoted 11 lines · Juergen Gross
void
xs_daemon_close(struct xs_handle *h)
{
- int fd =_xs_fileno(h);
- structxenbus_event*event, *next;
- for (event = files[fd].dev; event; event = next) - {
- next =event->next;
- free(event);
- }
+ close(_xs_fileno(h));
}Quoted 7 lines
You've deleted the sole caller of
xs_daemon_close()from the mainclose()function.That said, I'm very confused, because nothing in minios declares it. The declaration appears to come from
xenstore.h, which is clearly included unconditionally (when it ought not to be), but libxenstore also defines the function too...I already thought of restructuring this mess.
Quoted 8 lines
lib/xs.cis the Mini-OS variant of libxenstore, and as such it shares quite some code with libxenstore.So the correct thing to do would be to split libxenstore into a common part and a posix/Mini-OS part, drop
lib/xs.c, and use the library in Mini-OS instead.But this should be done in a separate series.
Sounds like a plan.
~Andrew
PATCH v2 4/12 mini-os: use alloc_file_type() and get_file_from_fd() in tpm_tis
In reply to Juergen GrossAllocate a file type dynamically via alloc_file_type().
Instead of directly accessing the files[] array use get_file_from_fd().
Make some now local functions static and modify their prototypes to match the file_ops requirements.
--- include/lib.h | 3 +- include/tpm_tis.h | 6 --- lib/sys.c | 23 --------- tpm_tis.c | 120 ++++++++++++++++++++++++++++++---------------- 4 files changed, 80 insertions(+), 72 deletions(-)
include/lib.h +1 -2
@@ -166,8 +166,7 @@ void sanity_check(void);#define FTYPE_TAP 8#define FTYPE_BLK 9#define FTYPE_TPMFRONT 10 -#define FTYPE_TPM_TIS 11 -#define FTYPE_N 12 +#define FTYPE_N 11#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
include/tpm_tis.h +0 -6
@@ -37,14 +37,11 @@ struct tpm_chip;struct tpm_chip *init_tpm_tis (unsigned long baseaddr,int localities,unsigned int irq);struct tpm_chip *init_tpm2_tis (unsigned long baseaddr,int localities,unsigned int irq); -void shutdown_tpm_tis (struct tpm_chip * tpm);int tpm_tis_request_locality (struct tpm_chip * tpm,int locality);int tpm_tis_cmd (struct tpm_chip * tpm,uint8_t * req,size_t reqlen,uint8_t ** resp,size_t * resplen);#ifdef HAVE_LIBC -#include <sys/stat.h> -#include <fcntl.h> /* POSIX IO functions: * use tpm_tis_open() to get a file descriptor to the tpm device * use write() on the fd to send a command to the backend. You must
@@ -53,9 +50,6 @@ int tpm_tis_cmd(struct tpm_chip* tpm, uint8_t* req, size_t reqlen, uint8_t** res* fstat() to get the size of the response and lseek() to seek on it. */ int tpm_tis_open (struct tpm_chip * tpm); -int tpm_tis_posix_read (int fd,uint8_t * buf,size_t count); -int tpm_tis_posix_write (int fd,const uint8_t * buf,size_t count); -int tpm_tis_posix_fstat (int fd,struct stat * buf);#endif #endif
lib/sys.c +0 -23
@@ -29,7 +29,6 @@#include <blkfront.h> #include <fbfront.h> #include <tpmfront.h> -#include <tpm_tis.h> #include <xenbus.h> #include <xenstore.h> #include <poll.h>
@@ -349,11 +348,6 @@ int read(int fd, void *buf, size_t nbytes)case FTYPE_TPMFRONT : {return tpmfront_posix_read (fd, buf, nbytes); } -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : { -return tpm_tis_posix_read (fd, buf, nbytes); - }#endif default :break ;
@@ -399,10 +393,6 @@ int write(int fd, const void *buf, size_t nbytes)#ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT :return tpmfront_posix_write (fd, buf, nbytes); -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -return tpm_tis_posix_write (fd, buf, nbytes);#endif default :break ;
@@ -459,10 +449,6 @@ off_t lseek(int fd, off_t offset, int whence)#ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT :break ; -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -break ;#endif case FTYPE_FILE :break ;
@@ -515,11 +501,6 @@ int close(int fd)shutdown_tpmfront (files[fd].dev);break ;#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -shutdown_tpm_tis (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);
@@ -599,10 +580,6 @@ int fstat(int fd, struct stat *buf)#ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT :return tpmfront_posix_fstat (fd, buf); -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -return tpm_tis_posix_fstat (fd, buf);#endif default :break ;
tpm_tis.c +79 -41
185 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH v2 04/12] mini-os: use alloc_file_type() and get_file_from_fd() in tpm_tis
Juergen Gross, le mar. 11 janv. 2022 16:12:07 +0100, a ecrit:
Quoted 8 lines
Allocate a file type dynamically via
alloc_file_type().Instead of directly accessing the files[] array use
get_file_from_fd().Make some now local functions static and modify their prototypes to match the
file_opsrequirements.Signed-off-by Juergen Gross <[email protected]>
modulo the int fd / file * thing,
Quoted 319 lines
--- include/lib.h | 3 +- include/tpm_tis.h | 6 --- lib/sys.c | 23 --------- tpm_tis.c | 120 ++++++++++++++++++++++++++++++---------------- 4 files changed, 80 insertions(+), 72 deletions(-)include/lib.h +1
-2@@ -166,8 +166,7 @@ void sanity_check(void);#define FTYPE_TAP 8#define FTYPE_BLK 9#define FTYPE_TPMFRONT 10 -#define FTYPE_TPM_TIS 11 -#define FTYPE_N 12 +#define FTYPE_N 11#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);include/tpm_tis.h +0
-6@@ -37,14 +37,11 @@ struct tpm_chip;struct tpm_chip *init_tpm_tis (unsigned long baseaddr,int localities,unsigned int irq);struct tpm_chip *init_tpm2_tis (unsigned long baseaddr,int localities,unsigned int irq); -void shutdown_tpm_tis (struct tpm_chip * tpm);int tpm_tis_request_locality (struct tpm_chip * tpm,int locality);int tpm_tis_cmd (struct tpm_chip * tpm,uint8_t * req,size_t reqlen,uint8_t ** resp,size_t * resplen);#ifdef HAVE_LIBC -#include <sys/stat.h> -#include <fcntl.h> /* POSIX IO functions: * use tpm_tis_open() to get a file descriptor to the tpm device * use write() on the fd to send a command to the backend. You must @@ -53,9 +50,6 @@ int tpm_tis_cmd(struct tpm_chip* tpm, uint8_t* req, size_t reqlen, uint8_t** res* fstat() to get the size of the response and lseek() to seek on it. */ int tpm_tis_open (struct tpm_chip * tpm); -int tpm_tis_posix_read (int fd,uint8_t * buf,size_t count); -int tpm_tis_posix_write (int fd,const uint8_t * buf,size_t count); -int tpm_tis_posix_fstat (int fd,struct stat * buf);#endif #endif lib/sys.c +0
-23@@ -29,7 +29,6 @@#include <blkfront.h> #include <fbfront.h> #include <tpmfront.h> -#include <tpm_tis.h> #include <xenbus.h> #include <xenstore.h> #include <poll.h> @@ -349,11 +348,6 @@ int read(int fd, void *buf, size_t nbytes)case FTYPE_TPMFRONT : {return tpmfront_posix_read (fd, buf, nbytes); } -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : { -return tpm_tis_posix_read (fd, buf, nbytes); - }#endif default :break ;@@ -399,10 +393,6 @@ int write(int fd, const void *buf, size_t nbytes)#ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT :return tpmfront_posix_write (fd, buf, nbytes); -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -return tpm_tis_posix_write (fd, buf, nbytes);#endif default :break ;@@ -459,10 +449,6 @@ off_t lseek(int fd, off_t offset, int whence)#ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT :break ; -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -break ;#endif case FTYPE_FILE :break ;@@ -515,11 +501,6 @@ int close(int fd)shutdown_tpmfront (files[fd].dev);break ;#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -shutdown_tpm_tis (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);@@ -599,10 +580,6 @@ int fstat(int fd, struct stat *buf)#ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT :return tpmfront_posix_fstat (fd, buf); -#endif -#ifdef CONFIG_TPM_TIS -case FTYPE_TPM_TIS : -return tpm_tis_posix_fstat (fd, buf);#endif default :break ;tpm_tis.c +79
-41@@ -792,6 +792,9 @@ int tpm_tis_send(struct tpm_chip* tpm, uint8_t* buf, size_t len) {int status, burstcnt =0 ;int count =0 ;uint32_t ordinal; +#ifdef HAVE_LIBC +struct file *file =get_file_from_fd (tpm->fd); +#endif if (tpm_tis_request_locality (tpm, tpm->locality) <0 ) {return -EBUSY ;@@ -844,9 +847,10 @@ int tpm_tis_send(struct tpm_chip* tpm, uint8_t* buf, size_t len) { } }#ifdef HAVE_LIBC -if (tpm->fd >=0 ) { - files[tpm->fd].read = false; - files[tpm->fd].offset =0 ; +if ( file ) + { + file->read = false; + file->offset =0 ; }#endif return len;@@ -1093,6 +1097,23 @@ ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap,return rc; } +static void shutdown_tpm_tis (struct tpm_chip * tpm){ +int i; + +printk ("Shutting down tpm_tis device\n" ); + +iowrite32 (TPM_INT_ENABLE (tpm, tpm->locality), ~TPM_GLOBAL_INT_ENABLE ); + +/*Unmap all of the mmio pages */ +for (i =0 ; i <5 ; ++i) { +if (tpm->pages[i] !=NULL ) { +iounmap (tpm->pages[i],PAGE_SIZE ); + tpm->pages[i] =NULL ; + } + } +free (tpm); +return ; +}struct tpm_chip *init_tpm_tis (unsigned long baseaddr,int localities,unsigned int irq) {@@ -1242,25 +1263,6 @@ abort_egress:return NULL ; } -void shutdown_tpm_tis (struct tpm_chip * tpm){ -int i; - -printk ("Shutting down tpm_tis device\n" ); - -iowrite32 (TPM_INT_ENABLE (tpm, tpm->locality), ~TPM_GLOBAL_INT_ENABLE ); - -/*Unmap all of the mmio pages */ -for (i =0 ; i <5 ; ++i) { -if (tpm->pages[i] !=NULL ) { -iounmap (tpm->pages[i],PAGE_SIZE ); - tpm->pages[i] =NULL ; - } - } -free (tpm); -return ; -} - -int tpm_tis_cmd (struct tpm_chip * tpm,uint8_t * req,size_t reqlen,uint8_t ** resp,size_t * resplen) {if (tpm->locality <0 ) {@@ -1279,23 +1281,24 @@ int tpm_tis_cmd(struct tpm_chip* tpm, uint8_t* req, size_t reqlen, uint8_t** res }#ifdef HAVE_LIBC -int tpm_tis_open (struct tpm_chip * tpm) +#include <sys/stat.h> +#include <fcntl.h> + +static int tpm_tis_close (int fd) { -/* Silently prevent multiple opens */ -if (tpm->fd !=-1 ) { -return tpm->fd; - } +struct file *file =get_file_from_fd (fd); - tpm->fd =alloc_fd (FTYPE_TPM_TIS ); -printk ("tpm_tis_open() -> %d\n" , tpm->fd); - files[tpm->fd].dev = tpm; -return tpm->fd; +shutdown_tpm_tis (file->dev); + +return 0 ; } -int tpm_tis_posix_write (int fd,const uint8_t * buf,size_t count) +static int tpm_tis_posix_write (int fd,const void *buf,size_t count) {struct tpm_chip * tpm; - tpm = files[fd].dev; +struct file *file =get_file_from_fd (fd); + + tpm = file->dev;if (tpm->locality <0 ) {printk ("tpm_tis_posix_write() failed! locality not set!\n" );@@ -1319,11 +1322,13 @@ int tpm_tis_posix_write(int fd, const uint8_t* buf, size_t count)return count; } -int tpm_tis_posix_read (int fd,uint8_t * buf,size_t count) +static int tpm_tis_posix_read (int fd,void *buf,size_t count) {int rc;struct tpm_chip * tpm; - tpm = files[fd].dev; +struct file *file =get_file_from_fd (fd); + + tpm = file->dev;if (count ==0 ) {return 0 ;@@ -1337,20 +1342,24 @@ int tpm_tis_posix_read(int fd, uint8_t* buf, size_t count)/* Handle EOF case */ -if (files[fd].offset >= tpm->data_len) { +if ( file->offset >= tpm->data_len ) + { rc =0 ; }else { - rc =min (tpm->data_len - files[fd].offset, count); -memcpy (buf, tpm->data_buffer + files[fd].offset, rc); + rc =min (tpm->data_len - file->offset, count); +memcpy (buf, tpm->data_buffer + file->offset, rc); } - files[fd].offset += rc; + file->offset += rc;/* Reset the data pending flag */ return rc; } -int tpm_tis_posix_fstat (int fd,struct stat * buf) + +static int tpm_tis_posix_fstat (int fd,struct stat *buf) {struct tpm_chip * tpm; - tpm = files[fd].dev; +struct file *file =get_file_from_fd (fd); + + tpm = file->dev; buf->st_mode =O_RDWR ; buf->st_uid =0 ;@@ -1360,6 +1369,35 @@ int tpm_tis_posix_fstat(int fd, struct stat* buf)return 0 ; } +static struct file_ops tpm_tis_ops = { + .name ="tpm_tis" , + .read = tpm_tis_posix_read, + .write = tpm_tis_posix_write, + .lseek = lseek_default, + .close = tpm_tis_close, + .fstat = tpm_tis_posix_fstat, +}; + +int tpm_tis_open (struct tpm_chip * tpm) +{ +struct file *file; +static unsigned int ftype_tis; + +/* Silently prevent multiple opens */ +if (tpm->fd !=-1 ) { +return tpm->fd; + } + +if ( !ftype_tis ) + ftype_tis =alloc_file_type (&tpm_tis_ops); + + tpm->fd =alloc_fd (ftype_tis); +printk ("tpm_tis_open() -> %d\n" , tpm->fd); + file =get_file_from_fd (tpm->fd); + file->dev = tpm; +return tpm->fd; +} +/* TPM 2.0 */ /*TPM2.0 Selftest*/ Signature · 1 line
2.26.2
Signature · 9 lines
Samuel Now, it we had this sort of thing: yield -a for yield to all traffic yield -t for yield to trucks yield -f for yield to people walking (yield foot) yield -d t* for yield on days starting with t ...you'd have a lot of dead people at intersections, and traffic jams you wouldn't believe... (Discussion in comp.os.linux.misc on the intuitiveness of commands.)
Re: [PATCH v2 04/12] mini-os: use alloc_file_type() and get_file_from_fd() in tpm_tis
In reply to Juergen Grosstpm_tis.c ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap, · line 1100
Quoted 9 lines · Juergen Gross
tpm_tis.c +1
-0@@ -1093,6 +1097,23 @@ ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap,return rc; } +static void shutdown_tpm_tis (struct tpm_chip * tpm){
Style, as you're moving it. Also in the function.
Quoted 4 lines
tpm_tis.c +0
-0@@ -1360,6 +1369,35 @@ int tpm_tis_posix_fstat(int fd, struct stat* buf)return 0 ; }
tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* buf) · line 1381
Quoted 10 lines
tpm_tis.c +10
-0+static struct file_ops tpm_tis_ops = { + .name ="tpm_tis" , + .read = tpm_tis_posix_read, + .write = tpm_tis_posix_write, + .lseek = lseek_default, + .close = tpm_tis_close, + .fstat = tpm_tis_posix_fstat, +}; + +int tpm_tis_open (struct tpm_chip * tpm)
Style.
tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* buf) · line 1389
Quoted 8 lines
tpm_tis.c +8
-0+{ +struct file *file; +static unsigned int ftype_tis; + +/* Silently prevent multiple opens */ +if (tpm->fd !=-1 ) { +return tpm->fd; + }
Another WTF moment. We silently swallow multiple open()s, but don't refcout close()s ?
This cannot be correct, or sensible, behaviour.
Jason/Daniel - thoughts?
~Andrew
Re: [PATCH v2 04/12] mini-os: use alloc_file_type() and get_file_from_fd() in tpm_tis
Quoted 43 lines · Andrew Cooper
tpm_tis.c ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap, · line 1100
Quoted 9 lines · Juergen Gross
tpm_tis.c +1
-0@@ -1093,6 +1097,23 @@ ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap,return rc; } +static void shutdown_tpm_tis (struct tpm_chip * tpm){Style, as you're moving it. Also in the function.
Quoted 4 lines
tpm_tis.c +0
-0@@ -1360,6 +1369,35 @@ int tpm_tis_posix_fstat(int fd, struct stat* buf)return 0 ; }tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* buf) · line 1381
Quoted 10 lines
tpm_tis.c +10
-0+static struct file_ops tpm_tis_ops = { + .name ="tpm_tis" , + .read = tpm_tis_posix_read, + .write = tpm_tis_posix_write, + .lseek = lseek_default, + .close = tpm_tis_close, + .fstat = tpm_tis_posix_fstat, +}; + +int tpm_tis_open (struct tpm_chip * tpm)Style.
tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* buf) · line 1389
Quoted 8 lines
tpm_tis.c +8
-0+{ +struct file *file; +static unsigned int ftype_tis; + +/* Silently prevent multiple opens */ +if (tpm->fd !=-1 ) { +return tpm->fd; + }Another WTF moment. We silently swallow multiple open()s, but don't refcout close()s ?
Quoted 3 lines
This cannot be correct, or sensible, behaviour.
Jason/Daniel - thoughts?
Looks like vtpmmgr only opens a single global fd, so it has not been a problem in practice.
You need some sort of synchronization to let multiple entities access the TPM. So limiting to only a single entity/single FD is a reasonable restriction.
Regards,
Jason
Re: [PATCH v2 04/12] mini-os: use alloc_file_type() and get_file_from_fd() in tpm_tis
In reply to Andrew CooperQuoted 17 lines · Andrew Cooper
tpm_tis.c ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap, · line 1100
Quoted 9 lines · Juergen Gross
tpm_tis.c +1
-0@@ -1093,6 +1097,23 @@ ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap,return rc; } +static void shutdown_tpm_tis (struct tpm_chip * tpm){Style, as you're moving it. Also in the function.
Quoted 4 lines
tpm_tis.c +0
-0@@ -1360,6 +1369,35 @@ int tpm_tis_posix_fstat(int fd, struct stat* buf)return 0 ; }
Quoted 12 lines
tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* buf) · line 1381
Quoted 10 lines
tpm_tis.c +10
-0+static struct file_ops tpm_tis_ops = { + .name ="tpm_tis" , + .read = tpm_tis_posix_read, + .write = tpm_tis_posix_write, + .lseek = lseek_default, + .close = tpm_tis_close, + .fstat = tpm_tis_posix_fstat, +}; + +int tpm_tis_open (struct tpm_chip * tpm)Style.
Ah, yes I should have corrected this while moving the function.
Quoted 13 lines
tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* buf) · line 1389
Quoted 9 lines
tpm_tis.c +8
-0+{ +struct file *file; +static unsigned int ftype_tis; + +/* Silently prevent multiple opens */ +if (tpm->fd !=-1 ) { +return tpm->fd; + }Another WTF moment. We silently swallow multiple open()s, but don't refcout close()s ?
Quoted 3 lines
This cannot be correct, or sensible, behaviour.
Jason/Daniel - thoughts?
I just moved the function, but I can change this, of course.
Juergen
Re: [PATCH v2 04/12] mini-os: use alloc_file_type() and get_file_from_fd() in tpm_tis
Quoted 46 lines · Juergen Gross
Quoted 13 lines · Andrew Cooper
tpm_tis.c ssize_t tpm_getcap(struct tpm_chip *chip, · line 1100
Quoted 9 lines · Juergen Gross
tpm_tis.c +0
-0@@ -1093,6 +1097,23 @@ ssize_t tpm_getcap(struct tpm_chip *chip, uint32_t subcap_id, cap_t *cap, return rc; } +static void shutdown_tpm_tis(struct tpm_chip* tpm){Style, as you're moving it. Also in the function.
Quoted 16 lines
Quoted 4 lines
tpm_tis.c +0
-0@@ -1360,6 +1369,35 @@ int tpm_tis_posix_fstat(int fd, struct stat* buf) return 0; }tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* · line 1381
Quoted 10 lines
tpm_tis.c +9
-0+static struct file_ops tpm_tis_ops = { + .name = "tpm_tis", + .read = tpm_tis_posix_read, + .write = tpm_tis_posix_write, + .lseek = lseek_default, + .close = tpm_tis_close, + .fstat = tpm_tis_posix_fstat, +}; + +int tpm_tis_open (struct tpm_chip * tpm)Style.
Ah, yes I should have corrected this while moving the function.
Quoted 13 lines
tpm_tis.c int tpm_tis_posix_fstat(int fd, struct stat* buf) · line 1389
Quoted 9 lines
tpm_tis.c +8
-0+{ + struct file *file; + static unsigned int ftype_tis; + + /* Silently prevent multiple opens */ + if(tpm->fd != -1) { + return tpm->fd; + }Another WTF moment. We silently swallow multiple open()s, but don't refcout close()s ?
Quoted 5 lines
Quoted 3 lines
This cannot be correct, or sensible, behaviour.
Jason/Daniel - thoughts?
I just moved the function, but I can change this, of course.
It's a pattern elsewhere too. Much as it's horrible logic, it probably doesn't want reworking in a patch like this, and the series as a whole is big enough so I'm not going to suggest you tackle it too.
~Andrew
PATCH v2 5/12 mini-os: use alloc_file_type() and get_file_from_fd() in tpmfront
In reply to Juergen GrossAllocate a file type dynamically via alloc_file_type().
Instead of directly accessing the files[] array use get_file_from_fd().
--- include/lib.h | 3 +- include/tpmfront.h | 5 --- lib/sys.c | 23 ----------- tpmfront.c | 100 ++++++++++++++++++++++++++++++++------------- 4 files changed, 72 insertions(+), 59 deletions(-)
include/lib.h +1 -2
@@ -165,8 +165,7 @@ void sanity_check(void);#define FTYPE_KBD 7#define FTYPE_TAP 8#define FTYPE_BLK 9 -#define FTYPE_TPMFRONT 10 -#define FTYPE_N 11 +#define FTYPE_N 10#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
include/tpmfront.h +0 -5
@@ -81,7 +81,6 @@ int tpmfront_cmd(struct tpmfront_dev* dev, uint8_t* req, size_t reqlen, uint8_t*int tpmfront_set_locality (struct tpmfront_dev * dev,int locality);#ifdef HAVE_LIBC -#include <sys/stat.h> /* POSIX IO functions: * use tpmfront_open() to get a file descriptor to the tpm device * use write() on the fd to send a command to the backend. You must
@@ -90,10 +89,6 @@ int tpmfront_set_locality(struct tpmfront_dev* dev, int locality);* fstat() to get the size of the response and lseek() to seek on it. */ int tpmfront_open (struct tpmfront_dev * dev); -int tpmfront_posix_read (int fd,uint8_t * buf,size_t count); -int tpmfront_posix_write (int fd,const uint8_t * buf,size_t count); -int tpmfront_posix_fstat (int fd,struct stat * buf);#endif -#endif
lib/sys.c +0 -23
@@ -28,7 +28,6 @@#include <netfront.h> #include <blkfront.h> #include <fbfront.h> -#include <tpmfront.h> #include <xenbus.h> #include <xenstore.h> #include <poll.h>
@@ -343,11 +342,6 @@ int read(int fd, void *buf, size_t nbytes)case FTYPE_BLK : {return blkfront_posix_read (fd, buf, nbytes); } -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : { -return tpmfront_posix_read (fd, buf, nbytes); - }#endif default :break ;
@@ -389,10 +383,6 @@ int write(int fd, const void *buf, size_t nbytes)#ifdef CONFIG_BLKFRONT case FTYPE_BLK :return blkfront_posix_write (fd, buf, nbytes); -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -return tpmfront_posix_write (fd, buf, nbytes);#endif default :break ;
@@ -445,10 +435,6 @@ off_t lseek(int fd, off_t offset, int whence)#ifdef CONFIG_BLKFRONT case FTYPE_BLK :break ; -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -break ;#endif case FTYPE_FILE :break ;
@@ -496,11 +482,6 @@ int close(int fd)shutdown_blkfront (files[fd].dev);break ;#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -shutdown_tpmfront (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);
@@ -576,10 +557,6 @@ int fstat(int fd, struct stat *buf)#ifdef CONFIG_BLKFRONT case FTYPE_BLK :return blkfront_posix_fstat (fd, buf); -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -return tpmfront_posix_fstat (fd, buf);#endif default :break ;
tpmfront.c +71 -29
173 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH v2 05/12] mini-os: use alloc_file_type() and get_file_from_fd() in tpmfront
Juergen Gross, le mar. 11 janv. 2022 16:12:08 +0100, a ecrit:
Quoted 5 lines
Allocate a file type dynamically via
alloc_file_type().Instead of directly accessing the files[] array use
get_file_from_fd().Signed-off-by Juergen Gross <[email protected]>
modulo the int fd / file * thing,
Quoted 301 lines
--- include/lib.h | 3 +- include/tpmfront.h | 5 --- lib/sys.c | 23 ----------- tpmfront.c | 100 ++++++++++++++++++++++++++++++++------------- 4 files changed, 72 insertions(+), 59 deletions(-)include/lib.h +1
-2@@ -165,8 +165,7 @@ void sanity_check(void);#define FTYPE_KBD 7#define FTYPE_TAP 8#define FTYPE_BLK 9 -#define FTYPE_TPMFRONT 10 -#define FTYPE_N 11 +#define FTYPE_N 10#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);include/tpmfront.h +0
-5@@ -81,7 +81,6 @@ int tpmfront_cmd(struct tpmfront_dev* dev, uint8_t* req, size_t reqlen, uint8_t*int tpmfront_set_locality (struct tpmfront_dev * dev,int locality);#ifdef HAVE_LIBC -#include <sys/stat.h> /* POSIX IO functions: * use tpmfront_open() to get a file descriptor to the tpm device * use write() on the fd to send a command to the backend. You must @@ -90,10 +89,6 @@ int tpmfront_set_locality(struct tpmfront_dev* dev, int locality);* fstat() to get the size of the response and lseek() to seek on it. */ int tpmfront_open (struct tpmfront_dev * dev); -int tpmfront_posix_read (int fd,uint8_t * buf,size_t count); -int tpmfront_posix_write (int fd,const uint8_t * buf,size_t count); -int tpmfront_posix_fstat (int fd,struct stat * buf);#endif -#endif lib/sys.c +0
-23@@ -28,7 +28,6 @@#include <netfront.h> #include <blkfront.h> #include <fbfront.h> -#include <tpmfront.h> #include <xenbus.h> #include <xenstore.h> #include <poll.h> @@ -343,11 +342,6 @@ int read(int fd, void *buf, size_t nbytes)case FTYPE_BLK : {return blkfront_posix_read (fd, buf, nbytes); } -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : { -return tpmfront_posix_read (fd, buf, nbytes); - }#endif default :break ;@@ -389,10 +383,6 @@ int write(int fd, const void *buf, size_t nbytes)#ifdef CONFIG_BLKFRONT case FTYPE_BLK :return blkfront_posix_write (fd, buf, nbytes); -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -return tpmfront_posix_write (fd, buf, nbytes);#endif default :break ;@@ -445,10 +435,6 @@ off_t lseek(int fd, off_t offset, int whence)#ifdef CONFIG_BLKFRONT case FTYPE_BLK :break ; -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -break ;#endif case FTYPE_FILE :break ;@@ -496,11 +482,6 @@ int close(int fd)shutdown_blkfront (files[fd].dev);break ;#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -shutdown_tpmfront (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);@@ -576,10 +557,6 @@ int fstat(int fd, struct stat *buf)#ifdef CONFIG_BLKFRONT case FTYPE_BLK :return blkfront_posix_fstat (fd, buf); -#endif -#ifdef CONFIG_TPMFRONT -case FTYPE_TPMFRONT : -return tpmfront_posix_fstat (fd, buf);#endif default :break ;tpmfront.c +71
-29@@ -49,6 +49,10 @@void tpmfront_handler (evtchn_port_t port,struct pt_regs *regs,void *data) {struct tpmfront_dev * dev = (struct tpmfront_dev *) data;tpmif_shared_page_t *shr = dev->page; +#ifdef HAVE_LIBC +struct file *file =get_file_from_fd (dev->fd); +#endif +/*If we get a response when we didnt make a request, just ignore it */ if (!dev->waiting) {return ;@@ -65,8 +69,9 @@ void tpmfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data) { dev->waiting =0 ;#ifdef HAVE_LIBC -if (dev->fd >=0 ) { - files[dev->fd].read = true; +if ( file ) + { + file->read = true; }#endif wake_up (&dev->waitq);@@ -405,6 +410,10 @@ int tpmfront_send(struct tpmfront_dev* dev, const uint8_t* msg, size_t length)#ifdef TPMFRONT_PRINT_DEBUG int i;#endif +#ifdef HAVE_LIBC +struct file *file = dev ?get_file_from_fd (dev->fd) :NULL ; +#endif +/* Error Checking */ if (dev ==NULL || dev->state != XenbusStateConnected) {TPMFRONT_ERR ("Tried to send message through disconnected frontend\n" );@@ -437,9 +446,10 @@ int tpmfront_send(struct tpmfront_dev* dev, const uint8_t* msg, size_t length) dev->waiting =1 ; dev->resplen =0 ;#ifdef HAVE_LIBC -if (dev->fd >=0 ) { - files[dev->fd].read = false; - files[dev->fd].offset =0 ; +if ( file ) + { + file->read = false; + file->offset =0 ; dev->respgot = false; }#endif @@ -529,25 +539,14 @@ int tpmfront_set_locality(struct tpmfront_dev* dev, int locality)#ifdef HAVE_LIBC #include <errno.h> -int tpmfront_open (struct tpmfront_dev * dev) -{ -/* Silently prevent multiple opens */ -if (dev->fd !=-1 ) { -return dev->fd; - } - dev->fd =alloc_fd (FTYPE_TPMFRONT ); -printk ("tpmfront_open(%s) -> %d\n" , dev->nodename, dev->fd); - files[dev->fd].dev = dev; - dev->respgot = false; -return dev->fd; -} - -int tpmfront_posix_write (int fd,const uint8_t * buf,size_t count) +static int tpmfront_posix_write (int fd,const void *buf,size_t count) {int rc;struct tpmfront_dev * dev; - dev = files[fd].dev; +struct file *file =get_file_from_fd (fd); + + dev = file->dev;if (count ==0 ) {return 0 ;@@ -566,14 +565,15 @@ int tpmfront_posix_write(int fd, const uint8_t* buf, size_t count)return count; } -int tpmfront_posix_read (int fd,uint8_t * buf,size_t count) +static int tpmfront_posix_read (int fd,void *buf,size_t count) {int rc;uint8_t * dummybuf;size_t dummysz;struct tpmfront_dev * dev; +struct file *file =get_file_from_fd (fd); - dev = files[fd].dev; + dev = file->dev;if (count ==0 ) {return 0 ;@@ -588,29 +588,33 @@ int tpmfront_posix_read(int fd, uint8_t* buf, size_t count) }/* handle EOF case */ -if (files[dev->fd].offset >= dev->resplen) { +if ( file->offset >= dev->resplen ) + {return 0 ; }/* Compute the number of bytes and do the copy operation */ -if ((rc =min (count, dev->resplen - files[dev->fd].offset)) !=0 ) { -memcpy (buf, dev->respbuf + files[dev->fd].offset, rc); - files[dev->fd].offset += rc; +if ( (rc =min (count, dev->resplen - file->offset)) !=0 ) + { +memcpy (buf, dev->respbuf + file->offset, rc); + file->offset += rc; }return rc; } -int tpmfront_posix_fstat (int fd,struct stat * buf) +static int tpmfront_posix_fstat (int fd,struct stat *buf) {uint8_t * dummybuf;size_t dummysz;int rc; -struct tpmfront_dev * dev = files[fd].dev; +struct file *file =get_file_from_fd (fd); +struct tpmfront_dev * dev = file->dev;/* If we have a response waiting, then read it now from the backend * so we can get its length*/ -if (dev->waiting || (files[dev->fd].read && !dev->respgot)) { +if ( dev->waiting || (file->read && !dev->respgot) ) + {if ((rc =tpmfront_recv (dev, &dummybuf, &dummysz)) !=0 ) { errno =EIO ;return -1 ;@@ -626,5 +630,43 @@ int tpmfront_posix_fstat(int fd, struct stat* buf)return 0 ; } +static int tpmfront_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +shutdown_tpmfront (file->dev); + +return 0 ; +} + +static struct file_ops tpmfront_ops = { + .name ="tpmfront" , + .read = tpmfront_posix_read, + .write = tpmfront_posix_write, + .lseek = lseek_default, + .close = tpmfront_close_fd, + .fstat = tpmfront_posix_fstat, +}; + +int tpmfront_open (struct tpmfront_dev * dev) +{ +struct file *file; +static unsigned int ftype_tpmfront; + +/* Silently prevent multiple opens */ +if (dev->fd !=-1 ) { +return dev->fd; + } + +if ( !ftype_tpmfront ) + ftype_tpmfront =alloc_file_type (&tpmfront_ops); + + dev->fd =alloc_fd (ftype_tpmfront); +printk ("tpmfront_open(%s) -> %d\n" , dev->nodename, dev->fd); + file =get_file_from_fd (dev->fd); + file->dev = dev; + dev->respgot = false; +return dev->fd; +}#endif Signature · 1 line
2.26.2
Signature · 5 lines
Samuel "...very few phenomena can pull someone out of Deep Hack Mode, with two noted exceptions: being struck by lightning, or worse, your *computer* being struck by lightning." (By Matt Welsh)
PATCH v2 6/12 mini-os: use alloc_file_type() and get_file_from_fd() in blkfront
In reply to Juergen GrossAllocate the file type dynamically via alloc_file_type().
Instead of directly accessing the files[] array use get_file_from_fd().
--- blkfront.c | 92 ++++++++++++++++++++++++++++++++++------------ include/blkfront.h | 5 --- include/lib.h | 3 +- lib/sys.c | 24 ------------ 4 files changed, 69 insertions(+), 55 deletions(-)
blkfront.c +68 -24
@@ -59,10 +59,10 @@ void blkfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data) {#ifdef HAVE_LIBC struct blkfront_dev *dev = data; -int fd = dev->fd; +struct file *file =get_file_from_fd (dev->fd); -if (fd !=-1 ) - files[fd].read = true; +if ( file ) + file->read = true;#endif wake_up (&blkfront_queue); }
@@ -483,9 +483,13 @@ int blkfront_aio_poll(struct blkfront_dev *dev) moretodo:#ifdef HAVE_LIBC -if (dev->fd !=-1 ) { - files[dev->fd].read = false; -mb ();/* Make sure to let the handler set read to 1 before we start looking at the ring */ + { +struct file *file =get_file_from_fd (dev->fd); + +if ( file ) { + file->read = false; +mb ();/* Make sure to let the handler set read to 1 before we start looking at the ring */ + } }#endif
@@ -554,22 +558,11 @@ moretodo: }#ifdef HAVE_LIBC -int blkfront_open (struct blkfront_dev *dev) -{ -/* Silently prevent multiple opens */ -if (dev->fd !=-1 ) { -return dev->fd; - } - dev->fd =alloc_fd (FTYPE_BLK ); -printk ("blk_open(%s) -> %d\n" , dev->nodename, dev->fd); - files[dev->fd].dev = dev; -return dev->fd; -} - -int blkfront_posix_rwop (int fd,uint8_t * buf,size_t count,int write) +static int blkfront_posix_rwop (int fd,uint8_t * buf,size_t count,int write) { -struct blkfront_dev * dev = files[fd].dev; -off_t offset = files[fd].offset; +struct file *file =get_file_from_fd (fd); +struct blkfront_dev * dev = file->dev; +off_t offset = file->offset;struct blkfront_aiocb aiocb;unsigned long long disksize = dev->info.sectors * dev->info.sector_size;unsigned int blocksize = dev->info.sector_size;
@@ -711,14 +704,25 @@ int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write) }free (copybuf); - files[fd].offset += rc; + file->offset += rc;return rc; } -int blkfront_posix_fstat (int fd,struct stat * buf) +static int blkfront_posix_read (int fd,void *buf,size_t nbytes) +{ +return blkfront_posix_rwop (fd, buf, nbytes,0 ); +} + +static int blkfront_posix_write (int fd,const void *buf,size_t nbytes) { -struct blkfront_dev * dev = files[fd].dev; +return blkfront_posix_rwop (fd, (void *)buf, nbytes,1 ); +} + +static int blkfront_posix_fstat (int fd,struct stat *buf) +{ +struct file *file =get_file_from_fd (fd); +struct blkfront_dev * dev = file->dev; buf->st_mode = dev->info.mode; buf->st_uid =0 ;
@@ -728,4 +732,44 @@ int blkfront_posix_fstat(int fd, struct stat* buf)return 0 ; } + +static int blkfront_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +shutdown_blkfront (file->dev); + +return 0 ; +} + +static struct file_ops blk_ops = { + .name ="blk" , + .read = blkfront_posix_read, + .write = blkfront_posix_write, + .lseek = lseek_default, + .close = blkfront_close_fd, + .fstat = blkfront_posix_fstat, + .select_rd = select_read_flag, +}; + +int blkfront_open (struct blkfront_dev *dev) +{ +struct file *file; +static unsigned int ftype_blk; + +/* Silently prevent multiple opens */ +if (dev->fd !=-1 ) { +return dev->fd; + } + +if ( !ftype_blk ) + ftype_blk =alloc_file_type (&blk_ops); + + dev->fd =alloc_fd (ftype_blk); +printk ("blk_open(%s) -> %d\n" , dev->nodename, dev->fd); + file =get_file_from_fd (dev->fd); + file->dev = dev; + +return dev->fd; +}#endif
include/blkfront.h +0 -5
@@ -28,17 +28,12 @@ struct blkfront_info };struct blkfront_dev *init_blkfront (char *nodename,struct blkfront_info *info);#ifdef HAVE_LIBC -#include <sys/stat.h> /* POSIX IO functions: * use blkfront_open() to get a file descriptor to the block device * Don't use the other blkfront posix functions here directly, instead use * read(), write(), lseek() and fstat() on the file descriptor */ int blkfront_open (struct blkfront_dev *dev); -int blkfront_posix_rwop (int fd,uint8_t * buf,size_t count,int write); -#define blkfront_posix_write (fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 1) -#define blkfront_posix_read (fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 0) -int blkfront_posix_fstat (int fd,struct stat * buf);#endif void blkfront_aio (struct blkfront_aiocb *aiocbp,int write);#define blkfront_aio_read (aiocbp) blkfront_aio(aiocbp, 0)
include/lib.h +1 -2
@@ -164,8 +164,7 @@ void sanity_check(void);#define FTYPE_FB 6#define FTYPE_KBD 7#define FTYPE_TAP 8 -#define FTYPE_BLK 9 -#define FTYPE_N 10 +#define FTYPE_N 9#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
lib/sys.c +0 -24
@@ -337,11 +337,6 @@ int read(int fd, void *buf, size_t nbytes) }return ret *sizeof (union xenfb_in_event ); } -#endif -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : { -return blkfront_posix_read (fd, buf, nbytes); - }#endif default :break ;
@@ -379,10 +374,6 @@ int write(int fd, const void *buf, size_t nbytes)case FTYPE_TAP :netfront_xmit (files[fd].dev, (void *) buf, nbytes);return nbytes; -#endif -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -return blkfront_posix_write (fd, buf, nbytes);#endif default :break ;
@@ -432,10 +423,6 @@ off_t lseek(int fd, off_t offset, int whence)return ops->lseek (fd, offset, whence);switch (files[fd].type) { -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -break ; -#endif case FTYPE_FILE :break ;default :
@@ -477,11 +464,6 @@ int close(int fd)shutdown_netfront (files[fd].dev);break ;#endif -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -shutdown_blkfront (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);
@@ -554,10 +536,6 @@ int fstat(int fd, struct stat *buf) buf->st_ctime =time (NULL );return 0 ; } -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -return blkfront_posix_fstat (fd, buf); -#endif default :break ; }
@@ -663,7 +641,6 @@ static const char *file_types[] = { [FTYPE_CONSOLE] = "console", [FTYPE_SOCKET] = "socket", [FTYPE_TAP] = "net", - [FTYPE_BLK] = "blk", [FTYPE_KBD] = "kbd", [FTYPE_FB] = "fb", };
@@ -841,7 +818,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceFD_CLR (i, exceptfds);break ;case FTYPE_TAP : -case FTYPE_BLK :case FTYPE_KBD :case FTYPE_FB :if (FD_ISSET (i, readfds)) {
Signature · 1 line
2.26.2
Re: [PATCH v2 06/12] mini-os: use alloc_file_type() and get_file_from_fd() in blkfront
Juergen Gross, le mar. 11 janv. 2022 16:12:09 +0100, a ecrit:
Quoted 5 lines
Allocate the file type dynamically via
alloc_file_type().Instead of directly accessing the files[] array use
get_file_from_fd().Signed-off-by Juergen Gross <[email protected]>
modulo the int fd / file * thing,
Quoted 259 lines
--- blkfront.c | 92 ++++++++++++++++++++++++++++++++++------------ include/blkfront.h | 5 --- include/lib.h | 3 +- lib/sys.c | 24 ------------ 4 files changed, 69 insertions(+), 55 deletions(-)blkfront.c +68
-24@@ -59,10 +59,10 @@ void blkfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data) {#ifdef HAVE_LIBC struct blkfront_dev *dev = data; -int fd = dev->fd; +struct file *file =get_file_from_fd (dev->fd); -if (fd !=-1 ) - files[fd].read = true; +if ( file ) + file->read = true;#endif wake_up (&blkfront_queue); }@@ -483,9 +483,13 @@ int blkfront_aio_poll(struct blkfront_dev *dev) moretodo:#ifdef HAVE_LIBC -if (dev->fd !=-1 ) { - files[dev->fd].read = false; -mb ();/* Make sure to let the handler set read to 1 before we start looking at the ring */ + { +struct file *file =get_file_from_fd (dev->fd); + +if ( file ) { + file->read = false; +mb ();/* Make sure to let the handler set read to 1 before we start looking at the ring */ + } }#endif @@ -554,22 +558,11 @@ moretodo: }#ifdef HAVE_LIBC -int blkfront_open (struct blkfront_dev *dev) -{ -/* Silently prevent multiple opens */ -if (dev->fd !=-1 ) { -return dev->fd; - } - dev->fd =alloc_fd (FTYPE_BLK ); -printk ("blk_open(%s) -> %d\n" , dev->nodename, dev->fd); - files[dev->fd].dev = dev; -return dev->fd; -} - -int blkfront_posix_rwop (int fd,uint8_t * buf,size_t count,int write) +static int blkfront_posix_rwop (int fd,uint8_t * buf,size_t count,int write) { -struct blkfront_dev * dev = files[fd].dev; -off_t offset = files[fd].offset; +struct file *file =get_file_from_fd (fd); +struct blkfront_dev * dev = file->dev; +off_t offset = file->offset;struct blkfront_aiocb aiocb;unsigned long long disksize = dev->info.sectors * dev->info.sector_size;unsigned int blocksize = dev->info.sector_size;@@ -711,14 +704,25 @@ int blkfront_posix_rwop(int fd, uint8_t* buf, size_t count, int write) }free (copybuf); - files[fd].offset += rc; + file->offset += rc;return rc; } -int blkfront_posix_fstat (int fd,struct stat * buf) +static int blkfront_posix_read (int fd,void *buf,size_t nbytes) +{ +return blkfront_posix_rwop (fd, buf, nbytes,0 ); +} + +static int blkfront_posix_write (int fd,const void *buf,size_t nbytes) { -struct blkfront_dev * dev = files[fd].dev; +return blkfront_posix_rwop (fd, (void *)buf, nbytes,1 ); +} + +static int blkfront_posix_fstat (int fd,struct stat *buf) +{ +struct file *file =get_file_from_fd (fd); +struct blkfront_dev * dev = file->dev; buf->st_mode = dev->info.mode; buf->st_uid =0 ;@@ -728,4 +732,44 @@ int blkfront_posix_fstat(int fd, struct stat* buf)return 0 ; } + +static int blkfront_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +shutdown_blkfront (file->dev); + +return 0 ; +} + +static struct file_ops blk_ops = { + .name ="blk" , + .read = blkfront_posix_read, + .write = blkfront_posix_write, + .lseek = lseek_default, + .close = blkfront_close_fd, + .fstat = blkfront_posix_fstat, + .select_rd = select_read_flag, +}; + +int blkfront_open (struct blkfront_dev *dev) +{ +struct file *file; +static unsigned int ftype_blk; + +/* Silently prevent multiple opens */ +if (dev->fd !=-1 ) { +return dev->fd; + } + +if ( !ftype_blk ) + ftype_blk =alloc_file_type (&blk_ops); + + dev->fd =alloc_fd (ftype_blk); +printk ("blk_open(%s) -> %d\n" , dev->nodename, dev->fd); + file =get_file_from_fd (dev->fd); + file->dev = dev; + +return dev->fd; +}#endif include/blkfront.h +0
-5@@ -28,17 +28,12 @@ struct blkfront_info };struct blkfront_dev *init_blkfront (char *nodename,struct blkfront_info *info);#ifdef HAVE_LIBC -#include <sys/stat.h> /* POSIX IO functions: * use blkfront_open() to get a file descriptor to the block device * Don't use the other blkfront posix functions here directly, instead use * read(), write(), lseek() and fstat() on the file descriptor */ int blkfront_open (struct blkfront_dev *dev); -int blkfront_posix_rwop (int fd,uint8_t * buf,size_t count,int write); -#define blkfront_posix_write (fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 1) -#define blkfront_posix_read (fd, buf, count) blkfront_posix_rwop(fd, (uint8_t*)buf, count, 0) -int blkfront_posix_fstat (int fd,struct stat * buf);#endif void blkfront_aio (struct blkfront_aiocb *aiocbp,int write);#define blkfront_aio_read (aiocbp) blkfront_aio(aiocbp, 0)include/lib.h +1
-2@@ -164,8 +164,7 @@ void sanity_check(void);#define FTYPE_FB 6#define FTYPE_KBD 7#define FTYPE_TAP 8 -#define FTYPE_BLK 9 -#define FTYPE_N 10 +#define FTYPE_N 9#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);lib/sys.c +0
-24@@ -337,11 +337,6 @@ int read(int fd, void *buf, size_t nbytes) }return ret *sizeof (union xenfb_in_event ); } -#endif -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : { -return blkfront_posix_read (fd, buf, nbytes); - }#endif default :break ;@@ -379,10 +374,6 @@ int write(int fd, const void *buf, size_t nbytes)case FTYPE_TAP :netfront_xmit (files[fd].dev, (void *) buf, nbytes);return nbytes; -#endif -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -return blkfront_posix_write (fd, buf, nbytes);#endif default :break ;@@ -432,10 +423,6 @@ off_t lseek(int fd, off_t offset, int whence)return ops->lseek (fd, offset, whence);switch (files[fd].type) { -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -break ; -#endif case FTYPE_FILE :break ;default :@@ -477,11 +464,6 @@ int close(int fd)shutdown_netfront (files[fd].dev);break ;#endif -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -shutdown_blkfront (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);@@ -554,10 +536,6 @@ int fstat(int fd, struct stat *buf) buf->st_ctime =time (NULL );return 0 ; } -#ifdef CONFIG_BLKFRONT -case FTYPE_BLK : -return blkfront_posix_fstat (fd, buf); -#endif default :break ; }@@ -663,7 +641,6 @@ static const char *file_types[] = { [FTYPE_CONSOLE] = "console", [FTYPE_SOCKET] = "socket", [FTYPE_TAP] = "net", - [FTYPE_BLK] = "blk", [FTYPE_KBD] = "kbd", [FTYPE_FB] = "fb", };@@ -841,7 +818,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceFD_CLR (i, exceptfds);break ;case FTYPE_TAP : -case FTYPE_BLK :case FTYPE_KBD :case FTYPE_FB :if (FD_ISSET (i, readfds)) {Signature · 1 line
2.26.2
Signature · 4 lines
Samuel gawk; talk; nice; date; wine; grep; touch; unzip; strip; \ touch; gasp; finger; gasp; lyx; gasp; latex; mount; fsck; \ more; yes; gasp; umount; make clean; make mrproper; sleep
PATCH v2 7/12 mini-os: use get_file_from_fd() in netfront
In reply to Juergen GrossInstead of directly accessing the files[] array use get_file_from_fd().
--- include/lib.h | 3 +-- lib/sys.c | 23 ------------------ netfront.c | 64 ++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 33 deletions(-)
include/lib.h +1 -2
@@ -163,8 +163,7 @@ void sanity_check(void);#define FTYPE_SAVEFILE 5#define FTYPE_FB 6#define FTYPE_KBD 7 -#define FTYPE_TAP 8 -#define FTYPE_N 9 +#define FTYPE_N 8#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
lib/sys.c +0 -23
@@ -303,17 +303,6 @@ int read(int fd, void *buf, size_t nbytes)case FTYPE_SOCKET :return lwip_read (files[fd].fd, buf, nbytes);#endif -#ifdef CONFIG_NETFRONT -case FTYPE_TAP : { -ssize_t ret; - ret =netfront_receive (files[fd].dev, buf, nbytes); -if (ret <=0 ) { - errno =EAGAIN ; -return -1 ; - } -return ret; - } -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD : {int ret, n;
@@ -369,11 +358,6 @@ int write(int fd, const void *buf, size_t nbytes)#ifdef HAVE_LWIP case FTYPE_SOCKET :return lwip_write (files[fd].fd, (void *) buf, nbytes); -#endif -#ifdef CONFIG_NETFRONT -case FTYPE_TAP : -netfront_xmit (files[fd].dev, (void *) buf, nbytes); -return nbytes;#endif default :break ;
@@ -459,11 +443,6 @@ int close(int fd) res =lwip_close (files[fd].fd);break ;#endif -#ifdef CONFIG_NETFRONT -case FTYPE_TAP : -shutdown_netfront (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);
@@ -640,7 +619,6 @@ static const char *file_types[] = { [FTYPE_NONE] = "none", [FTYPE_CONSOLE] = "console", [FTYPE_SOCKET] = "socket", - [FTYPE_TAP] = "net", [FTYPE_KBD] = "kbd", [FTYPE_FB] = "fb", };
@@ -817,7 +795,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exce n++;FD_CLR (i, exceptfds);break ; -case FTYPE_TAP :case FTYPE_KBD :case FTYPE_FB :if (FD_ISSET (i, readfds)) {
netfront.c +56 -8
103 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH v2 07/12] mini-os: use get_file_from_fd() in netfront
Juergen Gross, le mar. 11 janv. 2022 16:12:10 +0100, a ecrit:
Quoted 3 lines
Instead of directly accessing the files[] array use
get_file_from_fd().Signed-off-by Juergen Gross <[email protected]>
modulo the int fd / file * thing,
Quoted 197 lines
--- include/lib.h | 3 +-- lib/sys.c | 23 ------------------ netfront.c | 64 ++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 33 deletions(-)include/lib.h +1
-2@@ -163,8 +163,7 @@ void sanity_check(void);#define FTYPE_SAVEFILE 5#define FTYPE_FB 6#define FTYPE_KBD 7 -#define FTYPE_TAP 8 -#define FTYPE_N 9 +#define FTYPE_N 8#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);lib/sys.c +0
-23@@ -303,17 +303,6 @@ int read(int fd, void *buf, size_t nbytes)case FTYPE_SOCKET :return lwip_read (files[fd].fd, buf, nbytes);#endif -#ifdef CONFIG_NETFRONT -case FTYPE_TAP : { -ssize_t ret; - ret =netfront_receive (files[fd].dev, buf, nbytes); -if (ret <=0 ) { - errno =EAGAIN ; -return -1 ; - } -return ret; - } -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD : {int ret, n;@@ -369,11 +358,6 @@ int write(int fd, const void *buf, size_t nbytes)#ifdef HAVE_LWIP case FTYPE_SOCKET :return lwip_write (files[fd].fd, (void *) buf, nbytes); -#endif -#ifdef CONFIG_NETFRONT -case FTYPE_TAP : -netfront_xmit (files[fd].dev, (void *) buf, nbytes); -return nbytes;#endif default :break ;@@ -459,11 +443,6 @@ int close(int fd) res =lwip_close (files[fd].fd);break ;#endif -#ifdef CONFIG_NETFRONT -case FTYPE_TAP : -shutdown_netfront (files[fd].dev); -break ; -#endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD :shutdown_kbdfront (files[fd].dev);@@ -640,7 +619,6 @@ static const char *file_types[] = { [FTYPE_NONE] = "none", [FTYPE_CONSOLE] = "console", [FTYPE_SOCKET] = "socket", - [FTYPE_TAP] = "net", [FTYPE_KBD] = "kbd", [FTYPE_FB] = "fb", };@@ -817,7 +795,6 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exce n++;FD_CLR (i, exceptfds);break ; -case FTYPE_TAP :case FTYPE_KBD :case FTYPE_FB :if (FD_ISSET (i, readfds)) {netfront.c +56
-8@@ -248,14 +248,14 @@ void netfront_select_handler(evtchn_port_t port, struct pt_regs *regs, void *dat {int flags;struct netfront_dev *dev = data; -int fd = dev->fd; +struct file *file =get_file_from_fd (dev->fd);local_irq_save (flags);network_tx_buf_gc (dev);local_irq_restore (flags); -if (fd !=-1 ) - files[fd].read = true; +if ( file ) + file->read = true;wake_up (&netfront_queue); }#endif @@ -565,8 +565,54 @@ error: }#ifdef HAVE_LIBC +static int netfront_read (int fd,void *buf,size_t nbytes) +{ +ssize_t ret; +struct file *file =get_file_from_fd (fd); + + ret =netfront_receive (file->dev, buf, nbytes); +if ( ret <=0 ) + { + errno =EAGAIN ; +return -1 ; + } + +return ret; +} + +static int netfront_write (int fd,const void *buf,size_t nbytes) +{ +struct file *file =get_file_from_fd (fd); + +netfront_xmit (file->dev, (void *)buf, nbytes); + +return nbytes; +} + +static int netfront_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +shutdown_netfront (file->dev); + +return 0 ; +} + +static struct file_ops netfront_ops = { + .name ="net" , + .read = netfront_read, + .write = netfront_write, + .close = netfront_close_fd, + .select_rd = select_read_flag, +}; +int netfront_tap_open (char *nodename) {struct netfront_dev *dev; +struct file *file; +static unsigned int ftype_netfront; + +if ( !ftype_netfront ) + ftype_netfront =alloc_file_type (&netfront_ops); dev =init_netfront (nodename,NETIF_SELECT_RX ,NULL ,NULL );if (!dev) {@@ -574,9 +620,10 @@ int netfront_tap_open(char *nodename) { errno =EIO ; return -1; } - dev->fd =alloc_fd (FTYPE_TAP ); + dev->fd =alloc_fd (ftype_netfront);printk ("tap_open(%s) -> %d\n" , nodename, dev->fd); - files[dev->fd].dev = dev; + file =get_file_from_fd (dev->fd); + file->dev = dev;return dev->fd; }#endif @@ -772,7 +819,8 @@ void netfront_xmit(struct netfront_dev *dev, unsigned char* data,int len)ssize_t netfront_receive (struct netfront_dev *dev,unsigned char *data,size_t len) {unsigned long flags; -int fd = dev->fd; +struct file *file =get_file_from_fd (dev->fd); +ASSERT (current == main_thread); dev->rlen =0 ;@@ -781,9 +829,9 @@ ssize_t netfront_receive(struct netfront_dev *dev, unsigned char *data, size_t llocal_irq_save (flags);network_rx (dev); -if (!dev->rlen && fd !=-1 ) +if ( !dev->rlen && file )/* No data for us, make select stop returning */ - files[fd].read = false; + file->read = false;/* Before re-enabling the interrupts, in case a packet just arrived in the * meanwhile. */ local_irq_restore (flags);Signature · 1 line
2.26.2
Signature · 9 lines
Samuel /* * [...] Note that 120 sec is defined in the protocol as the maximum * possible RTT. I guess we'll have to use something other than TCP * to talk to the University of Mars. * PAWS allows us longer timeouts and large windows, so once implemented * ftp to mars will work nicely. */ (from /usr/src/linux/net/inet/tcp.c, concerning RTT [retransmission timeout])
PATCH v2 8/12 mini-os: use alloc_file_type() and get_file_from_fd() in fbfront
In reply to Juergen GrossAllocate file types dynamically via alloc_file_type().
Instead of directly accessing the files[] array use get_file_from_fd().
--- fbfront.c | 125 ++++++++++++++++++++++++++++++++++++++++++-------- include/lib.h | 4 +- lib/sys.c | 47 ------------------- 3 files changed, 106 insertions(+), 70 deletions(-)
fbfront.c +105 -20
@@ -14,6 +14,9 @@#include <mini-os/xmalloc.h> #include <mini-os/fbfront.h> #include <mini-os/lib.h> +#ifdef HAVE_LIBC +#include <errno.h> +#endif DECLARE_WAIT_QUEUE_HEAD (kbdfront_queue);
@@ -42,10 +45,10 @@ void kbdfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data) {#ifdef HAVE_LIBC struct kbdfront_dev *dev = data; -int fd = dev->fd; +struct file *file =get_file_from_fd (dev->fd); -if (fd !=-1 ) - files[fd].read = true; +if ( file ) + file->read = true;#endif wake_up (&kbdfront_queue); }
@@ -204,10 +207,12 @@ int kbdfront_receive(struct kbdfront_dev *dev, union xenkbd_in_event *buf, int nstruct xenkbd_page *page = dev->page;uint32_t prod, cons;int i; -#ifdef HAVE_LIBC -if (dev->fd !=-1 ) { - files[dev->fd].read = false; +struct file *file =get_file_from_fd (dev->fd); + +if ( file ) + { + file->read = false;mb ();/* Make sure to let the handler set read to 1 before we start looking at the ring */ }#endif
@@ -227,9 +232,9 @@ int kbdfront_receive(struct kbdfront_dev *dev, union xenkbd_in_event *buf, int nnotify_remote_via_evtchn (dev->evtchn);#ifdef HAVE_LIBC -if (cons != prod && dev->fd !=-1 ) +if ( cons != prod && file )/* still some events to read */ - files[dev->fd].read = true; + file->read = true;#endif return i;
@@ -298,11 +303,50 @@ close_kbdfront: }#ifdef HAVE_LIBC +static int kbd_read (int fd,void *buf,size_t nbytes) +{ +struct file *file =get_file_from_fd (fd); +int ret, n; + + n = nbytes /sizeof (union xenkbd_in_event ); + ret =kbdfront_receive (file->dev, buf, n); +if ( ret <=0 ) + { + errno =EAGAIN ; +return -1 ; + } + +return ret *sizeof (union xenkbd_in_event ); +} + +static int kbd_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +shutdown_kbdfront (file->dev); + +return 0 ; +} + +static struct file_ops kbd_ops = { + .name ="kbd" , + .read = kbd_read, + .close = kbd_close_fd, + .select_rd = select_read_flag, +}; +int kbdfront_open (struct kbdfront_dev *dev) { - dev->fd =alloc_fd (FTYPE_KBD ); +struct file *file; +static unsigned int ftype_kbd; + +if ( !ftype_kbd ) + ftype_kbd =alloc_file_type (&kbd_ops); + + dev->fd =alloc_fd (ftype_kbd);printk ("kbd_open(%s) -> %d\n" , dev->nodename, dev->fd); - files[dev->fd].dev = dev; + file =get_file_from_fd (dev->fd); + file->dev = dev;return dev->fd; }#endif
@@ -346,10 +390,10 @@ void fbfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data) {#ifdef HAVE_LIBC struct fbfront_dev *dev = data; -int fd = dev->fd; +struct file *file =get_file_from_fd (dev->fd); -if (fd !=-1 ) - files[fd].read = true; +if ( file ) + file->read = true;#endif wake_up (&fbfront_queue); }
@@ -373,10 +417,12 @@ int fbfront_receive(struct fbfront_dev *dev, union xenfb_in_event *buf, int n)struct xenfb_page *page = dev->page;uint32_t prod, cons;int i; -#ifdef HAVE_LIBC -if (dev->fd !=-1 ) { - files[dev->fd].read = false; +struct file *file =get_file_from_fd (dev->fd); + +if ( file ) + { + file->read = false;mb ();/* Make sure to let the handler set read to 1 before we start looking at the ring */ }#endif
@@ -396,9 +442,9 @@ int fbfront_receive(struct fbfront_dev *dev, union xenfb_in_event *buf, int n)notify_remote_via_evtchn (dev->evtchn);#ifdef HAVE_LIBC -if (cons != prod && dev->fd !=-1 ) +if ( cons != prod && file )/* still some events to read */ - files[dev->fd].read = true; + file->read = true;#endif return i;
@@ -699,11 +745,50 @@ close_fbfront: }#ifdef HAVE_LIBC +static int fbfront_read (int fd,void *buf,size_t nbytes) +{ +struct file *file =get_file_from_fd (fd); +int ret, n; + + n = nbytes /sizeof (union xenfb_in_event ); + ret =fbfront_receive (file->dev, buf, n); +if ( ret <=0 ) + { + errno =EAGAIN ; +return -1 ; + } + +return ret *sizeof (union xenfb_in_event ); +} + +static int fbfront_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +shutdown_fbfront (file->dev); + +return 0 ; +} + +static struct file_ops fb_ops = { + .name =".fb" , + .read = fbfront_read, + .close = fbfront_close_fd, + .select_rd = select_read_flag, +}; +int fbfront_open (struct fbfront_dev *dev) { - dev->fd =alloc_fd (FTYPE_FB ); +struct file *file; +static unsigned int ftype_fb; + +if ( !ftype_fb ) + ftype_fb =alloc_file_type (&fb_ops); + + dev->fd =alloc_fd (ftype_fb);printk ("fb_open(%s) -> %d\n" , dev->nodename, dev->fd); - files[dev->fd].dev = dev; + file =get_file_from_fd (dev->fd); + file->dev = dev;return dev->fd; }#endif
include/lib.h +1 -3
@@ -161,9 +161,7 @@ void sanity_check(void);#define FTYPE_SOCKET 3#define FTYPE_MEM 4#define FTYPE_SAVEFILE 5 -#define FTYPE_FB 6 -#define FTYPE_KBD 7 -#define FTYPE_N 8 +#define FTYPE_N 6#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
lib/sys.c +0 -47
71 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH v2 08/12] mini-os: use alloc_file_type() and get_file_from_fd() in fbfront
Juergen Gross, le mar. 11 janv. 2022 16:12:11 +0100, a ecrit:
fbfront.c close_fbfront: · line 774
Quoted 2 lines
fbfront.c +2
-0+static struct file_ops fb_ops = { + .name =".fb" ,
Why a dot?
Apart from that, and modulo the int fd / file * thing,
Re: [PATCH v2 08/12] mini-os: use alloc_file_type() and get_file_from_fd() in fbfront
Quoted 5 lines · Samuel Thibault
Juergen Gross, le mar. 11 janv. 2022 16:12:11 +0100, a ecrit:
fbfront.c close_fbfront: · line 774
Quoted 2 lines
fbfront.c +2
-0+static struct file_ops fb_ops = { + .name =".fb" ,Why a dot?
I had spare one lying around... ;-)
Thanks for noticing!
Juergen
PATCH v2 9/12 mini-os: use file_ops and get_file_from_fd() for console
In reply to Juergen GrossAdd struct file_ops for the console related file types (FTYPE_CONSOLE and savefile). FTYPE_CONSOLE remains statically defined, as it is used to statically init stdin, stdout and stderr.
Instead of directly accessing the files[] array use get_file_from_fd().
With CONSOLE now handled via file_ops the bogus file descriptor case in select_poll() now needs to be handled more explicit instead of dropping into console handling, assuming that this case was basically meant to cover SAVEFILE.
--- console/xenbus.c | 125 +++++++++++++++++++++++++++++++++++++++++ console/xencons_ring.c | 6 +- include/console.h | 5 ++ include/lib.h | 3 +- lib/sys.c | 87 +++++++--------------------- 5 files changed, 155 insertions(+), 71 deletions(-)
console/xenbus.c +125 -0
@@ -192,3 +192,128 @@ void fini_consfront(struct consfront_dev *dev) {if (dev)free_consfront (dev); } + +#ifdef HAVE_LIBC +static int consfront_read (int fd,void *buf,size_t nbytes) +{ +int ret; +struct file *file =get_file_from_fd (fd); +DEFINE_WAIT (w); + +while (1 ) + { +add_waiter (w, console_queue); + ret =xencons_ring_recv (file->dev, buf, nbytes); +if ( ret ) +break ; +schedule (); + } + +remove_waiter (w, console_queue); + +return ret; +} + +static int savefile_write (int fd,const void *buf,size_t nbytes) +{ +int ret =0 , tot = nbytes; +struct file *file =get_file_from_fd (fd); + +while ( nbytes >0 ) + { + ret =xencons_ring_send (file->dev, (char *)buf, nbytes); + nbytes -= ret; + buf = (char *)buf + ret; + } + +return tot - nbytes; +} + +static int console_write (int fd,const void *buf,size_t nbytes) +{ +struct file *file =get_file_from_fd (fd); + +console_print (file->dev, (char *)buf, nbytes); + +return nbytes; +} + +static int consfront_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +fini_consfront (file->dev); + +return 0 ; +} + +static int consfront_fstat (int fd,struct stat *buf) +{ +struct file *file =get_file_from_fd (fd); + + buf->st_mode =S_IRUSR |S_IWUSR ; + buf->st_mode |= (file->type ==FTYPE_CONSOLE ) ?S_IFCHR :S_IFREG ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +} + +static bool consfront_select_rd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +return xencons_ring_avail (file->dev); +} + +static struct file_ops savefile_ops = { + .name ="savefile" , + .read = consfront_read, + .write = savefile_write, + .close = consfront_close_fd, + .fstat = consfront_fstat, + .select_rd = consfront_select_rd, + .select_wr = select_yes, +}; + +struct file_ops console_ops = { + .name ="console" , + .read = consfront_read, + .write = console_write, + .close = consfront_close_fd, + .fstat = consfront_fstat, + .select_rd = consfront_select_rd, + .select_wr = select_yes, +}; + +int open_consfront (char *nodename) +{ +struct consfront_dev *dev; +static unsigned int ftype_savefile; +unsigned int ftype; +struct file *file; + + dev =init_consfront (nodename); +if ( !dev ) +return -1 ; + +if ( nodename ) + { +if ( !ftype_savefile ) + ftype_savefile =alloc_file_type (&savefile_ops); + ftype = ftype_savefile; + } +else + ftype =FTYPE_CONSOLE ; + + dev->fd =alloc_fd (ftype); + file =get_file_from_fd (dev->fd); +if ( !file ) + { +fini_consfront (dev); +return -1 ; + } + file->dev = dev; + +return dev->fd; +} +#endif
console/xencons_ring.c +3 -3
@@ -99,10 +99,10 @@ void console_handle_input(evtchn_port_t port, struct pt_regs *regs, void *data) {struct consfront_dev *dev = (struct consfront_dev *) data;#ifdef HAVE_LIBC -int fd = dev ? dev->fd :-1 ; +struct file *file = dev ?get_file_from_fd (dev->fd) :NULL ; -if (fd !=-1 ) - files[fd].read = true; +if ( file ) + file->read = true;wake_up (&console_queue);#else
include/console.h +5 -0
@@ -39,6 +39,7 @@#include <mini-os/os.h> #include <mini-os/traps.h> #include <mini-os/types.h> +#include <mini-os/lib.h> #include <xen/grant_table.h> #include <xenbus.h> #include <xen/io/console.h>
@@ -93,5 +94,9 @@ int xencons_ring_send_no_notify(struct consfront_dev *dev, const char *data, unsint xencons_ring_avail (struct consfront_dev *dev);int xencons_ring_recv (struct consfront_dev *dev,char *data,unsigned len);void free_consfront (struct consfront_dev *dev); +#ifdef HAVE_LIBC +extern struct file_ops console_ops; +int open_consfront (char *nodename); +#endif #endif /* _LIB_CONSOLE_H_ */
include/lib.h +1 -2
@@ -160,8 +160,7 @@ void sanity_check(void);#define FTYPE_FILE 2#define FTYPE_SOCKET 3#define FTYPE_MEM 4 -#define FTYPE_SAVEFILE 5 -#define FTYPE_N 6 +#define FTYPE_N 5#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);
lib/sys.c +21 -66
151 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH v2 09/12] mini-os: use file_ops and get_file_from_fd() for console
Juergen Gross, le mar. 11 janv. 2022 16:12:12 +0100, a ecrit:
console/xenbus.c void fini_consfront(struct consfront_dev *dev) · line 259
Quoted 10 lines
console/xenbus.c +10
-0+static int consfront_fstat (int fd,struct stat *buf) +{ +struct file *file =get_file_from_fd (fd); + + buf->st_mode =S_IRUSR |S_IWUSR ; + buf->st_mode |= (file->type ==FTYPE_CONSOLE ) ?S_IFCHR :S_IFREG ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +}
This seems to be missing filling st_uid, st_gid, etc.?
Samuel
Re: [PATCH v2 09/12] mini-os: use file_ops and get_file_from_fd() for console
Quoted 13 lines · Samuel Thibault
Juergen Gross, le mar. 11 janv. 2022 16:12:12 +0100, a ecrit:
console/xenbus.c void fini_consfront(struct consfront_dev *dev) · line 259
Quoted 10 lines
console/xenbus.c +10
-0+static int consfront_fstat (int fd,struct stat *buf) +{ +struct file *file =get_file_from_fd (fd); + + buf->st_mode =S_IRUSR |S_IWUSR ; + buf->st_mode |= (file->type ==FTYPE_CONSOLE ) ?S_IFCHR :S_IFREG ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +}This seems to be missing filling
st_uid,st_gid, etc.?
Not really. Those are set to zero via the call of init_stat() in fstat().
Juergen
Re: [PATCH v2 09/12] mini-os: use file_ops and get_file_from_fd() for console
Juergen Gross, le mer. 12 janv. 2022 08:57:07 +0100, a ecrit:
Quoted 15 lines · Samuel Thibault
Quoted 13 lines
Juergen Gross, le mar. 11 janv. 2022 16:12:12 +0100, a ecrit:
console/xenbus.c void fini_consfront(struct consfront_dev *dev) · line 259
Quoted 10 lines
console/xenbus.c +10
-0+static int consfront_fstat (int fd,struct stat *buf) +{ +struct file *file =get_file_from_fd (fd); + + buf->st_mode =S_IRUSR |S_IWUSR ; + buf->st_mode |= (file->type ==FTYPE_CONSOLE ) ?S_IFCHR :S_IFREG ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +}This seems to be missing filling
st_uid,st_gid, etc.?
Quoted 2 lines
Not really. Those are set to zero via the call of
init_stat()infstat().
Ah, I missed that call indeed.
Samuel
Re: [PATCH v2 09/12] mini-os: use file_ops and get_file_from_fd() for console
In reply to Juergen GrossJuergen Gross, le mar. 11 janv. 2022 16:12:12 +0100, a ecrit:
Quoted 4 lines
Add struct
file_opsfor the console related file types (FTYPE_CONSOLEand savefile).FTYPE_CONSOLEremains statically defined, as it is used to statically init stdin, stdout and stderr.
Quoted 8 lines
Instead of directly accessing the files[] array use
get_file_from_fd().With CONSOLE now handled via
file_opsthe bogus file descriptor case inselect_poll()now needs to be handled more explicit instead of dropping into console handling, assuming that this case was basically meant to cover SAVEFILE.Signed-off-by Juergen Gross <[email protected]>
modulo the int fd / file * thing,
Quoted 361 lines
--- console/xenbus.c | 125 +++++++++++++++++++++++++++++++++++++++++ console/xencons_ring.c | 6 +- include/console.h | 5 ++ include/lib.h | 3 +- lib/sys.c | 87 +++++++--------------------- 5 files changed, 155 insertions(+), 71 deletions(-)console/xenbus.c +125
-0@@ -192,3 +192,128 @@ void fini_consfront(struct consfront_dev *dev) {if (dev)free_consfront (dev); } + +#ifdef HAVE_LIBC +static int consfront_read (int fd,void *buf,size_t nbytes) +{ +int ret; +struct file *file =get_file_from_fd (fd); +DEFINE_WAIT (w); + +while (1 ) + { +add_waiter (w, console_queue); + ret =xencons_ring_recv (file->dev, buf, nbytes); +if ( ret ) +break ; +schedule (); + } + +remove_waiter (w, console_queue); + +return ret; +} + +static int savefile_write (int fd,const void *buf,size_t nbytes) +{ +int ret =0 , tot = nbytes; +struct file *file =get_file_from_fd (fd); + +while ( nbytes >0 ) + { + ret =xencons_ring_send (file->dev, (char *)buf, nbytes); + nbytes -= ret; + buf = (char *)buf + ret; + } + +return tot - nbytes; +} + +static int console_write (int fd,const void *buf,size_t nbytes) +{ +struct file *file =get_file_from_fd (fd); + +console_print (file->dev, (char *)buf, nbytes); + +return nbytes; +} + +static int consfront_close_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +fini_consfront (file->dev); + +return 0 ; +} + +static int consfront_fstat (int fd,struct stat *buf) +{ +struct file *file =get_file_from_fd (fd); + + buf->st_mode =S_IRUSR |S_IWUSR ; + buf->st_mode |= (file->type ==FTYPE_CONSOLE ) ?S_IFCHR :S_IFREG ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +} + +static bool consfront_select_rd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +return xencons_ring_avail (file->dev); +} + +static struct file_ops savefile_ops = { + .name ="savefile" , + .read = consfront_read, + .write = savefile_write, + .close = consfront_close_fd, + .fstat = consfront_fstat, + .select_rd = consfront_select_rd, + .select_wr = select_yes, +}; + +struct file_ops console_ops = { + .name ="console" , + .read = consfront_read, + .write = console_write, + .close = consfront_close_fd, + .fstat = consfront_fstat, + .select_rd = consfront_select_rd, + .select_wr = select_yes, +}; + +int open_consfront (char *nodename) +{ +struct consfront_dev *dev; +static unsigned int ftype_savefile; +unsigned int ftype; +struct file *file; + + dev =init_consfront (nodename); +if ( !dev ) +return -1 ; + +if ( nodename ) + { +if ( !ftype_savefile ) + ftype_savefile =alloc_file_type (&savefile_ops); + ftype = ftype_savefile; + } +else + ftype =FTYPE_CONSOLE ; + + dev->fd =alloc_fd (ftype); + file =get_file_from_fd (dev->fd); +if ( !file ) + { +fini_consfront (dev); +return -1 ; + } + file->dev = dev; + +return dev->fd; +} +#endif console/xencons_ring.c +3
-3@@ -99,10 +99,10 @@ void console_handle_input(evtchn_port_t port, struct pt_regs *regs, void *data) {struct consfront_dev *dev = (struct consfront_dev *) data;#ifdef HAVE_LIBC -int fd = dev ? dev->fd :-1 ; +struct file *file = dev ?get_file_from_fd (dev->fd) :NULL ; -if (fd !=-1 ) - files[fd].read = true; +if ( file ) + file->read = true;wake_up (&console_queue);#else include/console.h +5
-0@@ -39,6 +39,7 @@#include <mini-os/os.h> #include <mini-os/traps.h> #include <mini-os/types.h> +#include <mini-os/lib.h> #include <xen/grant_table.h> #include <xenbus.h> #include <xen/io/console.h> @@ -93,5 +94,9 @@ int xencons_ring_send_no_notify(struct consfront_dev *dev, const char *data, unsint xencons_ring_avail (struct consfront_dev *dev);int xencons_ring_recv (struct consfront_dev *dev,char *data,unsigned len);void free_consfront (struct consfront_dev *dev); +#ifdef HAVE_LIBC +extern struct file_ops console_ops; +int open_consfront (char *nodename); +#endif #endif /* _LIB_CONSOLE_H_ */ include/lib.h +1
-2@@ -160,8 +160,7 @@ void sanity_check(void);#define FTYPE_FILE 2#define FTYPE_SOCKET 3#define FTYPE_MEM 4 -#define FTYPE_SAVEFILE 5 -#define FTYPE_N 6 +#define FTYPE_N 5#define FTYPE_SPARE 16typedef int file_read_func (int fd,void *buf,size_t nbytes);lib/sys.c +21
-66@@ -101,6 +101,9 @@ static struct file_ops file_ops_none = {static struct file_ops *file_ops[FTYPE_N +FTYPE_SPARE ] = { [FTYPE_NONE ] = &file_ops_none, +#ifdef CONFIG_CONSFRONT + [FTYPE_CONSOLE ] = &console_ops, +#endif };unsigned int alloc_file_type (struct file_ops *ops)@@ -211,31 +214,26 @@ int mkdir(const char *pathname, mode_t mode)#ifdef CONFIG_CONSFRONT int posix_openpt (int flags) { -struct consfront_dev *dev; +int fd;/* Ignore flags */ + fd =open_consfront (NULL ); +printk ("fd(%d) = posix_openpt\n" , fd); - dev =init_consfront (NULL ); - dev->fd =alloc_fd (FTYPE_CONSOLE ); - files[dev->fd].dev = dev; - -printk ("fd(%d) = posix_openpt\n" , dev->fd); -return (dev->fd); +return fd; }int open_savefile (const char *path,int save) { -struct consfront_dev *dev; +int fd;char nodename[64 ];snprintf (nodename,sizeof (nodename),"device/console/%d" , save ?SAVE_CONSOLE :RESTORE_CONSOLE ); - dev =init_consfront (nodename); - dev->fd =alloc_fd (FTYPE_SAVEFILE ); - files[dev->fd].dev = dev; + fd =open_consfront (nodename); +printk ("fd(%d) = open_savefile\n" , fd); -printk ("fd(%d) = open_savefile\n" , dev->fd); -return (dev->fd); +return fd; }#else int posix_openpt (int flags)@@ -285,20 +283,6 @@ int read(int fd, void *buf, size_t nbytes)return ops->read (fd, buf, nbytes);switch (files[fd].type) { -case FTYPE_SAVEFILE : -case FTYPE_CONSOLE : { -int ret; -DEFINE_WAIT (w); -while (1 ) { -add_waiter (w, console_queue); - ret =xencons_ring_recv (files[fd].dev, buf, nbytes); -if (ret) -break ; -schedule (); - } -remove_waiter (w, console_queue); -return ret; - }#ifdef HAVE_LWIP case FTYPE_SOCKET :return lwip_read (files[fd].fd, buf, nbytes);@@ -319,18 +303,6 @@ int write(int fd, const void *buf, size_t nbytes)return ops->write (fd, buf, nbytes);switch (files[fd].type) { -case FTYPE_SAVEFILE : { -int ret =0 , tot = nbytes; -while (nbytes >0 ) { - ret =xencons_ring_send (files[fd].dev, (char *)buf, nbytes); - nbytes -= ret; - buf = (char *)buf + ret; - } -return tot - nbytes; - } -case FTYPE_CONSOLE : -console_print (files[fd].dev, (char *)buf, nbytes); -return nbytes;#ifdef HAVE_LWIP case FTYPE_SOCKET :return lwip_write (files[fd].fd, (void *) buf, nbytes);@@ -418,12 +390,6 @@ int close(int fd)case FTYPE_SOCKET : res =lwip_close (files[fd].fd);break ; -#endif -#ifdef CONFIG_CONSFRONT -case FTYPE_SAVEFILE : -case FTYPE_CONSOLE : -fini_consfront (files[fd].dev); -break ;#endif case FTYPE_NONE :printk ("close(%d): Bad descriptor\n" , fd);@@ -464,15 +430,8 @@ int fstat(int fd, struct stat *buf)return ops->fstat (fd, buf);switch (files[fd].type) { -case FTYPE_SAVEFILE : -case FTYPE_CONSOLE :case FTYPE_SOCKET : { -if (files[fd].type ==FTYPE_CONSOLE ) - buf->st_mode =S_IFCHR |S_IRUSR |S_IWUSR ; -else if (files[fd].type ==FTYPE_SOCKET ) - buf->st_mode =S_IFSOCK |S_IRUSR |S_IWUSR ; -else if (files[fd].type ==FTYPE_SAVEFILE ) - buf->st_mode =S_IFREG |S_IRUSR |S_IWUSR ; + buf->st_mode =S_IFSOCK |S_IRUSR |S_IWUSR ; buf->st_uid =0 ; buf->st_gid =0 ; buf->st_size =0 ;@@ -583,7 +542,6 @@ int closedir(DIR *dir)#if defined(LIBC_DEBUG ) || defined(LIBC_VERBOSE )static const char *file_types[] = { [FTYPE_NONE ] ="none" , - [FTYPE_CONSOLE ] ="console" , [FTYPE_SOCKET ] ="socket" , };@@ -744,21 +702,18 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exce }if (FD_ISSET (i, readfds) ||FD_ISSET (i, writefds) ||FD_ISSET (i, exceptfds)) + {printk ("bogus fd %d in select\n" , i); -/* Fallthrough. */ - } - -case FTYPE_CONSOLE : -if (FD_ISSET (i, readfds)) { -if (xencons_ring_avail (files[i].dev)) - n++; -else -FD_CLR (i, readfds); +if (FD_ISSET (i, readfds) ) +FD_CLR (i, readfds); +if (FD_ISSET (i, writefds) ) +FD_CLR (i, writefds); +if (FD_ISSET (i, exceptfds) ) +FD_CLR (i, exceptfds); } -if (FD_ISSET (i, writefds)) - n++; -FD_CLR (i, exceptfds);break ; + } +#ifdef HAVE_LWIP case FTYPE_SOCKET :if (FD_ISSET (i, readfds)) {Signature · 1 line
2.26.2
Signature · 4 lines
Samuel <y> t1 faich <y> les programmes ils segfaultent jamais quand on veut -+- #ens-mim en plein débogage -+-
Re: [PATCH v2 09/12] mini-os: use file_ops and get_file_from_fd() for console
In reply to Juergen GrossQuoted 10 lines · Juergen Gross
console/xenbus.c +10
-0+static int savefile_write (int fd,const void *buf,size_t nbytes) +{ +int ret =0 , tot = nbytes; +struct file *file =get_file_from_fd (fd); + +while ( nbytes >0 ) + { + ret =xencons_ring_send (file->dev, (char *)buf, nbytes); + nbytes -= ret; + buf = (char *)buf + ret;
console/xenbus.c void fini_consfront(struct consfront_dev *dev) · line 236
Quoted 10 lines
console/xenbus.c +10
-0+ } + +return tot - nbytes; +} + +static int console_write (int fd,const void *buf,size_t nbytes) +{ +struct file *file =get_file_from_fd (fd); + +console_print (file->dev, (char *)buf, nbytes);
I've just noticed this while committing the previous series, and I know it is a preexisting bug, but the casts here are utterly unsafe, because they're casting away constness.
console_print() is easy to fix, and just requires a prototype adjustment. That said, it also desperately also needs to fix 'int length' to size_t to avoid problems with negative length VLAs on the stack.
xencons_ring_send() already takes const char *, so I'm pretty sure you can just drop the casts here. It too ought to not truncate size_t bytes to "unsigned".
~Andrew
Re: [PATCH v2 09/12] mini-os: use file_ops and get_file_from_fd() for console
Quoted 26 lines · Andrew Cooper
Quoted 10 lines · Juergen Gross
console/xenbus.c +10
-0+static int savefile_write (int fd,const void *buf,size_t nbytes) +{ +int ret =0 , tot = nbytes; +struct file *file =get_file_from_fd (fd); + +while ( nbytes >0 ) + { + ret =xencons_ring_send (file->dev, (char *)buf, nbytes); + nbytes -= ret; + buf = (char *)buf + ret;console/xenbus.c void fini_consfront(struct consfront_dev *dev) · line 236
Quoted 10 lines
console/xenbus.c +10
-0+ } + +return tot - nbytes; +} + +static int console_write (int fd,const void *buf,size_t nbytes) +{ +struct file *file =get_file_from_fd (fd); + +console_print (file->dev, (char *)buf, nbytes);I've just noticed this while committing the previous series, and I know it is a preexisting bug, but the casts here are utterly unsafe, because they're casting away constness.
Quoted 7 lines
console_print()is easy to fix, and just requires a prototype adjustment. That said, it also desperately also needs to fix 'int length' tosize_tto avoid problems with negative length VLAs on the stack.
xencons_ring_send()already takes const char *, so I'm pretty sure you can just drop the casts here. It too ought to not truncatesize_tbytes to "unsigned".
Okay, will do that.
Juergen
PATCH v2 10/12 mini-os: add struct file_ops for file type socket
In reply to Juergen GrossEven with some special handling needed in select_poll(), add a struct file_ops for FTYPE_SOCKET. Due to the need of the special handling it isn't possible to use a dynamically allocated file type.
Most functions calling the file_ops methods can be simplified a lot now that no file type specific handling is left. Same applies to the file type name printing in debug/verbose mode.
--- lib/sys.c | 153 +++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 83 deletions(-)
lib/sys.c +70 -83
@@ -99,11 +99,70 @@ static struct file_ops file_ops_none = { .name = "none", }; +#ifdef HAVE_LWIP +static int socket_read (int fd,void *buf,size_t nbytes) +{ +return lwip_read (fd, buf, nbytes); +} + +static int socket_write (int fd,const void *buf,size_t nbytes) +{ +return lwip_write (fd, (void *)buf, nbytes); +} + +static int close_socket_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +return lwip_close (file->fd); +} + +static int socket_fstat (int fd,struct stat *buf) +{ + buf->st_mode =S_IFSOCK |S_IRUSR |S_IWUSR ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +} + +static int socket_fcntl (int fd,int cmd,va_list args) +{ +long arg; +struct file *file =get_file_from_fd (fd); + + arg =va_arg (args, long); + +if ( cmd ==F_SETFL && !(arg & ~O_NONBLOCK ) ) + { +/* Only flag supported: non-blocking mode */ +uint32_t nblock = !!(arg &O_NONBLOCK ); + +return lwip_ioctl (file->fd,FIONBIO , &nblock); + } + +printk ("fcntl(%d, %d, %lx/%lo)\n" , fd, cmd, arg, arg); + errno =ENOSYS ; +return -1 ; +} + +static struct file_ops socket_ops = { + .name ="socket" , + .read = socket_read, + .write = socket_write, + .close = close_socket_fd, + .fstat = socket_fstat, + .fcntl = socket_fcntl, +}; +#endif +static struct file_ops *file_ops[FTYPE_N +FTYPE_SPARE ] = { [FTYPE_NONE ] = &file_ops_none, #ifdef CONFIG_CONSFRONT [FTYPE_CONSOLE ] = &console_ops, #endif +#ifdef HAVE_LWIP + [FTYPE_SOCKET ] = &socket_ops, +#endif };unsigned int alloc_file_type (struct file_ops *ops)
@@ -282,14 +341,6 @@ int read(int fd, void *buf, size_t nbytes)if ( ops->read )return ops->read (fd, buf, nbytes); -switch (files[fd].type) { -#ifdef HAVE_LWIP -case FTYPE_SOCKET : -return lwip_read (files[fd].fd, buf, nbytes); -#endif -default : -break ; - }printk ("read(%d): Bad descriptor\n" , fd); errno =EBADF ;return -1 ;
@@ -302,14 +353,6 @@ int write(int fd, const void *buf, size_t nbytes)if ( ops->write )return ops->write (fd, buf, nbytes); -switch (files[fd].type) { -#ifdef HAVE_LWIP -case FTYPE_SOCKET : -return lwip_write (files[fd].fd, (void *) buf, nbytes); -#endif -default : -break ; - }printk ("write(%d): Bad descriptor\n" , fd); errno =EBADF ;return -1 ;
@@ -378,26 +421,14 @@ int close(int fd)printk ("close(%d)\n" , fd);if ( ops->close ) - { res = ops->close (fd); - goto out; - } - -switch (files[fd].type) { -default : -break ; -#ifdef HAVE_LWIP -case FTYPE_SOCKET : - res =lwip_close (files[fd].fd); -break ; -#endif -case FTYPE_NONE : -printk ("close(%d): Bad descriptor\n" , fd); - errno =EBADF ; -return -1 ; +else if ( files[fd].type ==FTYPE_NONE ) + { +printk ("close(%d): Bad descriptor\n" , fd); + errno =EBADF ; +return -1 ; } - out:memset (files + fd,0 ,sizeof (struct file )); files[fd].type =FTYPE_NONE ;return res;
@@ -429,21 +460,6 @@ int fstat(int fd, struct stat *buf)if ( ops->fstat )return ops->fstat (fd, buf); -switch (files[fd].type) { -case FTYPE_SOCKET : { - buf->st_mode =S_IFSOCK |S_IRUSR |S_IWUSR ; - buf->st_uid =0 ; - buf->st_gid =0 ; - buf->st_size =0 ; - buf->st_atime = - buf->st_mtime = - buf->st_ctime =time (NULL ); -return 0 ; - } -default : -break ; - } -printk ("statf(%d): Bad descriptor\n" , fd); errno =EBADF ;return -1 ;
@@ -491,21 +507,9 @@ int fcntl(int fd, int cmd, ...) arg =va_arg (ap, long);va_end (ap); -switch (cmd) { -#ifdef HAVE_LWIP -case F_SETFL : -if (files[fd].type ==FTYPE_SOCKET && !(arg & ~O_NONBLOCK )) { -/* Only flag supported: non-blocking mode */ -uint32_t nblock = !!(arg &O_NONBLOCK ); -return lwip_ioctl (files[fd].fd,FIONBIO , &nblock); - } -/* Fallthrough */ -#endif -default : -printk ("fcntl(%d, %d, %lx/%lo)\n" , fd, cmd, arg, arg); - errno =ENOSYS ; -return -1 ; - } +printk ("fcntl(%d, %d, %lx/%lo)\n" , fd, cmd, arg, arg); + errno =ENOSYS ; +return -1 ; }DIR *opendir (const char *name)
@@ -539,23 +543,6 @@ int closedir(DIR *dir)/* We assume that only the main thread calls select(). */ -#if defined(LIBC_DEBUG ) || defined(LIBC_VERBOSE ) -static const char *file_types[] = { - [FTYPE_NONE ] ="none" , - [FTYPE_SOCKET ] ="socket" , -}; - -static char *get_type_name (unsigned int type) -{ -if ( type <ARRAY_SIZE (file_ops) && file_ops[type] ) -return file_ops[type]->name; - -if ( type <ARRAY_SIZE (file_types) && file_types[type] ) -return file_types[type]; - -return "none" ; -} -#endif #ifdef LIBC_DEBUG static void dump_set (int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout) {
@@ -566,7 +553,7 @@ static void dump_set(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptif (FD_ISSET (i, set)) { \if (comma) \printk (", " ); \ -printk ("%d(%s)" , i,get_type_name (files[i].type)); \ +printk ("%d(%s)" , i,get_file_ops (files[i].type)->name); \ comma =1 ; \ } \ } \
@@ -600,7 +587,7 @@ static void dump_pollfds(struct pollfd *pfd, int nfds, int timeout) fd = pfd[i].fd;if (comma)printk (", " ); -printk ("%d(%s)/%02x" , fd,get_type_name (files[fd].type), +printk ("%d(%s)/%02x" , fd,get_file_ops (files[fd].type)->name, pfd[i].events); comma =1 ; }
@@ -754,7 +741,7 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceprintk ("%d(%d): " , nb, sock_n);for (i =0 ; i < nfds; i++) {if (nbread[i] || nbwrite[i] || nbexcept[i]) -printk (" %d(%c):" , i,get_type_name (files[i].type)); +printk (" %d(%c):" , i,get_file_ops (files[i].type)->name);if (nbread[i])printk (" %dR" , nbread[i]);if (nbwrite[i])
Signature · 1 line
2.26.2
Re: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket
Juergen Gross, le mar. 11 janv. 2022 16:12:13 +0100, a ecrit:
lib/sys.c static struct file_ops file_ops_none = { · line 126
Quoted 7 lines
lib/sys.c +7
-0+static int socket_fstat (int fd,struct stat *buf) +{ + buf->st_mode =S_IFSOCK |S_IRUSR |S_IWUSR ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +}
Similarly, this seems to be missing setting st_uid, st_gid, st_size?
Samuel
Re: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket
In reply to Juergen GrossJuergen Gross, le mar. 11 janv. 2022 16:12:13 +0100, a ecrit:
Quoted 9 lines
Even with some special handling needed in
select_poll(), add a structfile_opsforFTYPE_SOCKET. Due to the need of the special handling it isn't possible to use a dynamically allocated file type.Most functions calling the
file_opsmethods can be simplified a lot now that no file type specific handling is left. Same applies to the file type name printing in debug/verbose mode.Signed-off-by Juergen Gross <[email protected]>
modulo the int fd / file * thing,
Quoted 242 lines
--- lib/sys.c | 153 +++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 83 deletions(-)lib/sys.c +70
-83@@ -99,11 +99,70 @@ static struct file_ops file_ops_none = { .name = "none", }; +#ifdef HAVE_LWIP +static int socket_read (int fd,void *buf,size_t nbytes) +{ +return lwip_read (fd, buf, nbytes); +} + +static int socket_write (int fd,const void *buf,size_t nbytes) +{ +return lwip_write (fd, (void *)buf, nbytes); +} + +static int close_socket_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +return lwip_close (file->fd); +} + +static int socket_fstat (int fd,struct stat *buf) +{ + buf->st_mode =S_IFSOCK |S_IRUSR |S_IWUSR ; + buf->st_atime = buf->st_mtime = buf->st_ctime =time (NULL ); + +return 0 ; +} + +static int socket_fcntl (int fd,int cmd,va_list args) +{ +long arg; +struct file *file =get_file_from_fd (fd); + + arg =va_arg (args, long); + +if ( cmd ==F_SETFL && !(arg & ~O_NONBLOCK ) ) + { +/* Only flag supported: non-blocking mode */ +uint32_t nblock = !!(arg &O_NONBLOCK ); + +return lwip_ioctl (file->fd,FIONBIO , &nblock); + } + +printk ("fcntl(%d, %d, %lx/%lo)\n" , fd, cmd, arg, arg); + errno =ENOSYS ; +return -1 ; +} + +static struct file_ops socket_ops = { + .name ="socket" , + .read = socket_read, + .write = socket_write, + .close = close_socket_fd, + .fstat = socket_fstat, + .fcntl = socket_fcntl, +}; +#endif +static struct file_ops *file_ops[FTYPE_N +FTYPE_SPARE ] = { [FTYPE_NONE ] = &file_ops_none, #ifdef CONFIG_CONSFRONT [FTYPE_CONSOLE ] = &console_ops, #endif +#ifdef HAVE_LWIP + [FTYPE_SOCKET ] = &socket_ops, +#endif };unsigned int alloc_file_type (struct file_ops *ops)@@ -282,14 +341,6 @@ int read(int fd, void *buf, size_t nbytes)if ( ops->read )return ops->read (fd, buf, nbytes); -switch (files[fd].type) { -#ifdef HAVE_LWIP -case FTYPE_SOCKET : -return lwip_read (files[fd].fd, buf, nbytes); -#endif -default : -break ; - }printk ("read(%d): Bad descriptor\n" , fd); errno =EBADF ;return -1 ;@@ -302,14 +353,6 @@ int write(int fd, const void *buf, size_t nbytes)if ( ops->write )return ops->write (fd, buf, nbytes); -switch (files[fd].type) { -#ifdef HAVE_LWIP -case FTYPE_SOCKET : -return lwip_write (files[fd].fd, (void *) buf, nbytes); -#endif -default : -break ; - }printk ("write(%d): Bad descriptor\n" , fd); errno =EBADF ;return -1 ;@@ -378,26 +421,14 @@ int close(int fd)printk ("close(%d)\n" , fd);if ( ops->close ) - { res = ops->close (fd); - goto out; - } - -switch (files[fd].type) { -default : -break ; -#ifdef HAVE_LWIP -case FTYPE_SOCKET : - res =lwip_close (files[fd].fd); -break ; -#endif -case FTYPE_NONE : -printk ("close(%d): Bad descriptor\n" , fd); - errno =EBADF ; -return -1 ; +else if ( files[fd].type ==FTYPE_NONE ) + { +printk ("close(%d): Bad descriptor\n" , fd); + errno =EBADF ; +return -1 ; } - out:memset (files + fd,0 ,sizeof (struct file )); files[fd].type =FTYPE_NONE ;return res;@@ -429,21 +460,6 @@ int fstat(int fd, struct stat *buf)if ( ops->fstat )return ops->fstat (fd, buf); -switch (files[fd].type) { -case FTYPE_SOCKET : { - buf->st_mode =S_IFSOCK |S_IRUSR |S_IWUSR ; - buf->st_uid =0 ; - buf->st_gid =0 ; - buf->st_size =0 ; - buf->st_atime = - buf->st_mtime = - buf->st_ctime =time (NULL ); -return 0 ; - } -default : -break ; - } -printk ("statf(%d): Bad descriptor\n" , fd); errno =EBADF ;return -1 ;@@ -491,21 +507,9 @@ int fcntl(int fd, int cmd, ...) arg =va_arg (ap, long);va_end (ap); -switch (cmd) { -#ifdef HAVE_LWIP -case F_SETFL : -if (files[fd].type ==FTYPE_SOCKET && !(arg & ~O_NONBLOCK )) { -/* Only flag supported: non-blocking mode */ -uint32_t nblock = !!(arg &O_NONBLOCK ); -return lwip_ioctl (files[fd].fd,FIONBIO , &nblock); - } -/* Fallthrough */ -#endif -default : -printk ("fcntl(%d, %d, %lx/%lo)\n" , fd, cmd, arg, arg); - errno =ENOSYS ; -return -1 ; - } +printk ("fcntl(%d, %d, %lx/%lo)\n" , fd, cmd, arg, arg); + errno =ENOSYS ; +return -1 ; }DIR *opendir (const char *name)@@ -539,23 +543,6 @@ int closedir(DIR *dir)/* We assume that only the main thread calls select(). */ -#if defined(LIBC_DEBUG ) || defined(LIBC_VERBOSE ) -static const char *file_types[] = { - [FTYPE_NONE ] ="none" , - [FTYPE_SOCKET ] ="socket" , -}; - -static char *get_type_name (unsigned int type) -{ -if ( type <ARRAY_SIZE (file_ops) && file_ops[type] ) -return file_ops[type]->name; - -if ( type <ARRAY_SIZE (file_types) && file_types[type] ) -return file_types[type]; - -return "none" ; -} -#endif #ifdef LIBC_DEBUG static void dump_set (int nfds,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout) {@@ -566,7 +553,7 @@ static void dump_set(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptif (FD_ISSET (i, set)) { \if (comma) \printk (", " ); \ -printk ("%d(%s)" , i,get_type_name (files[i].type)); \ +printk ("%d(%s)" , i,get_file_ops (files[i].type)->name); \ comma =1 ; \ } \ } \@@ -600,7 +587,7 @@ static void dump_pollfds(struct pollfd *pfd, int nfds, int timeout) fd = pfd[i].fd;if (comma)printk (", " ); -printk ("%d(%s)/%02x" , fd,get_type_name (files[fd].type), +printk ("%d(%s)/%02x" , fd,get_file_ops (files[fd].type)->name, pfd[i].events); comma =1 ; }@@ -754,7 +741,7 @@ static int select_poll(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceprintk ("%d(%d): " , nb, sock_n);for (i =0 ; i < nfds; i++) {if (nbread[i] || nbwrite[i] || nbexcept[i]) -printk (" %d(%c):" , i,get_type_name (files[i].type)); +printk (" %d(%c):" , i,get_file_ops (files[i].type)->name);if (nbread[i])printk (" %dR" , nbread[i]);if (nbwrite[i])Signature · 1 line
2.26.2
Signature · 4 lines
Samuel Running Windows on a Pentium is like having a brand new Porsche but only be able to drive backwards with the handbrake on. (Unknown source)
Re: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket
In reply to Juergen Grosslib/sys.c static struct file_ops file_ops_none = { · line 110
Quoted 3 lines · Juergen Gross
lib/sys.c +3
-0+static int socket_write (int fd,const void *buf,size_t nbytes) +{ +return lwip_write (fd, (void *)buf, nbytes);
This void cast was bogus before, and can be dropped. lwip_write() already takes a const pointer.
~Andrew
Re: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket
Quoted 7 lines · Andrew Cooper
lib/sys.c static struct file_ops file_ops_none = { · line 110
Quoted 3 lines · Juergen Gross
lib/sys.c +3
-0+static int socket_write (int fd,const void *buf,size_t nbytes) +{ +return lwip_write (fd, (void *)buf, nbytes);This void cast was bogus before, and can be dropped.
lwip_write()already takes a const pointer.
Okay.
Juergen
Re: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket
In reply to Juergen GrossQuoted 15 lines · Juergen Gross
lib/sys.c +7
-0@@ -99,11 +99,70 @@ static struct file_ops file_ops_none = { .name = "none", }; +#ifdef HAVE_LWIP +static int socket_read (int fd,void *buf,size_t nbytes) +{ +return lwip_read (fd, buf, nbytes); +} + +static int socket_write (int fd,const void *buf,size_t nbytes)
lib/sys.c static struct file_ops file_ops_none = { · line 118
Quoted 10 lines
lib/sys.c +10
-0+{ +return lwip_write (fd, (void *)buf, nbytes); +} + +static int close_socket_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +return lwip_close (file->fd); +}
Actually, on further thoughts...
lib/sys.c static struct file_ops file_ops_none = { · line 155
Quoted 8 lines
lib/sys.c +8
-0+static struct file_ops socket_ops = { + .name ="socket" , + .read = socket_read, + .write = socket_write, + .close = close_socket_fd, + .fstat = socket_fstat, + .fcntl = socket_fcntl, +};
read, write and close should dispatch directly to lwip_* and not bounce through a no-op local function.
~Andrew
Re: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket
Quoted 29 lines · Andrew Cooper
Quoted 15 lines · Juergen Gross
lib/sys.c +7
-0@@ -99,11 +99,70 @@ static struct file_ops file_ops_none = { .name = "none", }; +#ifdef HAVE_LWIP +static int socket_read (int fd,void *buf,size_t nbytes) +{ +return lwip_read (fd, buf, nbytes); +} + +static int socket_write (int fd,const void *buf,size_t nbytes)lib/sys.c static struct file_ops file_ops_none = { · line 118
Quoted 10 lines
lib/sys.c +10
-0+{ +return lwip_write (fd, (void *)buf, nbytes); +} + +static int close_socket_fd (int fd) +{ +struct file *file =get_file_from_fd (fd); + +return lwip_close (file->fd); +}Actually, on further thoughts...
Quoted 11 lines
lib/sys.c static struct file_ops file_ops_none = { · line 155
Quoted 8 lines
lib/sys.c +8
-0+static struct file_ops socket_ops = { + .name ="socket" , + .read = socket_read, + .write = socket_write, + .close = close_socket_fd, + .fstat = socket_fstat, + .fcntl = socket_fcntl, +};read, write and close should dispatch directly to lwip_* and not bounce through a no-op local function.
Not with changing the first parameter to struct file *.
BTW, this patch had a bug, as the calls need to use file->fd instead of fd.
Juergen
Re: [PATCH v2 10/12] mini-os: add struct file_ops for file type socket
Quoted 41 lines · Juergen Gross
Quoted 28 lines · Andrew Cooper
Quoted 14 lines · Juergen Gross
lib/sys.c +0
-0@@ -99,11 +99,70 @@ static struct file_ops file_ops_none = { .name = "none", };+#ifdef
HAVE_LWIPlib/sys.c +6
-0+static int socket_read (int fd,void *buf,size_t nbytes) +{ + return lwip_read(fd, buf, nbytes); +} + +static int socket_write (int fd,const void *buf,size_t nbytes)lib/sys.c static struct file_ops file_ops_none = { · line 118
Quoted 10 lines
lib/sys.c +10
-0+{ + return lwip_write(fd, (void *)buf, nbytes); +} + +static int close_socket_fd (int fd) +{ + struct file *file = get_file_from_fd(fd); + + return lwip_close(file->fd); +}Actually, on further thoughts...
Quoted 11 lines
lib/sys.c static struct file_ops file_ops_none = { · line 155
Quoted 8 lines
lib/sys.c +8
-0+static struct file_ops socket_ops = { + .name = "socket", + .read = socket_read, + .write = socket_write, + .close = close_socket_fd, + .fstat = socket_fstat, + .fcntl = socket_fcntl, +};read, write and close should dispatch directly to lwip_* and not bounce through a no-op local function.
Quoted 1 line
Not with changing the first parameter to struct file *.
Oh, good point.
Quoted 3 lines
BTW, this patch had a bug, as the calls need to use
file->fdinstead of fd.
Yay for multiple fd spaces.
~Andrew
PATCH v2 11/12 mini-os: add struct file_ops for FTYPE_FILE
In reply to Juergen GrossFTYPE_FILE is the last relevant file type without a struct file_ops. Add it.
--- lib/sys.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
lib/sys.c +9 -10
@@ -99,6 +99,11 @@ static struct file_ops file_ops_none = { .name = "none", }; +static struct file_ops file_file_ops = { + .name ="file" , + .lseek = lseek_default, +}; +#ifdef HAVE_LWIP static int socket_read (int fd,void *buf,size_t nbytes) {
@@ -160,6 +165,7 @@ static struct file_ops *file_ops[FTYPE_N + FTYPE_SPARE] = { #ifdef CONFIG_CONSFRONT [FTYPE_CONSOLE] = &console_ops, #endif + [FTYPE_FILE ] = &file_file_ops, #ifdef HAVE_LWIP [FTYPE_SOCKET] = &socket_ops, #endif
@@ -397,16 +403,9 @@ off_t lseek(int fd, off_t offset, int whence)if ( ops->lseek )return ops->lseek (fd, offset, whence); -switch (files[fd].type) { -case FTYPE_FILE : -break ; -default : -/* Not implemented for this filetype */ - errno =ESPIPE ; -return (off_t) -1 ; - } - -return lseek_default (fd, offset, whence); +/* Not implemented for this filetype */ + errno =ESPIPE ; +return (off_t) -1 ; }int fsync (int fd) {
Signature · 1 line
2.26.2
Re: [PATCH v2 11/12] mini-os: add struct file_ops for FTYPE_FILE
Juergen Gross, le mar. 11 janv. 2022 16:12:14 +0100, a ecrit:
Quoted 4 lines
FTYPE_FILEis the last relevant file type without a structfile_ops. Add it.Signed-off-by Juergen Gross <[email protected]>
Quoted 51 lines
--- lib/sys.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)lib/sys.c +9
-10@@ -99,6 +99,11 @@ static struct file_ops file_ops_none = { .name = "none", }; +static struct file_ops file_file_ops = { + .name ="file" , + .lseek = lseek_default, +}; +#ifdef HAVE_LWIP static int socket_read (int fd,void *buf,size_t nbytes) {@@ -160,6 +165,7 @@ static struct file_ops *file_ops[FTYPE_N + FTYPE_SPARE] = { #ifdef CONFIG_CONSFRONT [FTYPE_CONSOLE] = &console_ops, #endif + [FTYPE_FILE ] = &file_file_ops, #ifdef HAVE_LWIP [FTYPE_SOCKET] = &socket_ops, #endif@@ -397,16 +403,9 @@ off_t lseek(int fd, off_t offset, int whence)if ( ops->lseek )return ops->lseek (fd, offset, whence); -switch (files[fd].type) { -case FTYPE_FILE : -break ; -default : -/* Not implemented for this filetype */ - errno =ESPIPE ; -return (off_t) -1 ; - } - -return lseek_default (fd, offset, whence); +/* Not implemented for this filetype */ + errno =ESPIPE ; +return (off_t) -1 ; }int fsync (int fd) {Signature · 1 line
2.26.2
Signature · 3 lines
Samuel "...[Linux's] capacity to talk via any medium except smoke signals." (By Dr. Greg Wettstein, Roger Maris Cancer Center)
PATCH v2 12/12 mini-os: make files array private to sys.c
In reply to Juergen GrossThere is no user of the files[] array outside of lib/sys.c left, so it can be made static.
--- include/lib.h | 2 -- lib/sys.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)
include/lib.h +0 -2
@@ -199,8 +199,6 @@ struct file { }; }; -extern struct file files[]; -struct file *get_file_from_fd (int fd);int alloc_fd (unsigned int type);void close_all_files (void );
lib/sys.c +1 -1
@@ -89,7 +89,7 @@ extern void minios_evtchn_close_fd(int fd);extern void minios_gnttab_close_fd (int fd);pthread_mutex_t fd_lock =PTHREAD_MUTEX_INITIALIZER ; -struct file files[NOFILE ] = { +static struct file files[NOFILE ] = { { .type =FTYPE_CONSOLE },/* stdin */ { .type =FTYPE_CONSOLE },/* stdout */ { .type =FTYPE_CONSOLE },/* stderr */
Signature · 1 line
2.26.2
Re: [PATCH v2 12/12] mini-os: make files array private to sys.c
Juergen Gross, le mar. 11 janv. 2022 16:12:15 +0100, a ecrit:
Quoted 2 lines
There is no user of the files[] array outside of
lib/sys.cleft, so it can be made static.
Yay!
Quoted 1 line
Signed-off-by Juergen Gross <[email protected]>
Quoted 34 lines
--- include/lib.h | 2 -- lib/sys.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-)include/lib.h +0
-2@@ -199,8 +199,6 @@ struct file { }; }; -extern struct file files[]; -struct file *get_file_from_fd (int fd);int alloc_fd (unsigned int type);void close_all_files (void );lib/sys.c +1
-1@@ -89,7 +89,7 @@ extern void minios_evtchn_close_fd(int fd);extern void minios_gnttab_close_fd (int fd);pthread_mutex_t fd_lock =PTHREAD_MUTEX_INITIALIZER ; -struct file files[NOFILE ] = { +static struct file files[NOFILE ] = { { .type =FTYPE_CONSOLE },/* stdin */ { .type =FTYPE_CONSOLE },/* stdout */ { .type =FTYPE_CONSOLE },/* stderr */ Signature · 1 line
2.26.2
Signature · 2 lines
Samuel #include <culture.h>