[PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-13 Thread Damien Zammit
Previous problems mentioned with 2x rumpdisk partitions all fixed.

Booted off rumpdisk / and mounted second partition in userspace:

root@zamhurd:~# showtrans /dev/wd0
/hurd/storeio -T typed device:@/dev/rumpdisk:/dev/wd0

root@zamhurd:~# fdisk -l /dev/wd0
Disk /dev/wd0: 298.9 GiB, 320072933376 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x90a2468d

Device Boot Start   End   Sectors  Size Id Type
/dev/wd0s1   2048   1953791   1951744  953M 82 Linux swap / Solaris
/dev/wd0s21953792  22925312  20971521   10G 83 Linux
/dev/wd0s3  106811392 316526592 209715201  100G 83 Linux
/dev/wd0s4  316528640 368962049  52433410   25G 83 Linux

root@zamhurd:~# mount /dev/wd0s3 /part3
root@zamhurd:~# ls /part3
demo  lost+found
 

---
 libmachdev/trivfs_server.c | 85 +++---
 rumpdisk/block-rump.c  | 65 ++---
 rumpdisk/main.c|  2 +-
 3 files changed, 114 insertions(+), 38 deletions(-)

diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index c5407145..5d01e61d 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -54,18 +54,47 @@ int trivfs_support_write = 0;
 int trivfs_support_exec = 0;
 int trivfs_allow_open = O_READ | O_WRITE;
 
-/* Our port classes.  */
-struct port_class *trivfs_protid_class;
+/* Our port classes */
 struct port_class *trivfs_cntl_class;
+struct port_class *trivfs_protid_class;
+
+/* Our control struct */
+struct trivfs_control *control;
+
+/* Are we providing bootstrap translator? */
+static boolean_t bootstrapped;
+
+/* Our underlying node for bootstrap */
+static mach_port_t underlying;
 
 /* Our control port */
-static mach_port_t machdev_ctl;
+static mach_port_t control_port;
+
+/* Our device path for injecting bootstrapped translator onto */
+static char *devnode;
 
 /* Startup and shutdown notifications management */
 struct port_class *machdev_shutdown_notify_class;
 
 static void arrange_shutdown_notification (void);
 
+static void
+install_as_translator (mach_port_t bootport)
+{
+  error_t err;
+
+  underlying = file_name_lookup (devnode, O_NOTRANS, 0);
+  if (! MACH_PORT_VALID (underlying))
+return;
+
+  /* Install translator */
+  err = file_set_translator (underlying,
+ 0, FS_TRANS_SET, 0,
+ NULL, 0,
+ bootport, MACH_MSG_TYPE_COPY_SEND);
+  assert_perror_backtrace (err);
+}
+
 /* Implementation of notify interface */
 kern_return_t
 do_mach_notify_port_deleted (struct port_info *pi,
@@ -209,15 +238,14 @@ trivfs_S_fsys_startup (mach_port_t bootport,
mach_port_t *realnode,
mach_msg_type_name_t *realnodetype)
 {
-  machdev_ctl = cntl;
-
+  control_port = cntl;
   *realnode = MACH_PORT_NULL;
   *realnodetype = MACH_MSG_TYPE_MOVE_SEND;
   return 0;
 }
 
 kern_return_t
-trivfs_S_fsys_init (struct trivfs_control *tc,
+trivfs_S_fsys_init (struct trivfs_control *fsys,
 mach_port_t reply, mach_msg_type_name_t replytype,
 mach_port_t procserver,
 mach_port_t authhandle)
@@ -229,10 +257,11 @@ trivfs_S_fsys_init (struct trivfs_control *tc,
   mach_port_t root;
   retry_type retry;
   string_t retry_name;
+  mach_port_t right = MACH_PORT_NULL;
 
-  err = fsys_getroot (machdev_ctl, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
-  idlist, 3, idlist, 3, 0,
-  &retry, retry_name, &root);
+  err = fsys_getroot (control_port, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
+  idlist, 3, idlist, 3, 0,
+  &retry, retry_name, &root);
   assert_perror_backtrace (err);
   assert_backtrace (retry == FS_RETRY_NORMAL);
   assert_backtrace (retry_name[0] == '\0');
@@ -249,6 +278,15 @@ trivfs_S_fsys_init (struct trivfs_control *tc,
   _hurd_init (0, NULL, portarray, INIT_PORT_MAX, NULL, 0);
 
   arrange_shutdown_notification ();
+
+  /* Install the bootstrap port on /dev/something so users
+   * can still access the bootstrapped device */
+  if (bootstrapped)
+{
+  right = ports_get_send_right (&control->pi);
+  install_as_translator (right);
+  control->underlying = underlying;
+}
   return 0;
 }
 
@@ -301,14 +339,8 @@ trivfs_S_fsys_getpriv (struct diskfs_control 
*init_bootstrap_port,
 {
   error_t err;
   mach_port_t right;
-  struct port_info *server_info;
-
-  err = ports_create_port (trivfs_protid_class, port_bucket,
-   sizeof (struct port_info), &server_info);
-  assert_perror_backtrace (err);
-  right = ports_get_send_right (server_info);
-  ports_port_deref (server_info);
 
+  right = ports_get_send_right (&control->pi);
   err = get_privileged_ports (host_priv, NULL);
   if (!err)
 {
@@ -326,

pci-arbiter + rumpdisk

2020-11-13 Thread Damien Zammit
Hi Samuel,

I need to figure out the next step of rumpdisk, using the arbiter instead of 
faking the arbiter.

> youpi: pci-arbiter could be exposed as a device name in the master device 
> port and the userland pci-arbiter running on /server/bus/pci can try to open 
> that
> youpi: just like netdde tries to open eth0 to check whether there are already 
> device drivers in gnumach, in which case it shouldn't handle network cards
> youpi: in the end, when we know for sure that pci-arbiter is run as a 
> bootstrap translator, we can make /server/bus/pci a mere device node

How do i make a bootstrap translator such as pci-arbiter expose a device name 
in the master device port?
I want to be able to device_open("pci") and a bootstrap translator responds.
I'm guessing i need to make the arbiter into a "master device" instead of 
rumpdisk and then every device_open() call goes through the arbiter first?

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-14 Thread Damien Zammit



On 14/11/20 8:17 pm, Samuel Thibault wrote:
> Hello,
> 
> Damien Zammit, le sam. 14 nov. 2020 14:37:37 +1100, a ecrit:
>> Previous problems mentioned with 2x rumpdisk partitions all fixed.
> 
> Congrats!
> 
> What was the issue?

The logic around default behaviour when the device was already open was
broken, causing D_ALREADY_OPEN from being returned I think gave EBUSY
but the send right was still being set?  I'm not exactly sure,
but after I fixed the device_open logic it worked correctly.

By the way, should we keep the device right stored in struct block_device *bd,
so we can give already open devices the same right? Or is it okay to continue
calling ports_get_right(bd) every time we reopen?

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-14 Thread Damien Zammit
Hi,

On 14/11/20 9:09 pm, Samuel Thibault wrote:
> This looks odd to me. The "@/dev/master:" part should rather be
> dropped way before, by the code that opens /dev/master to access the
> master device port before calling device_open() on it: the translators
> shouldn't have to grok which way they were accessed.

There are two cases to think about here:
1) rumpdisk was booted as a bootstrapped task
2) rumpdisk was called from a userspace translator
In both cases, you need @/dev/rumpdisk:/dev/X to be recognised as a valid 
access rule.
Perhaps I confused myself and maybe it is not needed, you could be correct.

I will test again without it.

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-14 Thread Damien Zammit
Hi,

On 15/11/20 11:33 am, Damien Zammit wrote:
> I will test again without it.

It works in both modes with current master!

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-15 Thread Damien Zammit



On 15/11/20 8:56 pm, Samuel Thibault wrote:
> I have uploaded it in Debian a minute ago. We'll be able to start to use
> this in production :)

Unfortunately, it still does not work with the pci-arbiter.
I had a go at fixing that today but it's not quite working.
I am getting "mapping time: Gratutitous error".
Something is not quite right because I set the maptime to use_mach_dev but
there must be some other place trying to use the time and I haven't found that 
yet.

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-15 Thread Damien Zammit
Hi,

On 16/11/20 8:16 am, Samuel Thibault wrote:
> wd1 at atabus7 drive 0
Do you have two controllers or one?  This looks like IDE (?)

> wd1: 
> wd1: drive supports 16-sector PIO transfers, LBA48 addressing
> wd1: 20480 MB, 41610 cyl, 16 head, 63 sec, 512 bytes/sect x 41943040 sectors
> wd1: drive supports PIO mode 4, DMA mode 2, Ultra-DMA mode 5 (Ultra/100)
> wd1(ahcisata1:1:0): using PIO mode 4, DMA mode 2, Ultra-DMA mode 5 
> (Ultra/100) (using DMA)
That definitely looks like an AHCI controller with disk attached.

> [...]
> opening /dev/wd1d
> could not open
> ext2fs: device:/dev/wd1: No such device or addressHmm, what does showtrans 
> say on your disk node?

> It seems rump didn't manage to open it, do you have an idea?
Perhaps your controller does not support DMA?
You might have to patch librumpdisk0 to append flags to turn off DMA if so.

> BTW, I believe we don't want to expose the /dev/ prefix, that'd be
> surprising for users (I myself made the mistake). translate_name can
> prepend /dev/ itself.
OK I can send in a patch for this.

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-15 Thread Damien Zammit
Hi,

On 16/11/20 9:24 am, Samuel Thibault wrote:
> opening /dev/wd1d
> could not open
> ext2fs: device:/dev/wd1: No such device or address

FYI, I am using the following grub entry for booting off a rumpdisk:

menuentry "rumpdisk!" {
set root=(hd0,msdos2)
multiboot /boot/gnumach-1.8-486-noahci.gz root=part:2:device:/dev/wd0

module /hurd/rumpdisk.static rumpdisk \
--host-priv-port='${host-port}' \
--device-master-port='${device-port}' \
--filesystem-task='${fs-task}' \
'$(task-create)' '$(task-resume)'

module /hurd/ext2fs.static ext2fs \
--multiboot-command-line='${kernel-command-line}' \
--exec-server-task='${exec-task}' -T typed 
part:2:device:/dev/wd0 \
'$(fs-task=task-create)'

module /hurd/exec.static exec '$(exec-task=task-create)'
}

Note that you need to build a gnumach kernel with no AHCI probing.

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-15 Thread Damien Zammit
Hi,

On 16/11/20 10:33 am, Samuel Thibault wrote:
> Did you already try wd1 rather than wd0?I have only tried wd0 to boot / but 
> have tried wd1 in userspace and that works.

I have a patched QEMU for IDE sync cache atapi cmd and no AHCI controller by 
default in q35, see attached.
I recall having issues with the driver without patching QEMU.
Perhaps this will help?

Damien
>From 2c672e1902757dcd202c58382dcfc0727217aafa Mon Sep 17 00:00:00 2001
From: Damien Zammit 
Date: Wed, 24 Jun 2020 20:14:48 +1000
Subject: [PATCH] No built-in AHCI on q35 && sync cache on IDE

---
 hw/i386/pc_q35.c  | 6 --
 hw/ide/atapi.c| 7 +++
 hw/ide/core.c | 2 +-
 hw/pci-host/q35.c | 4 ++--
 include/hw/ide/internal.h | 3 +++
 5 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/hw/i386/pc_q35.c b/hw/i386/pc_q35.c
index d37c425e22..a89aa11646 100644
--- a/hw/i386/pc_q35.c
+++ b/hw/i386/pc_q35.c
@@ -132,9 +132,9 @@ static void pc_q35_init(MachineState *machine)
 ISABus *isa_bus;
 int i;
 ICH9LPCState *ich9_lpc;
-PCIDevice *ahci;
+//PCIDevice *ahci;
 ram_addr_t lowmem;
-DriveInfo *hd[MAX_SATA_PORTS];
+//DriveInfo *hd[MAX_SATA_PORTS];
 MachineClass *mc = MACHINE_GET_CLASS(machine);
 
 /* Check whether RAM fits below 4G (leaving 1/2 GByte for IO memory
@@ -281,6 +281,7 @@ static void pc_q35_init(MachineState *machine)
 
 if (pcms->sata_enabled) {
 /* ahci and SATA device, for q35 1 ahci controller is built-in */
+/*XXXDZ no built in ahci
 ahci = pci_create_simple_multifunction(host_bus,
PCI_DEVFN(ICH9_SATA1_DEV,
  ICH9_SATA1_FUNC),
@@ -290,6 +291,7 @@ static void pc_q35_init(MachineState *machine)
 g_assert(MAX_SATA_PORTS == ahci_get_num_ports(ahci));
 ide_drive_get(hd, ahci_get_num_ports(ahci));
 ahci_ide_create_devs(ahci, hd);
+*/
 } else {
 idebus[0] = idebus[1] = NULL;
 }
diff --git a/hw/ide/atapi.c b/hw/ide/atapi.c
index 17a9d635d8..b40cfcce78 100644
--- a/hw/ide/atapi.c
+++ b/hw/ide/atapi.c
@@ -1042,6 +1042,12 @@ static void cmd_seek(IDEState *s, uint8_t* buf)
 ide_atapi_cmd_ok(s);
 }
 
+static void cmd_sync_cache(IDEState *s, uint8_t* buf)
+{
+ide_flush_cache(s); 
+ide_atapi_cmd_ok(s);
+}
+
 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
 {
 int sense;
@@ -1267,6 +1273,7 @@ static const struct AtapiCmd {
 [ 0x25 ] = { cmd_read_cdvd_capacity,CHECK_READY },
 [ 0x28 ] = { cmd_read, /* (10) */   CHECK_READY },
 [ 0x2b ] = { cmd_seek,  CHECK_READY | NONDATA },
+[ 0x35 ] = { cmd_sync_cache,CHECK_READY | NONDATA },
 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 689bb36409..779567ea9c 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -1089,7 +1089,7 @@ static void ide_flush_cb(void *opaque, int ret)
 ide_set_irq(s->bus);
 }
 
-static void ide_flush_cache(IDEState *s)
+void ide_flush_cache(IDEState *s)
 {
 if (s->blk == NULL) {
 ide_flush_cb(s, 0);
diff --git a/hw/pci-host/q35.c b/hw/pci-host/q35.c
index 2bbc90b28f..7c82e963a9 100644
--- a/hw/pci-host/q35.c
+++ b/hw/pci-host/q35.c
@@ -576,7 +576,7 @@ static void mch_realize(PCIDevice *d, Error **errp)
 /* if *disabled* show SMRAM to all CPUs */
 memory_region_init_alias(&mch->smram_region, OBJECT(mch), "smram-region",
  mch->pci_address_space, MCH_HOST_BRIDGE_SMRAM_C_BASE,
- MCH_HOST_BRIDGE_SMRAM_C_SIZE);
+ 0x5);
 memory_region_add_subregion_overlap(mch->system_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
 &mch->smram_region, 1);
 memory_region_set_enabled(&mch->smram_region, true);
@@ -593,7 +593,7 @@ static void mch_realize(PCIDevice *d, Error **errp)
 memory_region_set_enabled(&mch->smram, true);
 memory_region_init_alias(&mch->low_smram, OBJECT(mch), "smram-low",
  mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
- MCH_HOST_BRIDGE_SMRAM_C_SIZE);
+ 0x5);
 memory_region_set_enabled(&mch->low_smram, true);
 memory_region_add_subregion(&mch->smram, MCH_HOST_BRIDGE_SMRAM_C_BASE,
 &mch->low_smram);
diff --git a/include/hw/ide/internal.h b/include/hw/ide/internal.h
index 55da35d768..4466273381 100644
--- a/include/hw/ide/internal.h
+++ b/include/hw/ide/internal.h
@@ -646,4 +646,7 @@ IDEDevice *ide_create_drive(IDE

Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-15 Thread Damien Zammit
Hi,

On 16/11/20 11:04 am, Samuel Thibault wrote:
> I tried wd0, it does work (but my setup is using wd1, so doesn't boot :) )

Hmm, I have the same problem on my end trying to boot off wd1:

ahcisata0 port 1: device present, speed: 1.5Gb/s
ahcisata0 port 0: device present, speed: 1.5Gb/s
wd0 at atabus0 drive 0
wd0: 
wd0: drive supports 16-sector PIO transfers, LBA48 addressing
wd0: 100 MB, 203 cyl, 16 head, 63 sec, 512 bytes/sect x 204800 sectors
wd0: drive supports PIO mode 4, DMA mode 2, Ultra-DMA mode 5 (Ultra/100)
wd0(ahcisata0:0:0): using PIO mode 4, DMA mode 2, Ultra-DMA mode 5 (Ultra/100) 
(using DMA)
wd1 at atabus1 drive 0
wd1: 
wd1: drive supports 16-sector PIO transfers, LBA48 addressing
wd1: 298 GB, 620181 cyl, 16 head, 63 sec, 512 bytes/sect x 625142448 sectors
wd1: drive supports PIO mode 4, DMA mode 2, Ultra-DMA mode 5 (Ultra/100)
wd1(ahcisata0:1:0): using PIO mode 4, DMA mode 2, Ultra-DMA mode 5 (Ultra/100) 
(using DMA)
Hurd bootstrap rumpdisk ext2fs: part:2:device:/dev/wd1: No such device or 
address
What would be interesting to see is the contents of "ls /dev" inside the 
rumpfs, but I can't get that very easily.

Damien



Re: [PATCH] libmachdev: Install as translator when bootstrapping && fix rumpdisk injection

2020-11-15 Thread Damien Zammit
On 16/11/20 12:40 pm, Samuel Thibault wrote:
> It booted fine :D

\o/ YAY!



Re: pci-arbiter + rumpdisk

2020-11-16 Thread Damien Zammit
Hi,

How do I expose the hurdish pci subsystem that has no underlying node to attach 
to for a netfs
in pci-arbiter during bootstrap?

I have almost completed the loop with rumpdisk and the arbiter, but I cannot 
start pci-arbiter
as it has no underlying node to attach to, and the specific implementation 
seems to rely on having
a kind of filesystem to traverse and access the nodes...

Damien



Re: pci-arbiter + rumpdisk

2020-11-16 Thread Damien Zammit
Hi,

On 16/11/20 9:02 pm, Samuel Thibault wrote:
> ? Like rumpdisk does?

start pci-arbiter: Hurd bootstrap pci pci-arbiter: Must be started as a 
translator
Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
The NetBSD Foundation, Inc.  All rights reserved.
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California.  All rights reserved.

NetBSD 7.99.34 (RUMP-ROAST)
total memory = unlimited (host limit)
timecounter: Timecounters tick every 10.000 msec
timecounter: Timecounter "clockinterrupt" frequency 100 Hz quality 0
cpu0 at thinair0: rump virtual cpu
root file system type: rumpfs
kern.module.path=/stand/i386/7.99.34/modules
mainbus0 (root)
pci: I/O space init error 22, I/O space not available
pci0 at mainbus0 bus 0
pci0: memory space enabled, rd/line, rd/mult, wr/inv ok

I'm hitting netfs_startup(bootstrap, O_READ) where bootstrap is
being passed in as MACH_PORT_NULL, therefore netfs cannot start up.
What should bootstrap be set to in pci-arbiter/main.c ? 
I have modified it with a patch to introduce machdev,
but machdev overwrites this port to MACH_PORT_NULL at the end of its 
initialisation.

Damien



Re: pci-arbiter + rumpdisk

2020-11-17 Thread Damien Zammit
Somehow I was able to boot / via rumpdisk and then the arbiter
still worked afterwards, so networking via netdde started working.
This is the first time I've had a rumpdisk / with network access!

Alas, I cannot seem to make it work via the arbiter though.
My latest attempt looks like the arbiter started but could not give the i/o 
ports
access to rumpdisk:

start pci-arbiter: PCI start
PCI machdev start
Hurd bootstrap pci Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
The NetBSD Foundation, Inc.  All rights reserved.
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California.  All rights reserved.

NetBSD 7.99.34 (RUMP-ROAST)
total memory = unlimited (host limit)
timecounter: Timecounters tick every 10.000 msec
timecounter: Timecounter "clockinterrupt" frequency 100 Hz quality 0
cpu0 at thinair0: rump virtual cpu
root file system type: rumpfs
kern.module.path=/stand/i386/7.99.34/modules
mainbus0 (root)
pci: I/O space init error 22, I/O space not available
pci0 at mainbus0 bus 0
pci0: memory space enabled, rd/line, rd/mult, wr/inv ok

Damien



Re: rumpdisk status

2020-11-23 Thread Damien Zammit
On 24/11/20 5:16 am, Samuel Thibault wrote:
> Hello,
> 
> Had you tested support for cd-rom? With qemu I am getting a media sense
> error with status 0x50.

I think that was the reason for the qemu patch for syncing IDE cache iirc.

Damien



Re: problems with daily images

2021-01-23 Thread Damien Zammit
Hi,

On 24/1/21 3:32 am, Samuel Thibault wrote:
> Yes, but the real answer is that we should fix vim.

I noticed that vim is building with -O2, shouldn't it also have these flags for 
hurd-i386?

-mno-sse -mno-mmx -mno-sse2 -mno-3dnow

I will try rebuilding the package with these flags and see if it fixes it.

Damien



Re: problems with daily images

2021-01-23 Thread Damien Zammit
Hi,

On 24/1/21 11:28 am, Samuel Thibault wrote:
> Why so? We do support SSE*.
> 
> (glibc 2.33 will even use them automatically for memcpy etc. thanks to
> ifunc support recently getting enabled)

OK, I ran the failing test in GDB:

initmaster(int f UNUSED)
{
#ifndef VMS
# ifdef POSIX
tcflush(f, TCIOFLUSH);
# else
#  ifdef TIOCFLUSH
(void)ioctl(f, TIOCFLUSH, (char *) 0);
#  endif
# endif
# ifdef LOCKPTY
(void)ioctl(f, TIOCEXCL, (char *) 0);
# endif
#endif
}

It's failing on an ioctl()

Thread 4 received signal SIGSEGV, Segmentation fault.
0x032fe808 in in (count=, type=IOC_32) at 
../sysdeps/mach/hurd/ioctl.c:132
132 ../sysdeps/mach/hurd/ioctl.c: No such file or directory.

#0  0x032fe808 in in (count=, type=IOC_32) at 
../sysdeps/mach/hurd/ioctl.c:132
#1  0x032fe9d3 in send_rpc (ioport=162) at ../sysdeps/mach/hurd/ioctl.c:137
#2  0x032ff1b2 in __GI___ioctl (fd=, request=)
at ../sysdeps/mach/hurd/ioctl.c:272
#3  0x081cc30f in initmaster (f=4) at pty.c:212
#4  mch_openpty (ttyn=0x4802928) at pty.c:212
#5  0x081b68ac in open_pty
(pty_master_fd=0x4802998, pty_slave_fd=0x480299c, name1=0x1007a310, 
name2=0x1007a30c) at os_unix.c:4301

Not sure what to do next.

Damien



Re: problems with daily images

2021-01-23 Thread Damien Zammit
Hi,

On 24/1/21 11:41 am, Damien Zammit wrote:
> OK, I ran the failing test in GDB:

Forgot to paste the command I used:

vim/src/vim-gtk3/testdir $ VIMRUNTIME=../../runtime  gdb --args ../vim -f  -u 
unix.vim -U NONE --noplugin --not-a-term -S runtest.vim test_arglist.vim --cmd 
'au SwapExists * let v:swapchoice = "e"'

Damien



Re: problems with daily images

2021-01-23 Thread Damien Zammit
On 24/1/21 12:18 pm, Samuel Thibault wrote:
> I'm starting to think that perhaps we should as well just comment the
> #define TIOCFLUSH from the headers, since there are so many bogus
> callers of the interface.

I think we still need to pass -DPOSIX to vim CFLAGS for hurd,
otherwise it will not call any flush function if you remove the #define.

Damien




Re: problems with daily images

2021-01-23 Thread Damien Zammit
Hi,

On 23/1/21 12:03 pm, Paul Dufresne wrote:
> I have seen problems with daily images from:
> https://people.debian.org/~sthibault/hurd-i386/installer/cdimage/
> 
> Was problems with vim, emacs, gdb... in previous days... I think but unsure 
> Idon't seems to see them now.
> Had tried previous monthly image, was also having vim and emacs installation 
> problem.

Does anyone have an exhaustive list of packages that are currently broken
and part of the initial bootstrap install?

I have a little time now, I can try following up on bugs.

I think we know roughly how to fix the vim bug now, i.e. is there anything else 
blocking fresh installs?

Damien



Re: problems with daily images

2021-01-23 Thread Damien Zammit
Hi Samuel,

On 24/1/21 12:32 pm, Samuel Thibault wrote:
> Damien Zammit, le dim. 24 janv. 2021 12:26:35 +1100, a ecrit:
>> I think we still need to pass -DPOSIX to vim CFLAGS for hurd,
>> otherwise it will not call any flush function if you remove the #define.
> 
> Ok, but AIUI it doesn't do any such flush on Linux either?
> If so we want to get the same fix applied for the Linux case. Otherwise
> we'll again use a different codepath than Linux, and thus be affected by
> bugs that Linux doesn't get only by "mis"-luck.

Well, we could just remove the #define in hurd as you suggested,
and leave the vim package as is. That would get us on par with Linux (?)
If it's truly a vim bug, we could report it and have it fixed in vim separately?

Damien



Re: problems with daily images

2021-01-25 Thread Damien Zammit
On 25/1/21 11:40 pm, Samuel Thibault wrote:
> Paul Dufresne, le lun. 25 janv. 2021 07:21:33 -0500, a ecrit:
>> Today I tried to install [1]https://people.debian.org/~sthibault/hurd-i386/
>> installer/cdimage/daily/debian-sid-hurd-i386-NETINST-1.iso in QEMU.
>>
>> I got:
>> gdb depends on libboost-regex1.74.0-icu67 which is not installed
>>
>> Seems to be the only problem that stop bootstrap-base to succeed.
> 
> Ok, I fixed the missing dep in the cd script, tomorrow's build should be
> fine in that concern.

Thank you Samuel.



Re: problems with daily images

2021-01-25 Thread Damien Zammit
Hi,

On 25/1/21 11:40 pm, Samuel Thibault wrote:
> Ok, I fixed the missing dep in the cd script, tomorrow's build should be
> fine in that concern.

I burnt the cd to physical media and tried to install natively on X220 thinkpad.

We have an issue because the SSD is not detected by gnumach, but still hogs IRQ 
10,
so when rumpdisk tries to enumerate disks from the ramdisk image, it cannot 
gain control of the disks.

Can we add to gnumach an optional command line flag "disk=off" so the kernel 
does not probe any disk controllers,
I think that would enable the installer to proceed on my hardware.

Damien



Re: problems with daily images

2021-01-25 Thread Damien Zammit
On 26/1/21 4:00 pm, Damien Zammit wrote:
> I burnt the cd to physical media and tried to install natively on X220 
> thinkpad.

I also tried the same image on a physical X230 thinkpad, this time the disk was 
detected by gnumach,
but it failed to detect the CD ROM so the installer could not proceed, and 
there was no way to make rumpdisk
find the ATAPI drive because it was already controlled by gnumach.

I think the same solution of providing a manual override to boot with disk=off 
would also fix the installer on this hardware.

Damien



[PATCH] gnumach: Add nodisk option to kernel cli

2021-01-25 Thread Damien Zammit
This option disables the built-in disk drivers completely.
Passing " nodisk" to gnumach command line will allow the user
to boot the installer with a ramdisk, and rumpdisk should
initialise AHCI controller in userspace.

This is a workaround for the installer for some modern hardware that
the gnumach built-in kernel AHCI driver cannot drive.
It is also useful for testing rumpdisk without kernel drivers.

---
 linux/dev/drivers/block/genhd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/linux/dev/drivers/block/genhd.c b/linux/dev/drivers/block/genhd.c
index 903135c3..eb4801a6 100644
--- a/linux/dev/drivers/block/genhd.c
+++ b/linux/dev/drivers/block/genhd.c
@@ -1054,7 +1054,9 @@ void device_setup(void)
 #ifndef MACH
chr_dev_init();
 #endif
-   blk_dev_init();
+   extern char *kernel_cmdline;
+   if (!strstr(kernel_cmdline, " nodisk"))
+   blk_dev_init();
sti();
 #ifdef CONFIG_SCSI
scsi_dev_init();
-- 
2.30.0




Re: vim build [was: problems with daily images]

2021-01-30 Thread Damien Zammit
On 31/1/21 7:54 am, Samuel Thibault wrote:
> I uploaded to unreleased a version with that fixed, but apparently the
> build fails a bit later:
> 
> [...]
> ../vim -f  -u unix.vim -U NONE --noplugin --not-a-term -S runtest.vim 
> test_help.vim --cmd 'au SwapExists * let v:swapchoice = "e"' > /dev/null
> double free or corruption (!prev)
> 
> https://buildd.debian.org/status/fetch.php?pkg=vim&arch=hurd-i386&ver=2%3A8.2.2367-1%2Bb1&stamp=1612040005&raw=0

It appears to be an issue with unix_expandpath in filepath.c in vim: it tries 
to free "buf" in one of the recursive calls,
but the memory address is not accessible (?)

(gdb) thread apply all bt

Thread 5 (Thread 4179.5):
#0  0x031d651c in mach_msg_trap () at 
./build-tree/hurd-i386-libc/mach/mach_msg_trap.S:2
#1  0x031d6cc6 in __GI___mach_msg (msg=0x5008f10, option=2, send_size=0, 
rcv_size=4096, rcv_name=141, timeout=0, notify=0) at msg.c:111
#2  0x031d7259 in __mach_msg_server_timeout (demux=0x31ec210 , 
max_size=4096, rcv_name=141, option=0, timeout=0) at msgserver.c:100
#3  0x031d73b4 in __mach_msg_server (demux=0x31ec210 , 
max_size=4096, rcv_name=141) at msgserver.c:195
#4  0x031ec2fe in _hurd_msgport_receive () at msgportdemux.c:67
#5  0x026863ff in entry_point (self=, start_routine=, arg=) at pt-create.c:62
#6  0x in  ()

Thread 4 (Thread 4179.4):
#0  0x031d651c in mach_msg_trap () at 
./build-tree/hurd-i386-libc/mach/mach_msg_trap.S:2
#1  0x031d6cc6 in __GI___mach_msg (msg=0x480399c, option=3, send_size=48, 
rcv_size=32, rcv_name=148, timeout=0, notify=0) at msg.c:111
#2  0x03ab414c in __msg_sig_post (process=141, signal=0, sigcode=0, refport=1) 
at ./build-tree/hurd-i386-libc/hurd/RPC_msg_sig_post.c:149
#3  0x031f8a84 in __GI__hurd_raise_signal (ss=, signo=6, 
detail=0x4803a38) at hurd-raise.c:49
#4  0x0268ac8c in __pthread_kill (thread=1, sig=6) at 
../sysdeps/hurd/htl/pt-kill.c:49
#5  0x03223797 in __GI_raise (signo=6) at ../sysdeps/htl/raise.c:38
#6  0x031d4e74 in __GI_abort () at abort.c:79
#7  0x03268b5f in __libc_message (action=, fmt=) 
at ../sysdeps/posix/libc_fatal.c:155
#8  0x0327046d in malloc_printerr (str=str@entry=0x6540a98 ) at malloc.c:5345
#9  0x032718ad in _int_free (av=0x33f9fe0 , p=0x10089da8, 
have_lock=0) at malloc.c:4315
#10 0x08182f89 in vim_free (x=0x10089db0) at misc2.c:1807
#11 0x081278fb in unix_expandpath (gap=0x4804208, path=0x10031790 "Xdir/", 'd' 
..., wildoff=773, flags=34, didstar=1) at filepath.c:3665
#12 0x08127a4a in unix_expandpath (gap=0x4804208, path=0x10029720 "Xdir/", 'd' 
..., wildoff=517, flags=34, didstar=1) at filepath.c:3622
#13 0x08127a4a in unix_expandpath (gap=0x4804208, path=0x100695d0 "Xdir/", 'd' 
..., wildoff=261, flags=34, didstar=1) at filepath.c:3622
#14 0x08127a4a in unix_expandpath (gap=0x4804208, path=0x10027460 
"Xdir/**/*.txt", wildoff=0, flags=34, didstar=0) at filepath.c:3622
#15 0x081bb6f4 in mch_expandpath (gap=0x4804208, path=0x10027460 
"Xdir/**/*.txt", flags=34) at os_unix.c:6465
#16 0x08128122 in gen_expand_wildcards (num_pat=, pat=0x8453aa0 
, num_file=num_file@entry=0x480430c, file=0x4804310, flags=34) at 
filepath.c:3864
#17 0x081282cb in gen_expand_wildcards (num_pat=, pat=, num_file=0x480430c, file=0x4804310, flags=34) at filepath.c:3765
#18 0x081411a2 in helptags_one (ignore_writeerr=0, add_help_tags=0, 
tagfname=0x4804344 "tags", ext=0x480433f ".txt", dir=0x10064a70 "Xdir") at 
help.c:981
#19 do_helptags (dirname=dirname@entry=0x10064a70 "Xdir", add_help_tags=0, 
ignore_writeerr=ignore_writeerr@entry=0) at help.c:1272
#20 0x08143654 in ex_helptags (eap=0x480460c) at help.c:1322
#21 0x0810ba44 in do_one_cmd (cookie=0x0, fgetline=0x0, cstack=0x4804708, 
flags=11, cmdlinep=0x48045a4) at e
x_docmd.c:2588
#22 do_cmdline (cmdline=0x1006bc60 "helptags Xdir", fgetline=0x0, cookie=0x0, 
flags=11) at ex_docmd.c:1003
#23 0x0810c812 in do_cmdline_cmd (cmd=0x1006bc60 "helptags Xdir") at 
ex_docmd.c:592
#24 0x08260e87 in f_assert_fails (argvars=0x4804f0c, rettv=0x48050e8) at 
testing.c:564
#25 0x080e74ed in call_internal_func (name=0x10069a80 "assert_fails", 
argcount=2, argvars=0x4804f0c, rettv=0x48050e8) at evalfunc.c:2042
#26 0x0827e863 in call_func (funcname=0x10069c40 "assert_fails", len=-1, 
rettv=0x48050e8, argcount_in=, argvars_in=0x4804f0c, 
funcexe=0x48050f8) at userfunc.c:2523
#27 0x0827ec07 in get_func_tv (name=0x10069c40 "assert_fails", len=-1, 
rettv=0x48050e8, arg=0x48050c8, evalarg=0x480511c, funcexe=0x48050f8) at 
userfunc.c:906
#28 0x082827e2 in ex_call (eap=0x48053ac) at userfunc.c:4480
#29 0x0810ba44 in do_one_cmd (cookie=0x1002a1c0, fgetline=0x827ac60 
, cstack=0x48054a8, flags=7, cmdlinep=0x4805344) at 
ex_docmd.c:2588
#30 do_cmdline (cmdline=0x0, fgetline=0x827ac60 , 
cookie=0x1002a1c0, flags=7) at ex_docmd.c:1003
#31 0x0827bccc in call_user_func (fp=fp@entry=0x100658c0, 
argcount=argcount@entry=0, argvars=argvars@entry=0x4805f2c, rettv=, funcexe=, selfdict=) at userfunc.c:1885
#32 0x0827d94a in call_user_func_check (selfdict=0x0, funcexe=0x4806118, 
ret

[PATCH x3] hurd: pci arbiter bootstrap

2021-02-01 Thread Damien Zammit
Hi all,

[PATCH 1/3] libnetfs: Fix mapping of time fallback to mach device

This could be merged as is.


[PATCH 2/3] Bootstrappable pci-arbiter

This is a work in progress, I would like some comments on how I can
make the arbiter start up without a / filesystem when it needs to
expose the pci interface netfs on a real node.


[PATCH 3/3] rumpdisk: Don't pretend to be the arbiter anymore

This will need to be merged once patch 2 is fixed and merged.

Cheers,
Damien



[PATCH 1/3] libnetfs: Fix mapping of time fallback to mach device 'time'

2021-02-01 Thread Damien Zammit
---
 libnetfs/init-init.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libnetfs/init-init.c b/libnetfs/init-init.c
index 984a5e71..b63e7ffa 100644
--- a/libnetfs/init-init.c
+++ b/libnetfs/init-init.c
@@ -39,6 +39,8 @@ netfs_init ()
 {
   error_t err;
   err = maptime_map (0, 0, &netfs_mtime);
+  if (err)
+err = maptime_map (1, 0, &netfs_mtime);
   if (err)
 error (2, err, "mapping time");
 
-- 
2.30.0




[PATCH 3/3] rumpdisk: Don't pretend to be the arbiter anymore

2021-02-01 Thread Damien Zammit
---
 rumpdisk/main.c | 5 -
 1 file changed, 5 deletions(-)

diff --git a/rumpdisk/main.c b/rumpdisk/main.c
index 150dc996..b35cbfa7 100644
--- a/rumpdisk/main.c
+++ b/rumpdisk/main.c
@@ -31,11 +31,6 @@
 #include 
 #include 
 
-/* TODO: Add api to pciaccess to allow selecting backend.
- * For now we pretend to be the arbiter and claim x86 method.
- */
-char *netfs_server_name = "pci-arbiter";
-
 mach_port_t bootstrap_resume_task = MACH_PORT_NULL;
 
 static const struct argp_option options[] = {
-- 
2.30.0




[PATCH 2/3] Bootstrappable pci-arbiter

2021-02-01 Thread Damien Zammit
---
 pci-arbiter/Makefile  |  2 +-
 pci-arbiter/main.c| 72 ---
 pci-arbiter/options.c | 19 +++
 pci-arbiter/options.h |  8 +
 pci-arbiter/pcifs.h   |  3 ++
 pci-arbiter/startup-ops.c |  2 ++
 6 files changed, 100 insertions(+), 6 deletions(-)

diff --git a/pci-arbiter/Makefile b/pci-arbiter/Makefile
index 1d0309dc..aab59456 100644
--- a/pci-arbiter/Makefile
+++ b/pci-arbiter/Makefile
@@ -26,7 +26,7 @@ SRCS  = main.c pci-ops.c netfs_impl.c \
 MIGSRCS= pciServer.c startup_notifyServer.c
 OBJS   = $(patsubst %.S,%.o,$(patsubst %.c,%.o, $(SRCS) $(MIGSRCS)))
 
-HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash
+HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash trivfs machdev
 LDLIBS = -lpthread $(libpciaccess_LIBS)
 
 target = pci-arbiter
diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 181bdee2..61ff220c 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -25,6 +25,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -34,18 +35,62 @@
 #include "libnetfs/fsys_S.h"
 #include "libports/interrupt_S.h"
 #include "libnetfs/ifsock_S.h"
+#include "libmachdev/machdev.h"
 #include 
+#include 
 #include "pcifs.h"
 #include "startup.h"
 
 struct pcifs *fs;
-volatile struct mapped_time_value *pcifs_maptime;
+volatile struct mapped_time_value *pcifs_maptime = NULL;
 
 /* Libnetfs stuff */
 int netfs_maxsymlinks = 0;
 char *netfs_server_name = "pci-arbiter";
 char *netfs_server_version = HURD_VERSION;
 
+static io_return_t
+device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
+ dev_mode_t mode, char *name, device_t * devp,
+ mach_msg_type_name_t * devicePoly)
+{
+  io_return_t err;
+  struct port_info newpi;
+
+  if (strncmp(name, "pci", 3))
+return D_NO_SUCH_DEVICE;
+
+  err = ports_create_port (netfs_control_class, netfs_port_bucket,
+ sizeof (struct port_info), &newpi);
+  if (err)
+return D_NO_SUCH_DEVICE;
+
+  *devp = ports_get_right (&newpi);
+  *devicePoly = MACH_MSG_TYPE_MAKE_SEND;
+  return D_SUCCESS;
+}
+
+static struct machdev_device_emulation_ops pci_arbiter_emulation_ops = {
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  device_open,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+};
+
 int
 netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outp)
 {
@@ -72,19 +117,36 @@ main (int argc, char **argv)
 {
   error_t err;
   mach_port_t bootstrap;
+  mach_port_t disk_server_task;
+  pthread_t t;
 
   /* Parse options */
   alloc_file_system (&fs);
   argp_parse (netfs_runtime_argp, argc, argv, 0, 0, 0);
+  disk_server_task = fs->params.disk_server_task;
 
-  task_get_bootstrap_port (mach_task_self (), &bootstrap);
-  if (bootstrap == MACH_PORT_NULL)
-error (1, 0, "must be started as a translator");
-
+  if (disk_server_task != MACH_PORT_NULL)
+{
+  machdev_register (&pci_arbiter_emulation_ops);
+  machdev_device_init ();
+  machdev_trivfs_init (disk_server_task, "pci", "/servers/bus/pci", 
&bootstrap);
+  err = pthread_create (&t, NULL, machdev_server, NULL);
+  if (err)
+error (1, err, "Creating machdev thread");
+  pthread_detach (t);
+}
+  else
+{
+  task_get_bootstrap_port (mach_task_self (), &bootstrap);
+  if (bootstrap == MACH_PORT_NULL)
+error (1, 0, "must be started as a translator");
+}
   /* Initialize netfs and start the translator. */
   netfs_init ();
 
   err = maptime_map (0, 0, &pcifs_maptime);
+  if (err)
+err = maptime_map (1, 0, &pcifs_maptime);
   if (err)
 error (1, err, "mapping time");
 
diff --git a/pci-arbiter/options.c b/pci-arbiter/options.c
index 5767ad27..141c69e3 100644
--- a/pci-arbiter/options.c
+++ b/pci-arbiter/options.c
@@ -241,6 +241,15 @@ parse_opt (int opt, char *arg, struct argp_state *state)
 case 'n':
   h->ncache_len = atoi (arg);
   break;
+case 'T':
+  h->disk_server_task = atoi (arg);
+  break;
+case 'H':
+  h->host_priv_port = atoi (arg);
+  break;
+case 'P':
+  h->dev_master_port = atoi (arg);
+  break;
 case ARGP_KEY_INIT:
   /* Initialize our parsing state.  */
   h = malloc (sizeof (struct parse_hook));
@@ -250,6 +259,9 @@ parse_opt (int opt, char *arg, struct argp_state *state)
   h->permsets = 0;
   h->num_permsets = 0;
   h->ncache_len = NODE_CACHE_MAX;
+  h->disk_server_task = MACH_PORT_NULL;
+  h->host_priv_port = MACH_PORT_NULL;
+  h->dev_master_port = MACH_PORT_NULL;
   err = parse_hook_add_set (h);
   if (err)
FAIL (err, 1, err, "option parsing");
@@ -286,6 +298,11 @@ parse_opt (int opt, char *arg, struct argp_state *state)
   /* Set cache len */
   fs->params.node_cache_max = h->ncache_len;
 
+  /* Set bootstrap ports */
+  fs->params.disk_server_task = h->disk_s

Re: [PATCH x3] hurd: pci arbiter bootstrap

2021-02-01 Thread Damien Zammit
Hi,

Here is the tail of my start up log with some extra debug mach_prints:
I think the "Must be started as a translator" is coming from the netfs start up
when it tries to create a node for the pci filesystem.


start pci-arbiter: PCI start
PCI machdev start
Hurd bootstrap pci PCI machdev done
PCI netfs done
PCI maptime done
PCI pci_system_init done
pci-arbiter: Must be started as a translator
Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
The NetBSD Foundation, Inc.  All rights reserved.
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California.  All rights reserved.

NetBSD 7.99.34 (RUMP-ROAST)
total memory = unlimited (host limit)
timecounter: Timecounters tick every 10.000 msec
timecounter: Timecounter "clockinterrupt" frequency 100 Hz quality 0
cpu0 at thinair0: rump virtual cpu
root file system type: rumpfs
kern.module.path=/stand/i386/7.99.34/modules
mainbus0 (root)
pci: I/O space init error 22, I/O space not available
pci0 at mainbus0 bus 0
pci0: memory space enabled, rd/line, rd/mult, wr/inv ok



Re: [PATCH x3] hurd: pci arbiter bootstrap

2021-02-04 Thread Damien Zammit



On 3/2/21 8:53 am, Samuel Thibault wrote:
> Damien Zammit, le mar. 02 févr. 2021 18:29:42 +1100, a ecrit:
>> This is a work in progress, I would like some comments on how I can
>> make the arbiter start up without a / filesystem when it needs to
>> expose the pci interface netfs on a real node.
> 
> That should already be handled by libmachdev's machdev_trivfs_init?  The
> disk translator, in the meanwhile, can device_open("pci") on the device
> master port that it got from pci-arbiter.

I'm not sure what you mean, in pci-arbiter's main we have:

  /* Start the PCI system: NB: pciaccess will choose x86 first and take lock */
  err = pci_system_init ();
  if (err)
error (1, err, "Starting the PCI system");

  /* Create the PCI filesystem */
  err = init_file_system (netfs_startup (bootstrap, O_READ), fs);

After machdev_trivfs_init is called, bootstrap is set to MACH_PORT_NULL.
So, how is netfs_startup supposed to get a non-null bootstrap port and what 
port should this be?
Do we need to modify machdev_trivfs_init to return some kind of port back to 
"bootstrap"?

Cheers,
Damien



pci-arbiter: netfs startup - chicken/egg problem

2021-02-05 Thread Damien Zammit
Hi,

I think we have another chicken/egg problem:
netfs needs an underlying node to expose pcifs on, so that the disk driver can 
start up and expose the real underlying node.

It seems like diskfs_startup_diskfs opens the real disk and finds an actual 
root fs node,
but we can't do that in netfs until the disk driver has started up.

Do I create an artificial underlying node in netfs when it is the bootstrap 
filesystem?

Damien



Re: pci-arbiter: netfs startup - chicken/egg problem

2021-02-05 Thread Damien Zammit
I'm getting logs like the following:


PCI netfs done
PCI maptime done
PCI pci_system_init done
machdev_trivfs_server
pci-arbiter: Translator startup failure: fsys_startup: (ipc/send) invalid destin
ation portCopyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 20
05,
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
The NetBSD Foundation, Inc.  All rights reserved.
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California.  All rights reserved.

NetBSD 7.99.34 (RUMP-ROAST)
total memory = unlimited (host limit)

timecounter: Timecounters tick every 10.000 msec
timecounter: Timecounter "clockinterrupt" frequency 100 Hz quality 0
cpu0 at thinair0: rump virtual cpu
root file system type: rumpfs
kern.module.path=/stand/i386/7.99.34/modules
mainbus0 (root)
pci: I/O space init error 22, I/O space not available
pci0 at mainbus0 bus 0
pci0: memory space enabled, rd/line, rd/mult, wr/inv ok




Re: pci-arbiter: netfs startup - chicken/egg problem

2021-02-06 Thread Damien Zammit
More logs after making the underlying node for netfs not required:
It hangs somewhere on netfs_startup() using a bootstrap port i passed from 
libmachdev.
Maybe I am passing the wrong kind of port?

start pci-arbiter: PCI start
PCI machdev start
Hurd bootstrap pci PCI machdev done
PCI netfs done
PCI maptime done
PCI pci_system_init done
Create root entry
done
init_file_system done
PCI init_file_system done
PCI create_fs_tree done
PCI fs_set_perms done
Stopped at  machine_idle+0xe:   leave
machine_idle(0,0,c102b8f0,c102b4c0,f562b7b0)+0xe
idle_thread_continue(1,c102aba0,0,c104868d,f562b7b0)+0x2a
thread_continue(f562b7b0,f562ade0,c102aba0,f5622fe4,0)+0x2a
Thread_continue()
db> show all tasks
 ID TASK NAME [THREADS]
  0 f562df00 gnumach [8]
  1 f562de40 pci-arbiter [2]
  2 f562dd80 rumpdisk [1]
  3 f562dcc0 ext2fs [1]
  4 f562dc00 exec [1]
db> 



Almost working pci-arbiter with rumpdisk

2021-02-08 Thread Damien Zammit
Hi,

I'm getting very close to working pci + rump.

In pci-arbiter, what port rights do I return in device_open?
I am currently doing this:

static io_return_t
device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
 dev_mode_t mode, char *name, device_t * devp,
 mach_msg_type_name_t * devicePoly)
{
  mach_print("PCI device_open (");
  mach_print(name);
  mach_print(")\n");
  io_return_t err;
  struct port_info newpi;

  if (strncmp(name, "pci", 3))
return D_NO_SUCH_DEVICE;

  err = ports_create_port (netfs_control_class, netfs_port_bucket,
 sizeof (struct port_info), &newpi);
  if (err)
return D_NO_SUCH_DEVICE;

  *devp = ports_get_send_right (&newpi);
  *devicePoly = MACH_MSG_TYPE_MOVE_SEND;
  return D_SUCCESS;
}


And in rumpdisk I am doing this to device_open pci:

...
  /* Fetch a port to the bootstrap system, the host priv and
 master device ports, and the pci-arbiter  */
  if (task_get_bootstrap_port (mach_task_self (), &bootport)
|| fsys_getpriv (bootport, &host_priv, &device_master, &fstask)
|| device_open (device_master, D_READ|D_WRITE, "pci", &device))
{
  mach_print("rumpdisk: Cannot reach the pci arbiter...continuing\n");
}

  rump_init ();
}

Logs:

PCI netfs loop pthread spawn done
machdev_trivfs_server
PCI done, exit thread
PCI device_open (pci)
pci-arbiter: ../../libports/get-right.c:52: ports_get_right: Unexpected error: 
(ipc/send) invalid port right.
rumpdisk: Cannot reach the pci arbiter...continuing
Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016
The NetBSD Foundation, Inc.  All rights reserved.
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California.  All rights reserved.

NetBSD 7.99.34 (RUMP-ROAST)
total memory = unlimited (host limit)
timecounter: Timecounters tick every 10.000 msec
timecounter: Timecounter "clockinterrupt" frequency 100 Hz quality 0
cpu0 at thinair0: rump virtual cpu
root file system type: rumpfs
kern.module.path=/stand/i386/7.99.34/modules
mainbus0 (root)
pci: I/O space init error 22, I/O space not available
pci0 at mainbus0 bus 0
pci0: memory space enabled, rd/line, rd/mult, wr/inv ok

Damien



Re: Almost working pci-arbiter with rumpdisk

2021-02-09 Thread Damien Zammit
On 9/2/21 5:33 am, Samuel Thibault wrote:
> Damien Zammit, le lun. 08 févr. 2021 20:33:16 +1100, a ecrit:
>> I'm getting very close to working pci + rump.
> 
> Did you manage to find answers to your previous questions?
> (I have to admit I didn't have time to dive into them)

I managed to remove the chicken/egg problem by not requiring the underlying 
node for pcifs.

However, I'm still getting very stuck because I don't seem to have a 
"bootstrap" port for fsys_startup() in netfs for pci startup.
Where would the bootstrap port come from to do the arbiter netfs_startup -> 
fsys_startup?
As the arbiter netfs IS the bootstrap filesystem, bootstrap is NULL, or am I 
confused?

Do I need to implement some kind of netfs_S_fsys_* ?

Hurd bootstrap pci PCI machdev done
PCI netfs done
PCI maptime done
PCI pci_system_init done
XXX bootstrap is NULL
pci-arbiter: Translator startup failure: fsys_startup: (ipc/send) invalid 
destination port 

Damien



Re: Almost working pci-arbiter with rumpdisk

2021-02-19 Thread Damien Zammit
I'm getting some issue with libpciaccess, I changed it locally to detect
the pci port from device_open and it seems to be opening the right port, but 
there must be a bug with it.
I must be pretty close now, because the arbiter opens the x86 method and rump 
opens the hurd method of pciaccess:

start pci-arbiter: PCI start
PCI machdev start
Hurd bootstrap pci PCI machdev done
PCI netfs done
PCI maptime done
pciaccess: x86 all good!
PCI pci_system_init done
machdev_trivfs_server
FSYS_STARTUP (machdev)
Create root entry
done
init_file_system done
PCI init_file_system done
PCI create_fs_tree done
PCI fs_set_perms done
PCI netfs loop pthread spawn done
PCI done, exit thread
The NetBSD Foundation, Inc.  All rights reserved.
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California.  All rights reserved.

NetBSD 7.99.34 (RUMP-ROAST)
total memory = unlimited (host limit)
timecounter: Timecounters tick every 10.000 msec
timecounter: Timecounter "clockinterrupt" frequency 100 Hz quality 0
cpu0 at thinair0: rump virtual cpu
root file system type: rumpfs
kern.module.path=/stand/i386/7.99.34/modules
mainbus0 (root)
pci0 at mainbus0 bus 0
pci0: i/o space, memory space enabled, rd/line, rd/mult, wr/inv ok
PCI device_open (irq)
pciaccess: hurd got device master port
PCI device_open (pci)
pciaccess: XXX hurd some error stopped us from initing pci
PCI device_open (console)
Hurd bootstrap rumpdisk machdev_trivfs_server
PCI device_open (console)
PCI device_open (time)
PCI device_open (wd0)
ext2fs: part:2:device:wd0: (ipc/mig) server died

Damien



Re: Almost working pci-arbiter with rumpdisk

2021-02-20 Thread Damien Zammit
Ok so the problem is that its returning ENODIR on the file_name_lookup_under 
call.
How do I get the root filesystem of pci arbiter to exist so it can look up 
paths on the hurd pci arbiter
before rumpdisk initiates the disk driver?

Do I need to call fsys_getroot somewhere in the arbiter?

pci0 at mainbus0 bus 0
pci0: i/o space, memory space enabled, rd/line, rd/mult, wr/inv ok
PCI device_open (irq)
PCI not pci, fallback to kernel
pciaccess: hurd got device master port
PCI device_open (pci)
PCI opening...
PCI open success
enum_devices
enum_devices: pci_port is not null
enum_devices: file is null
enum_devices failed
err=4014
pciaccess: XXX hurd some error stopped us from initing pci
PCI device_open (console)
PCI not pci, fallback to kernel
Hurd bootstrap rumpdisk machdev_trivfs_server
PCI device_open (console)
PCI not pci, fallback to kernel
PCI device_open (time)
PCI not pci, fallback to kernel
PCI device_open (wd0)
PCI not pci, fallback to kernel
ext2fs: part:2:device:wd0: (ipc/mig) server died



Re: Almost working pci-arbiter with rumpdisk

2021-02-20 Thread Damien Zammit
Hi Samuel,

On 20/2/21 10:16 pm, Samuel Thibault wrote:
> It's hard to tell exactly without seeing what exactly you have done
> already. As I wrote before, device_open should return a port to the
> root directory of the pci-arbiter tree. I'm not sure exactly what you
> wrote to achieve that.

I think I am missing the auth handle and proc server handles.
Do I need to call _hurd_init() before starting rump?
If so, how do I get a port to auth and proc without starting init?

Maybe I can bypass the auth just to get a protid on root of pci arbiter tree?
But then how will libpciaccess do lookups during rump bootstrap?

Damien



Re: Almost working pci-arbiter with rumpdisk

2021-02-20 Thread Damien Zammit
Hi,

On 21/2/21 12:24 pm, Samuel Thibault wrote:
> Ah, possibly. Perhaps you could make libnetfs to accept any access when
> auth/proc are not available

Done this!

> ? once libpciaccess gets a port on pci-arbiter's root, it can use
> file_name_lookup_under to open directories and files in it. libnetfs
> will want to check permissions, but it could be made to accept anything
> when auth/proc are not available yet.

Not sure why, but my rump code calling into libpciaccess using hurd method
hangs on trying to openport the root pcifs: 

Here is part of enum_devices in my libpciaccess:

mach_print("enum_devices\n");
if (pci_port == MACH_PORT_NULL) {
mach_print("enum_devices: pci_port is null\n");
dir = opendir(parent);
if (!dir)
return errno;
} else {
mach_print("enum_devices: pci_port is not null\n");
root = file_name_lookup_under (pci_port, parent, O_DIRECTORY | O_RDONLY 
| O_EXEC, 0);
if (root == MACH_PORT_NULL) {
mach_print("enum_devices: root is null\n");
return errno;
} else {
mach_print("enum_devices: root is not null\n");
mach_print("openport...");
fd = openport (root, O_DIRECTORY | O_RDONLY | O_EXEC);
if (fd < 0)
return errno;
mach_print("openport done\n");
dir = fdopendir(fd);
if (!dir)
return errno;
mach_print("fdopendir done\n");
}
}

I see:

pci0: i/o space, memory space enabled, rd/line, rd/mult, wr/inv ok
PCI device_open (irq)
PCI not pci, fallback to kernel
pciaccess: hurd got device master port
PCI device_open (pci)
PCI opening...
PCI open success
enum_devices
enum_devices: pci_port is not null
netfs_S_dir_lookup!
dir_lookup 1
dir_lookup 2
dir_lookup 3
dir_lookup 4
dir_lookup 5
dir_lookup 6
dir_lookup 7
dir_lookup 8
dir_lookup end
enum_devices: root is not null
openport...

rumpdisk.static crashed

Damien



Re: Almost working pci-arbiter with rumpdisk

2021-02-24 Thread Damien Zammit
On 21/2/21 9:21 pm, Samuel Thibault wrote:
> Mmm. I guess that's missing _hurd_init initialization. It would
> be tricky to separate out the really basic pieces from proc/auth
> initialization, I'd say it will be a lot less tricky to just make
> hurd_pci.c use the dir_readdir RPC directly on the root port instead of
> going through the readdir layer. dir_readdir is not that complex to use.

It seems to be getting stuck on dir_readdir RPC as well:

pci0: i/o space, memory space enabled, rd/line, rd/mult, wr/inv ok
PCI device_open (irq)
PCI not pci, fallback to kernel
pciaccess: hurd got device master port
PCI device_open (pci)
PCI opening...
PCI open success
enum_devices
enum_devices: pci_port is not null
netfs_S_dir_lookup!
dir_lookup 1
dir_lookup 2
dir_lookup 3
dir_lookup 4
dir_lookup 5
dir_lookup 6
dir_lookup 7
dir_lookup 8
dir_lookup end
enum_devices: root is not null
enum_devices: got to while loop
dir_readdir...

static struct dirent *
simple_readdir(mach_port_t port, uint32_t *first_entry)
{
struct dirent *e;
char *data;
int nentries = 0;
vm_size_t size;
int err;
mach_print ("dir_readdir...");
err = dir_readdir (port, &data, &size, *first_entry, -1, 0, &nentries);
mach_print ("done\n");
if (err) {
mach_print("dir_readdir failed\n");
return NULL;
}
if (nentries) {
*first_entry = *first_entry + 1;
e = (struct dirent *)(data+4);
return e;
}
*first_entry = 0;
return NULL;
}

Damien



Re: Almost working pci-arbiter with rumpdisk

2021-02-27 Thread Damien Zammit
Hi,

On 25/2/21 9:30 am, Samuel Thibault wrote:
> From reading the libnetfs/dir-readdir.c source and
> pci-arbiter/netfs_impl.c source I don't see anything obvious. Probably
> some prints in there would allow to make sure how far it is going
> actually.

I got past a hang, by installing the port regardless if the cred was present or 
not.

However, I have a new problem, that when dircred is not defined as a protid
when netfs_S_dir_lookup is called, the node that you want to look up is supposed
to be hanging off the dircred->po->np but dircred is not defined when
_hurd_init is not initialised.
I am trying to make it work by always referring to the netfs_root_node, but
its difficult to make the traversal because the pcifs does not seem to be 
implemented
as a regular netfs directory structure, it has fs->entries instead of 
np->nn->ln->dir->entries (?)

I could use some advice.

Thanks,
Damien



pci-arbiter + rumpdisk boots!

2021-03-01 Thread Damien Zammit
Hi all,

I got the arbiter to play nice with rumpdisk!

But as soon as you log in it crashes the disk and qemu, I think probably 
because there is a mismatched
libpciaccess.so in userspace versus the statically linked ones in the pci/disk 
servers?
I'm not sure yet.

--
PCI device_open (mem)
PCI not pci, fallback to kernel
Starting enhanced syslogd: rsyslogdrsyslogd: could not load module 'imklog', err
ors: trying to load module /usr/lib/i386-gnu/rsyslog/imklog.so: /usr/lib/i386-gn
u/rsyslog/imklog.so: undefined symbol: klogWillRunPrePrivDrop [v8.39.0 try http:
//www.rsyslog.com/e/2066 ]
.
Starting deferred execution scheduler: atd.
Starting periodic command scheduler: cron.
Starting system message bus: dbus.

Debian GNU/Hurd bullseye/sid zamhurd console

login: root
Password:

The programs included with the Debian GNU/Hurd system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Hurd comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
ahcisata0 channel 0: setting WDCTL_RST failed for drive 0
./noide: line 4: 535871 Segmentation fault  (core dumped) 
/extra/qemu/bin/qemu-system-i386



[PATCH 2/4] libmachdev: Spawn a thread for server loop so main pathway can continue

2021-03-02 Thread Damien Zammit
---
 libmachdev/trivfs_server.c | 31 ---
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index 3ab15049..06756f82 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -398,7 +398,8 @@ machdev_trivfs_init(mach_port_t bootstrap_resume_task, 
const char *name, const c
   if (path)
devnode = strdup(path);
   resume_bootstrap_server(bootstrap_resume_task, name);
-  *bootstrap = MACH_PORT_NULL;
+  *bootstrap = ports_get_send_right (&control->pi);
+
   /* We need to install as a translator later */
   bootstrapped = TRUE;
 }
@@ -479,13 +480,29 @@ trivfs_modify_stat (struct trivfs_protid *cred, 
io_statbuf_t *stat)
 {
 }
 
+static void *
+machdev_trivfs_loop(void *arg)
+{
+  struct trivfs_control *fsys = (struct trivfs_control *)arg;
+
+  /* Launch.  */
+  do
+{
+  ports_manage_port_operations_one_thread (port_bucket, demuxer, 0);
+} while (trivfs_goaway (fsys, 0));
+
+  /* Never reached */
+  return 0;
+}
+
 void
 machdev_trivfs_server(mach_port_t bootstrap)
 {
   struct trivfs_control *fsys = NULL;
   int err;
+  pthread_t t;
 
-  if (bootstrap != MACH_PORT_NULL)
+  if (bootstrapped == FALSE)
 {
   /* This path is executed when a parent exists */
   err = trivfs_startup (bootstrap, 0,
@@ -500,9 +517,9 @@ machdev_trivfs_server(mach_port_t bootstrap)
   fsys = control;
 }
 
-  /* Launch.  */
-  do
-{
-  ports_manage_port_operations_one_thread (port_bucket, demuxer, 0);
-} while (trivfs_goaway (fsys, 0));
+  err = pthread_create (&t, NULL, machdev_trivfs_loop, (void *)fsys);
+  if (err)
+error (1, err, "Creating machdev server thread");
+  pthread_detach (t);
 }
+
-- 
2.30.0




[PATCH 4/4] rumpdisk: Don't pretend to be the arbiter anymore

2021-03-02 Thread Damien Zammit
---
 rumpdisk/block-rump.c | 16 +---
 rumpdisk/main.c   | 12 +++-
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/rumpdisk/block-rump.c b/rumpdisk/block-rump.c
index 3ee0773d..584056f7 100644
--- a/rumpdisk/block-rump.c
+++ b/rumpdisk/block-rump.c
@@ -27,6 +27,7 @@
 
 #include 
 #include 
+#include  /* fsys_getpriv */
 #include 
 #include 
 
@@ -136,12 +137,12 @@ dev_mode_to_rump_mode (const dev_mode_t mode)
 static void
 rumpdisk_device_init (void)
 {
-  mach_port_t device_master;
+  mach_port_t bootport, device_master;
+  device_t device;
+  task_t fstask;
 
   if (! get_privileged_ports (0, &device_master))
 {
-  device_t device;
-
 #if 0
   if (! device_open (device_master, D_READ, "hd0", &device)
|| ! device_open (device_master, D_READ, "hd1", &device)
@@ -164,6 +165,15 @@ rumpdisk_device_init (void)
  return;
}
 }
+
+  /* FIXME: need to synchronise with arbiter to wait for fsys_startup */
+  sleep(1);
+  
+  task_get_bootstrap_port (mach_task_self (), &bootport);
+  fsys_getpriv (bootport, &_hurd_host_priv, &_hurd_device_master, &fstask);
+
+  sleep(1);
+
   rump_init ();
 }
 
diff --git a/rumpdisk/main.c b/rumpdisk/main.c
index 150dc996..b2426e7d 100644
--- a/rumpdisk/main.c
+++ b/rumpdisk/main.c
@@ -31,11 +31,6 @@
 #include 
 #include 
 
-/* TODO: Add api to pciaccess to allow selecting backend.
- * For now we pretend to be the arbiter and claim x86 method.
- */
-char *netfs_server_name = "pci-arbiter";
-
 mach_port_t bootstrap_resume_task = MACH_PORT_NULL;
 
 static const struct argp_option options[] = {
@@ -124,5 +119,12 @@ main (int argc, char **argv)
 return err;
   pthread_detach (t);
   machdev_trivfs_server (bootstrap);
+
+  while (TRUE)
+{
+  sleep(2);
+}
+
+  /* Never reached */
   return 0;
 }
-- 
2.30.0




[PATCH x4] hurd: pci-arbiter and rumpdisk static server support

2021-03-02 Thread Damien Zammit


Hi all,

This has been a work in progress for some time.
I am hoping it does not need much reworking.

The only problem I can see is the uid=0 faking the cred by allowing
netfs to install ports that have no cred at all, perhaps we need
a new RPC for obtaining a temporary uid=0 cred during bootstrap?
(I don't know how to make it appear in the protid, for example).

With these changes, plus the pciaccess change I will post shortly,
rumpdisk.static boots with pci-arbiter.static as the bootstrap server.

I have reported a bug upstream with qemu because running Hurd as a guest
crashes qemu randomly with a strange message from the rumpdisk AHCI driver:

https://bugs.launchpad.net/qemu/+bug/1917442

Thanks,
Damien Z



[PATCH 3/4] Bootstrappable pci-arbiter

2021-03-02 Thread Damien Zammit
---
 pci-arbiter/Makefile  |   7 +-
 pci-arbiter/main.c| 140 --
 pci-arbiter/netfs_impl.c  |  39 +++
 pci-arbiter/options.c |  19 ++
 pci-arbiter/options.h |   8 +++
 pci-arbiter/pci-ops.c |   6 +-
 pci-arbiter/pcifs.c   |  18 +++--
 pci-arbiter/pcifs.h   |   3 +
 pci-arbiter/startup-ops.c |   2 +
 9 files changed, 207 insertions(+), 35 deletions(-)

diff --git a/pci-arbiter/Makefile b/pci-arbiter/Makefile
index 1d0309dc..3f374d18 100644
--- a/pci-arbiter/Makefile
+++ b/pci-arbiter/Makefile
@@ -22,11 +22,10 @@ PORTDIR = $(srcdir)/port
 
 SRCS   = main.c pci-ops.c netfs_impl.c \
  pcifs.c ncache.c options.c func_files.c startup.c \
- startup-ops.c
-MIGSRCS= pciServer.c startup_notifyServer.c
-OBJS   = $(patsubst %.S,%.o,$(patsubst %.c,%.o, $(SRCS) $(MIGSRCS)))
+ startup-ops.c pciServer.c startup_notifyServer.c
+OBJS   = $(SRCS:.c=.o) $(MIGSTUBS)
 
-HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash
+HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash trivfs machdev
 LDLIBS = -lpthread $(libpciaccess_LIBS)
 
 target = pci-arbiter
diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 181bdee2..dea4af61 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -24,7 +24,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
+#include 
+#include 
 
 #include 
 #include 
@@ -34,18 +39,70 @@
 #include "libnetfs/fsys_S.h"
 #include "libports/interrupt_S.h"
 #include "libnetfs/ifsock_S.h"
+#include "libmachdev/machdev.h"
 #include 
+#include 
 #include "pcifs.h"
 #include "startup.h"
 
 struct pcifs *fs;
-volatile struct mapped_time_value *pcifs_maptime;
+volatile struct mapped_time_value *pcifs_maptime = NULL;
 
 /* Libnetfs stuff */
 int netfs_maxsymlinks = 0;
 char *netfs_server_name = "pci-arbiter";
 char *netfs_server_version = HURD_VERSION;
 
+static mach_port_t pci_control_port;
+
+
+static io_return_t
+pci_device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
+ dev_mode_t mode, char *name, device_t * devp,
+ mach_msg_type_name_t * devicePoly)
+{
+  io_return_t err = D_SUCCESS;
+  mach_port_t dev_master, root;
+
+  if (strncmp(name, "pci", 3))
+err = D_NO_SUCH_DEVICE;
+
+  /* Fall back to opening kernel device master */
+  if (err)
+{
+  get_privileged_ports(NULL, &dev_master);
+  err = device_open (dev_master, mode, name, devp);
+  *devicePoly = MACH_MSG_TYPE_MOVE_SEND;
+  return D_SUCCESS;
+}
+
+  root = pci_control_port;
+  *devp = root;
+  *devicePoly = MACH_MSG_TYPE_COPY_SEND;
+  return D_SUCCESS;
+}
+
+static struct machdev_device_emulation_ops pci_arbiter_emulation_ops = {
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  pci_device_open,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+};
+
 int
 netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outp)
 {
@@ -67,24 +124,82 @@ netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t 
* outp)
 return FALSE;
 }
 
+static void *
+netfs_server_func (void *arg)
+{
+  error_t err;
+
+  do 
+{
+  ports_manage_port_operations_multithread (netfs_port_bucket,
+   netfs_demuxer,
+   1000 * 60 * 2, /* two minutes 
thread */
+   1000 * 60 * 10,/* ten minutes 
server */
+   0);
+  err = netfs_shutdown (0);
+}
+  while (err);
+  return NULL;
+}
+
+
+static mach_port_t
+pcifs_startup(mach_port_t bootstrap, int flags)
+{
+  error_t err;
+  mach_port_t realnode;
+  struct port_info *newpi;
+
+  err = ports_create_port (netfs_control_class, netfs_port_bucket,
+sizeof (struct port_info), &newpi);
+  if (!err)
+{
+  pci_control_port = ports_get_send_right (newpi);
+  err = fsys_startup (bootstrap, flags, pci_control_port, 
MACH_MSG_TYPE_COPY_SEND,
+   &realnode);
+  assert_perror_backtrace (err);
+}
+  if (err)
+error (11, err, "Translator startup failure: fsys_startup");
+
+  return realnode;
+}
+
 int
 main (int argc, char **argv)
 {
   error_t err;
   mach_port_t bootstrap;
+  mach_port_t disk_server_task;
+  pthread_t t, nt;
 
   /* Parse options */
   alloc_file_system (&fs);
   argp_parse (netfs_runtime_argp, argc, argv, 0, 0, 0);
+  disk_server_task = fs->params.disk_server_task;
 
-  task_get_bootstrap_port (mach_task_self (), &bootstrap);
-  if (bootstrap == MACH_PORT_NULL)
-error (1, 0, "must be started as a translator");
-
+  if (disk_server_task != MACH_PORT_NULL)
+{
+  machdev_register (&pci_arbiter_emulation_ops);
+  machdev_device_init ();
+  machdev_trivfs_init (disk_server_task, "pci", "/servers/bus/pci", 
&boots

[PATCH 1/4] libnetfs: Behave more like diskfs_startup_diskfs

2021-03-02 Thread Damien Zammit
---
 libnetfs/dir-lookup.c  | 30 +-
 libnetfs/dir-readdir.c |  3 ---
 libnetfs/file-getcontrol.c | 13 ++---
 libnetfs/init-startup.c|  5 ++---
 libnetfs/make-protid.c |  8 +---
 5 files changed, 30 insertions(+), 29 deletions(-)

diff --git a/libnetfs/dir-lookup.c b/libnetfs/dir-lookup.c
index 23adeb33..9bf78225 100644
--- a/libnetfs/dir-lookup.c
+++ b/libnetfs/dir-lookup.c
@@ -50,9 +50,6 @@ netfs_S_dir_lookup (struct protid *dircred,
   struct protid *newpi = NULL;
   struct iouser *user;
 
-  if (!dircred)
-return EOPNOTSUPP;
-
   create = (flags & O_CREAT);
   excl = (flags & O_EXCL);
 
@@ -73,6 +70,12 @@ netfs_S_dir_lookup (struct protid *dircred,
   *do_retry = FS_RETRY_NORMAL;
   *retry_name = '\0';
 
+  if (!dircred)
+{
+  dircred = netfs_make_protid (netfs_make_peropen (netfs_root_node, flags, 
0), 0);
+  dircred->po->path = filename;
+}
+
   if (*filename == '\0')
 {
   /* Set things up in the state expected by the code from gotit: on. */
@@ -423,15 +426,24 @@ netfs_S_dir_lookup (struct protid *dircred,
 
   flags &= ~OPENONLY_STATE_MODES;
 
-  err = iohelp_dup_iouser (&user, dircred->user);
-  if (err)
-goto out;
+  if (dircred->user)
+{
+  err = iohelp_dup_iouser (&user, dircred->user);
+  if (err)
+goto out;
+}
+  else
+{
+  user = NULL;
+  err = 0;
+}
+
+  newpi = netfs_make_protid (netfs_make_peropen (np, flags, dircred->po), 
user);
 
-  newpi = netfs_make_protid (netfs_make_peropen (np, flags, dircred->po),
-user);
   if (! newpi)
 {
-  iohelp_free_iouser (user);
+  if (user)
+iohelp_free_iouser (user);
   err = errno;
   goto out;
 }
diff --git a/libnetfs/dir-readdir.c b/libnetfs/dir-readdir.c
index 3d40dfa5..83dbf2d4 100644
--- a/libnetfs/dir-readdir.c
+++ b/libnetfs/dir-readdir.c
@@ -36,9 +36,6 @@ netfs_S_dir_readdir (struct protid *user,
   error_t err;
   struct node *np;
 
-  if (!user)
-return EOPNOTSUPP;
-
   np = user->po->np;
   pthread_mutex_lock (&np->lock);
 
diff --git a/libnetfs/file-getcontrol.c b/libnetfs/file-getcontrol.c
index 0e29ccf9..833fe196 100644
--- a/libnetfs/file-getcontrol.c
+++ b/libnetfs/file-getcontrol.c
@@ -31,13 +31,12 @@ netfs_S_file_getcontrol (struct protid *user,
   error_t err;
   struct port_info *pi;
 
-  if (!user)
-return EOPNOTSUPP;
-
-  err = fshelp_iscontroller (&netfs_root_node->nn_stat, user->user);
-  if (err)
-return err;
-  
+  if (user)
+{
+  err = fshelp_iscontroller (&netfs_root_node->nn_stat, user->user);
+  if (err)
+return err;
+} 
   /* They've have the appropriate credentials; give it to them. */
   err = ports_create_port (netfs_control_class, netfs_port_bucket,
   sizeof (struct port_info), &pi);
diff --git a/libnetfs/init-startup.c b/libnetfs/init-startup.c
index e17c4f16..7aed3b62 100644
--- a/libnetfs/init-startup.c
+++ b/libnetfs/init-startup.c
@@ -30,8 +30,9 @@ netfs_startup (mach_port_t bootstrap, int flags)
   mach_port_t realnode, right;
   struct port_info *newpi;
   
+  /* If we have a bootstrap port we must call fsys_startup */
   if (bootstrap == MACH_PORT_NULL)
-error (10, 0, "Must be started as a translator");
+task_get_bootstrap_port (mach_task_self (), &bootstrap);
 
   err = ports_create_port (netfs_control_class, netfs_port_bucket,
 sizeof (struct port_info), &newpi);
@@ -46,8 +47,6 @@ netfs_startup (mach_port_t bootstrap, int flags)
   if (err)
 error (11, err, "Translator startup failure: fsys_startup");
 
-  mach_port_deallocate (mach_task_self (), bootstrap);
-
   /* Mark us as important.  */
   mach_port_t proc = getproc ();
   if (proc == MACH_PORT_NULL)
diff --git a/libnetfs/make-protid.c b/libnetfs/make-protid.c
index 995ac1ea..2087f220 100644
--- a/libnetfs/make-protid.c
+++ b/libnetfs/make-protid.c
@@ -25,14 +25,8 @@ netfs_make_protid (struct peropen *po, struct iouser *cred)
 {
   struct protid *pi;
 
-  if (cred)
-errno = ports_create_port (netfs_protid_class, netfs_port_bucket, 
+  errno = ports_create_port (netfs_protid_class, netfs_port_bucket, 
   sizeof (struct protid), &pi);
-  else
-errno = ports_create_port_noinstall (netfs_protid_class,
-netfs_port_bucket, 
-sizeof (struct protid), &pi);
-
   if (errno)
 return 0;
 
-- 
2.30.0




[PATCH libpciaccess] hurd: device_open the pre-existing pci server and use dir_readdir RPC

2021-03-02 Thread Damien Zammit
This patch for libpciaccess goes in lockstep with 
the previous patches I sent for hurd.

---
 src/hurd_pci.c | 125 ++---
 1 file changed, 98 insertions(+), 27 deletions(-)

diff --git a/src/hurd_pci.c b/src/hurd_pci.c
index ada7af8..ff14285 100644
--- a/src/hurd_pci.c
+++ b/src/hurd_pci.c
@@ -33,9 +33,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include "x86_pci.h"
 #include "pciaccess.h"
@@ -302,26 +305,74 @@ pci_device_hurd_destroy(struct pci_device *dev)
 mach_port_deallocate (mach_task_self (), d->device_port);
 }
 
+static struct dirent *
+simple_readdir(mach_port_t port, uint32_t *first_entry)
+{
+struct dirent *e;
+char *data;
+int nentries = 0;
+vm_size_t size;
+
+dir_readdir (port, &data, &size, *first_entry, 1, 0, &nentries);
+
+if (nentries == 0) {
+   return NULL;
+}
+
+*first_entry = *first_entry + 1;
+e = (struct dirent *)(data+4);
+return e;
+}
+
+static struct dirent *
+readdir_wrapper(DIR *dir, mach_port_t port, uint32_t *count)
+{
+if (dir) {
+return readdir(dir);
+} else {
+   return simple_readdir(port, count);
+}
+}
+
+static int
+closedir_wrapper(DIR *dir)
+{
+if (dir) {
+return closedir(dir);
+}
+return 0;
+}
+
 /* Walk through the FS tree to see what is allowed for us */
 static int
-enum_devices(const char *parent, int domain,
-int bus, int dev, int func, tree_level lev)
+enum_devices(mach_port_t pci_port, mach_port_t cwd_port, const char *parent, 
int domain,
+ int bus, int dev, int func, tree_level lev)
 {
 int err, ret;
-DIR *dir;
-struct dirent *entry;
+DIR *dir = NULL;
+struct dirent *entry = NULL;
 char path[NAME_MAX];
 char server[NAME_MAX];
-uint32_t reg;
+uint32_t reg, count = 0;
 size_t toread;
 mach_port_t device_port;
 struct pci_device_private *d, *devices;
 
-dir = opendir(parent);
-if (!dir)
-return errno;
+if (pci_port == MACH_PORT_NULL) {
+dir = opendir(parent);
+if (!dir)
+return errno;
+} else {
+if (lev > LEVEL_FUNC + 1) {
+   return 0;
+   }
+cwd_port = file_name_lookup_under (pci_port, parent, O_DIRECTORY | 
O_RDWR | O_EXEC, 0);
+if (cwd_port == MACH_PORT_NULL) {
+   return 0;
+}
+}
 
-while ((entry = readdir(dir)) != 0) {
+while ((entry = readdir_wrapper(dir, cwd_port, &count)) != NULL) {
 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
 if (entry->d_type == DT_DIR) {
 if (!strncmp(entry->d_name, ".", NAME_MAX)
@@ -331,7 +382,7 @@ enum_devices(const char *parent, int domain,
 errno = 0;
 ret = strtol(entry->d_name, 0, 16);
 if (errno) {
-closedir(dir);
+closedir_wrapper(dir);
 return errno;
 }
 
@@ -353,14 +404,14 @@ enum_devices(const char *parent, int domain,
 func = ret;
 break;
 default:
-if (closedir(dir) < 0)
+if (closedir_wrapper(dir) < 0)
 return errno;
 return -1;
 }
 
-err = enum_devices(path, domain, bus, dev, func, lev+1);
+err = enum_devices(pci_port, cwd_port, path, domain, bus, dev, 
func, lev+1);
 if (err && err != EPERM && err != EACCES) {
-if (closedir(dir) < 0)
+if (closedir_wrapper(dir) < 0)
 return errno;
 return err;
 }
@@ -370,12 +421,22 @@ enum_devices(const char *parent, int domain,
 continue;
 
 /* We found an available virtual device, add it to our list */
-snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
- _SERVERS_BUS_PCI, domain, bus, dev, func,
- entry->d_name);
-device_port = file_name_lookup(server, 0, 0);
+if (pci_port == MACH_PORT_NULL) {
+snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
+ _SERVERS_BUS_PCI, domain, bus, dev, func,
+ entry->d_name);
+device_port = file_name_lookup(server, 0, 0);
+} else {
+snprintf(server, NAME_MAX, "./%04x/%02x/%02x/%01u/%s",
+ domain, bus, dev, func,
+ entry->d_name);
+device_port = file_name_lookup_under(pci_port, server, O_RDWR, 
0);
+mach_print("pciaccess(hurd): Found ");
+mach_print(server);
+mach_print("\n");
+}
 if (device_port == MACH_PORT_NULL) {
-closedir(dir);
+closedir_wrapper(dir);
 return errno;
 }
 
@@ -383,12 +444,12 @@ e

[PATCH libpciaccess] hurd: device_open the pre-existing pci server

2021-03-05 Thread Damien Zammit
This is a cleaned up version of my previous patch.
It fixes a bug by sorting the device array by B/D/F for all backends.
(It assumes only domain  exists and I think that assumption is also made 
elsewhere).

---
 src/common_init.c |  37 
 src/hurd_pci.c| 108 +++---
 2 files changed, 101 insertions(+), 44 deletions(-)

diff --git a/src/common_init.c b/src/common_init.c
index 1940cff..14f0c6b 100644
--- a/src/common_init.c
+++ b/src/common_init.c
@@ -40,6 +40,40 @@
 
 _pci_hidden struct pci_system * pci_sys;
 
+static int
+sort_devices(void)
+{
+int bus, dev, func;
+struct pci_device_private *sorted;
+struct pci_device_private *device;
+struct pci_device_private *tmpdev;
+
+sorted = calloc(pci_sys->num_devices, sizeof(struct pci_device_private));
+if (!sorted) {
+return ENOMEM;
+}
+
+tmpdev = sorted;
+
+for (bus = 0; bus < 256; bus++) {
+for (dev = 0; dev < 32; dev++) {
+for (func = 0; func < 8; func++) {
+device = (struct pci_device_private *)
+ pci_device_find_by_slot(0, bus, dev, func);
+if (device) {
+*tmpdev = *device;
+tmpdev++;
+}
+}
+}
+}
+if (pci_sys->devices) {
+free(pci_sys->devices);
+pci_sys->devices = sorted;
+}
+return 0;
+}
+
 /**
  * Initialize the PCI subsystem for access.
  *
@@ -72,6 +106,9 @@ pci_system_init( void )
 #else
 # error "Unsupported OS"
 #endif
+if (!err) {
+err = sort_devices();
+}
 
 return err;
 }
diff --git a/src/hurd_pci.c b/src/hurd_pci.c
index ada7af8..ad6883a 100644
--- a/src/hurd_pci.c
+++ b/src/hurd_pci.c
@@ -33,9 +33,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include "x86_pci.h"
 #include "pciaccess.h"
@@ -302,26 +305,52 @@ pci_device_hurd_destroy(struct pci_device *dev)
 mach_port_deallocate (mach_task_self (), d->device_port);
 }
 
+static struct dirent *
+simple_readdir(mach_port_t port, uint32_t *first_entry)
+{
+struct dirent *e;
+char *data;
+int nentries = 0;
+vm_size_t size;
+
+dir_readdir (port, &data, &size, *first_entry, 1, 0, &nentries);
+
+if (nentries == 0) {
+   return NULL;
+}
+
+*first_entry = *first_entry + 1;
+e = (struct dirent *)(data+4);
+return e;
+}
+
 /* Walk through the FS tree to see what is allowed for us */
 static int
-enum_devices(const char *parent, int domain,
-int bus, int dev, int func, tree_level lev)
+enum_devices(mach_port_t pci_port, mach_port_t cwd_port, const char *parent, 
int domain,
+ int bus, int dev, int func, tree_level lev)
 {
 int err, ret;
-DIR *dir;
-struct dirent *entry;
+struct dirent *entry = NULL;
 char path[NAME_MAX];
 char server[NAME_MAX];
-uint32_t reg;
+uint32_t reg, count = 0;
 size_t toread;
 mach_port_t device_port;
 struct pci_device_private *d, *devices;
 
-dir = opendir(parent);
-if (!dir)
-return errno;
+if (pci_port == MACH_PORT_NULL) {
+return EGRATUITOUS;
+} else {
+if (lev > LEVEL_FUNC + 1) {
+return 0;
+}
+cwd_port = file_name_lookup_under (pci_port, parent, O_DIRECTORY | 
O_RDWR | O_EXEC, 0);
+if (cwd_port == MACH_PORT_NULL) {
+return 0;
+}
+}
 
-while ((entry = readdir(dir)) != 0) {
+while ((entry = simple_readdir(cwd_port, &count)) != NULL) {
 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
 if (entry->d_type == DT_DIR) {
 if (!strncmp(entry->d_name, ".", NAME_MAX)
@@ -331,7 +360,6 @@ enum_devices(const char *parent, int domain,
 errno = 0;
 ret = strtol(entry->d_name, 0, 16);
 if (errno) {
-closedir(dir);
 return errno;
 }
 
@@ -353,16 +381,12 @@ enum_devices(const char *parent, int domain,
 func = ret;
 break;
 default:
-if (closedir(dir) < 0)
-return errno;
-return -1;
+return 0;
 }
 
-err = enum_devices(path, domain, bus, dev, func, lev+1);
+err = enum_devices(pci_port, cwd_port, path, domain, bus, dev, 
func, lev+1);
 if (err && err != EPERM && err != EACCES) {
-if (closedir(dir) < 0)
-return errno;
-return err;
+return 0;
 }
 } else {
 if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
@@ -370,34 +394,27 @@ enum_devices(const char *parent, int domain,
 continue;
 
 /* We found an available virtual device, add it to our list */
-snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
-

[PATCH libpciaccess] hurd: device_open the pre-existing pci server

2021-03-06 Thread Damien Zammit
This is a revised version of previous patch.
When there is no pci "device" present in the system, the fallback
mechanism to use /servers/bus/pci works.
It also boots from a static arbiter.

---
 src/common_init.c |  37 +
 src/hurd_pci.c| 138 ++
 2 files changed, 128 insertions(+), 47 deletions(-)

diff --git a/src/common_init.c b/src/common_init.c
index 1940cff..14f0c6b 100644
--- a/src/common_init.c
+++ b/src/common_init.c
@@ -40,6 +40,40 @@
 
 _pci_hidden struct pci_system * pci_sys;
 
+static int
+sort_devices(void)
+{
+int bus, dev, func;
+struct pci_device_private *sorted;
+struct pci_device_private *device;
+struct pci_device_private *tmpdev;
+
+sorted = calloc(pci_sys->num_devices, sizeof(struct pci_device_private));
+if (!sorted) {
+return ENOMEM;
+}
+
+tmpdev = sorted;
+
+for (bus = 0; bus < 256; bus++) {
+for (dev = 0; dev < 32; dev++) {
+for (func = 0; func < 8; func++) {
+device = (struct pci_device_private *)
+ pci_device_find_by_slot(0, bus, dev, func);
+if (device) {
+*tmpdev = *device;
+tmpdev++;
+}
+}
+}
+}
+if (pci_sys->devices) {
+free(pci_sys->devices);
+pci_sys->devices = sorted;
+}
+return 0;
+}
+
 /**
  * Initialize the PCI subsystem for access.
  *
@@ -72,6 +106,9 @@ pci_system_init( void )
 #else
 # error "Unsupported OS"
 #endif
+if (!err) {
+err = sort_devices();
+}
 
 return err;
 }
diff --git a/src/hurd_pci.c b/src/hurd_pci.c
index ada7af8..3086b63 100644
--- a/src/hurd_pci.c
+++ b/src/hurd_pci.c
@@ -33,9 +33,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include "x86_pci.h"
 #include "pciaccess.h"
@@ -67,6 +70,8 @@ struct pci_system_hurd {
 struct pci_system system;
 };
 
+static mach_port_t pci_port;
+
 static int
 pci_device_hurd_probe(struct pci_device *dev)
 {
@@ -295,33 +300,64 @@ pci_device_hurd_read_rom(struct pci_device * dev, void * 
buffer)
  * Deallocate the port before destroying the device.
  */
 static void
-pci_device_hurd_destroy(struct pci_device *dev)
+pci_device_hurd_destroy_device(struct pci_device *dev)
 {
 struct pci_device_private *d = (struct pci_device_private*) dev;
 
 mach_port_deallocate (mach_task_self (), d->device_port);
 }
 
+static void
+pci_device_hurd_destroy(void)
+{
+if (pci_port != MACH_PORT_NULL) {
+device_close (pci_port);
+}
+}
+
+static struct dirent64 *
+simple_readdir(mach_port_t port, uint32_t *first_entry)
+{
+char *data;
+int nentries = 0;
+vm_size_t size;
+
+dir_readdir (port, &data, &size, *first_entry, 1, 0, &nentries);
+
+if (nentries == 0) {
+return NULL;
+}
+
+*first_entry = *first_entry + 1;
+return (struct dirent64 *)data;
+}
+
 /* Walk through the FS tree to see what is allowed for us */
 static int
-enum_devices(const char *parent, int domain,
-int bus, int dev, int func, tree_level lev)
+enum_devices(mach_port_t cwd_port, const char *parent, int domain,
+ int bus, int dev, int func, tree_level lev)
 {
 int err, ret;
-DIR *dir;
-struct dirent *entry;
+struct dirent64 *entry = NULL;
 char path[NAME_MAX];
 char server[NAME_MAX];
-uint32_t reg;
+uint32_t reg, count = 0;
 size_t toread;
 mach_port_t device_port;
 struct pci_device_private *d, *devices;
 
-dir = opendir(parent);
-if (!dir)
-return errno;
+if (pci_port == MACH_PORT_NULL) {
+return EGRATUITOUS;
+}
+if (lev > LEVEL_FUNC + 1) {
+return 0;
+}
+cwd_port = file_name_lookup_under (pci_port, parent, O_DIRECTORY | O_RDWR 
| O_EXEC, 0);
+if (cwd_port == MACH_PORT_NULL) {
+return 0;
+}
 
-while ((entry = readdir(dir)) != 0) {
+while ((entry = simple_readdir(cwd_port, &count)) != NULL) {
 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
 if (entry->d_type == DT_DIR) {
 if (!strncmp(entry->d_name, ".", NAME_MAX)
@@ -331,7 +367,6 @@ enum_devices(const char *parent, int domain,
 errno = 0;
 ret = strtol(entry->d_name, 0, 16);
 if (errno) {
-closedir(dir);
 return errno;
 }
 
@@ -353,16 +388,12 @@ enum_devices(const char *parent, int domain,
 func = ret;
 break;
 default:
-if (closedir(dir) < 0)
-return errno;
-return -1;
+return 0;
 }
 
-err = enum_devices(path, domain, bus, dev, func, lev+1);
+err = enum_devices(cwd_port, path, domain, bus, dev, func, lev+1);
 if (err && err != EPERM && err != EACCES) {
-if (closedir(dir) <

[PATCH libpciaccess x2]

2021-03-06 Thread Damien Zammit


This splits the previous patch into two, one for sorting devices,
one for hurd improvements.

Thanks,
Damien



[PATCH libpciaccess 1/2] common_init: Sort pci devices by B/D/F

2021-03-06 Thread Damien Zammit
---
 src/common_init.c | 37 +
 1 file changed, 37 insertions(+)

diff --git a/src/common_init.c b/src/common_init.c
index 1940cff..14f0c6b 100644
--- a/src/common_init.c
+++ b/src/common_init.c
@@ -40,6 +40,40 @@
 
 _pci_hidden struct pci_system * pci_sys;
 
+static int
+sort_devices(void)
+{
+int bus, dev, func;
+struct pci_device_private *sorted;
+struct pci_device_private *device;
+struct pci_device_private *tmpdev;
+
+sorted = calloc(pci_sys->num_devices, sizeof(struct pci_device_private));
+if (!sorted) {
+return ENOMEM;
+}
+
+tmpdev = sorted;
+
+for (bus = 0; bus < 256; bus++) {
+for (dev = 0; dev < 32; dev++) {
+for (func = 0; func < 8; func++) {
+device = (struct pci_device_private *)
+ pci_device_find_by_slot(0, bus, dev, func);
+if (device) {
+*tmpdev = *device;
+tmpdev++;
+}
+}
+}
+}
+if (pci_sys->devices) {
+free(pci_sys->devices);
+pci_sys->devices = sorted;
+}
+return 0;
+}
+
 /**
  * Initialize the PCI subsystem for access.
  *
@@ -72,6 +106,9 @@ pci_system_init( void )
 #else
 # error "Unsupported OS"
 #endif
+if (!err) {
+err = sort_devices();
+}
 
 return err;
 }
-- 
2.30.1




[PATCH libpciaccess 2/2] hurd: device_open(pci), /servers/bus/pci fallback

2021-03-06 Thread Damien Zammit
---
 src/hurd_pci.c | 135 -
 1 file changed, 88 insertions(+), 47 deletions(-)

diff --git a/src/hurd_pci.c b/src/hurd_pci.c
index ada7af8..6b6e299 100644
--- a/src/hurd_pci.c
+++ b/src/hurd_pci.c
@@ -33,9 +33,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include "x86_pci.h"
 #include "pciaccess.h"
@@ -67,6 +70,8 @@ struct pci_system_hurd {
 struct pci_system system;
 };
 
+static mach_port_t pci_port;
+
 static int
 pci_device_hurd_probe(struct pci_device *dev)
 {
@@ -295,33 +300,64 @@ pci_device_hurd_read_rom(struct pci_device * dev, void * 
buffer)
  * Deallocate the port before destroying the device.
  */
 static void
-pci_device_hurd_destroy(struct pci_device *dev)
+pci_device_hurd_destroy_device(struct pci_device *dev)
 {
 struct pci_device_private *d = (struct pci_device_private*) dev;
 
 mach_port_deallocate (mach_task_self (), d->device_port);
 }
 
+static void
+pci_device_hurd_destroy(void)
+{
+if (pci_port != MACH_PORT_NULL) {
+device_close (pci_port);
+}
+}
+
+static struct dirent64 *
+simple_readdir(mach_port_t port, uint32_t *first_entry)
+{
+char *data;
+int nentries = 0;
+vm_size_t size;
+
+dir_readdir (port, &data, &size, *first_entry, 1, 0, &nentries);
+
+if (nentries == 0) {
+return NULL;
+}
+
+*first_entry = *first_entry + 1;
+return (struct dirent64 *)data;
+}
+
 /* Walk through the FS tree to see what is allowed for us */
 static int
-enum_devices(const char *parent, int domain,
-int bus, int dev, int func, tree_level lev)
+enum_devices(mach_port_t cwd_port, const char *parent, int domain,
+ int bus, int dev, int func, tree_level lev)
 {
 int err, ret;
-DIR *dir;
-struct dirent *entry;
+struct dirent64 *entry = NULL;
 char path[NAME_MAX];
 char server[NAME_MAX];
-uint32_t reg;
+uint32_t reg, count = 0;
 size_t toread;
 mach_port_t device_port;
 struct pci_device_private *d, *devices;
 
-dir = opendir(parent);
-if (!dir)
-return errno;
+if (pci_port == MACH_PORT_NULL) {
+return EGRATUITOUS;
+}
+if (lev > LEVEL_FUNC + 1) {
+return 0;
+}
+cwd_port = file_name_lookup_under (pci_port, parent, O_DIRECTORY | O_RDWR 
| O_EXEC, 0);
+if (cwd_port == MACH_PORT_NULL) {
+return 0;
+}
 
-while ((entry = readdir(dir)) != 0) {
+while ((entry = simple_readdir(cwd_port, &count)) != NULL) {
 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
 if (entry->d_type == DT_DIR) {
 if (!strncmp(entry->d_name, ".", NAME_MAX)
@@ -331,7 +367,6 @@ enum_devices(const char *parent, int domain,
 errno = 0;
 ret = strtol(entry->d_name, 0, 16);
 if (errno) {
-closedir(dir);
 return errno;
 }
 
@@ -353,16 +388,12 @@ enum_devices(const char *parent, int domain,
 func = ret;
 break;
 default:
-if (closedir(dir) < 0)
-return errno;
-return -1;
+return 0;
 }
 
-err = enum_devices(path, domain, bus, dev, func, lev+1);
+err = enum_devices(cwd_port, path, domain, bus, dev, func, lev+1);
 if (err && err != EPERM && err != EACCES) {
-if (closedir(dir) < 0)
-return errno;
-return err;
+return 0;
 }
 } else {
 if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
@@ -370,34 +401,30 @@ enum_devices(const char *parent, int domain,
 continue;
 
 /* We found an available virtual device, add it to our list */
-snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
- _SERVERS_BUS_PCI, domain, bus, dev, func,
+snprintf(server, NAME_MAX, "./%04x/%02x/%02x/%01u/%s",
+ domain, bus, dev, func,
  entry->d_name);
-device_port = file_name_lookup(server, 0, 0);
+device_port = file_name_lookup_under(pci_port, server, O_RDWR, 0);
 if (device_port == MACH_PORT_NULL) {
-closedir(dir);
-return errno;
+return 0;
 }
 
 toread = sizeof(reg);
 err = pciclient_cfg_read(device_port, PCI_VENDOR_ID, (char*)®,
  &toread);
 if (err) {
-if (closedir(dir) < 0)
-return errno;
+mach_port_deallocate (mach_task_self (), device_port);
 return err;
 }
 if (toread != sizeof(reg)) {
-if (closedir(dir) < 0)
-return errno;
+mach_port_deallocate (mach_task_self (), device_port);

[PATCH libpciaccess 2/2] hurd: device_open(pci), /servers/bus/pci fallback

2021-03-06 Thread Damien Zammit
---
 src/hurd_pci.c | 122 ++---
 1 file changed, 74 insertions(+), 48 deletions(-)

diff --git a/src/hurd_pci.c b/src/hurd_pci.c
index ada7af8..d988a6e 100644
--- a/src/hurd_pci.c
+++ b/src/hurd_pci.c
@@ -33,9 +33,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include "x86_pci.h"
 #include "pciaccess.h"
@@ -295,33 +298,53 @@ pci_device_hurd_read_rom(struct pci_device * dev, void * 
buffer)
  * Deallocate the port before destroying the device.
  */
 static void
-pci_device_hurd_destroy(struct pci_device *dev)
+pci_device_hurd_destroy_device(struct pci_device *dev)
 {
 struct pci_device_private *d = (struct pci_device_private*) dev;
 
 mach_port_deallocate (mach_task_self (), d->device_port);
 }
 
+static struct dirent64 *
+simple_readdir(mach_port_t port, uint32_t *first_entry)
+{
+char *data;
+int nentries = 0;
+vm_size_t size;
+
+dir_readdir (port, &data, &size, *first_entry, 1, 0, &nentries);
+
+if (nentries == 0) {
+return NULL;
+}
+
+*first_entry = *first_entry + 1;
+return (struct dirent64 *)data;
+}
+
 /* Walk through the FS tree to see what is allowed for us */
 static int
-enum_devices(const char *parent, int domain,
-int bus, int dev, int func, tree_level lev)
+enum_devices(mach_port_t pci_port, const char *parent, int domain,
+ int bus, int dev, int func, tree_level lev)
 {
 int err, ret;
-DIR *dir;
-struct dirent *entry;
+struct dirent64 *entry = NULL;
 char path[NAME_MAX];
 char server[NAME_MAX];
-uint32_t reg;
+uint32_t reg, count = 0;
 size_t toread;
-mach_port_t device_port;
+mach_port_t cwd_port, device_port;
 struct pci_device_private *d, *devices;
 
-dir = opendir(parent);
-if (!dir)
-return errno;
+if (lev > LEVEL_FUNC + 1) {
+return 0;
+}
+cwd_port = file_name_lookup_under (pci_port, parent, O_DIRECTORY | O_RDWR 
| O_EXEC, 0);
+if (cwd_port == MACH_PORT_NULL) {
+return 0;
+}
 
-while ((entry = readdir(dir)) != 0) {
+while ((entry = simple_readdir(cwd_port, &count)) != NULL) {
 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
 if (entry->d_type == DT_DIR) {
 if (!strncmp(entry->d_name, ".", NAME_MAX)
@@ -331,7 +354,6 @@ enum_devices(const char *parent, int domain,
 errno = 0;
 ret = strtol(entry->d_name, 0, 16);
 if (errno) {
-closedir(dir);
 return errno;
 }
 
@@ -353,16 +375,12 @@ enum_devices(const char *parent, int domain,
 func = ret;
 break;
 default:
-if (closedir(dir) < 0)
-return errno;
-return -1;
+return 0;
 }
 
-err = enum_devices(path, domain, bus, dev, func, lev+1);
+err = enum_devices(pci_port, path, domain, bus, dev, func, lev+1);
 if (err && err != EPERM && err != EACCES) {
-if (closedir(dir) < 0)
-return errno;
-return err;
+return 0;
 }
 } else {
 if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
@@ -370,34 +388,30 @@ enum_devices(const char *parent, int domain,
 continue;
 
 /* We found an available virtual device, add it to our list */
-snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
- _SERVERS_BUS_PCI, domain, bus, dev, func,
+snprintf(server, NAME_MAX, "./%04x/%02x/%02x/%01u/%s",
+ domain, bus, dev, func,
  entry->d_name);
-device_port = file_name_lookup(server, 0, 0);
+device_port = file_name_lookup_under(pci_port, server, O_RDWR, 0);
 if (device_port == MACH_PORT_NULL) {
-closedir(dir);
-return errno;
+return 0;
 }
 
 toread = sizeof(reg);
 err = pciclient_cfg_read(device_port, PCI_VENDOR_ID, (char*)®,
  &toread);
 if (err) {
-if (closedir(dir) < 0)
-return errno;
+mach_port_deallocate (mach_task_self (), device_port);
 return err;
 }
 if (toread != sizeof(reg)) {
-if (closedir(dir) < 0)
-return errno;
+mach_port_deallocate (mach_task_self (), device_port);
 return -1;
 }
 
 devices = realloc(pci_sys->devices, (pci_sys->num_devices + 1)
   * sizeof(struct pci_device_private));
 if (!devices) {
-if (closedir(dir) < 0)
-return errno;
+mach_port_deallocate (mach_ta

[PATCH libpciaccess 2/2] hurd: device_open(pci), /servers/bus/pci fallback

2021-03-06 Thread Damien Zammit
Reverted change to .destroy

---
 src/hurd_pci.c | 121 ++---
 1 file changed, 74 insertions(+), 47 deletions(-)

diff --git a/src/hurd_pci.c b/src/hurd_pci.c
index ada7af8..40c9925 100644
--- a/src/hurd_pci.c
+++ b/src/hurd_pci.c
@@ -33,9 +33,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
+#include 
+#include 
 
 #include "x86_pci.h"
 #include "pciaccess.h"
@@ -295,33 +298,53 @@ pci_device_hurd_read_rom(struct pci_device * dev, void * 
buffer)
  * Deallocate the port before destroying the device.
  */
 static void
-pci_device_hurd_destroy(struct pci_device *dev)
+pci_device_hurd_destroy_device(struct pci_device *dev)
 {
 struct pci_device_private *d = (struct pci_device_private*) dev;
 
 mach_port_deallocate (mach_task_self (), d->device_port);
 }
 
+static struct dirent64 *
+simple_readdir(mach_port_t port, uint32_t *first_entry)
+{
+char *data;
+int nentries = 0;
+vm_size_t size;
+
+dir_readdir (port, &data, &size, *first_entry, 1, 0, &nentries);
+
+if (nentries == 0) {
+return NULL;
+}
+
+*first_entry = *first_entry + 1;
+return (struct dirent64 *)data;
+}
+
 /* Walk through the FS tree to see what is allowed for us */
 static int
-enum_devices(const char *parent, int domain,
-int bus, int dev, int func, tree_level lev)
+enum_devices(mach_port_t pci_port, const char *parent, int domain,
+ int bus, int dev, int func, tree_level lev)
 {
 int err, ret;
-DIR *dir;
-struct dirent *entry;
+struct dirent64 *entry = NULL;
 char path[NAME_MAX];
 char server[NAME_MAX];
-uint32_t reg;
+uint32_t reg, count = 0;
 size_t toread;
-mach_port_t device_port;
+mach_port_t cwd_port, device_port;
 struct pci_device_private *d, *devices;
 
-dir = opendir(parent);
-if (!dir)
-return errno;
+if (lev > LEVEL_FUNC + 1) {
+return 0;
+}
+cwd_port = file_name_lookup_under (pci_port, parent, O_DIRECTORY | O_RDWR 
| O_EXEC, 0);
+if (cwd_port == MACH_PORT_NULL) {
+return 0;
+}
 
-while ((entry = readdir(dir)) != 0) {
+while ((entry = simple_readdir(cwd_port, &count)) != NULL) {
 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
 if (entry->d_type == DT_DIR) {
 if (!strncmp(entry->d_name, ".", NAME_MAX)
@@ -331,7 +354,6 @@ enum_devices(const char *parent, int domain,
 errno = 0;
 ret = strtol(entry->d_name, 0, 16);
 if (errno) {
-closedir(dir);
 return errno;
 }
 
@@ -353,16 +375,12 @@ enum_devices(const char *parent, int domain,
 func = ret;
 break;
 default:
-if (closedir(dir) < 0)
-return errno;
-return -1;
+return 0;
 }
 
-err = enum_devices(path, domain, bus, dev, func, lev+1);
+err = enum_devices(pci_port, path, domain, bus, dev, func, lev+1);
 if (err && err != EPERM && err != EACCES) {
-if (closedir(dir) < 0)
-return errno;
-return err;
+return 0;
 }
 } else {
 if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
@@ -370,34 +388,30 @@ enum_devices(const char *parent, int domain,
 continue;
 
 /* We found an available virtual device, add it to our list */
-snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
- _SERVERS_BUS_PCI, domain, bus, dev, func,
+snprintf(server, NAME_MAX, "./%04x/%02x/%02x/%01u/%s",
+ domain, bus, dev, func,
  entry->d_name);
-device_port = file_name_lookup(server, 0, 0);
+device_port = file_name_lookup_under(pci_port, server, O_RDWR, 0);
 if (device_port == MACH_PORT_NULL) {
-closedir(dir);
-return errno;
+return 0;
 }
 
 toread = sizeof(reg);
 err = pciclient_cfg_read(device_port, PCI_VENDOR_ID, (char*)®,
  &toread);
 if (err) {
-if (closedir(dir) < 0)
-return errno;
+mach_port_deallocate (mach_task_self (), device_port);
 return err;
 }
 if (toread != sizeof(reg)) {
-if (closedir(dir) < 0)
-return errno;
+mach_port_deallocate (mach_task_self (), device_port);
 return -1;
 }
 
 devices = realloc(pci_sys->devices, (pci_sys->num_devices + 1)
   * sizeof(struct pci_device_private));
 if (!devices) {
-if (closedir(dir) < 0)
-return errno;
+

Re: [PATCH libpciaccess 2/2] hurd: device_open(pci), /servers/bus/pci fallback

2021-03-06 Thread Damien Zammit
On 7/3/21 11:51 am, Samuel Thibault wrote:
> That looks good, I'd say you can submit to upstream libpciaccess :)
> 
> Samuel

I created an issue upstream with a merge request that fixes it:

https://gitlab.freedesktop.org/xorg/lib/libpciaccess/-/issues/14

Damien



[PATCH 1/6] pci-arbiter: Fix brokenness

2021-03-06 Thread Damien Zammit
---
 pci-arbiter/netfs_impl.c | 42 ++--
 pci-arbiter/pci-ops.c|  6 +++---
 pci-arbiter/pcifs.c  | 18 +++--
 3 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/pci-arbiter/netfs_impl.c b/pci-arbiter/netfs_impl.c
index b987a0bc..e977dce3 100644
--- a/pci-arbiter/netfs_impl.c
+++ b/pci-arbiter/netfs_impl.c
@@ -59,25 +59,19 @@ get_dirents (struct pcifs_dirent *dir,
   int i, count;
   size_t size;
   char *p;
+  int nentries = (int)dir->dir->num_entries;
 
-  if (first_entry >= dir->dir->num_entries)
+  if (first_entry >= nentries)
 {
   *data_len = 0;
   *data_entries = 0;
   return 0;
 }
 
-  if (max_entries < 0)
-count = dir->dir->num_entries;
-  else
-{
-  count = ((first_entry + max_entries) >= dir->dir->num_entries ?
-  dir->dir->num_entries : max_entries) - first_entry;
-}
-
-  size =
-(count * DIRENTS_CHUNK_SIZE) >
-max_data_len ? max_data_len : count * DIRENTS_CHUNK_SIZE;
+  count = nentries - first_entry;
+  if (max_entries > 0 && count > max_entries)
+count = max_entries;
+  size = count * DIRENTS_CHUNK_SIZE;
 
   *data = mmap (0, size, PROT_READ | PROT_WRITE, MAP_ANON, 0, 0);
   err = ((void *) *data == (void *) -1) ? errno : 0;
@@ -234,8 +228,8 @@ netfs_get_dirents (struct iouser * cred, struct node * dir,
 
   if (dir->nn->ln->dir)
 {
-  err = get_dirents (dir->nn->ln, first_entry, max_entries,
-data, data_len, max_entries, data_entries);
+  err = get_dirents (dir->nn->ln, first_entry, -1,
+data, data_len, 0, data_entries);
 }
   else
 err = ENOTDIR;
@@ -257,6 +251,24 @@ netfs_attempt_lookup (struct iouser * user, struct node * 
dir,
 {
   error_t err = 0;
   struct pcifs_dirent *entry;
+  char *last = name;
+
+  /* Strip trailing slashes */
+  if (*last)
+{
+  while (*last)
+last++;
+  last--;
+  while (*last == '/' && last >= name)
+{
+  *last = '\0';
+  last--;
+}
+}
+
+/* Skip leading dot slashes */
+while (*name == '.' && *(name+1) == '/')
+  name += 2;
 
   if (*name == '\0' || strcmp (name, ".") == 0)
 /* Current directory -- just add an additional reference to DIR's node
@@ -292,7 +304,7 @@ netfs_attempt_lookup (struct iouser * user, struct node * 
dir,
   /* `dir' is a directory */
 
   /* Check dir permissions */
-  err = entry_check_perms (user, dir->nn->ln, O_READ | O_EXEC);
+  err = netfs_check_open_permissions (user, dir, O_READ | O_EXEC, 0);
   if (!err)
{
  entry = lookup (dir, name);
diff --git a/pci-arbiter/pci-ops.c b/pci-arbiter/pci-ops.c
index 3346e55a..07228fbe 100644
--- a/pci-arbiter/pci-ops.c
+++ b/pci-arbiter/pci-ops.c
@@ -39,12 +39,12 @@ check_permissions (struct protid *master, int flags)
   node = master->po->np;
   e = node->nn->ln;
 
-  /* Check wheter the user has permissions to access this node */
-  err = entry_check_perms (master->user, e, flags);
+  /* Check whether the user has permissions to access this node */
+  err = netfs_check_open_permissions (master->user, node, flags, 0);
   if (err)
 return err;
 
-  /* Check wheter the request has been sent to the proper node */
+  /* Check whether the request has been sent to the proper node */
   if (e->domain != 0   /* Only domain 0 can be accessed by I/O ports */
   || e->bus < 0 || e->dev < 0 || e->func < 0)
 err = EINVAL;
diff --git a/pci-arbiter/pcifs.c b/pci-arbiter/pcifs.c
index 0535779e..e09bdd13 100644
--- a/pci-arbiter/pcifs.c
+++ b/pci-arbiter/pcifs.c
@@ -31,6 +31,9 @@
 #include "ncache.h"
 #include "func_files.h"
 
+/* Empty status for root node when bootstrapping */
+static io_statbuf_t underlying_stat;
+
 static error_t
 create_dir_entry (int32_t domain, int16_t bus, int16_t dev,
  int16_t func, int32_t device_class, char *name,
@@ -93,17 +96,20 @@ init_file_system (file_t underlying_node, struct pcifs * fs)
 {
   error_t err;
   struct node *np;
-  io_statbuf_t underlying_node_stat;
+  io_statbuf_t *underlying_node_stat = &underlying_stat;
 
-  /* Initialize status from underlying node.  */
-  err = io_stat (underlying_node, &underlying_node_stat);
-  if (err)
-return err;
+  if (underlying_node)
+{
+  /* Initialize status from underlying node.  */
+  err = io_stat (underlying_node, underlying_node_stat);
+  if (err)
+return err;
+}
 
   np = netfs_make_node_alloc (sizeof (struct netnode));
   if (!np)
 return ENOMEM;
-  np->nn_stat = underlying_node_stat;
+  np->nn_stat = *underlying_node_stat;
   np->nn_stat.st_fsid = getpid ();
   np->nn_stat.st_mode =
 S_IFDIR | S_IROOT | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH |
-- 
2.30.1




[PATCH 2/6] pci-arbiter: Introduce machdev into this server

2021-03-06 Thread Damien Zammit
---
 pci-arbiter/Makefile  |   7 +-
 pci-arbiter/main.c| 147 +++---
 pci-arbiter/options.c |  19 +
 pci-arbiter/options.h |   8 +++
 pci-arbiter/pcifs.h   |   3 +
 pci-arbiter/startup-ops.c |   2 +
 6 files changed, 174 insertions(+), 12 deletions(-)

diff --git a/pci-arbiter/Makefile b/pci-arbiter/Makefile
index 1d0309dc..3f374d18 100644
--- a/pci-arbiter/Makefile
+++ b/pci-arbiter/Makefile
@@ -22,11 +22,10 @@ PORTDIR = $(srcdir)/port
 
 SRCS   = main.c pci-ops.c netfs_impl.c \
  pcifs.c ncache.c options.c func_files.c startup.c \
- startup-ops.c
-MIGSRCS= pciServer.c startup_notifyServer.c
-OBJS   = $(patsubst %.S,%.o,$(patsubst %.c,%.o, $(SRCS) $(MIGSRCS)))
+ startup-ops.c pciServer.c startup_notifyServer.c
+OBJS   = $(SRCS:.c=.o) $(MIGSTUBS)
 
-HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash
+HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash trivfs machdev
 LDLIBS = -lpthread $(libpciaccess_LIBS)
 
 target = pci-arbiter
diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 181bdee2..33e9053a 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -24,7 +24,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
+#include 
+#include 
 
 #include 
 #include 
@@ -34,7 +39,9 @@
 #include "libnetfs/fsys_S.h"
 #include "libports/interrupt_S.h"
 #include "libnetfs/ifsock_S.h"
+#include "libmachdev/machdev.h"
 #include 
+#include 
 #include "pcifs.h"
 #include "startup.h"
 
@@ -46,6 +53,62 @@ int netfs_maxsymlinks = 0;
 char *netfs_server_name = "pci-arbiter";
 char *netfs_server_version = HURD_VERSION;
 
+static mach_port_t pci_control_port;
+
+
+static io_return_t
+pci_device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
+ dev_mode_t mode, char *name, device_t * devp,
+ mach_msg_type_name_t * devicePoly)
+{
+  io_return_t err = D_SUCCESS;
+  mach_port_t dev_master, root;
+
+  if (strncmp(name, "pci", 3))
+err = D_NO_SUCH_DEVICE;
+
+  /* Fall back to opening kernel device master */
+  if (err)
+{
+  err = get_privileged_ports(NULL, &dev_master);
+  if (err)
+return err;
+  if (dev_master == MACH_PORT_NULL)
+return D_NO_SUCH_DEVICE;
+  err = device_open (dev_master, mode, name, devp);
+  if (err)
+return err;
+  *devicePoly = MACH_MSG_TYPE_MOVE_SEND;
+  return D_SUCCESS;
+}
+
+  root = pci_control_port;
+  *devp = root;
+  *devicePoly = MACH_MSG_TYPE_COPY_SEND;
+  return D_SUCCESS;
+}
+
+static struct machdev_device_emulation_ops pci_arbiter_emulation_ops = {
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  pci_device_open,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+};
+
 int
 netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outp)
 {
@@ -67,24 +130,82 @@ netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t 
* outp)
 return FALSE;
 }
 
+static void *
+netfs_server_func (void *arg)
+{
+  error_t err;
+
+  do 
+{
+  ports_manage_port_operations_multithread (netfs_port_bucket,
+   netfs_demuxer,
+   1000 * 60 * 2, /* two minutes 
thread */
+   1000 * 60 * 10,/* ten minutes 
server */
+   0);
+  err = netfs_shutdown (0);
+}
+  while (err);
+  return NULL;
+}
+
+
+static mach_port_t
+pcifs_startup(mach_port_t bootstrap, int flags)
+{
+  error_t err;
+  mach_port_t realnode;
+  struct port_info *newpi;
+
+  err = ports_create_port (netfs_control_class, netfs_port_bucket,
+sizeof (struct port_info), &newpi);
+  if (!err)
+{
+  pci_control_port = ports_get_send_right (newpi);
+  err = fsys_startup (bootstrap, flags, pci_control_port, 
MACH_MSG_TYPE_COPY_SEND,
+   &realnode);
+  assert_perror_backtrace (err);
+}
+  if (err)
+error (11, err, "Translator startup failure: fsys_startup");
+
+  return realnode;
+}
+
 int
 main (int argc, char **argv)
 {
   error_t err;
   mach_port_t bootstrap;
+  mach_port_t disk_server_task;
+  pthread_t t, nt;
 
   /* Parse options */
   alloc_file_system (&fs);
   argp_parse (netfs_runtime_argp, argc, argv, 0, 0, 0);
+  disk_server_task = fs->params.disk_server_task;
 
-  task_get_bootstrap_port (mach_task_self (), &bootstrap);
-  if (bootstrap == MACH_PORT_NULL)
-error (1, 0, "must be started as a translator");
-
+  if (disk_server_task != MACH_PORT_NULL)
+{
+  machdev_register (&pci_arbiter_emulation_ops);
+  machdev_device_init ();
+  machdev_trivfs_init (disk_server_task, "pci", "/servers/bus/pci", 
&bootstrap);
+  err = pthread_create (&t, NULL, machdev_server, NULL);
+  if (err)
+error (1

[PATCH 3/6] libnetfs: Hacks to allow pci to run with no cred

2021-03-06 Thread Damien Zammit
---
 libnetfs/dir-lookup.c  | 30 +-
 libnetfs/dir-readdir.c |  3 ---
 libnetfs/file-getcontrol.c | 13 ++---
 libnetfs/make-protid.c |  8 +---
 pci-arbiter/netfs_impl.c   |  2 ++
 5 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/libnetfs/dir-lookup.c b/libnetfs/dir-lookup.c
index 23adeb33..9bf78225 100644
--- a/libnetfs/dir-lookup.c
+++ b/libnetfs/dir-lookup.c
@@ -50,9 +50,6 @@ netfs_S_dir_lookup (struct protid *dircred,
   struct protid *newpi = NULL;
   struct iouser *user;
 
-  if (!dircred)
-return EOPNOTSUPP;
-
   create = (flags & O_CREAT);
   excl = (flags & O_EXCL);
 
@@ -73,6 +70,12 @@ netfs_S_dir_lookup (struct protid *dircred,
   *do_retry = FS_RETRY_NORMAL;
   *retry_name = '\0';
 
+  if (!dircred)
+{
+  dircred = netfs_make_protid (netfs_make_peropen (netfs_root_node, flags, 
0), 0);
+  dircred->po->path = filename;
+}
+
   if (*filename == '\0')
 {
   /* Set things up in the state expected by the code from gotit: on. */
@@ -423,15 +426,24 @@ netfs_S_dir_lookup (struct protid *dircred,
 
   flags &= ~OPENONLY_STATE_MODES;
 
-  err = iohelp_dup_iouser (&user, dircred->user);
-  if (err)
-goto out;
+  if (dircred->user)
+{
+  err = iohelp_dup_iouser (&user, dircred->user);
+  if (err)
+goto out;
+}
+  else
+{
+  user = NULL;
+  err = 0;
+}
+
+  newpi = netfs_make_protid (netfs_make_peropen (np, flags, dircred->po), 
user);
 
-  newpi = netfs_make_protid (netfs_make_peropen (np, flags, dircred->po),
-user);
   if (! newpi)
 {
-  iohelp_free_iouser (user);
+  if (user)
+iohelp_free_iouser (user);
   err = errno;
   goto out;
 }
diff --git a/libnetfs/dir-readdir.c b/libnetfs/dir-readdir.c
index 3d40dfa5..83dbf2d4 100644
--- a/libnetfs/dir-readdir.c
+++ b/libnetfs/dir-readdir.c
@@ -36,9 +36,6 @@ netfs_S_dir_readdir (struct protid *user,
   error_t err;
   struct node *np;
 
-  if (!user)
-return EOPNOTSUPP;
-
   np = user->po->np;
   pthread_mutex_lock (&np->lock);
 
diff --git a/libnetfs/file-getcontrol.c b/libnetfs/file-getcontrol.c
index 0e29ccf9..833fe196 100644
--- a/libnetfs/file-getcontrol.c
+++ b/libnetfs/file-getcontrol.c
@@ -31,13 +31,12 @@ netfs_S_file_getcontrol (struct protid *user,
   error_t err;
   struct port_info *pi;
 
-  if (!user)
-return EOPNOTSUPP;
-
-  err = fshelp_iscontroller (&netfs_root_node->nn_stat, user->user);
-  if (err)
-return err;
-  
+  if (user)
+{
+  err = fshelp_iscontroller (&netfs_root_node->nn_stat, user->user);
+  if (err)
+return err;
+} 
   /* They've have the appropriate credentials; give it to them. */
   err = ports_create_port (netfs_control_class, netfs_port_bucket,
   sizeof (struct port_info), &pi);
diff --git a/libnetfs/make-protid.c b/libnetfs/make-protid.c
index 995ac1ea..2087f220 100644
--- a/libnetfs/make-protid.c
+++ b/libnetfs/make-protid.c
@@ -25,14 +25,8 @@ netfs_make_protid (struct peropen *po, struct iouser *cred)
 {
   struct protid *pi;
 
-  if (cred)
-errno = ports_create_port (netfs_protid_class, netfs_port_bucket, 
+  errno = ports_create_port (netfs_protid_class, netfs_port_bucket, 
   sizeof (struct protid), &pi);
-  else
-errno = ports_create_port_noinstall (netfs_protid_class,
-netfs_port_bucket, 
-sizeof (struct protid), &pi);
-
   if (errno)
 return 0;
 
diff --git a/pci-arbiter/netfs_impl.c b/pci-arbiter/netfs_impl.c
index e977dce3..22c35292 100644
--- a/pci-arbiter/netfs_impl.c
+++ b/pci-arbiter/netfs_impl.c
@@ -179,6 +179,8 @@ error_t
 netfs_check_open_permissions (struct iouser * user, struct node * node,
  int flags, int newnode)
 {
+  if (!user)
+return 0;
   return entry_check_perms (user, node->nn->ln, flags);
 }
 
-- 
2.30.1




[PATCH 4/6] rumpdisk: Don't pretend to be the arbiter anymore

2021-03-06 Thread Damien Zammit
---
 rumpdisk/block-rump.c | 16 +---
 rumpdisk/main.c   |  5 -
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/rumpdisk/block-rump.c b/rumpdisk/block-rump.c
index 3ee0773d..584056f7 100644
--- a/rumpdisk/block-rump.c
+++ b/rumpdisk/block-rump.c
@@ -27,6 +27,7 @@
 
 #include 
 #include 
+#include  /* fsys_getpriv */
 #include 
 #include 
 
@@ -136,12 +137,12 @@ dev_mode_to_rump_mode (const dev_mode_t mode)
 static void
 rumpdisk_device_init (void)
 {
-  mach_port_t device_master;
+  mach_port_t bootport, device_master;
+  device_t device;
+  task_t fstask;
 
   if (! get_privileged_ports (0, &device_master))
 {
-  device_t device;
-
 #if 0
   if (! device_open (device_master, D_READ, "hd0", &device)
|| ! device_open (device_master, D_READ, "hd1", &device)
@@ -164,6 +165,15 @@ rumpdisk_device_init (void)
  return;
}
 }
+
+  /* FIXME: need to synchronise with arbiter to wait for fsys_startup */
+  sleep(1);
+  
+  task_get_bootstrap_port (mach_task_self (), &bootport);
+  fsys_getpriv (bootport, &_hurd_host_priv, &_hurd_device_master, &fstask);
+
+  sleep(1);
+
   rump_init ();
 }
 
diff --git a/rumpdisk/main.c b/rumpdisk/main.c
index a26e17cb..c01372eb 100644
--- a/rumpdisk/main.c
+++ b/rumpdisk/main.c
@@ -31,11 +31,6 @@
 #include 
 #include 
 
-/* TODO: Add api to pciaccess to allow selecting backend.
- * For now we pretend to be the arbiter and claim x86 method.
- */
-char *netfs_server_name = "pci-arbiter";
-
 mach_port_t bootstrap_resume_task = MACH_PORT_NULL;
 
 static const struct argp_option options[] = {
-- 
2.30.1




[PATCH: hurd x6]

2021-03-06 Thread Damien Zammit
Hi,

[PATCH 1/6] pci-arbiter: Fix brokenness
[PATCH 2/6] pci-arbiter: Introduce machdev into this server

First one or both patches could be applied as is to the tree,
and work with the new libpciaccess.


[PATCH 3/6] libnetfs: Hacks to allow pci to run with no cred

This is still needed to bypass the cred uid=0.
I tried to call fsys_getroot, but it failed/hung when running it
at the point where I needed the root port.
Can we live with this patch for now?


[PATCH 4/6] rumpdisk: Don't pretend to be the arbiter anymore
[PATCH 5/6] machdev,pci,rump: fix shutdown dosync
[PATCH 6/6] pci-arbiter: Close correctly

These follow up patches make the disk boot and fix the sync.



[PATCH 6/6] pci-arbiter: Close correctly

2021-03-06 Thread Damien Zammit
---
 pci-arbiter/main.c | 16 
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 4a10b264..ddea5aa7 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -62,7 +62,7 @@ pci_device_open (mach_port_t reply_port, mach_msg_type_name_t 
reply_port_type,
  mach_msg_type_name_t * devicePoly)
 {
   io_return_t err = D_SUCCESS;
-  mach_port_t dev_master, root;
+  mach_port_t dev_master;
 
   if (strncmp(name, "pci", 3))
 err = D_NO_SUCH_DEVICE;
@@ -82,12 +82,18 @@ pci_device_open (mach_port_t reply_port, 
mach_msg_type_name_t reply_port_type,
   return D_SUCCESS;
 }
 
-  root = pci_control_port;
-  *devp = root;
+  *devp = pci_control_port;
   *devicePoly = MACH_MSG_TYPE_COPY_SEND;
   return D_SUCCESS;
 }
 
+static io_return_t
+pci_device_close (void *d)
+{
+  ports_port_deref (&pci_control_port);
+  return 0;
+}
+
 static void
 pci_device_shutdown (mach_port_t dosync_handle)
 {
@@ -102,6 +108,8 @@ pci_device_shutdown (mach_port_t dosync_handle)
 
   ports_port_deref (inpi);
 
+  ports_destroy_right (&pci_control_port);
+
   netfs_shutdown (FSYS_GOAWAY_FORCE);
 }
 
@@ -111,7 +119,7 @@ static struct machdev_device_emulation_ops 
pci_arbiter_emulation_ops = {
   NULL,
   NULL,
   pci_device_open,
-  NULL,
+  pci_device_close,
   NULL,
   NULL,
   NULL,
-- 
2.30.1




[PATCH 5/6] machdev,pci,rump: fix shutdown dosync

2021-03-06 Thread Damien Zammit
---
 libmachdev/ds_routines.c |  4 +--
 libmachdev/machdev-device_emul.h |  2 +-
 libmachdev/machdev.h |  2 +-
 libmachdev/trivfs_server.c   |  2 +-
 pci-arbiter/Makefile |  2 +-
 pci-arbiter/main.c   | 19 -
 pci-arbiter/startup-ops.c| 46 
 rumpdisk/block-rump.c|  2 +-
 8 files changed, 25 insertions(+), 54 deletions(-)
 delete mode 100644 pci-arbiter/startup-ops.c

diff --git a/libmachdev/ds_routines.c b/libmachdev/ds_routines.c
index d2b5352e..c2de4b26 100644
--- a/libmachdev/ds_routines.c
+++ b/libmachdev/ds_routines.c
@@ -315,13 +315,13 @@ void machdev_device_init()
 }
 }
 
-void machdev_device_shutdown()
+void machdev_device_shutdown(mach_port_t dosync_handle)
 {
   int i;
   for (i = 0; i < num_emul; i++)
 {
   if (emulation_list[i]->shutdown)
-emulation_list[i]->shutdown();
+emulation_list[i]->shutdown(dosync_handle);
 }
 }
 
diff --git a/libmachdev/machdev-device_emul.h b/libmachdev/machdev-device_emul.h
index edf79b96..7748f37f 100644
--- a/libmachdev/machdev-device_emul.h
+++ b/libmachdev/machdev-device_emul.h
@@ -64,7 +64,7 @@ struct machdev_device_emulation_ops
 recnum_t, vm_offset_t, vm_size_t);
   io_return_t (*writev_trap) (void *, dev_mode_t,
  recnum_t, io_buf_vec_t *, vm_size_t);
-  void (*shutdown) (void);
+  void (*shutdown) (mach_port_t);
 };
 
 #endif /* _MACHDEV_DEVICE_EMUL_H_ */
diff --git a/libmachdev/machdev.h b/libmachdev/machdev.h
index 41afe36d..8f613b35 100644
--- a/libmachdev/machdev.h
+++ b/libmachdev/machdev.h
@@ -30,7 +30,7 @@
 void machdev_register (struct machdev_device_emulation_ops *ops);
 
 void machdev_device_init(void);
-void machdev_device_shutdown(void);
+void machdev_device_shutdown(mach_port_t dosync_handle);
 void * machdev_server(void *);
 error_t machdev_create_device_port (size_t size, void *result);
 int machdev_trivfs_init(mach_port_t bootstrap_resume_task, const char *name, 
const char *path, mach_port_t *bootstrap);
diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index d1d3a71d..e3e4045d 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -429,7 +429,7 @@ S_startup_dosync (mach_port_t handle)
   ports_port_deref (inpi);
 
   /* Sync and close device(s) */
-  machdev_device_shutdown ();
+  machdev_device_shutdown (handle);
 
   return trivfs_goaway (NULL, FSYS_GOAWAY_FORCE);
 }
diff --git a/pci-arbiter/Makefile b/pci-arbiter/Makefile
index 3f374d18..b32bc579 100644
--- a/pci-arbiter/Makefile
+++ b/pci-arbiter/Makefile
@@ -22,7 +22,7 @@ PORTDIR = $(srcdir)/port
 
 SRCS   = main.c pci-ops.c netfs_impl.c \
  pcifs.c ncache.c options.c func_files.c startup.c \
- startup-ops.c pciServer.c startup_notifyServer.c
+ pciServer.c startup_notifyServer.c
 OBJS   = $(SRCS:.c=.o) $(MIGSTUBS)
 
 HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash trivfs machdev
diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 33e9053a..4a10b264 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -88,6 +88,23 @@ pci_device_open (mach_port_t reply_port, 
mach_msg_type_name_t reply_port_type,
   return D_SUCCESS;
 }
 
+static void
+pci_device_shutdown (mach_port_t dosync_handle)
+{
+  struct port_info *inpi = ports_lookup_port (netfs_port_bucket, dosync_handle,
+ pci_shutdown_notify_class);
+
+  if (!inpi)
+return;
+
+  // Free all libpciaccess resources
+  pci_system_cleanup ();
+
+  ports_port_deref (inpi);
+
+  netfs_shutdown (FSYS_GOAWAY_FORCE);
+}
+
 static struct machdev_device_emulation_ops pci_arbiter_emulation_ops = {
   NULL,
   NULL,
@@ -106,7 +123,7 @@ static struct machdev_device_emulation_ops 
pci_arbiter_emulation_ops = {
   NULL,
   NULL,
   NULL,
-  NULL,
+  pci_device_shutdown,
 };
 
 int
diff --git a/pci-arbiter/startup-ops.c b/pci-arbiter/startup-ops.c
deleted file mode 100644
index fc285572..
--- a/pci-arbiter/startup-ops.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
-   Copyright (C) 2017 Free Software Foundation, Inc.
-   Written by Michael I. Bushnell, p/BSG.
-
-   This file is part of the GNU Hurd.
-
-   The GNU Hurd is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2, or (at
-   your option) any later version.
-
-   The GNU Hurd is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with the GNU Hurd.  If not, see .
-*/
-
-#include 
-
-#include 
-#include 
-
-#include "startup.h"
-

[PATCH hurd x2] - Rebased

2021-03-07 Thread Damien Zammit
[PATCH 1/2] pci-arbiter: Fix brokenness

This patch does not include mustbedir option because I believe there
is already logic that detects if it is looking up a directory,
by checking the entry->dir != 0.
When I tried adding it in, it did not work correctly.


[PATCH 2/2] pci-arbiter: Introduce machdev into this server

This one now includes the device_close and device_shutdown



[PATCH 1/2] pci-arbiter: Fix brokenness

2021-03-07 Thread Damien Zammit
* pci-arbiter/netfs_impl.c
(get_dirents): Fix count and size detection
(netfs_attempt_lookup): Strip trailing slashes and leading dot slashes
* pci-arbiter/pci-ops.c
(check_permissions): Use the overridable method instead

---
 pci-arbiter/netfs_impl.c | 41 
 pci-arbiter/pci-ops.c|  2 +-
 2 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/pci-arbiter/netfs_impl.c b/pci-arbiter/netfs_impl.c
index b987a0bc..15ca1d08 100644
--- a/pci-arbiter/netfs_impl.c
+++ b/pci-arbiter/netfs_impl.c
@@ -59,25 +59,26 @@ get_dirents (struct pcifs_dirent *dir,
   int i, count;
   size_t size;
   char *p;
+  int nentries = (int)dir->dir->num_entries;
 
-  if (first_entry >= dir->dir->num_entries)
+  if (first_entry >= nentries)
 {
   *data_len = 0;
   *data_entries = 0;
   return 0;
 }
 
-  if (max_entries < 0)
-count = dir->dir->num_entries;
-  else
+  count = nentries - first_entry;
+  if (max_entries >= 0 && count > max_entries)
+count = max_entries;
+  if (max_data_len)
 {
-  count = ((first_entry + max_entries) >= dir->dir->num_entries ?
-  dir->dir->num_entries : max_entries) - first_entry;
+  size =
+((count * DIRENTS_CHUNK_SIZE) > max_data_len) ?
+max_data_len : count * DIRENTS_CHUNK_SIZE;
 }
-
-  size =
-(count * DIRENTS_CHUNK_SIZE) >
-max_data_len ? max_data_len : count * DIRENTS_CHUNK_SIZE;
+  else
+size = count * DIRENTS_CHUNK_SIZE;
 
   *data = mmap (0, size, PROT_READ | PROT_WRITE, MAP_ANON, 0, 0);
   err = ((void *) *data == (void *) -1) ? errno : 0;
@@ -235,7 +236,7 @@ netfs_get_dirents (struct iouser * cred, struct node * dir,
   if (dir->nn->ln->dir)
 {
   err = get_dirents (dir->nn->ln, first_entry, max_entries,
-data, data_len, max_entries, data_entries);
+data, data_len, max_data_len, data_entries);
 }
   else
 err = ENOTDIR;
@@ -257,6 +258,22 @@ netfs_attempt_lookup (struct iouser * user, struct node * 
dir,
 {
   error_t err = 0;
   struct pcifs_dirent *entry;
+  char *last = name;
+
+  /* Strip trailing slashes */
+  if (*last)
+{
+  last += strlen(name) - 1;
+  while (*last == '/' && last >= name)
+{
+  *last = '\0';
+  last--;
+}
+}
+
+/* Skip leading dot slashes */
+while (*name == '.' && *(name+1) == '/')
+  name += 2;
 
   if (*name == '\0' || strcmp (name, ".") == 0)
 /* Current directory -- just add an additional reference to DIR's node
@@ -292,7 +309,7 @@ netfs_attempt_lookup (struct iouser * user, struct node * 
dir,
   /* `dir' is a directory */
 
   /* Check dir permissions */
-  err = entry_check_perms (user, dir->nn->ln, O_READ | O_EXEC);
+  err = netfs_check_open_permissions (user, dir, O_READ | O_EXEC, 0);
   if (!err)
{
  entry = lookup (dir, name);
diff --git a/pci-arbiter/pci-ops.c b/pci-arbiter/pci-ops.c
index 72720832..07228fbe 100644
--- a/pci-arbiter/pci-ops.c
+++ b/pci-arbiter/pci-ops.c
@@ -40,7 +40,7 @@ check_permissions (struct protid *master, int flags)
   e = node->nn->ln;
 
   /* Check whether the user has permissions to access this node */
-  err = entry_check_perms (master->user, e, flags);
+  err = netfs_check_open_permissions (master->user, node, flags, 0);
   if (err)
 return err;
 
-- 
2.30.1




[PATCH 2/2] pci-arbiter: Introduce machdev into this server

2021-03-07 Thread Damien Zammit
* pci-arbiter/Makefile
Add machdev lib and simplify
* pci-arbiter/main.c
(pci_device_{open,close,shutdown}): New methods
(netfs_server_func): Thread the demuxer loop
(pcifs_startup): Custom startup method
(main): Use machdev for server and detach worker threads
* pci-arbiter/options.{c,h}
Add disk-server-task and priv ports
* pci-arbiter/startup-ops.c
Delete unneeded file
---
 pci-arbiter/Makefile  |   7 +-
 pci-arbiter/main.c| 172 --
 pci-arbiter/options.c |  19 +
 pci-arbiter/options.h |   8 ++
 pci-arbiter/pcifs.h   |   3 +
 pci-arbiter/startup-ops.c |  44 --
 6 files changed, 197 insertions(+), 56 deletions(-)
 delete mode 100644 pci-arbiter/startup-ops.c

diff --git a/pci-arbiter/Makefile b/pci-arbiter/Makefile
index 1d0309dc..b32bc579 100644
--- a/pci-arbiter/Makefile
+++ b/pci-arbiter/Makefile
@@ -22,11 +22,10 @@ PORTDIR = $(srcdir)/port
 
 SRCS   = main.c pci-ops.c netfs_impl.c \
  pcifs.c ncache.c options.c func_files.c startup.c \
- startup-ops.c
-MIGSRCS= pciServer.c startup_notifyServer.c
-OBJS   = $(patsubst %.S,%.o,$(patsubst %.c,%.o, $(SRCS) $(MIGSRCS)))
+ pciServer.c startup_notifyServer.c
+OBJS   = $(SRCS:.c=.o) $(MIGSTUBS)
 
-HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash
+HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash trivfs machdev
 LDLIBS = -lpthread $(libpciaccess_LIBS)
 
 target = pci-arbiter
diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 181bdee2..ddea5aa7 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -24,7 +24,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
+#include 
+#include 
 
 #include 
 #include 
@@ -34,7 +39,9 @@
 #include "libnetfs/fsys_S.h"
 #include "libports/interrupt_S.h"
 #include "libnetfs/ifsock_S.h"
+#include "libmachdev/machdev.h"
 #include 
+#include 
 #include "pcifs.h"
 #include "startup.h"
 
@@ -46,6 +53,87 @@ int netfs_maxsymlinks = 0;
 char *netfs_server_name = "pci-arbiter";
 char *netfs_server_version = HURD_VERSION;
 
+static mach_port_t pci_control_port;
+
+
+static io_return_t
+pci_device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
+ dev_mode_t mode, char *name, device_t * devp,
+ mach_msg_type_name_t * devicePoly)
+{
+  io_return_t err = D_SUCCESS;
+  mach_port_t dev_master;
+
+  if (strncmp(name, "pci", 3))
+err = D_NO_SUCH_DEVICE;
+
+  /* Fall back to opening kernel device master */
+  if (err)
+{
+  err = get_privileged_ports(NULL, &dev_master);
+  if (err)
+return err;
+  if (dev_master == MACH_PORT_NULL)
+return D_NO_SUCH_DEVICE;
+  err = device_open (dev_master, mode, name, devp);
+  if (err)
+return err;
+  *devicePoly = MACH_MSG_TYPE_MOVE_SEND;
+  return D_SUCCESS;
+}
+
+  *devp = pci_control_port;
+  *devicePoly = MACH_MSG_TYPE_COPY_SEND;
+  return D_SUCCESS;
+}
+
+static io_return_t
+pci_device_close (void *d)
+{
+  ports_port_deref (&pci_control_port);
+  return 0;
+}
+
+static void
+pci_device_shutdown (mach_port_t dosync_handle)
+{
+  struct port_info *inpi = ports_lookup_port (netfs_port_bucket, dosync_handle,
+ pci_shutdown_notify_class);
+
+  if (!inpi)
+return;
+
+  // Free all libpciaccess resources
+  pci_system_cleanup ();
+
+  ports_port_deref (inpi);
+
+  ports_destroy_right (&pci_control_port);
+
+  netfs_shutdown (FSYS_GOAWAY_FORCE);
+}
+
+static struct machdev_device_emulation_ops pci_arbiter_emulation_ops = {
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  pci_device_open,
+  pci_device_close,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  pci_device_shutdown,
+};
+
 int
 netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outp)
 {
@@ -67,24 +155,82 @@ netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t 
* outp)
 return FALSE;
 }
 
+static void *
+netfs_server_func (void *arg)
+{
+  error_t err;
+
+  do 
+{
+  ports_manage_port_operations_multithread (netfs_port_bucket,
+   netfs_demuxer,
+   1000 * 60 * 2, /* two minutes 
thread */
+   1000 * 60 * 10,/* ten minutes 
server */
+   0);
+  err = netfs_shutdown (0);
+}
+  while (err);
+  return NULL;
+}
+
+
+static mach_port_t
+pcifs_startup(mach_port_t bootstrap, int flags)
+{
+  error_t err;
+  mach_port_t realnode;
+  struct port_info *newpi;
+
+  err = ports_create_port (netfs_control_class, netfs_port_bucket,
+sizeof (struct port_info), &newpi);
+  if (!err)
+{
+  pci_control_port = ports_get_send_right (newpi);
+  err = fsys_startup (bootstrap, flags, pci_control_port, 
MACH_MSG_TYPE

Re: [PATCH: hurd x6]

2021-03-07 Thread Damien Zammit
On 7/3/21 9:25 pm, Samuel Thibault wrote:
> Damien Zammit, le dim. 07 mars 2021 17:39:04 +1100, a ecrit:
>> [PATCH 3/6] libnetfs: Hacks to allow pci to run with no cred
>>
>> This is still needed to bypass the cred uid=0.
>> I tried to call fsys_getroot, but it failed/hung when running it
>> at the point where I needed the root port.
> 
> Where is that point?

As soon as fsys_startup returns in pcifs_startup,
it returns a realnode == MACH_PORT_NULL.
Ideally, I need to call fsys_getroot right then and
return from pcifs_startup with an actual protid to the pci root.
But if I do this, it hangs. (I think it doesn't have a auth or proc server 
available).

>> Can we live with this patch for now?
> 
> No, !dircred can come from various cases, like the passed port name
> being bogus or for an improper class etc. Basically you'd give to
> anybody malignant enough root access to all libnetfs translators.

Ok, this is a problem that I don't know how to solve.

Damien



[PATCH] acpi: Fix brokenness

2021-03-07 Thread Damien Zammit
Fix count and size

---
 acpi/netfs_impl.c | 41 +
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/acpi/netfs_impl.c b/acpi/netfs_impl.c
index 84f52c89..d9853a08 100644
--- a/acpi/netfs_impl.c
+++ b/acpi/netfs_impl.c
@@ -58,25 +58,26 @@ get_dirents (struct acpifs_dirent *dir,
   int i, count;
   size_t size;
   char *p;
+  int nentries = (int)dir->dir->num_entries;
 
-  if (first_entry >= dir->dir->num_entries)
+  if (first_entry >= nentries)
 {
   *data_len = 0;
   *data_entries = 0;
   return 0;
 }
 
-  if (max_entries < 0)
-count = dir->dir->num_entries;
-  else
+  count = nentries - first_entry;
+  if (max_entries >= 0 && count > max_entries)
+count = max_entries;
+  if (max_data_len)
 {
-  count = ((first_entry + max_entries) >= dir->dir->num_entries ?
-  dir->dir->num_entries : max_entries) - first_entry;
+  size =
+((count * DIRENTS_CHUNK_SIZE) > max_data_len) ?
+max_data_len : count * DIRENTS_CHUNK_SIZE;
 }
-
-  size =
-(count * DIRENTS_CHUNK_SIZE) >
-max_data_len ? max_data_len : count * DIRENTS_CHUNK_SIZE;
+  else
+size = count * DIRENTS_CHUNK_SIZE;
 
   *data = mmap (0, size, PROT_READ | PROT_WRITE, MAP_ANON, 0, 0);
   err = ((void *) *data == (void *) -1) ? errno : 0;
@@ -251,11 +252,27 @@ netfs_get_dirents (struct iouser * cred, struct node * 
dir,
(*NODE, if found, should be locked, this call should unlock DIR no matter
what.) */
 error_t
-netfs_attempt_lookup (struct iouser *user, struct node *dir,
-  char *name, struct node **node)
+netfs_attempt_lookup (struct iouser * user, struct node * dir,
+ char *name, struct node ** node)
 {
   error_t err = 0;
   struct acpifs_dirent *entry;
+  char *last = name;
+
+  /* Strip trailing slashes */
+  if (*last)
+{
+  last += strlen(name) - 1;
+  while (*last == '/' && last >= name)
+{
+  *last = '\0';
+  last--;
+}
+}
+
+/* Skip leading dot slashes */
+while (*name == '.' && *(name+1) == '/')
+  name += 2;
 
   if (*name == '\0' || strcmp (name, ".") == 0)
 /* Current directory -- just add an additional reference to DIR's node
-- 
2.30.1




[PATCH 3/4] pci-arbiter: Introduce machdev into this server

2021-03-08 Thread Damien Zammit
* pci-arbiter/Makefile
Add machdev lib and simplify
* pci-arbiter/main.c
(pci_device_{open,close,shutdown}): New methods
(netfs_server_func): Thread the demuxer loop
(pcifs_startup): Custom startup method
(main): Use machdev for server and detach worker threads
* pci-arbiter/options.{c,h}
Add disk-server-task and priv ports
* pci-arbiter/startup-ops.c
Delete file
---
 pci-arbiter/Makefile  |   7 +-
 pci-arbiter/main.c| 187 --
 pci-arbiter/options.c |  19 
 pci-arbiter/options.h |   8 ++
 pci-arbiter/pcifs.c   |  29 +++---
 pci-arbiter/pcifs.h   |   6 +-
 pci-arbiter/startup-ops.c |  44 -
 7 files changed, 229 insertions(+), 71 deletions(-)
 delete mode 100644 pci-arbiter/startup-ops.c

diff --git a/pci-arbiter/Makefile b/pci-arbiter/Makefile
index 1d0309dc..b32bc579 100644
--- a/pci-arbiter/Makefile
+++ b/pci-arbiter/Makefile
@@ -22,11 +22,10 @@ PORTDIR = $(srcdir)/port
 
 SRCS   = main.c pci-ops.c netfs_impl.c \
  pcifs.c ncache.c options.c func_files.c startup.c \
- startup-ops.c
-MIGSRCS= pciServer.c startup_notifyServer.c
-OBJS   = $(patsubst %.S,%.o,$(patsubst %.c,%.o, $(SRCS) $(MIGSRCS)))
+ pciServer.c startup_notifyServer.c
+OBJS   = $(SRCS:.c=.o) $(MIGSTUBS)
 
-HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash
+HURDLIBS= fshelp ports shouldbeinlibc netfs iohelp ihash trivfs machdev
 LDLIBS = -lpthread $(libpciaccess_LIBS)
 
 target = pci-arbiter
diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 181bdee2..efb9f65c 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -24,7 +24,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
+#include 
+#include 
 
 #include 
 #include 
@@ -34,7 +39,9 @@
 #include "libnetfs/fsys_S.h"
 #include "libports/interrupt_S.h"
 #include "libnetfs/ifsock_S.h"
+#include "libmachdev/machdev.h"
 #include 
+#include 
 #include "pcifs.h"
 #include "startup.h"
 
@@ -46,6 +53,96 @@ int netfs_maxsymlinks = 0;
 char *netfs_server_name = "pci-arbiter";
 char *netfs_server_version = HURD_VERSION;
 
+static mach_port_t pci_control_port;
+
+
+static io_return_t
+pci_device_open (mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
+ dev_mode_t mode, char *name, device_t * devp,
+ mach_msg_type_name_t * devicePoly)
+{
+  io_return_t err = D_SUCCESS;
+  mach_port_t dev_master, root;
+  string_t retry_name;
+  retry_type retry;
+  uid_t idlist[] = {0, 0, 0};
+
+  if (strncmp(name, "pci", 3))
+err = D_NO_SUCH_DEVICE;
+
+  /* Fall back to opening kernel device master */
+  if (err)
+{
+  err = get_privileged_ports(NULL, &dev_master);
+  if (err)
+return err;
+  if (dev_master == MACH_PORT_NULL)
+return D_NO_SUCH_DEVICE;
+  err = device_open (dev_master, mode, name, devp);
+  if (err)
+return err;
+  *devicePoly = MACH_MSG_TYPE_MOVE_SEND;
+  return D_SUCCESS;
+}
+
+  err = fsys_getroot(pci_control_port, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
+ idlist, 3, idlist, 3, 0,
+ &retry, retry_name, &root);
+  if (err)
+return err;
+
+  *devp = root;
+  *devicePoly = MACH_MSG_TYPE_COPY_SEND;
+  return D_SUCCESS;
+}
+
+static io_return_t
+pci_device_close (void *d)
+{
+  ports_port_deref (&pci_control_port);
+  return 0;
+}
+
+static void
+pci_device_shutdown (mach_port_t dosync_handle)
+{
+  struct port_info *inpi = ports_lookup_port (netfs_port_bucket, dosync_handle,
+ pci_shutdown_notify_class);
+
+  if (!inpi)
+return;
+
+  // Free all libpciaccess resources
+  pci_system_cleanup ();
+
+  ports_port_deref (inpi);
+
+  ports_destroy_right (&pci_control_port);
+
+  netfs_shutdown (FSYS_GOAWAY_FORCE);
+}
+
+static struct machdev_device_emulation_ops pci_arbiter_emulation_ops = {
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  pci_device_open,
+  pci_device_close,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  NULL,
+  pci_device_shutdown,
+};
+
 int
 netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t * outp)
 {
@@ -67,24 +164,82 @@ netfs_demuxer (mach_msg_header_t * inp, mach_msg_header_t 
* outp)
 return FALSE;
 }
 
+static void *
+netfs_server_func (void *arg)
+{
+  error_t err;
+
+  do 
+{
+  ports_manage_port_operations_multithread (netfs_port_bucket,
+   netfs_demuxer,
+   1000 * 60 * 2, /* two minutes 
thread */
+   1000 * 60 * 10,/* ten minutes 
server */
+   0);
+  err = netfs_shutdown (0);
+}
+  while (err);
+  return NULL;
+}
+
+
+static mach_port_t
+pcifs_startup(mach_port_t bootstrap, int flags)
+{
+  error_t err;
+  mach_port_t realnode;
+  stru

[PATCH 4/4] rumpdisk: Don't pretend to be the arbiter anymore

2021-03-08 Thread Damien Zammit
---
 rumpdisk/block-rump.c | 16 +---
 rumpdisk/main.c   |  5 -
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/rumpdisk/block-rump.c b/rumpdisk/block-rump.c
index 9c6fa10c..a61f7b80 100644
--- a/rumpdisk/block-rump.c
+++ b/rumpdisk/block-rump.c
@@ -27,6 +27,7 @@
 
 #include 
 #include 
+#include  /* fsys_getpriv */
 #include 
 #include 
 
@@ -136,12 +137,12 @@ dev_mode_to_rump_mode (const dev_mode_t mode)
 static void
 rumpdisk_device_init (void)
 {
-  mach_port_t device_master;
+  mach_port_t bootport, device_master;
+  device_t device;
+  task_t fstask;
 
   if (! get_privileged_ports (0, &device_master))
 {
-  device_t device;
-
 #if 0
   if (! device_open (device_master, D_READ, "hd0", &device)
|| ! device_open (device_master, D_READ, "hd1", &device)
@@ -164,6 +165,15 @@ rumpdisk_device_init (void)
  return;
}
 }
+
+  /* FIXME: need to synchronise with arbiter to wait for fsys_startup */
+  sleep(1);
+  
+  task_get_bootstrap_port (mach_task_self (), &bootport);
+  fsys_getpriv (bootport, &_hurd_host_priv, &_hurd_device_master, &fstask);
+
+  sleep(1);
+
   rump_init ();
 }
 
diff --git a/rumpdisk/main.c b/rumpdisk/main.c
index a26e17cb..c01372eb 100644
--- a/rumpdisk/main.c
+++ b/rumpdisk/main.c
@@ -31,11 +31,6 @@
 #include 
 #include 
 
-/* TODO: Add api to pciaccess to allow selecting backend.
- * For now we pretend to be the arbiter and claim x86 method.
- */
-char *netfs_server_name = "pci-arbiter";
-
 mach_port_t bootstrap_resume_task = MACH_PORT_NULL;
 
 static const struct argp_option options[] = {
-- 
2.30.1




[PATCH hurd x4] - Working arbiter + rumpdisk

2021-03-08 Thread Damien Zammit
Hi all,

This series introduces a working disk driver
with no more ugly netfs hacks.

Cheers,
Damien



[PATCH 2/4] pci-arbiter: Fix brokenness

2021-03-08 Thread Damien Zammit
---
 pci-arbiter/netfs_impl.c | 39 ++-
 pci-arbiter/pci-ops.c|  2 +-
 2 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/pci-arbiter/netfs_impl.c b/pci-arbiter/netfs_impl.c
index b987a0bc..2748c382 100644
--- a/pci-arbiter/netfs_impl.c
+++ b/pci-arbiter/netfs_impl.c
@@ -59,25 +59,22 @@ get_dirents (struct pcifs_dirent *dir,
   int i, count;
   size_t size;
   char *p;
+  int nentries = (int)dir->dir->num_entries;
 
-  if (first_entry >= dir->dir->num_entries)
+  if (first_entry >= nentries)
 {
   *data_len = 0;
   *data_entries = 0;
   return 0;
 }
 
-  if (max_entries < 0)
-count = dir->dir->num_entries;
-  else
-{
-  count = ((first_entry + max_entries) >= dir->dir->num_entries ?
-  dir->dir->num_entries : max_entries) - first_entry;
-}
+  count = nentries - first_entry;
+  if (max_entries >= 0 && count > max_entries)
+count = max_entries;
 
-  size =
-(count * DIRENTS_CHUNK_SIZE) >
-max_data_len ? max_data_len : count * DIRENTS_CHUNK_SIZE;
+  size = count * DIRENTS_CHUNK_SIZE;
+  if (max_data_len && size > max_data_len)
+size = max_data_len;
 
   *data = mmap (0, size, PROT_READ | PROT_WRITE, MAP_ANON, 0, 0);
   err = ((void *) *data == (void *) -1) ? errno : 0;
@@ -235,7 +232,7 @@ netfs_get_dirents (struct iouser * cred, struct node * dir,
   if (dir->nn->ln->dir)
 {
   err = get_dirents (dir->nn->ln, first_entry, max_entries,
-data, data_len, max_entries, data_entries);
+data, data_len, max_data_len, data_entries);
 }
   else
 err = ENOTDIR;
@@ -257,6 +254,22 @@ netfs_attempt_lookup (struct iouser * user, struct node * 
dir,
 {
   error_t err = 0;
   struct pcifs_dirent *entry;
+  char *last = name;
+
+  /* Strip trailing slashes */
+  if (*last)
+{
+  last += strlen(name) - 1;
+  while (*last == '/' && last >= name)
+{
+  *last = '\0';
+  last--;
+}
+}
+
+/* Skip leading dot slashes */
+while (*name == '.' && *(name+1) == '/')
+  name += 2;
 
   if (*name == '\0' || strcmp (name, ".") == 0)
 /* Current directory -- just add an additional reference to DIR's node
@@ -292,7 +305,7 @@ netfs_attempt_lookup (struct iouser * user, struct node * 
dir,
   /* `dir' is a directory */
 
   /* Check dir permissions */
-  err = entry_check_perms (user, dir->nn->ln, O_READ | O_EXEC);
+  err = netfs_check_open_permissions (user, dir, O_READ | O_EXEC, 0);
   if (!err)
{
  entry = lookup (dir, name);
diff --git a/pci-arbiter/pci-ops.c b/pci-arbiter/pci-ops.c
index 72720832..07228fbe 100644
--- a/pci-arbiter/pci-ops.c
+++ b/pci-arbiter/pci-ops.c
@@ -40,7 +40,7 @@ check_permissions (struct protid *master, int flags)
   e = node->nn->ln;
 
   /* Check whether the user has permissions to access this node */
-  err = entry_check_perms (master->user, e, flags);
+  err = netfs_check_open_permissions (master->user, node, flags, 0);
   if (err)
 return err;
 
-- 
2.30.1




[PATCH 1/4] acpi: Fix brokenness

2021-03-08 Thread Damien Zammit
---
 acpi/netfs_impl.c | 39 ++-
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/acpi/netfs_impl.c b/acpi/netfs_impl.c
index 84f52c89..b6cb466d 100644
--- a/acpi/netfs_impl.c
+++ b/acpi/netfs_impl.c
@@ -58,25 +58,22 @@ get_dirents (struct acpifs_dirent *dir,
   int i, count;
   size_t size;
   char *p;
+  int nentries = (int)dir->dir->num_entries;
 
-  if (first_entry >= dir->dir->num_entries)
+  if (first_entry >= nentries)
 {
   *data_len = 0;
   *data_entries = 0;
   return 0;
 }
 
-  if (max_entries < 0)
-count = dir->dir->num_entries;
-  else
-{
-  count = ((first_entry + max_entries) >= dir->dir->num_entries ?
-  dir->dir->num_entries : max_entries) - first_entry;
-}
+  count = nentries - first_entry;
+  if (max_entries >= 0 && count > max_entries)
+count = max_entries;
 
-  size =
-(count * DIRENTS_CHUNK_SIZE) >
-max_data_len ? max_data_len : count * DIRENTS_CHUNK_SIZE;
+  size = count * DIRENTS_CHUNK_SIZE;
+  if (max_data_len && size > max_data_len)
+size = max_data_len;
 
   *data = mmap (0, size, PROT_READ | PROT_WRITE, MAP_ANON, 0, 0);
   err = ((void *) *data == (void *) -1) ? errno : 0;
@@ -251,11 +248,27 @@ netfs_get_dirents (struct iouser * cred, struct node * 
dir,
(*NODE, if found, should be locked, this call should unlock DIR no matter
what.) */
 error_t
-netfs_attempt_lookup (struct iouser *user, struct node *dir,
-  char *name, struct node **node)
+netfs_attempt_lookup (struct iouser * user, struct node * dir,
+ char *name, struct node ** node)
 {
   error_t err = 0;
   struct acpifs_dirent *entry;
+  char *last = name;
+
+  /* Strip trailing slashes */
+  if (*last)
+{
+  last += strlen(name) - 1;
+  while (*last == '/' && last >= name)
+{
+  *last = '\0';
+  last--;
+}
+}
+
+/* Skip leading dot slashes */
+while (*name == '.' && *(name+1) == '/')
+  name += 2;
 
   if (*name == '\0' || strcmp (name, ".") == 0)
 /* Current directory -- just add an additional reference to DIR's node
-- 
2.30.1




[PATCH] machdev,pci,rump: Reuse shutdown notify from machdev

2021-03-12 Thread Damien Zammit
This set of changes allows the fsys_init flow of RPCs to work better
during bootstrap.  It also removes duplicated code for the
startup shutdown notify between machdev and the arbiter.

However, I am still unsure why the lookup of _SERVERS_STARTUP fails
in the arbiter's fsys_init, see the WARNING at bootup.

---
 libmachdev/ds_routines.c   |  2 ++
 libmachdev/machdev.h   |  5 
 libmachdev/trivfs_server.c | 43 +--
 pci-arbiter/Makefile   |  2 +-
 pci-arbiter/main.c | 18 +---
 pci-arbiter/startup.c  | 60 --
 pci-arbiter/startup.h  | 31 
 rumpdisk/main.c|  6 +---
 8 files changed, 51 insertions(+), 116 deletions(-)
 delete mode 100644 pci-arbiter/startup.c
 delete mode 100644 pci-arbiter/startup.h

diff --git a/libmachdev/ds_routines.c b/libmachdev/ds_routines.c
index c2de4b26..3ef81805 100644
--- a/libmachdev/ds_routines.c
+++ b/libmachdev/ds_routines.c
@@ -62,6 +62,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -75,6 +76,7 @@
 
 struct port_bucket *machdev_device_bucket;
 struct port_class *machdev_device_class;
+mach_port_t machdev_bootport;
 
 #define MAX_NUM_EMULATION 32
 
diff --git a/libmachdev/machdev.h b/libmachdev/machdev.h
index 8f613b35..80e1e3c2 100644
--- a/libmachdev/machdev.h
+++ b/libmachdev/machdev.h
@@ -27,6 +27,8 @@
 #include "machdev-device_emul.h"
 #include "machdev-dev_hdr.h"
 
+extern mach_port_t machdev_bootport;
+
 void machdev_register (struct machdev_device_emulation_ops *ops);
 
 void machdev_device_init(void);
@@ -36,5 +38,8 @@ error_t machdev_create_device_port (size_t size, void 
*result);
 int machdev_trivfs_init(mach_port_t bootstrap_resume_task, const char *name, 
const char *path, mach_port_t *bootstrap);
 void machdev_trivfs_server(mach_port_t bootstrap);
 boolean_t machdev_is_master_device (mach_port_t port);
+error_t machdev_fsys_init(mach_port_t proc, mach_port_t auth);
+void machdev_use_this_port(mach_port_t bootstrap);
+void machdev_get_ports(void);
 
 #endif
diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index e3e4045d..8ee6be65 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -245,6 +245,39 @@ trivfs_S_fsys_startup (mach_port_t bootport,
   return 0;
 }
 
+error_t
+machdev_fsys_init(mach_port_t proc, mach_port_t auth)
+{
+  /* No more nesting of servers */
+  if (!machdev_bootport)
+return 0;
+
+  return fsys_init (machdev_bootport, proc, MACH_MSG_TYPE_COPY_SEND, auth);
+}
+
+void
+machdev_use_this_port(mach_port_t bootstrap)
+{
+  machdev_bootport = bootstrap;
+}
+
+void
+machdev_get_ports(void)
+{
+  int i;
+  task_t parent_task;
+
+  /* If we are not the bootstrap filesystem, get this port from parent */
+  if (!machdev_bootport)
+{
+  /* Need to synchronise with parent to wait for fsys_startup */
+  for (i = 0; i < 5 && machdev_bootport == MACH_PORT_NULL; i++, sleep(1))
+task_get_bootstrap_port (mach_task_self (), &machdev_bootport);
+
+}
+  fsys_getpriv (machdev_bootport, &_hurd_host_priv, &_hurd_device_master, 
&parent_task);
+}
+
 kern_return_t
 trivfs_S_fsys_init (struct trivfs_control *fsys,
 mach_port_t reply, mach_msg_type_name_t replytype,
@@ -260,6 +293,10 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   string_t retry_name;
   mach_port_t right = MACH_PORT_NULL;
 
+  /* Traverse to the bootstrapping server first */
+  err = machdev_fsys_init (procserver, authhandle);
+  assert_perror_backtrace (err);
+
   err = fsys_getroot (control_port, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
   idlist, 3, idlist, 3, 0,
   &retry, retry_name, &root);
@@ -278,7 +315,6 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   portarray[INIT_PORT_CWDIR] = root;
   _hurd_init (0, NULL, portarray, INIT_PORT_MAX, NULL, 0);
 
-  arrange_shutdown_notification ();
 
   /* Install the bootstrap port on /dev/something so users
* can still access the bootstrapped device */
@@ -288,6 +324,9 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   install_as_translator (right);
   control->underlying = underlying;
 }
+
+  arrange_shutdown_notification ();
+
   return 0;
 }
 
@@ -380,7 +419,7 @@ resume_bootstrap_server(mach_port_t server_task, const char 
*server_name)
   stdout = stderr = mach_open_devstream (cons, "w");
   mach_port_deallocate (mach_task_self (), cons);
 
-  printf ("Hurd bootstrap %s ", server_name);
+  printf ("%s ", server_name);
   fflush (stdout);
 }
 
diff --git a/pci-arbiter/Makefile b/pci-arbiter/Makefile
index b32bc579..d3d205ec 100644
--- a/pci-arbiter/Makefile
+++ b/pci-arbiter/Makefile
@@ -21,7 +21,7 @@ makemode  = server
 PORTDIR = $(srcdir)/port
 
 SRCS   = main.c pci-ops.c netfs_impl.c \
- pcifs.c ncache.c options.c func_files.c startup.c \
+ pcifs.c ncache.c options.c func_files.c \
   

[PATCH] machdev: Fix passthrough of control port and fsys_startup

2021-03-13 Thread Damien Zammit
This fixes the WARNING on the previous patch.
The lookup of _SERVERS_STARTUP now works.

---
 libmachdev/trivfs_server.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index 8ee6be65..be5abc9d 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -239,9 +239,14 @@ trivfs_S_fsys_startup (mach_port_t bootport,
mach_port_t *realnode,
mach_msg_type_name_t *realnodetype)
 {
+  mach_port_t mybootport;
+
   control_port = cntl;
   *realnode = MACH_PORT_NULL;
-  *realnodetype = MACH_MSG_TYPE_MOVE_SEND;
+  *realnodetype = MACH_MSG_TYPE_COPY_SEND;
+
+  task_get_bootstrap_port (mach_task_self (), &mybootport);
+  fsys_startup (mybootport, flags, control_port, MACH_MSG_TYPE_COPY_SEND, 
realnode);
   return 0;
 }
 
-- 
2.30.1




[PATCH] machdev,pci,rump: Rework bootstrap

2021-03-14 Thread Damien Zammit
Hi,

This patch fixes the bootstrapping,
except for a strange anomaly with:

cat /proc/5/stat /proc/6/stat

My grub config looks like this:

menuentry "pci-arbiter + rumpdisk!" {
set root=(hd0,msdos2)
multiboot /boot/gnumach-1.8-486-dbg.gz root=part:2:device:wd0 noide

module /hurd/pci-arbiter.static pci-arbiter \
--host-priv-port='${host-port}' \
--dev-master-port='${device-port}' \
--disk-server-task='${disk-task}' \
'$(task-create)' '$(task-resume)'

module /hurd/rumpdisk.static rumpdisk \
--filesystem-task='${fs-task}' \
'$(disk-task=task-create)'

module /hurd/ext2fs.static ext2fs \
--multiboot-command-line='${kernel-command-line}' \
--exec-server-task='${exec-task}' -T typed '${root}' \
'$(fs-task=task-create)'

module /hurd/exec.static exec '$(exec-task=task-create)'
}

---
 libmachdev/trivfs_server.c | 86 +-
 pci-arbiter/Makefile   |  2 +-
 pci-arbiter/main.c | 19 +
 pci-arbiter/startup.c  | 60 --
 pci-arbiter/startup.h  | 31 --
 rumpdisk/main.c|  7 +---
 6 files changed, 70 insertions(+), 135 deletions(-)
 delete mode 100644 pci-arbiter/startup.c
 delete mode 100644 pci-arbiter/startup.h

diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index e3e4045d..8062fb44 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -63,7 +63,7 @@ struct port_class *trivfs_protid_class;
 struct trivfs_control *control;
 
 /* Are we providing bootstrap translator? */
-static boolean_t bootstrapped;
+static boolean_t bootstrapping;
 
 /* Our underlying node in the FS for bootstrap */
 static mach_port_t underlying;
@@ -239,12 +239,37 @@ trivfs_S_fsys_startup (mach_port_t bootport,
mach_port_t *realnode,
mach_msg_type_name_t *realnodetype)
 {
+  mach_port_t mybootport;
+
   control_port = cntl;
   *realnode = MACH_PORT_NULL;
-  *realnodetype = MACH_MSG_TYPE_MOVE_SEND;
+  *realnodetype = MACH_MSG_TYPE_COPY_SEND;
+
+  task_get_bootstrap_port (mach_task_self (), &mybootport);
+  if (mybootport)
+fsys_startup (mybootport, flags, control_port, MACH_MSG_TYPE_COPY_SEND, 
realnode);
   return 0;
 }
 
+static void
+essential_task (void)
+{
+  mach_port_t host, startup;
+
+  get_privileged_ports (&host, 0);
+  startup = file_name_lookup (_SERVERS_STARTUP, 0, 0);
+  if (startup == MACH_PORT_NULL)
+{
+  mach_print ("WARNING: Cannot register as essential task\n");
+  mach_port_deallocate (mach_task_self (), host);
+  return;
+}
+  startup_essential_task (startup, mach_task_self (), MACH_PORT_NULL,
+  program_invocation_short_name, host);
+  mach_port_deallocate (mach_task_self (), startup);
+  mach_port_deallocate (mach_task_self (), host);
+}
+
 kern_return_t
 trivfs_S_fsys_init (struct trivfs_control *fsys,
 mach_port_t reply, mach_msg_type_name_t replytype,
@@ -255,11 +280,19 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   mach_port_t *portarray;
   unsigned int i;
   uid_t idlist[] = {0, 0, 0};
-  mach_port_t root;
+  mach_port_t root, bootstrap;
   retry_type retry;
   string_t retry_name;
   mach_port_t right = MACH_PORT_NULL;
+  process_t proc;
 
+  /* Traverse to the bootstrapping server first */
+  task_get_bootstrap_port (mach_task_self (), &bootstrap);
+  if (bootstrap)
+{
+  err = fsys_init (bootstrap, procserver, MACH_MSG_TYPE_COPY_SEND, 
authhandle);
+  assert_perror_backtrace (err);
+}
   err = fsys_getroot (control_port, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
   idlist, 3, idlist, 3, 0,
   &retry, retry_name, &root);
@@ -278,16 +311,28 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   portarray[INIT_PORT_CWDIR] = root;
   _hurd_init (0, NULL, portarray, INIT_PORT_MAX, NULL, 0);
 
-  arrange_shutdown_notification ();
+  /* Mark us as important.  */
+  proc = getproc ();
+  assert_backtrace (proc);
+  err = proc_mark_important (proc);
+  assert_perror_backtrace (err);
+  err = proc_mark_exec (proc);
+  assert_perror_backtrace (err);
+  proc_set_exe (proc, program_invocation_short_name);
+  mach_port_deallocate (mach_task_self (), proc);
 
   /* Install the bootstrap port on /dev/something so users
* can still access the bootstrapped device */
-  if (bootstrapped && devnode)
+  if (bootstrapping && devnode)
 {
   right = ports_get_send_right (&control->pi);
   install_as_translator (right);
   control->underlying = underlying;
+  essential_task ();
 }
+
+  arrange_shutdown_notification ();
+
   return 0;
 }
 
@@ -296,12 +341,8 @@ arrange_shutdown_notification (void)
 {
   error_t err;
   mach_port_t initport, notify;
-  process_t proc;
   struct port_info *pi;
 
-  proc

[PATCH v2] machdev,pci,rump: Rework bootstrap

2021-03-15 Thread Damien Zammit
* libmachdev/machdev.h
(machdev_trivfs_init): Add controlport output parameter
* libmachdev/trivfs_server.c
Rework the bootstrapping of machdev
* pci-arbiter/Makefile
Remove startup.c
* pci-arbiter/startup.{c,h}
Delete files
* pci-arbiter/main.c
(main): Rework bootstrap, remove notification, add controlport
* rumpdisk/main.c
(main): Don't pretend to be the arbiter anymore, add controlport

---
 libmachdev/machdev.h   |   5 +-
 libmachdev/trivfs_server.c | 103 -
 pci-arbiter/Makefile   |   2 +-
 pci-arbiter/main.c |  29 ---
 pci-arbiter/startup.c  |  60 -
 pci-arbiter/startup.h  |  31 ---
 rumpdisk/main.c|   8 +--
 7 files changed, 93 insertions(+), 145 deletions(-)
 delete mode 100644 pci-arbiter/startup.c
 delete mode 100644 pci-arbiter/startup.h

diff --git a/libmachdev/machdev.h b/libmachdev/machdev.h
index 8f613b35..c45f3d67 100644
--- a/libmachdev/machdev.h
+++ b/libmachdev/machdev.h
@@ -33,8 +33,9 @@ void machdev_device_init(void);
 void machdev_device_shutdown(mach_port_t dosync_handle);
 void * machdev_server(void *);
 error_t machdev_create_device_port (size_t size, void *result);
-int machdev_trivfs_init(mach_port_t bootstrap_resume_task, const char *name, 
const char *path, mach_port_t *bootstrap);
-void machdev_trivfs_server(mach_port_t bootstrap);
+int machdev_trivfs_init(mach_port_t bootstrap_resume_task, const char *name, 
const char *path,
+mach_port_t *bootstrap, mach_port_t *controlport);
+void machdev_trivfs_server(mach_port_t controlport);
 boolean_t machdev_is_master_device (mach_port_t port);
 
 #endif
diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index e3e4045d..adf111bf 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -63,7 +63,7 @@ struct port_class *trivfs_protid_class;
 struct trivfs_control *control;
 
 /* Are we providing bootstrap translator? */
-static boolean_t bootstrapped;
+static boolean_t bootstrapping;
 
 /* Our underlying node in the FS for bootstrap */
 static mach_port_t underlying;
@@ -239,12 +239,37 @@ trivfs_S_fsys_startup (mach_port_t bootport,
mach_port_t *realnode,
mach_msg_type_name_t *realnodetype)
 {
+  mach_port_t mybootport;
+
   control_port = cntl;
   *realnode = MACH_PORT_NULL;
-  *realnodetype = MACH_MSG_TYPE_MOVE_SEND;
+  *realnodetype = MACH_MSG_TYPE_COPY_SEND;
+
+  task_get_bootstrap_port (mach_task_self (), &mybootport);
+  if (mybootport)
+fsys_startup (mybootport, flags, control_port, MACH_MSG_TYPE_COPY_SEND, 
realnode);
   return 0;
 }
 
+static void
+essential_task (void)
+{
+  mach_port_t host, startup;
+
+  get_privileged_ports (&host, 0);
+  startup = file_name_lookup (_SERVERS_STARTUP, 0, 0);
+  if (startup == MACH_PORT_NULL)
+{
+  mach_print ("WARNING: Cannot register as essential task\n");
+  mach_port_deallocate (mach_task_self (), host);
+  return;
+}
+  startup_essential_task (startup, mach_task_self (), MACH_PORT_NULL,
+  program_invocation_short_name, host);
+  mach_port_deallocate (mach_task_self (), startup);
+  mach_port_deallocate (mach_task_self (), host);
+}
+
 kern_return_t
 trivfs_S_fsys_init (struct trivfs_control *fsys,
 mach_port_t reply, mach_msg_type_name_t replytype,
@@ -255,11 +280,19 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   mach_port_t *portarray;
   unsigned int i;
   uid_t idlist[] = {0, 0, 0};
-  mach_port_t root;
+  mach_port_t root, bootstrap;
   retry_type retry;
   string_t retry_name;
   mach_port_t right = MACH_PORT_NULL;
+  process_t proc;
 
+  /* Traverse to the bootstrapping server first */
+  task_get_bootstrap_port (mach_task_self (), &bootstrap);
+  if (bootstrap)
+{
+  err = fsys_init (bootstrap, procserver, MACH_MSG_TYPE_COPY_SEND, 
authhandle);
+  assert_perror_backtrace (err);
+}
   err = fsys_getroot (control_port, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
   idlist, 3, idlist, 3, 0,
   &retry, retry_name, &root);
@@ -278,16 +311,32 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   portarray[INIT_PORT_CWDIR] = root;
   _hurd_init (0, NULL, portarray, INIT_PORT_MAX, NULL, 0);
 
-  arrange_shutdown_notification ();
+  /* Mark us as important.  */
+  proc = getproc ();
+  assert_backtrace (proc);
+  err = proc_mark_important (proc);
+  assert_perror_backtrace (err);
+  err = proc_mark_exec (proc);
+  assert_perror_backtrace (err);
+  proc_set_exe (proc, program_invocation_short_name);
+  mach_port_deallocate (mach_task_self (), proc);
 
-  /* Install the bootstrap port on /dev/something so users
-   * can still access the bootstrapped device */
-  if (bootstrapped && devnode)
+  if (bootstrapping)
 {
-  right = ports_get_send_right (&control->pi);
-  install_as_translator (right);
-  control->underlying = underlying;
+  

[PATCH v3] machdev,rump,pci: Rework bootstrap

2021-03-15 Thread Damien Zammit
* libmachdev/trivfs_server.c
Rework the bootstrapping of machdev
* pci-arbiter/Makefile
Remove startup.c
* pci-arbiter/startup.{c,h}
Delete files
* pci-arbiter/main.c
(main): Rework bootstrap, remove duplicated notification
* rumpdisk/main.c
(main): Don't pretend to be the arbiter anymore

---
 libmachdev/trivfs_server.c | 101 -
 pci-arbiter/Makefile   |   2 +-
 pci-arbiter/main.c |  29 +++
 pci-arbiter/startup.c  |  60 --
 pci-arbiter/startup.h  |  31 
 rumpdisk/main.c|   7 +--
 6 files changed, 86 insertions(+), 144 deletions(-)
 delete mode 100644 pci-arbiter/startup.c
 delete mode 100644 pci-arbiter/startup.h

diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index e3e4045d..607166f9 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -63,7 +63,7 @@ struct port_class *trivfs_protid_class;
 struct trivfs_control *control;
 
 /* Are we providing bootstrap translator? */
-static boolean_t bootstrapped;
+static boolean_t bootstrapping;
 
 /* Our underlying node in the FS for bootstrap */
 static mach_port_t underlying;
@@ -239,12 +239,37 @@ trivfs_S_fsys_startup (mach_port_t bootport,
mach_port_t *realnode,
mach_msg_type_name_t *realnodetype)
 {
+  mach_port_t mybootport;
+
   control_port = cntl;
   *realnode = MACH_PORT_NULL;
-  *realnodetype = MACH_MSG_TYPE_MOVE_SEND;
+  *realnodetype = MACH_MSG_TYPE_COPY_SEND;
+
+  task_get_bootstrap_port (mach_task_self (), &mybootport);
+  if (mybootport)
+fsys_startup (mybootport, flags, control_port, MACH_MSG_TYPE_COPY_SEND, 
realnode);
   return 0;
 }
 
+static void
+essential_task (void)
+{
+  mach_port_t host, startup;
+
+  get_privileged_ports (&host, 0);
+  startup = file_name_lookup (_SERVERS_STARTUP, 0, 0);
+  if (startup == MACH_PORT_NULL)
+{
+  mach_print ("WARNING: Cannot register as essential task\n");
+  mach_port_deallocate (mach_task_self (), host);
+  return;
+}
+  startup_essential_task (startup, mach_task_self (), MACH_PORT_NULL,
+  program_invocation_short_name, host);
+  mach_port_deallocate (mach_task_self (), startup);
+  mach_port_deallocate (mach_task_self (), host);
+}
+
 kern_return_t
 trivfs_S_fsys_init (struct trivfs_control *fsys,
 mach_port_t reply, mach_msg_type_name_t replytype,
@@ -255,11 +280,19 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   mach_port_t *portarray;
   unsigned int i;
   uid_t idlist[] = {0, 0, 0};
-  mach_port_t root;
+  mach_port_t root, bootstrap;
   retry_type retry;
   string_t retry_name;
   mach_port_t right = MACH_PORT_NULL;
+  process_t proc;
 
+  /* Traverse to the bootstrapping server first */
+  task_get_bootstrap_port (mach_task_self (), &bootstrap);
+  if (bootstrap)
+{
+  err = fsys_init (bootstrap, procserver, MACH_MSG_TYPE_COPY_SEND, 
authhandle);
+  assert_perror_backtrace (err);
+}
   err = fsys_getroot (control_port, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
   idlist, 3, idlist, 3, 0,
   &retry, retry_name, &root);
@@ -278,16 +311,32 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   portarray[INIT_PORT_CWDIR] = root;
   _hurd_init (0, NULL, portarray, INIT_PORT_MAX, NULL, 0);
 
-  arrange_shutdown_notification ();
+  /* Mark us as important.  */
+  proc = getproc ();
+  assert_backtrace (proc);
+  err = proc_mark_important (proc);
+  assert_perror_backtrace (err);
+  err = proc_mark_exec (proc);
+  assert_perror_backtrace (err);
+  proc_set_exe (proc, program_invocation_short_name);
+  mach_port_deallocate (mach_task_self (), proc);
 
-  /* Install the bootstrap port on /dev/something so users
-   * can still access the bootstrapped device */
-  if (bootstrapped && devnode)
+  if (bootstrapping)
 {
-  right = ports_get_send_right (&control->pi);
-  install_as_translator (right);
-  control->underlying = underlying;
+  if (devnode)
+{
+  /* Install the bootstrap port on /dev/something so users
+   * can still access the bootstrapped device */
+  right = ports_get_send_right (&control->pi);
+  install_as_translator (right);
+  control->underlying = underlying;
+}
+  /* Mark us as essential if bootstrapping */
+  essential_task ();
 }
+
+  arrange_shutdown_notification ();
+
   return 0;
 }
 
@@ -296,12 +345,8 @@ arrange_shutdown_notification (void)
 {
   error_t err;
   mach_port_t initport, notify;
-  process_t proc;
   struct port_info *pi;
 
-  proc = getproc ();
-  assert_backtrace (proc);
-
   machdev_shutdown_notify_class = ports_create_class (0, 0);
 
   /* Arrange to get notified when the system goes down */
@@ -310,10 +355,6 @@ arrange_shutdown_notification (void)
   if (err)
 return;
 
-  /* Mark us as important.  */
-  err = proc_mark_important (proc);
-  mach_port_dea

[PATCH 2/2] machdev: Pass argv through to _hurd_init

2021-03-18 Thread Damien Zammit
---
 libmachdev/machdev.h   | 2 +-
 libmachdev/trivfs_server.c | 9 ++---
 pci-arbiter/main.c | 2 +-
 rumpdisk/main.c| 2 +-
 4 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libmachdev/machdev.h b/libmachdev/machdev.h
index 8f613b35..9884c70d 100644
--- a/libmachdev/machdev.h
+++ b/libmachdev/machdev.h
@@ -33,7 +33,7 @@ void machdev_device_init(void);
 void machdev_device_shutdown(mach_port_t dosync_handle);
 void * machdev_server(void *);
 error_t machdev_create_device_port (size_t size, void *result);
-int machdev_trivfs_init(mach_port_t bootstrap_resume_task, const char *name, 
const char *path, mach_port_t *bootstrap);
+int machdev_trivfs_init(mach_port_t bootstrap_resume_task, const char *name, 
const char *path, mach_port_t *bootstrap, char **argv);
 void machdev_trivfs_server(mach_port_t bootstrap);
 boolean_t machdev_is_master_device (mach_port_t port);
 
diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index 60711348..1f2276d4 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -76,12 +76,14 @@ static char *devnode;
 
 /* Startup and shutdown notifications management */
 struct port_class *machdev_shutdown_notify_class;
-
 static void arrange_shutdown_notification (void);
 
 /* Our parent's task, if applicable */
 static task_t parent_task;
 
+/* Our argument vector */
+static char **machdev_argv;
+
 static void
 install_as_translator (mach_port_t bootport)
 {
@@ -315,7 +317,7 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   portarray[INIT_PORT_AUTH] = authhandle;
   portarray[INIT_PORT_CRDIR] = root;
   portarray[INIT_PORT_CWDIR] = root;
-  _hurd_init (0, NULL, portarray, INIT_PORT_MAX, NULL, 0);
+  _hurd_init (0, machdev_argv, portarray, INIT_PORT_MAX, NULL, 0);
 
   /* Mark us as important.  */
   proc = getproc ();
@@ -435,7 +437,7 @@ resume_bootstrap_server(mach_port_t server_task, const char 
*server_name)
 
 int
 machdev_trivfs_init(mach_port_t bootstrap_resume_task, const char *name, const 
char *path,
-mach_port_t *bootstrap)
+mach_port_t *bootstrap, char **argv)
 {
   mach_port_t mybootstrap = MACH_PORT_NULL;
   port_bucket = ports_create_bucket ();
@@ -445,6 +447,7 @@ machdev_trivfs_init(mach_port_t bootstrap_resume_task, 
const char *name, const c
  trivfs_protid_class, 0, &control);
 
   *bootstrap = MACH_PORT_NULL;
+  machdev_argv = argv;
 
   task_get_bootstrap_port (mach_task_self (), &mybootstrap);
   if (mybootstrap)
diff --git a/pci-arbiter/main.c b/pci-arbiter/main.c
index 1815994a..8783c693 100644
--- a/pci-arbiter/main.c
+++ b/pci-arbiter/main.c
@@ -214,7 +214,7 @@ main (int argc, char **argv)
   if (disk_server_task != MACH_PORT_NULL)
 {
   machdev_register (&pci_arbiter_emulation_ops);
-  machdev_trivfs_init (disk_server_task, "pci", "/servers/bus/pci", 
&bootstrap);
+  machdev_trivfs_init (disk_server_task, "pci", "/servers/bus/pci", 
&bootstrap, argv);
   machdev_device_init ();
   err = pthread_create (&t, NULL, machdev_server, NULL);
   if (err)
diff --git a/rumpdisk/main.c b/rumpdisk/main.c
index 87bc5573..b2a57fe8 100644
--- a/rumpdisk/main.c
+++ b/rumpdisk/main.c
@@ -112,7 +112,7 @@ main (int argc, char **argv)
 }
 
   rump_register_block ();
-  machdev_trivfs_init (bootstrap_resume_task, "rumpdisk", "/dev/rumpdisk", 
&bootstrap);
+  machdev_trivfs_init (bootstrap_resume_task, "rumpdisk", "/dev/rumpdisk", 
&bootstrap, argv);
   machdev_device_init ();
   err = pthread_create (&t, NULL, machdev_server, NULL);
   if (err)
-- 
2.30.1




[PATCH 1/2] machdev: Fix parent proc passthrough in fsys_init

2021-03-18 Thread Damien Zammit
---
 libmachdev/trivfs_server.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/libmachdev/trivfs_server.c b/libmachdev/trivfs_server.c
index 607166f9..60711348 100644
--- a/libmachdev/trivfs_server.c
+++ b/libmachdev/trivfs_server.c
@@ -79,6 +79,9 @@ struct port_class *machdev_shutdown_notify_class;
 
 static void arrange_shutdown_notification (void);
 
+/* Our parent's task, if applicable */
+static task_t parent_task;
+
 static void
 install_as_translator (mach_port_t bootport)
 {
@@ -284,13 +287,16 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   retry_type retry;
   string_t retry_name;
   mach_port_t right = MACH_PORT_NULL;
-  process_t proc;
+  process_t proc, parent_proc;
 
   /* Traverse to the bootstrapping server first */
   task_get_bootstrap_port (mach_task_self (), &bootstrap);
   if (bootstrap)
 {
-  err = fsys_init (bootstrap, procserver, MACH_MSG_TYPE_COPY_SEND, 
authhandle);
+
+  err = proc_task2proc (procserver, parent_task, &parent_proc);
+  assert_perror_backtrace (err);
+  err = fsys_init (bootstrap, parent_proc, MACH_MSG_TYPE_COPY_SEND, 
authhandle);
   assert_perror_backtrace (err);
 }
   err = fsys_getroot (control_port, MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND,
@@ -315,10 +321,12 @@ trivfs_S_fsys_init (struct trivfs_control *fsys,
   proc = getproc ();
   assert_backtrace (proc);
   err = proc_mark_important (proc);
-  assert_perror_backtrace (err);
+  if (err && err != EPERM)
+assert_perror_backtrace (err);
   err = proc_mark_exec (proc);
   assert_perror_backtrace (err);
-  proc_set_exe (proc, program_invocation_short_name);
+  err = proc_set_exe (proc, program_invocation_short_name);
+  assert_perror_backtrace (err);
   mach_port_deallocate (mach_task_self (), proc);
 
   if (bootstrapping)
@@ -430,7 +438,6 @@ machdev_trivfs_init(mach_port_t bootstrap_resume_task, 
const char *name, const c
 mach_port_t *bootstrap)
 {
   mach_port_t mybootstrap = MACH_PORT_NULL;
-  task_t parent_task;
   port_bucket = ports_create_bucket ();
   trivfs_cntl_class = ports_create_class (trivfs_clean_cntl, 0);
   trivfs_protid_class = ports_create_class (trivfs_clean_protid, 0);
-- 
2.30.1




SMP for gnumach

2021-03-19 Thread Damien Zammit
Hi all,

As you know, AlmuHS did a bunch of great work for SMP support so far.

However, I have noticed there are a stack of commits in 0.0.10_2 branch
that are not rebased onto master.

Is it desirable/possible to squash all the smp support into one or two commits
so we can rebase it onto master and start the next phase of smp + apic + lapic 
timers?

I don't know which commit to start from, but am eager to start writing some 
more.

As discussed on IRC, I think we cannot have SMP support with PIC interrupts,
so we need to add APIC and lapic timers, which means we need to add basic ACPI 
parsing to gnumach to deduce the PCI interrupt override settings.

Therefore, we need a solid, up-to-date base commit to start this work.

How do we best proceed?

Damien Z



[PATCH] WIP: ioapic support - linux drivers disabled

2021-03-20 Thread Damien Zammit
100644
--- a/i386/i386at/idt.h
+++ b/i386/i386at/idt.h
@@ -24,13 +24,18 @@
 #ifndef _I386AT_IDT_
 #define _I386AT_IDT_
 
-/* On a standard PC, we only need 16 interrupt vectors,
-   because that's all the PIC hardware supports.  */
-/* XX But for some reason we program the PIC
-   to use vectors 0x40-0x4f rather than 0x20-0x2f.  Fix.  */
-#define IDTSZ (0x20+0x20+0x10)
+/* There are 256 interrupt vectors on x86,
+ * the first 32 are taken by cpu faults */
+#define IDTSZ (0x100)
 
-#define PIC_INT_BASE 0x40
+/* PIC now sits (unused) at 0x20-0x2f */
+#define PIC_INT_BASE 0x20
+
+/* IOAPIC sits at 0x30-0x47 */
+#define IOAPIC_INT_BASE 0x30
+
+/* IOAPIC spurious interrupt vector set to 0xff */
+#define IOAPIC_SPURIOUS_BASE 0xff
 
 #include 
 
diff --git a/i386/i386at/int_init.c b/i386/i386at/int_init.c
index 43daad8b..3c96c589 100644
--- a/i386/i386at/int_init.c
+++ b/i386/i386at/int_init.c
@@ -31,9 +31,13 @@ void int_init(void)
 {
int i;
 
-   for (i = 0; i < 16; i++)
-   fill_idt_gate(PIC_INT_BASE + i,
+   for (i = 0; i < 24; i++)
+   fill_idt_gate(IOAPIC_INT_BASE + i,
  int_entry_table[i], KERNEL_CS,
  ACC_PL_K|ACC_INTR_GATE, 0);
+
+   fill_idt_gate(IOAPIC_SPURIOUS_BASE,
+ int_entry_table[24], KERNEL_CS,
+ ACC_PL_K|ACC_INTR_GATE, 0);
 }
 
diff --git a/i386/i386at/interrupt.S b/i386/i386at/interrupt.S
index 23a2e582..512163a6 100644
--- a/i386/i386at/interrupt.S
+++ b/i386/i386at/interrupt.S
@@ -29,6 +29,8 @@
 ENTRY(interrupt)
pushl   %eax/* save irq number */
movl%eax,%ecx   /* copy irq number */
+   cmpl$255,%eax   /* was this a spurious intr? */
+   je  0f  /* if so, null handler */
shll$2,%ecx /* irq * 4 */
callspl7/* set ipl */
movlEXT(iunit)(%ecx),%edx   /* get device unit number */
@@ -40,6 +42,7 @@ ENTRY(interrupt)
addl$4,%esp /* pop previous ipl */
 
cli /* XXX no more nested interrupts */
+#ifndef APIC
popl%ecx/* restore irq number */
 
movl$1,%eax
@@ -78,4 +81,17 @@ ENTRY(interrupt)
outb%al,$(PIC_MASTER_OCW)   /* unmask master */
 2:
ret
+#else
+   popl%ecx/* restore irq number */
+   movl%ecx,%eax   /* put irq number in eax */
+   callEXT(mask_irq)   /* mask irq */
+   callEXT(lapic_eoi)  /* EOI */
+   callEXT(unmask_irq) /* unmask irq */
+   ret /* return */
+0:
+   popl%ecx/* restore irq number */
+
+   callEXT(lapic_eoi)  /* EOI */
+   ret /* return */
+#endif
 END(interrupt)
diff --git a/i386/i386at/ioapic.c b/i386/i386at/ioapic.c
new file mode 100644
index ..72830653
--- /dev/null
+++ b/i386/i386at/ioapic.c
@@ -0,0 +1,320 @@
+/*
+ * Copyright (c) 2019 Damien Zammit
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+uint32_t lapic_timer_val = 0;
+uint32_t calibrated_ticks = 0;
+
+spl_t  curr_ipl;
+intcurr_pic_mask;
+
+intiunit[NINTR] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
+   16, 17, 18, 19, 20, 21, 22, 23};
+
+void (*ivect[NINTR])() = {
+   /* 00 */hardclock,  /* always */
+   /* 01 */kdintr, /* kdintr, ... */
+   /* 02 */intnull,
+   /* 03 */intnull,/* lnpoll, comintr, ... */
+
+   /* 04 */intnull,/* comintr, ... */
+   /* 05 */intnull,/* comintr, wtintr, ... */
+   /* 06 */intnull,/* fdintr, ... */
+   /* 07 */intnull,/* qdintr, ... */
+
+   /* 08 */intnull,
+   /* 09 */intnull,/* ether */
+   /* 10 */intnull,
+   /* 11 */intnull,
+
+   /* 12 */intnull,
+   /* 13 */fpintr, /* always */
+   /* 14 */intnull,/* hdintr, ... */
+   /* 15 */intnull,/* ??? */
+
+   /* 16 */intnull,/* PIRQA */
+   /* 17 */intnull,/* PIRQB */
+   /* 18 */intnull,/* PIRQC */
+   /* 19 */intnull,/* PIRQD */
+   /* 20 */intnull,/* PIRQE */
+   /* 21 */intnull,/* PIRQF */
+   /* 22 */intnull,/* PIRQG */
+   /* 23 */intnull,/* PIRQH */
+};
+
+void
+picdisable(void)
+{
+   asm("cli");
+
+   /*
+   ** Disable PIC
+   */
+   outb ( 0xa1, 0xff );
+   outb ( 0x21, 0xff );
+
+

Re: [PATCH] WIP: ioapic support - linux drivers disabled

2021-03-20 Thread Damien Zammit



On 21/3/21 11:54 am, Samuel Thibault wrote:
> Damien Zammit, le dim. 21 mars 2021 11:48:29 +1100, a ecrit:
>> {cpu0} ../kern/slab.c:1021: kmem_cache_free_to_slab: Assertion `((unsigned 
>> long)
>> buf + cache->buf_size) <= vm_page_trunc((unsigned long)slab->addr + 
>> cache->slab_
>> size)' failed.Debugger invoked: assertion failure
> 
> This looks like an array overrun.
> Use the trace command to get a backtrace.
> 
> Samuel
> 

db{0}> trace
Debugger(c115eec5,c116ee10,0,0,0)+0x13
Assert(c116ee10,c115f2ac,3fd,c11549ac,0)+0x65
kmem_cache_free_to_slab(0,f000ff53,f63aaf68,c103d113,c11de980)+0x20b
kmem_cache_free(c11de980,f000ff53,0,0)+0x3f
vm_map_copy_discard(f000ff53,0,f63aaf88,c1085b24)+0x33
free_skbuffs(0,0,0,c104cc70,0)+0x6e
io_done_thread_continue(0,0,f63aafd8,c102fc90,f63b3690)+0x15
io_done_thread(0,c102fc70,0,c1052bb5,f63b3690)+0x29
thread_continue(f63b3690,f63b2810,c102fc70,f63aafe4,0)+0x2a
Thread_continue()
db{0}>



[PATCH gnumach] fix timer bug

2021-03-25 Thread Damien Zammit
---
 kern/mach_clock.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kern/mach_clock.c b/kern/mach_clock.c
index e4d44f57..ed5cdfd3 100644
--- a/kern/mach_clock.c
+++ b/kern/mach_clock.c
@@ -152,7 +152,9 @@ void clock_interrupt(
timer_bump(&thread->user_timer, usec);
}
else {
-   timer_bump(&thread->system_timer, usec);
+   /* Only bump timer if thread exists */
+   if (thread)
+   timer_bump(&thread->system_timer, usec);
}
 #endif /* STAT_TIME */
 
@@ -189,6 +191,7 @@ void clock_interrupt(
if (usermode)
 #endif
{
+   if (thread)
take_pc_sample_macro(thread, SAMPLED_PC_PERIODIC, usermode, pc);
}
 #endif /* MACH_PCSAMPLE */
-- 
2.30.1




[PATCH gnumach] fixup acpi base table search

2021-03-25 Thread Damien Zammit
---
 i386/i386at/acpi_parse_apic.c | 25 +
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/i386/i386at/acpi_parse_apic.c b/i386/i386at/acpi_parse_apic.c
index 7855e8a5..9dbc6825 100644
--- a/i386/i386at/acpi_parse_apic.c
+++ b/i386/i386at/acpi_parse_apic.c
@@ -129,10 +129,10 @@ acpi_check_rsdp(struct acpi_rsdp *rsdp)
  */
 
 static int8_t
-acpi_check_rsdp_align(uint32_t addr)
+acpi_check_rsdp_align(void *addr)
 {
 /* check alignment. */
-if (addr & (ACPI_RSDP_ALIGN-1))
+if ((uintptr_t)addr & (ACPI_RSDP_ALIGN-1))
 return ACPI_BAD_ALIGN;
 
 return ACPI_SUCCESS;
@@ -175,29 +175,22 @@ acpi_search_rsdp(void *addr, uint32_t length)
 struct acpi_rsdp*
 acpi_get_rsdp(void)
 {
+uint16_t *start = 0;
+phys_addr_t base = 0;
 struct acpi_rsdp *rsdp = NULL;
-uint16_t *start = 0x0;
-uint32_t base = 0x0;
 
 /* EDBA start address. */
 start = (uint16_t*) phystokv(0x040e);
-base = *start;
+base = (*start) << 4; /* address = paragraph number * 16 */
 
-if (base != 0) { /* Memory check. */
-
-base <<= 4; /* base = base * 16 */
-
-/* check alignment. */
-if (acpi_check_rsdp_align(base) == ACPI_BAD_ALIGN)
-return NULL;
-
-/* Search the RSDP in first 1024 bytes from EDBA. */
-rsdp = acpi_search_rsdp((void*)base,1024);
+rsdp = acpi_search_rsdp((void *)phystokv(base), 1024);
+if (acpi_check_rsdp_align(rsdp) == ACPI_BAD_ALIGN) {
+return NULL;
 }
 
 if (rsdp == NULL) {
 /* If RSDP isn't in EDBA, search in the BIOS read-only memory space 
between 0Eh and 0Fh */
-rsdp = acpi_search_rsdp((void*) 0x0e, 0x10 - 0x0e);
+rsdp = acpi_search_rsdp((void *)phystokv(0xe), 0x2);
 }
 
 return rsdp;
-- 
2.30.1




gnumach: IOAPIC and LAPIC support

2021-03-26 Thread Damien Zammit
This patchset provides a way to disable linux/ drivers and build
gnumach with basic IOAPIC interrupts and LAPIC timer support in SMP
mode.

You can still build gnumach the old way with PIC interrupts and single
core, with or without linux/ drivers.

Note that it currently assumes there is only one IOAPIC in the system,
but can be extended to support systems with more.

We still need an ACPI server to provide the mappings for the
PIRQx to APIC pins by parsing it out of the DSDT ACPI table.
As discussed, this should be done via a new pci-arbiter RPC that
requests the IRQ id for a particular device.




[PATCH 5/5] ioapic interrupts and lapic timer support

2021-03-26 Thread Damien Zammit
Defaults to --enable-apic

To build with SMP and APIC support, use for example:

--enable-ncpus=4

Otherwise to build with PIC only and no SMP:

--enable-apic=no
---
 i386/Makefrag.am |  13 +-
 i386/configfrag.ac   |  12 ++
 i386/i386/apic.c |  34 ++-
 i386/i386/apic.h |  74 ++-
 i386/i386/fpu.c  |   2 +-
 i386/i386/irq.c  |   4 +
 i386/i386/irq.h  |   6 +-
 i386/i386/locore.S   |  13 ++
 i386/i386/pic.h  |   4 +-
 i386/i386/pit.c  |  38 +++-
 i386/i386/pit.h  |   2 +
 i386/i386at/acpi_parse_apic.c|  19 +-
 i386/i386at/acpi_parse_apic.h|   2 +-
 i386/i386at/autoconf.c   |   2 +-
 i386/i386at/idt.h|  17 +-
 i386/i386at/int_init.c   |   8 +-
 i386/i386at/interrupt.S  |  16 +-
 i386/i386at/ioapic.c | 345 +++
 i386/i386at/kd_mouse.c   |   2 +-
 i386/i386at/model_dep.c  |  21 +-
 linux/dev/arch/i386/kernel/irq.c |  26 +--
 21 files changed, 594 insertions(+), 66 deletions(-)
 create mode 100644 i386/i386at/ioapic.c

diff --git a/i386/Makefrag.am b/i386/Makefrag.am
index 73df45f4..de3cddc8 100644
--- a/i386/Makefrag.am
+++ b/i386/Makefrag.am
@@ -57,7 +57,6 @@ libkernel_a_SOURCES += \
i386/i386at/kdsoft.h \
i386/i386at/mem.c \
i386/i386at/mem.h \
-   i386/i386at/pic_isa.c \
i386/i386at/rtc.c \
i386/i386at/rtc.h
 endif
@@ -155,6 +154,16 @@ EXTRA_DIST += \
i386/i386/mach_i386.srv
 
 if PLATFORM_at
+if enable_apic
+libkernel_a_SOURCES += \
+   i386/i386at/ioapic.c
+else
+libkernel_a_SOURCES += \
+   i386/i386/pic.c \
+   i386/i386/pic.h \
+   i386/i386at/pic_isa.c
+endif
+
 libkernel_a_SOURCES += \
i386/i386/apic.h \
i386/i386/apic.c \
@@ -163,8 +172,6 @@ libkernel_a_SOURCES += \
i386/i386/io_map.c \
i386/i386/irq.c \
i386/i386/irq.h \
-   i386/i386/pic.c \
-   i386/i386/pic.h \
i386/i386/pit.c \
i386/i386/pit.h
 endif
diff --git a/i386/configfrag.ac b/i386/configfrag.ac
index bf4af110..9a39ccbb 100644
--- a/i386/configfrag.ac
+++ b/i386/configfrag.ac
@@ -92,6 +92,18 @@ if [ x"$enable_lpr" = xyes ]; then]
   AM_CONDITIONAL([enable_lpr], [false])
 [fi]
 
+AC_ARG_ENABLE([apic],
+  AS_HELP_STRING([--enable-apic], [LAPIC/IOAPIC support (ix86-only); enabled 
by default]))
+[case $host_platform:$host_cpu in
+  *:i?86)
+enable_apic=${enable_apic-yes};;
+esac
+if [ x"$enable_apic" = xyes ]; then]
+  AC_DEFINE([APIC], [1], [APIC support])
+  AM_CONDITIONAL([enable_apic], [true])
+[else]
+  AM_CONDITIONAL([enable_apic], [false])
+[fi]
 
 [case $host_platform:$host_cpu in
   xen:i?86)
diff --git a/i386/i386/apic.c b/i386/i386/apic.c
index f0b4a153..099f80a7 100644
--- a/i386/i386/apic.c
+++ b/i386/i386/apic.c
@@ -95,6 +95,19 @@ apic_add_irq_override(IrqOverrideData irq_over)
 apic_data.nirqoverride++;
 }
 
+IrqOverrideData *
+acpi_get_irq_override(uint8_t gsi)
+{
+int i;
+
+for (i = 0; i < apic_data.nirqoverride; i++) {
+if (apic_data.irq_override_list[i].gsi == gsi) {
+return &apic_data.irq_override_list[i];
+}
+}
+return NULL;
+}
+
 /*
  * apic_get_cpu_apic_id: returns the apic_id of a cpu.
  * Receives as input the kernel ID of a CPU.
@@ -118,16 +131,14 @@ apic_get_lapic(void)
 /*
  * apic_get_ioapic: returns the IOAPIC identified by its kernel ID.
  * Receives as input the IOAPIC's Kernel ID.
- * Returns a ioapic_data structure with the IOAPIC's data.
+ * Returns a ioapic_data structure pointer with the IOAPIC's data.
  */
-struct IoApicData
+struct IoApicData *
 apic_get_ioapic(int kernel_id)
 {
-IoApicData io_apic = {};
-
 if (kernel_id < MAX_IOAPICS)
-return apic_data.ioapic_list[kernel_id];
-return io_apic;
+return &apic_data.ioapic_list[kernel_id];
+return (struct IoApicData *)0;
 }
 
 /* apic_get_numcpus: returns the current number of cpus. */
@@ -204,18 +215,21 @@ void apic_print_info(void)
 uint16_t lapic_id;
 uint16_t ioapic_id;
 
-IoApicData ioapic;
+IoApicData *ioapic;
 
 printf("CPUS:\n");
 for (i = 0; i < ncpus; i++) {
 lapic_id = apic_get_cpu_apic_id(i);
-printf(" CPU %d - APIC ID %x\n", i, lapic_id);
+printf(" CPU %d - APIC ID %x - addr=0x%p\n", i, lapic_id, 
apic_get_lapic());
 }
 
 printf("IOAPICS:\n");
 for (i = 0; i < nioapics; i++) {
 ioapic = apic_get_ioapic(i);
-ioapic_id = ioapic.apic_id;
-printf(" IOAPIC %d - APIC ID %x\n", i, ioapic_id);
+if (!ioapic) {
+printf("ERROR: invalid IOAPIC ID %x\n", i);
+}
+ioapic_id = ioapic->apic_id;
+printf(" IOAPIC %d - APIC ID %x - addr=0x%p\n", i, ioapic_id, 
ioapic->ioapic);
 }
 }
diff --git a/i386/i386/apic.h b/i386/i386/apic.h
index e2d2c508..f62f2ef

[PATCH 1/5] Make linux drivers optional

2021-03-26 Thread Damien Zammit
Defaults to build with linux drivers.
To build without linux drivers, use:

--enable-qemu-group=no --enable-net-group=no
--enable-scsi-group=no --enable-wireless-group=no --enable-pcmcia-group=no

---
 device/intr.c   | 36 
 linux/configfrag.ac | 38 +++---
 2 files changed, 59 insertions(+), 15 deletions(-)

diff --git a/device/intr.c b/device/intr.c
index fbb9f495..5a8ee758 100644
--- a/device/intr.c
+++ b/device/intr.c
@@ -12,6 +12,7 @@
  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -26,6 +27,14 @@
 queue_head_t main_intr_queue;
 static boolean_t deliver_intr (int id, ipc_port_t dst_port);
 
+#ifndef LINUX_DEV
+struct intr_list {
+  user_intr_t *user_intr;
+  struct intr_list *next;
+};
+static struct intr_list *user_intr_handlers;
+#endif
+
 static user_intr_t *
 search_intr (struct irqdev *dev, ipc_port_t dst_port)
 {
@@ -147,6 +156,33 @@ out:
   return ret;
 }
 
+#ifndef LINUX_DEV
+int
+install_user_intr_handler (struct irqdev *dev, int id, unsigned long flags,
+ user_intr_t *user_intr)
+{
+  unsigned int irq = dev->irq[id];
+  struct intr_list *old = user_intr_handlers;
+
+  assert (irq < NINTR);
+
+  while (old)
+{
+  /* Test whether the irq handler has been set */
+  if (old->user_intr->dst_port && old->user_intr->dst_port == 
user_intr->dst_port)
+{
+  printf ("This interrupt handler has already been installed on line 
%d", irq);
+  return 1;
+}
+  old = old->next;
+}
+
+  old = kalloc (sizeof (struct intr_list));
+  *(old->user_intr) = *user_intr;
+  return deliver_user_intr(dev, irq, old->user_intr);
+}
+#endif
+
 void
 intr_thread (void)
 {
diff --git a/linux/configfrag.ac b/linux/configfrag.ac
index 78b59d7f..26c99e45 100644
--- a/linux/configfrag.ac
+++ b/linux/configfrag.ac
@@ -33,6 +33,8 @@ dnl USE OF THIS SOFTWARE.
 '')
   # No group.
   :;;
+qemu)
+  device_driver_group_qemu=selected;;
 net)
   device_driver_group_net=selected;;
 pcmcia)
@@ -75,7 +77,7 @@ fi
 case $host_platform:$host_cpu in
   at:i?86)
 case $enable_device_drivers:'$2' in
-  default:*by\ default* | qemu:*for\ qemu*)
+  default:*by\ default*)
 enableval=${enableval-yes};;
   *)
 enableval=${enableval-no};;
@@ -129,10 +131,6 @@ AC_DEFUN([AC_Linux_DRIVER], [
   AC_OPTION_Linux_ix86_at([$1], [Linux device driver for $2; on ix86-at 
enabled]
 [by default], [$3], [$4])
 ])
-AC_DEFUN([AC_Linux_DRIVER_qemu], [
-  AC_OPTION_Linux_ix86_at([$1], [Linux device driver for $2; on ix86-at 
enabled]
-[by default and for qemu], [$3], [$4])
-])
 AC_DEFUN([AC_Linux_DRIVER_nodef], [
   AC_OPTION_Linux_ix86_at_nodef([$1], [Linux device driver for $2], [$3], [$4])
 ])
@@ -141,13 +139,21 @@ AC_DEFUN([AC_Linux_DRIVER_nodef], [
 # Configuration options.
 #
 
-AC_Linux_DRIVER_qemu([floppy],
-  [PC floppy],
-  [CONFIG_BLK_DEV_FD])
+dnl QEMU drivers.
+AC_OPTION_Linux_group([qemu], [QEMU drivers])
 
-AC_Linux_DRIVER_qemu([ide],
+AC_Linux_DRIVER([floppy],
+  [PC floppy],
+  [CONFIG_BLK_DEV_FD],
+  [qemu])
+AC_Linux_DRIVER([ide],
   [IDE disk controllers],
-  [CONFIG_BLK_DEV_IDE])
+  [CONFIG_BLK_DEV_IDE],
+  [qemu])
+AC_Linux_DRIVER([ne],
+  [Ethernet controller NE2000/NE1000 ISA (ne, ne1000, ne2000)],
+  [CONFIG_NE2000],
+  [qemu])
 
 AC_ARG_ENABLE([ide-forcedma],
   AS_HELP_STRING([--enable-ide-forcedma], [enable forced use of DMA on IDE]),
@@ -431,10 +437,6 @@ AC_Linux_DRIVER([natsemi],
   [Ethernet controller National Semiconductor DP8381x series PCI Ethernet],
   [CONFIG_NATSEMI],
   [net])
-AC_Linux_DRIVER_qemu([ne],
-  [Ethernet controller NE2000/NE1000 ISA (ne, ne1000, ne2000)],
-  [CONFIG_NE2000],
-  [net])
 AC_Linux_DRIVER([ne2k-pci],
   [Ethernet controller PCI NE2000],
   [CONFIG_NE2K_PCI],
@@ -582,7 +584,13 @@ AC_Linux_DRIVER([orinoco_cs],
 # that get brought in, need special symbols defined, etc.
 #
 
-[if [ x"$device_driver_group_net" = xselected ]; then]
+[if [ x"$device_driver_group_qemu" = xselected ]; then]
+  AC_DEFINE([CONFIG_QEMU], [1], [CONFIG_QEMU])
+  AM_CONDITIONAL([device_driver_group_qemu], [true])
+[else] AM_CONDITIONAL([device_driver_group_qemu], [false])
+[fi
+
+if [ x"$device_driver_group_net" = xselected ]; then]
   AC_DEFINE([CONFIG_INET], [1], [CONFIG_INET])
   AM_CONDITIONAL([device_driver_group_net], [true])
 [else] AM_CONDITIONAL([device_driver_group_net], [false])
-- 
2.30.1




[PATCH 2/5] fixup acpi base table search

2021-03-26 Thread Damien Zammit
---
 i386/i386at/acpi_parse_apic.c | 25 +
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/i386/i386at/acpi_parse_apic.c b/i386/i386at/acpi_parse_apic.c
index 7855e8a5..9dbc6825 100644
--- a/i386/i386at/acpi_parse_apic.c
+++ b/i386/i386at/acpi_parse_apic.c
@@ -129,10 +129,10 @@ acpi_check_rsdp(struct acpi_rsdp *rsdp)
  */
 
 static int8_t
-acpi_check_rsdp_align(uint32_t addr)
+acpi_check_rsdp_align(void *addr)
 {
 /* check alignment. */
-if (addr & (ACPI_RSDP_ALIGN-1))
+if ((uintptr_t)addr & (ACPI_RSDP_ALIGN-1))
 return ACPI_BAD_ALIGN;
 
 return ACPI_SUCCESS;
@@ -175,29 +175,22 @@ acpi_search_rsdp(void *addr, uint32_t length)
 struct acpi_rsdp*
 acpi_get_rsdp(void)
 {
+uint16_t *start = 0;
+phys_addr_t base = 0;
 struct acpi_rsdp *rsdp = NULL;
-uint16_t *start = 0x0;
-uint32_t base = 0x0;
 
 /* EDBA start address. */
 start = (uint16_t*) phystokv(0x040e);
-base = *start;
+base = (*start) << 4; /* address = paragraph number * 16 */
 
-if (base != 0) { /* Memory check. */
-
-base <<= 4; /* base = base * 16 */
-
-/* check alignment. */
-if (acpi_check_rsdp_align(base) == ACPI_BAD_ALIGN)
-return NULL;
-
-/* Search the RSDP in first 1024 bytes from EDBA. */
-rsdp = acpi_search_rsdp((void*)base,1024);
+rsdp = acpi_search_rsdp((void *)phystokv(base), 1024);
+if (acpi_check_rsdp_align(rsdp) == ACPI_BAD_ALIGN) {
+return NULL;
 }
 
 if (rsdp == NULL) {
 /* If RSDP isn't in EDBA, search in the BIOS read-only memory space 
between 0Eh and 0Fh */
-rsdp = acpi_search_rsdp((void*) 0x0e, 0x10 - 0x0e);
+rsdp = acpi_search_rsdp((void *)phystokv(0xe), 0x2);
 }
 
 return rsdp;
-- 
2.30.1




[PATCH 3/5] fix timer bug

2021-03-26 Thread Damien Zammit
---
 kern/mach_clock.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/kern/mach_clock.c b/kern/mach_clock.c
index e4d44f57..ed5cdfd3 100644
--- a/kern/mach_clock.c
+++ b/kern/mach_clock.c
@@ -152,7 +152,9 @@ void clock_interrupt(
timer_bump(&thread->user_timer, usec);
}
else {
-   timer_bump(&thread->system_timer, usec);
+   /* Only bump timer if thread exists */
+   if (thread)
+   timer_bump(&thread->system_timer, usec);
}
 #endif /* STAT_TIME */
 
@@ -189,6 +191,7 @@ void clock_interrupt(
if (usermode)
 #endif
{
+   if (thread)
take_pc_sample_macro(thread, SAMPLED_PC_PERIODIC, usermode, pc);
}
 #endif /* MACH_PCSAMPLE */
-- 
2.30.1




[PATCH 4/5] fix EISA check in init_IRQ

2021-03-26 Thread Damien Zammit
---
 linux/dev/arch/i386/kernel/irq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/linux/dev/arch/i386/kernel/irq.c b/linux/dev/arch/i386/kernel/irq.c
index 522ed0ac..06889e58 100644
--- a/linux/dev/arch/i386/kernel/irq.c
+++ b/linux/dev/arch/i386/kernel/irq.c
@@ -743,7 +743,7 @@ init_IRQ (void)
   /*
* Check if the machine has an EISA bus.
*/
-  p = (char *) 0x0FFFD9;
+  p = (char *) phystokv(0x0FFFD9);
   if (*p++ == 'E' && *p++ == 'I' && *p++ == 'S' && *p == 'A')
 EISA_bus = 1;
   
-- 
2.30.1




Re: gnumach: IOAPIC and LAPIC support

2021-03-26 Thread Damien Zammit
On 26/3/21 8:59 pm, Samuel Thibault wrote:
> Damien Zammit, le ven. 26 mars 2021 20:48:45 +1100, a ecrit:
>> As discussed, this should be done via a new pci-arbiter RPC that
>> requests the IRQ id for a particular device.
> 
> Mmm, AIUI a driver can already read the PCI IRQ information with
> pci_conf_read, and lookup in acpi for the irq line, I don't think
> pci-arbiter itself needs an additional RPC? That could rather be on the
> acpi server.

As I understand it, the PCI IRQ information is incorrect in the config space,
the only way to know the APIC pin for a specific device is via ACPI.
So you have to pass in the B/D/F of the device to an acpi RPC and discover its 
pin.

You can't use a pci device without the arbiter and ACPI
is just one specific implementation of the lookup.
We could, for example, fall back to guessing the pin if ACPI is not available.
Why should every user of a pci device have specific logic to discover its pin?
Wouldn't this be better to be centralised in a pci RPC?
Also, in the future if we use IOMMU the irq would need to be remapped by the 
arbiter.

Damien



[PATCH v2 gnumach] - IOAPIC and LAPIC support

2021-03-27 Thread Damien Zammit
I have split the patches up into logical pieces.
Perhaps all can be merged except the last patch until we fix
the selection of apic via kernel cmdline (?)

It now almost boots when you give it the right IRQ.
I am not sure why it hangs just before "proc" is supposed to appear,
but it probes and finds the disk wd0 device.

Damien



[PATCH 2/6] fixup acpi base table search

2021-03-27 Thread Damien Zammit
---
 i386/i386at/acpi_parse_apic.c | 32 ++--
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/i386/i386at/acpi_parse_apic.c b/i386/i386at/acpi_parse_apic.c
index 7855e8a5..36e2805d 100644
--- a/i386/i386at/acpi_parse_apic.c
+++ b/i386/i386at/acpi_parse_apic.c
@@ -129,10 +129,10 @@ acpi_check_rsdp(struct acpi_rsdp *rsdp)
  */
 
 static int8_t
-acpi_check_rsdp_align(uint32_t addr)
+acpi_check_rsdp_align(void *addr)
 {
 /* check alignment. */
-if (addr & (ACPI_RSDP_ALIGN-1))
+if ((uintptr_t)addr & (ACPI_RSDP_ALIGN-1))
 return ACPI_BAD_ALIGN;
 
 return ACPI_SUCCESS;
@@ -175,29 +175,22 @@ acpi_search_rsdp(void *addr, uint32_t length)
 struct acpi_rsdp*
 acpi_get_rsdp(void)
 {
+uint16_t *start = 0;
+phys_addr_t base = 0;
 struct acpi_rsdp *rsdp = NULL;
-uint16_t *start = 0x0;
-uint32_t base = 0x0;
 
 /* EDBA start address. */
 start = (uint16_t*) phystokv(0x040e);
-base = *start;
+base = phystokv((*start) << 4); /* address = paragraph number * 16 */
 
-if (base != 0) { /* Memory check. */
-
-base <<= 4; /* base = base * 16 */
-
-/* check alignment. */
-if (acpi_check_rsdp_align(base) == ACPI_BAD_ALIGN)
-return NULL;
-
-/* Search the RSDP in first 1024 bytes from EDBA. */
-rsdp = acpi_search_rsdp((void*)base,1024);
-}
+/* check alignment. */
+if (acpi_check_rsdp_align((void *)base) == ACPI_BAD_ALIGN)
+return NULL;
+rsdp = acpi_search_rsdp((void *)base, 1024);
 
 if (rsdp == NULL) {
 /* If RSDP isn't in EDBA, search in the BIOS read-only memory space 
between 0Eh and 0Fh */
-rsdp = acpi_search_rsdp((void*) 0x0e, 0x10 - 0x0e);
+rsdp = acpi_search_rsdp((void *)phystokv(0xe), 0x10 - 
0x0e);
 }
 
 return rsdp;
@@ -421,6 +414,9 @@ acpi_apic_parse_table(struct acpi_apic *apic)
 acpi_apic_add_irq_override(irq_override_entry);
 break;
 
+default:
+/* FIXME: There is another unhandled case */
+break;
 }
 
 /* Get next APIC entry. */
@@ -480,7 +476,7 @@ acpi_apic_setup(struct acpi_apic *apic)
 /* Refit the apic-cpu array. */
 if(ncpus < NCPUS) {
 int refit = apic_refit_cpulist();
-if (refit != -0)
+if (refit != 0)
 return ACPI_FIT_FAILURE;
 }
 
-- 
2.30.1




[PATCH 4/6] Existing SMP apic code improvements

2021-03-27 Thread Damien Zammit
---
 i386/i386/apic.c  | 35 +++-
 i386/i386/apic.h  | 75 +--
 i386/i386at/acpi_parse_apic.c | 15 ---
 i386/i386at/acpi_parse_apic.h |  2 +-
 4 files changed, 106 insertions(+), 21 deletions(-)

diff --git a/i386/i386/apic.c b/i386/i386/apic.c
index f0b4a153..2e0c1776 100644
--- a/i386/i386/apic.c
+++ b/i386/i386/apic.c
@@ -95,6 +95,19 @@ apic_add_irq_override(IrqOverrideData irq_over)
 apic_data.nirqoverride++;
 }
 
+IrqOverrideData *
+acpi_get_irq_override(uint8_t pin)
+{
+int i;
+
+for (i = 0; i < apic_data.nirqoverride; i++) {
+if (apic_data.irq_override_list[i].irq == pin) {
+return &apic_data.irq_override_list[i];
+}
+}
+return NULL;
+}
+
 /*
  * apic_get_cpu_apic_id: returns the apic_id of a cpu.
  * Receives as input the kernel ID of a CPU.
@@ -118,16 +131,14 @@ apic_get_lapic(void)
 /*
  * apic_get_ioapic: returns the IOAPIC identified by its kernel ID.
  * Receives as input the IOAPIC's Kernel ID.
- * Returns a ioapic_data structure with the IOAPIC's data.
+ * Returns a ioapic_data structure pointer with the IOAPIC's data.
  */
-struct IoApicData
+struct IoApicData *
 apic_get_ioapic(int kernel_id)
 {
-IoApicData io_apic = {};
-
 if (kernel_id < MAX_IOAPICS)
-return apic_data.ioapic_list[kernel_id];
-return io_apic;
+return &apic_data.ioapic_list[kernel_id];
+return NULL;
 }
 
 /* apic_get_numcpus: returns the current number of cpus. */
@@ -204,18 +215,22 @@ void apic_print_info(void)
 uint16_t lapic_id;
 uint16_t ioapic_id;
 
-IoApicData ioapic;
+IoApicData *ioapic;
 
 printf("CPUS:\n");
 for (i = 0; i < ncpus; i++) {
 lapic_id = apic_get_cpu_apic_id(i);
-printf(" CPU %d - APIC ID %x\n", i, lapic_id);
+printf(" CPU %d - APIC ID %x - addr=0x%p\n", i, lapic_id, 
apic_get_lapic());
 }
 
 printf("IOAPICS:\n");
 for (i = 0; i < nioapics; i++) {
 ioapic = apic_get_ioapic(i);
-ioapic_id = ioapic.apic_id;
-printf(" IOAPIC %d - APIC ID %x\n", i, ioapic_id);
+if (!ioapic) {
+printf("ERROR: invalid IOAPIC ID %x\n", i);
+} else {
+ioapic_id = ioapic->apic_id;
+printf(" IOAPIC %d - APIC ID %x - addr=0x%p\n", i, ioapic_id, 
ioapic->ioapic);
+}
 }
 }
diff --git a/i386/i386/apic.h b/i386/i386/apic.h
index e2d2c508..54ccda5b 100644
--- a/i386/i386/apic.h
+++ b/i386/i386/apic.h
@@ -28,8 +28,8 @@
 #include 
 
 typedef struct ApicReg {
-unsigned r;/* the actual register */
-unsigned p[3]; /* pad to the next 128-bit boundary */
+uint32_t r;/* the actual register */
+uint32_t p[3]; /* pad to the next 128-bit boundary */
 } ApicReg;
 
 typedef struct ApicIoUnit {
@@ -37,6 +37,27 @@ typedef struct ApicIoUnit {
 ApicReg window;
 } ApicIoUnit;
 
+struct ioapic_route_entry {
+uint32_t vector  : 8,
+delvmode: 3, /* 000=fixed 001=lowest 111=ExtInt */
+destmode: 1, /* 0=physical 1=logical */
+delvstatus  : 1,
+polarity: 1, /* 0=activehigh 1=activelow */
+irr : 1,
+trigger : 1, /* 0=edge 1=level */
+mask: 1, /* 0=enabled 1=disabled */
+reserved1   : 15;
+uint32_t reserved2   : 24,
+dest: 8;
+} __attribute__ ((packed));
+
+union ioapic_route_entry_union {
+struct {
+   uint32_t lo;
+   uint32_t hi;
+};
+struct ioapic_route_entry both;
+};
 
 typedef struct ApicLocalUnit {
 ApicReg reserved0;   /* 0x000 */
@@ -82,11 +103,14 @@ typedef struct ApicLocalUnit {
 typedef struct IoApicData {
 uint8_t  apic_id;
 uint32_t addr;
-uint32_t base;
+uint32_t gsi_base;
+ApicIoUnit *ioapic;
 } IoApicData;
 
 #define APIC_IRQ_OVERRIDE_ACTIVE_LOW 2
+#define APIC_IRQ_OVERRIDE_POLARITY_MASK 1
 #define APIC_IRQ_OVERRIDE_LEVEL_TRIGGERED 8
+#define APIC_IRQ_OVERRIDE_TRIGGER_MASK 4
 
 typedef struct IrqOverrideData {
 uint8_t  bus;
@@ -112,14 +136,28 @@ void apic_add_cpu(uint16_t apic_id);
 void apic_lapic_init(ApicLocalUnit* lapic_ptr);
 void apic_add_ioapic(struct IoApicData);
 void apic_add_irq_override(struct IrqOverrideData irq_over);
+IrqOverrideData *acpi_get_irq_override(uint8_t gsi);
 uint16_t apic_get_cpu_apic_id(int kernel_id);
 volatile ApicLocalUnit* apic_get_lapic(void);
-struct IoApicData apic_get_ioapic(int kernel_id);
+struct IoApicData *apic_get_ioapic(int kernel_id);
 uint8_t apic_get_numcpus(void);
 uint8_t apic_get_num_ioapics(void);
 uint16_t apic_get_current_cpu(void);
 void apic_print_info(void);
 int apic_refit_cpulist(void);
+void picdisable(void);
+void lapic_eoi(void);
+void ioapic_irq_eoi(int pin);
+void lapic_enable_timer(void);
+void ioapic_mask_irqs(void);
+void ioapic_toggle(int pin, int mask);
+void ioapic_configure(void);
+
+extern 

[PATCH 1/6] Make linux drivers optional

2021-03-27 Thread Damien Zammit
Defaults to build with linux drivers.
To build without linux drivers, use:

--enable-block-group=no --enable-net-group=no
--enable-scsi-group=no --enable-wireless-group=no --enable-pcmcia-group=no
---
 device/intr.c   | 91 +
 linux/configfrag.ac | 32 ++--
 2 files changed, 111 insertions(+), 12 deletions(-)

diff --git a/device/intr.c b/device/intr.c
index fbb9f495..7cf991de 100644
--- a/device/intr.c
+++ b/device/intr.c
@@ -12,6 +12,7 @@
  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -26,6 +27,18 @@
 queue_head_t main_intr_queue;
 static boolean_t deliver_intr (int id, ipc_port_t dst_port);
 
+#ifndef LINUX_DEV
+#define SA_SHIRQ 0x0400
+#define EBUSY 16
+
+struct intr_list {
+  user_intr_t *user_intr;
+  unsigned long flags;
+  struct intr_list *next;
+};
+static struct intr_list *user_intr_handlers;
+#endif
+
 static user_intr_t *
 search_intr (struct irqdev *dev, ipc_port_t dst_port)
 {
@@ -147,6 +160,84 @@ out:
   return ret;
 }
 
+#ifndef LINUX_DEV
+
+static void
+user_irq_handler (int id)
+{
+  struct intr_list *handler_list = user_intr_handlers;
+  user_intr_t *e;
+  spl_t s;
+
+  s = splhigh();
+
+  while (handler_list)
+{
+  e = handler_list->user_intr;
+  if (e->id == id)
+deliver_user_intr(&irqtab, id, e);
+  handler_list = handler_list->next;
+}
+  splx(s);
+}
+
+int
+install_user_intr_handler (struct irqdev *dev, int id, unsigned long flags,
+ user_intr_t *user_intr)
+{
+  unsigned int irq = dev->irq[id];
+  struct intr_list *old = user_intr_handlers;
+  struct intr_list *new;
+  spl_t s;
+
+  assert (irq < NINTR);
+
+  while (old)
+{
+  /* Try to find an irq handler that has been already set on the same pin 
*/
+  if (old->user_intr->dst_port && old->user_intr->id == user_intr->id)
+break;
+  old = old->next;
+}
+
+  if (old)
+{
+  mach_print("Duplicate handler\n");
+  new = (struct intr_list *)kalloc (sizeof (struct intr_list));
+  new->user_intr = user_intr;
+  new->flags = flags;
+
+  /* Duplicated, so test if sharing allowed */
+  if (!(old->flags & new->flags & SA_SHIRQ))
+{
+  mach_print ("EBUSY! (no sharing)\n");
+  return EBUSY;
+}
+  /* Fine, insert new handler */
+  old->next = new;
+}
+  else
+{
+  /* First entry */
+  mach_print ("First handler\n");
+  old = (struct intr_list *)kalloc (sizeof (struct intr_list));
+  old->user_intr = user_intr;
+  old->flags = flags;
+  old->next = NULL;
+  user_intr_handlers = old;
+}
+
+  s = splhigh();
+
+  ivect[irq] = user_irq_handler;
+  iunit[irq] = (int)irq;
+  unmask_irq (irq);
+
+  splx(s);
+  return 0;
+}
+#endif
+
 void
 intr_thread (void)
 {
diff --git a/linux/configfrag.ac b/linux/configfrag.ac
index 78b59d7f..6fb60935 100644
--- a/linux/configfrag.ac
+++ b/linux/configfrag.ac
@@ -33,6 +33,8 @@ dnl USE OF THIS SOFTWARE.
 '')
   # No group.
   :;;
+block)
+  device_driver_group_block=selected;;
 net)
   device_driver_group_net=selected;;
 pcmcia)
@@ -75,7 +77,7 @@ fi
 case $host_platform:$host_cpu in
   at:i?86)
 case $enable_device_drivers:'$2' in
-  default:*by\ default* | qemu:*for\ qemu*)
+  default:*by\ default*)
 enableval=${enableval-yes};;
   *)
 enableval=${enableval-no};;
@@ -129,10 +131,6 @@ AC_DEFUN([AC_Linux_DRIVER], [
   AC_OPTION_Linux_ix86_at([$1], [Linux device driver for $2; on ix86-at 
enabled]
 [by default], [$3], [$4])
 ])
-AC_DEFUN([AC_Linux_DRIVER_qemu], [
-  AC_OPTION_Linux_ix86_at([$1], [Linux device driver for $2; on ix86-at 
enabled]
-[by default and for qemu], [$3], [$4])
-])
 AC_DEFUN([AC_Linux_DRIVER_nodef], [
   AC_OPTION_Linux_ix86_at_nodef([$1], [Linux device driver for $2], [$3], [$4])
 ])
@@ -141,13 +139,17 @@ AC_DEFUN([AC_Linux_DRIVER_nodef], [
 # Configuration options.
 #
 
-AC_Linux_DRIVER_qemu([floppy],
-  [PC floppy],
-  [CONFIG_BLK_DEV_FD])
+dnl Block drivers.
+AC_OPTION_Linux_group([block], [Block drivers])
 
-AC_Linux_DRIVER_qemu([ide],
+AC_Linux_DRIVER([floppy],
+  [PC floppy],
+  [CONFIG_BLK_DEV_FD],
+  [block])
+AC_Linux_DRIVER([ide],
   [IDE disk controllers],
-  [CONFIG_BLK_DEV_IDE])
+  [CONFIG_BLK_DEV_IDE],
+  [block])
 
 AC_ARG_ENABLE([ide-forcedma],
   AS_HELP_STRING([--enable-ide-forcedma], [enable forced use of DMA on IDE]),
@@ -431,7 +433,7 @@ AC_Linux_DRIVER([natsemi],
   [Ethernet controller National Semiconductor DP8381x series PCI Ethernet],
   [CONFIG_NATSEMI],
   [net])
-AC_Linux_DRIVER_qemu([ne],
+AC_Linux_DRIVER([ne],
   [Ethernet controller NE2000/NE1000 ISA (ne, ne1000, ne2000)],
   [CONFIG_NE2000],
   [net])
@@ -582,7 +584,13 @@ AC_Linux_DRIVER([orinoco_cs],
 # that get brought in, need special symbols defined, etc.
 #
 
-[if [ x"$device_driver_group_net" = xselected ]; then]

[PATCH 5/6] Move PIC interrupt vector base from 0x40 to 0x20

2021-03-27 Thread Damien Zammit
---
 i386/i386/locore.S | 15 +++
 i386/i386/pic.h|  4 +++-
 i386/i386at/idt.h  | 17 +++--
 i386/i386at/int_init.c | 15 +--
 4 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/i386/i386/locore.S b/i386/i386/locore.S
index bee3630c..8a1054a6 100644
--- a/i386/i386/locore.S
+++ b/i386/i386/locore.S
@@ -607,6 +607,7 @@ ENTRY(call_continuation)
jmp *%eax   /* goto continuation */
 
 
+/* IOAPIC has 24 interrupts, put spurious in the same array */
 
 #define INTERRUPT(n)   \
.data   2   ;\
@@ -621,6 +622,7 @@ ENTRY(call_continuation)
.data   2
 DATA(int_entry_table)
.text
+/* Legacy APIC interrupts or PIC interrupts */
 INTERRUPT(0)
 INTERRUPT(1)
 INTERRUPT(2)
@@ -637,6 +639,19 @@ INTERRUPT(12)
 INTERRUPT(13)
 INTERRUPT(14)
 INTERRUPT(15)
+#ifdef APIC
+/* APIC PCI interrupts PIRQ A-H */
+INTERRUPT(16)
+INTERRUPT(17)
+INTERRUPT(18)
+INTERRUPT(19)
+INTERRUPT(20)
+INTERRUPT(21)
+INTERRUPT(22)
+INTERRUPT(23)
+/* Spurious interrupt, set irq number to vect number */
+INTERRUPT(255)
+#endif
 
 /* XXX handle NMI - at least print a warning like Linux does.  */
 
diff --git a/i386/i386/pic.h b/i386/i386/pic.h
index 6434bf08..0ccf1c9a 100644
--- a/i386/i386/pic.h
+++ b/i386/i386/pic.h
@@ -96,7 +96,7 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
 #ifdefined(AT386) || defined(ATX86_64)
-#definePICM_VECTBASE   0x40
+#definePICM_VECTBASE   0x20
 #define PICS_VECTBASE  PICM_VECTBASE + 0x08
 #endif /* defined(AT386) */
 
@@ -176,6 +176,8 @@ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #define READ_IR_ONRD   0x00
 #define READ_IS_ONRD   0x01
 
+#define PIC_MASK_ZERO  0x00
+
 #ifndef __ASSEMBLER__
 extern void picinit (void);
 extern int curr_pic_mask;
diff --git a/i386/i386at/idt.h b/i386/i386at/idt.h
index 56e6296c..ac065aef 100644
--- a/i386/i386at/idt.h
+++ b/i386/i386at/idt.h
@@ -24,13 +24,18 @@
 #ifndef _I386AT_IDT_
 #define _I386AT_IDT_
 
-/* On a standard PC, we only need 16 interrupt vectors,
-   because that's all the PIC hardware supports.  */
-/* XX But for some reason we program the PIC
-   to use vectors 0x40-0x4f rather than 0x20-0x2f.  Fix.  */
-#define IDTSZ (0x20+0x20+0x10)
+/* There are 256 interrupt vectors on x86,
+ * the first 32 are taken by cpu faults */
+#define IDTSZ (0x100)
 
-#define PIC_INT_BASE 0x40
+/* PIC sits at 0x20-0x2f */
+#define PIC_INT_BASE 0x20
+
+/* IOAPIC sits at 0x30-0x47 */
+#define IOAPIC_INT_BASE 0x30
+
+/* IOAPIC spurious interrupt vector set to 0xff */
+#define IOAPIC_SPURIOUS_BASE 0xff
 
 #include 
 
diff --git a/i386/i386at/int_init.c b/i386/i386at/int_init.c
index 43daad8b..6da627dd 100644
--- a/i386/i386at/int_init.c
+++ b/i386/i386at/int_init.c
@@ -30,10 +30,21 @@ extern vm_offset_t int_entry_table[];
 void int_init(void)
 {
int i;
-
-   for (i = 0; i < 16; i++)
+#ifndef APIC
+   for (i = 0; i < 16; i++) {
fill_idt_gate(PIC_INT_BASE + i,
  int_entry_table[i], KERNEL_CS,
  ACC_PL_K|ACC_INTR_GATE, 0);
+   }
+#else
+   for (i = 0; i < 24; i++) {
+   fill_idt_gate(IOAPIC_INT_BASE + i,
+ int_entry_table[i], KERNEL_CS,
+ ACC_PL_K|ACC_INTR_GATE, 0);
+   }
+   fill_idt_gate(IOAPIC_SPURIOUS_BASE,
+ int_entry_table[24], KERNEL_CS,
+ ACC_PL_K|ACC_INTR_GATE, 0);
+#endif
 }
 
-- 
2.30.1




[PATCH 3/6] Don't allow writing to acpi table in memory

2021-03-27 Thread Damien Zammit
---
 i386/i386at/acpi_parse_apic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/i386/i386at/acpi_parse_apic.c b/i386/i386at/acpi_parse_apic.c
index 36e2805d..af70934d 100644
--- a/i386/i386at/acpi_parse_apic.c
+++ b/i386/i386at/acpi_parse_apic.c
@@ -280,7 +280,7 @@ acpi_get_apic(struct acpi_rsdt *rsdt, int acpi_rsdt_n)
 /* Search APIC entries in rsdt table. */
 for (int i = 0; i < acpi_rsdt_n; i++) {
 descr_header = (struct acpi_dhdr*) 
kmem_map_aligned_table(rsdt->entry[i], sizeof(struct acpi_dhdr),
-  VM_PROT_READ 
| VM_PROT_WRITE);
+  
VM_PROT_READ);
 
 /* Check if the entry contains an APIC. */
 check_signature = acpi_check_signature(descr_header->signature, 
ACPI_APIC_SIG, 4*sizeof(uint8_t));
-- 
2.30.1




  1   2   3   4   5   6   7   >