[PATCH] xlnx-zynqmp-efuse: hw/nvram: Remove deprecated device reset

2023-10-03 Thread Tong Ho
This change implements the ResettableClass interface for the device.

Signed-off-by: Tong Ho 
---
 hw/nvram/xlnx-zynqmp-efuse.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/hw/nvram/xlnx-zynqmp-efuse.c b/hw/nvram/xlnx-zynqmp-efuse.c
index 228ba0bbfa..3db5f98ec1 100644
--- a/hw/nvram/xlnx-zynqmp-efuse.c
+++ b/hw/nvram/xlnx-zynqmp-efuse.c
@@ -2,6 +2,7 @@
  * QEMU model of the ZynqMP eFuse
  *
  * Copyright (c) 2015 Xilinx Inc.
+ * Copyright (c) 2023 Advanced Micro Devices, Inc.
  *
  * Written by Edgar E. Iglesias 
  *
@@ -769,9 +770,9 @@ static void zynqmp_efuse_register_reset(RegisterInfo *reg)
 register_reset(reg);
 }
 
-static void zynqmp_efuse_reset(DeviceState *dev)
+static void zynqmp_efuse_reset_hold(Object *obj)
 {
-XlnxZynqMPEFuse *s = XLNX_ZYNQMP_EFUSE(dev);
+XlnxZynqMPEFuse *s = XLNX_ZYNQMP_EFUSE(obj);
 unsigned int i;
 
 for (i = 0; i < ARRAY_SIZE(s->regs_info); ++i) {
@@ -837,8 +838,9 @@ static Property zynqmp_efuse_props[] = {
 static void zynqmp_efuse_class_init(ObjectClass *klass, void *data)
 {
 DeviceClass *dc = DEVICE_CLASS(klass);
+ResettableClass *rc = RESETTABLE_CLASS(klass);
 
-dc->reset = zynqmp_efuse_reset;
+rc->phases.hold = zynqmp_efuse_reset_hold;
 dc->realize = zynqmp_efuse_realize;
 dc->vmsd = _efuse;
 device_class_set_props(dc, zynqmp_efuse_props);
-- 
2.25.1




[PATCH] xlnx-versal-efuse: hw/nvram: Remove deprecated device reset

2023-10-03 Thread Tong Ho
This change implements the ResettableClass interface for the device.

Signed-off-by: Tong Ho 
---
 hw/nvram/xlnx-versal-efuse-ctrl.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/hw/nvram/xlnx-versal-efuse-ctrl.c 
b/hw/nvram/xlnx-versal-efuse-ctrl.c
index b35ba65ab5..beb5661c35 100644
--- a/hw/nvram/xlnx-versal-efuse-ctrl.c
+++ b/hw/nvram/xlnx-versal-efuse-ctrl.c
@@ -2,6 +2,7 @@
  * QEMU model of the Versal eFuse controller
  *
  * Copyright (c) 2020 Xilinx Inc.
+ * Copyright (c) 2023 Advanced Micro Devices, Inc.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to 
deal
@@ -657,9 +658,9 @@ static void efuse_ctrl_register_reset(RegisterInfo *reg)
 register_reset(reg);
 }
 
-static void efuse_ctrl_reset(DeviceState *dev)
+static void efuse_ctrl_reset_hold(Object *obj)
 {
-XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(dev);
+XlnxVersalEFuseCtrl *s = XLNX_VERSAL_EFUSE_CTRL(obj);
 unsigned int i;
 
 for (i = 0; i < ARRAY_SIZE(s->regs_info); ++i) {
@@ -749,8 +750,9 @@ static Property efuse_ctrl_props[] = {
 static void efuse_ctrl_class_init(ObjectClass *klass, void *data)
 {
 DeviceClass *dc = DEVICE_CLASS(klass);
+ResettableClass *rc = RESETTABLE_CLASS(klass);
 
-dc->reset = efuse_ctrl_reset;
+rc->phases.hold = efuse_ctrl_reset_hold;
 dc->realize = efuse_ctrl_realize;
 dc->vmsd = _efuse_ctrl;
 device_class_set_props(dc, efuse_ctrl_props);
-- 
2.25.1




Re: [PATCH v3 03/15] vfio/common: Move IOMMU agnostic helpers to a separate file

2023-10-03 Thread Cédric Le Goater

Eric,

On 10/3/23 12:14, Eric Auger wrote:

From: Yi Liu 

Move low-level iommu agnostic helpers to a separate helpers.c
file. They relate to regions, interrupts, device/region
capabilities and etc.

Signed-off-by: Eric Auger 
Signed-off-by: Yi Sun 
Signed-off-by: Yi Liu 
Signed-off-by: Zhenzhong Duan 
Reviewed-by: Cédric Le Goater 
---
  include/hw/vfio/vfio-common.h |   9 +
  hw/vfio/common.c  | 588 
  hw/vfio/helpers.c | 611 ++
  hw/vfio/meson.build   |   1 +
  4 files changed, 621 insertions(+), 588 deletions(-)
  create mode 100644 hw/vfio/helpers.c

diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index e9b8954595..e0483893d1 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -196,6 +196,12 @@ typedef struct VFIODisplay {
  } dmabuf;
  } VFIODisplay;
  
+typedef struct {

+unsigned long *bitmap;
+hwaddr size;
+hwaddr pages;
+} VFIOBitmap;
+
  void vfio_put_base_device(VFIODevice *vbasedev);
  void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
  void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
@@ -245,6 +251,8 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info 
*info,
   unsigned int *avail);
  struct vfio_info_cap_header *
  vfio_get_device_info_cap(struct vfio_device_info *info, uint16_t id);
+struct vfio_info_cap_header *
+vfio_get_cap(void *ptr, uint32_t cap_offset, uint16_t id);
  #endif
  extern const MemoryListener vfio_prereg_listener;
  
@@ -257,4 +265,5 @@ int vfio_spapr_remove_window(VFIOContainer *container,

  bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
  void vfio_migration_exit(VFIODevice *vbasedev);
  
+int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size);

  #endif /* HW_VFIO_VFIO_COMMON_H */
diff --git a/hw/vfio/common.c b/hw/vfio/common.c
index 134649226d..4e122fc4e4 100644
--- a/hw/vfio/common.c
+++ b/hw/vfio/common.c
@@ -62,84 +62,6 @@ static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =
  static int vfio_kvm_device_fd = -1;
  #endif
  
-/*

- * Common VFIO interrupt disable
- */
-void vfio_disable_irqindex(VFIODevice *vbasedev, int index)
-{
-struct vfio_irq_set irq_set = {
-.argsz = sizeof(irq_set),
-.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER,
-.index = index,
-.start = 0,
-.count = 0,
-};
-
-ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, _set);
-}
-
-void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index)
-{
-struct vfio_irq_set irq_set = {
-.argsz = sizeof(irq_set),
-.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
-.index = index,
-.start = 0,
-.count = 1,
-};
-
-ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, _set);
-}
-
-void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
-{
-struct vfio_irq_set irq_set = {
-.argsz = sizeof(irq_set),
-.flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK,
-.index = index,
-.start = 0,
-.count = 1,
-};
-
-ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, _set);
-}
-
-static inline const char *action_to_str(int action)
-{
-switch (action) {
-case VFIO_IRQ_SET_ACTION_MASK:
-return "MASK";
-case VFIO_IRQ_SET_ACTION_UNMASK:
-return "UNMASK";
-case VFIO_IRQ_SET_ACTION_TRIGGER:
-return "TRIGGER";
-default:
-return "UNKNOWN ACTION";
-}
-}
-
-static const char *index_to_str(VFIODevice *vbasedev, int index)
-{
-if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
-return NULL;
-}
-
-switch (index) {
-case VFIO_PCI_INTX_IRQ_INDEX:
-return "INTX";
-case VFIO_PCI_MSI_IRQ_INDEX:
-return "MSI";
-case VFIO_PCI_MSIX_IRQ_INDEX:
-return "MSIX";
-case VFIO_PCI_ERR_IRQ_INDEX:
-return "ERR";
-case VFIO_PCI_REQ_IRQ_INDEX:
-return "REQ";
-default:
-return NULL;
-}
-}
-
  static int vfio_ram_block_discard_disable(VFIOContainer *container, bool 
state)
  {
  switch (container->iommu_type) {
@@ -163,183 +85,10 @@ static int vfio_ram_block_discard_disable(VFIOContainer 
*container, bool state)
  }
  }
  
-int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,

-   int action, int fd, Error **errp)
-{
-struct vfio_irq_set *irq_set;
-int argsz, ret = 0;
-const char *name;
-int32_t *pfd;
-
-argsz = sizeof(*irq_set) + sizeof(*pfd);
-
-irq_set = g_malloc0(argsz);
-irq_set->argsz = argsz;
-irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | action;
-irq_set->index = index;
-irq_set->start = subindex;
-irq_set->count = 1;
-pfd = (int32_t *)_set->data;
-*pfd = fd;
-
-if (ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
-ret = -errno;
-}
-g_free(irq_set);
-
-

Re: [PATCH v10 00/10] migration: Modify 'migrate' and 'migrate-incoming' QAPI commands for migration

2023-10-03 Thread Het Gala



On 05/09/23 6:52 pm, Fabiano Rosas wrote:

Het Gala  writes:


Hi qemu-devel community,

A gentle reminder and request for all migration maintainers - Peter,
Juan, Dr. Gilbert and others too for review of the patchset series.
Received reviewed-by from Daniel on migration implementation patches but
need final approval from migration maintainers before getting it merged.
Also got acked-by tag from Markus on the QAPI patches. This is Part1 of
the 4 patchset series. Ultimate goal of the whole 4 series is to
'introduce multiple interface support on top of existing multifd
capability'. Hope to get approval or comments from migration maintainers
on the patches soon.


Hi,

Is this the latest version of this series? I see errors with make
check. Let me know if I should wait for your next version to comment.


This series was last posted in July end and make worked fine then. There 
might be changes with respect to migration workflow by now I suspect, 
that might be the reason for make check errors. Let me send a fresh new 
patchset version and will resolve make issues there. Thanks !


Regards,
Het Gala



Re: [PATCH RESEND 09/15] ppc: spapr: Implement nested PAPR hcall - H_GUEST_CREATE_VCPU

2023-10-03 Thread Harsh Prateek Bora




On 9/7/23 08:19, Nicholas Piggin wrote:

On Wed Sep 6, 2023 at 2:33 PM AEST, Harsh Prateek Bora wrote:

This patch implements support for hcall H_GUEST_CREATE_VCPU which is
used to instantiate a new VCPU for a previously created nested guest.
The L1 provide the guest-id (returned by L0 during call to
H_GUEST_CREATE) and an associated unique vcpu-id to refer to this
instance in future calls. It is assumed that vcpu-ids are being
allocated in a sequential manner and max vcpu limit is 2048.

Signed-off-by: Michael Neuling 
Signed-off-by: Shivaprasad G Bhat 
Signed-off-by: Harsh Prateek Bora 
---
  hw/ppc/spapr_nested.c | 110 ++
  include/hw/ppc/spapr.h|   1 +
  include/hw/ppc/spapr_nested.h |   1 +
  3 files changed, 112 insertions(+)

diff --git a/hw/ppc/spapr_nested.c b/hw/ppc/spapr_nested.c
index 09bbbfb341..e7956685af 100644
--- a/hw/ppc/spapr_nested.c
+++ b/hw/ppc/spapr_nested.c
@@ -376,6 +376,47 @@ void spapr_exit_nested(PowerPCCPU *cpu, int excp)
  address_space_unmap(CPU(cpu)->as, regs, len, len, true);
  }
  
+static

+SpaprMachineStateNestedGuest *spapr_get_nested_guest(SpaprMachineState *spapr,
+ target_ulong lpid)
+{
+SpaprMachineStateNestedGuest *guest;
+
+guest = g_hash_table_lookup(spapr->nested.guests, GINT_TO_POINTER(lpid));
+return guest;
+}


Are you namespacing the new API stuff with papr or no? Might be good to
reduce confusion.


I guess you were referring to vcpu_check below.
Renaming vcpu_check to spapr_nested_vcpu_check().


+
+static bool vcpu_check(SpaprMachineStateNestedGuest *guest,
+   target_ulong vcpuid,
+   bool inoutbuf)


What's it checking? That the id is valid? Allocated? Enabled?



This is being introduced to do sanity checks for the provided vcpuid of 
a guest. It should check if the vcpuid is valid, allocated and enabled 
before using further.



+{
+struct SpaprMachineStateNestedGuestVcpu *vcpu;
+
+if (vcpuid >= NESTED_GUEST_VCPU_MAX) {
+return false;
+}
+
+if (!(vcpuid < guest->vcpus)) {
+return false;
+}
+
+vcpu = >vcpu[vcpuid];
+if (!vcpu->enabled) {
+return false;
+}
+
+if (!inoutbuf) {
+return true;
+}
+
+/* Check to see if the in/out buffers are registered */
+if (vcpu->runbufin.addr && vcpu->runbufout.addr) {
+return true;
+}
+


I think I shall move in/out buf related checks to vcpu_run patch.


+return false;
+}
+
  static target_ulong h_guest_get_capabilities(PowerPCCPU *cpu,
   SpaprMachineState *spapr,
   target_ulong opcode,
@@ -448,6 +489,11 @@ static void
  destroy_guest_helper(gpointer value)
  {
  struct SpaprMachineStateNestedGuest *guest = value;
+int i = 0;


Don't need to set i = 0 twice. A newline would be good though.



Yeh, declaring with for loop and removing above init.


+for (i = 0; i < guest->vcpus; i++) {
+cpu_ppc_tb_free(>vcpu[i].env);
+}
+g_free(guest->vcpu);
  g_free(guest);
  }
  
@@ -518,6 +564,69 @@ static target_ulong h_guest_create(PowerPCCPU *cpu,

  return H_SUCCESS;
  }
  
+static target_ulong h_guest_create_vcpu(PowerPCCPU *cpu,

+SpaprMachineState *spapr,
+target_ulong opcode,
+target_ulong *args)
+{
+CPUPPCState *env = >env, *l2env;
+target_ulong flags = args[0];
+target_ulong lpid = args[1];
+target_ulong vcpuid = args[2];
+SpaprMachineStateNestedGuest *guest;
+
+if (flags) { /* don't handle any flags for now */
+return H_UNSUPPORTED_FLAG;
+}
+
+guest = spapr_get_nested_guest(spapr, lpid);
+if (!guest) {
+return H_P2;
+}
+
+if (vcpuid < guest->vcpus) {
+return H_IN_USE;
+}
+
+if (guest->vcpus >= NESTED_GUEST_VCPU_MAX) {
+return H_P3;
+}
+
+if (guest->vcpus) {
+struct SpaprMachineStateNestedGuestVcpu *vcpus;


Ditto for using typedefs. Do a sweep for this.


Sure, done.


+vcpus = g_try_renew(struct SpaprMachineStateNestedGuestVcpu,
+guest->vcpu,
+guest->vcpus + 1);


g_try_renew doesn't work with NULL mem? That's unfortunate.



Hmm, behaviour with NULL is undefined, so keeping as is.


+if (!vcpus) {
+return H_NO_MEM;
+}
+memset([guest->vcpus], 0,
+   sizeof(struct SpaprMachineStateNestedGuestVcpu));
+guest->vcpu = vcpus;
+l2env = [guest->vcpus].env;
+} else {
+guest->vcpu = g_try_new0(struct SpaprMachineStateNestedGuestVcpu, 1);
+if (guest->vcpu == NULL) {
+return H_NO_MEM;
+}
+l2env = >vcpu->env;
+}


These two legs seem to be doing the same thing in 

Re: [PATCH v5 2/2] migration: Update error description outside migration.c

2023-10-03 Thread Tejus GK

On 03/10/23 6:14 pm, Juan Quintela wrote:

Tejus GK  wrote:

A few code paths exist in the source code,where a migration is
marked as failed via MIGRATION_STATUS_FAILED, but the failure happens
outside of migration.c

In such cases, an error_report() call is made, however the current
MigrationState is never updated with the error description, and hence
clients like libvirt never know the actual reason for the failure.

This patch covers such cases outside of migration.c and updates the
error description at the appropriate places.

Acked-by: Peter Xu 
Signed-off-by: Tejus GK 


Reviewed-by: Juan Quintela 

Queued.

Thanks, will be sending out a patch with the "Reviewed by" trailer added.


But I wonder.


index 1f65294bf4..60eec7c31f 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -979,6 +979,8 @@ static void save_section_footer(QEMUFile *f, SaveStateEntry 
*se)
  static int vmstate_save(QEMUFile *f, SaveStateEntry *se, JSONWriter *vmdesc)
  {
  int ret;
+Error *local_err = NULL;
+MigrationState *s = migrate_get_current();
  
  if ((!se->ops || !se->ops->save_state) && !se->vmsd) {

  return 0;
@@ -1002,6 +1004,8 @@ static int vmstate_save(QEMUFile *f, SaveStateEntry *se, 
JSONWriter *vmdesc)
  } else {
  ret = vmstate_save_state_with_err(f, se->vmsd, se->opaque, vmdesc, 
_err);
  if (ret) {
+migrate_set_error(s, local_err);
+error_report_err(local_err);


We are setting the error and reporting it.


  return ret;
  }
  }
@@ -1068,10 +1072,14 @@ void qemu_savevm_send_open_return_path(QEMUFile *f)
  int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
  {
  uint32_t tmp;
+MigrationState *ms = migrate_get_current();
+Error *local_err = NULL;
  
  if (len > MAX_VM_CMD_PACKAGED_SIZE) {

-error_report("%s: Unreasonably large packaged state: %zu",
+error_setg(_err, "%s: Unreasonably large packaged state: %zu",
   __func__, len);
+migrate_set_error(ms, local_err);
+error_report_err(local_err);


Again we set the error and we report it.


  return -1;
  }
  
@@ -1499,8 +1507,11 @@ int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,

   * bdrv_activate_all() on the other end won't fail. */
  ret = bdrv_inactivate_all();
  if (ret) {
-error_report("%s: bdrv_inactivate_all() failed (%d)",
- __func__, ret);
+Error *local_err = NULL;
+error_setg(_err, "%s: bdrv_inactivate_all() failed (%d)",
+   __func__, ret);
+migrate_set_error(ms, local_err);
+error_report_err(local_err);


Again.


  qemu_file_set_error(f, ret);


And we still have qemu_file_set_error() here, ouch.


  return ret;
  }
diff --git a/migration/vmstate.c b/migration/vmstate.c
index dd9c76dbeb..4cde30bf2d 100644
--- a/migration/vmstate.c
+++ b/migration/vmstate.c
@@ -14,6 +14,7 @@
  #include "migration.h"
  #include "migration/vmstate.h"
  #include "savevm.h"
+#include "qapi/error.h"
  #include "qapi/qmp/json-writer.h"
  #include "qemu-file.h"
  #include "qemu/bitops.h"
@@ -336,7 +337,7 @@ int vmstate_save_state_v(QEMUFile *f, const 
VMStateDescription *vmsd,
  ret = vmsd->pre_save(opaque);
  trace_vmstate_save_state_pre_save_res(vmsd->name, ret);
  if (ret) {
-error_report("pre-save failed: %s", vmsd->name);
+error_setg(errp, "pre-save failed: %s", vmsd->name);


Here we only set the error


  return ret;
  }
  }
@@ -389,8 +390,8 @@ int vmstate_save_state_v(QEMUFile *f, const 
VMStateDescription *vmsd,
   vmdesc_loop);
  }
  if (ret) {
-error_report("Save of field %s/%s failed",
- vmsd->name, field->name);
+error_setg(errp, "Save of field %s/%s failed",
+vmsd->name, field->name);


Same here.
You're right, I'm only setting it here and reporting it eventually in 
savevm.c. The trivial solution for this would have been directly doing a 
migrate_set_error() here, but that ended up breaking the build for the 
unit test test-vmstate.c



  if (vmsd->post_save) {
  vmsd->post_save(opaque);
  }



So, I am wondering if it could be better to just report the error in a
single place for migration, and set it whenever we need it?
Yes, that would be very convenient, for all the errors to be reported in 
lets say migration.c. Though that'd also require all the subsystems 
under migration.c to properly propagate the errors.


That is independent of this patch, though.

Later, Juan.



regards,
tejus




Re: [PATCH v2 0/3] vhost: clean up device reset

2023-10-03 Thread Raphael Norwitz



> On Oct 3, 2023, at 9:45 PM, Stefan Hajnoczi  wrote:
> 
> v2:
> - Fix compilation error when vhost is not built [Michael]
> 
> Stateful vhost devices may need to free resources or clear device state upon
> device reset. The vhost-user protocol has a VHOST_USER_RESET_DEVICE message 
> for
> this and vDPA has SET_STATUS 0, but only QEMU's vhost-user-scsi device 
> actually
> implements this today.
> 
> This patch series performs device reset across all device types. When
> virtio_reset() is called, the associated vhost_dev's ->vhost_reset_device() is
> called. vhost-user-scsi's one-off implementation is obsoleted and removed.
> 
> This patch affects behavior as follows:
> - vhost-kernel: no change in behavior. No ioctl calls are made.
> - vhost-user: back-ends that negotiate
>  VHOST_USER_PROTOCOL_F_RESET_DEVICE now receive a
>  VHOST_USER_DEVICE_RESET message upon device reset. Otherwise there is
>  no change in behavior. DPDK, SPDK, libvhost-user, and the
>  vhost-user-backend crate do not negotiate
>  VHOST_USER_PROTOCOL_F_RESET_DEVICE automatically.
> - vhost-vdpa: an extra SET_STATUS 0 call is made during device reset.
> 
> I have tested this series with vhost-net (kernel), vhost-user-blk, and
> vhost-user-fs (both Rust and legacy C).
> 
> Stefan Hajnoczi (3):
>  vhost-user: do not send RESET_OWNER on device reset
>  vhost-backend: remove vhost_kernel_reset_device()
>  virtio: call ->vhost_reset_device() during reset

For the series:

Reviewed-by: Raphael Norwitz 

> 
> meson.build   |  1 +
> include/hw/virtio/vhost.h | 10 ++
> hw/scsi/vhost-user-scsi.c | 20 
> hw/virtio/vhost-backend.c |  6 --
> hw/virtio/vhost-user.c| 13 +
> hw/virtio/vhost.c |  9 +
> hw/virtio/virtio.c|  4 
> 7 files changed, 33 insertions(+), 30 deletions(-)
> 
> -- 
> 2.41.0
> 




[PATCH v2 1/3] vhost-user: do not send RESET_OWNER on device reset

2023-10-03 Thread Stefan Hajnoczi
The VHOST_USER_RESET_OWNER message is deprecated in the spec:

   This is no longer used. Used to be sent to request disabling all
   rings, but some back-ends interpreted it to also discard connection
   state (this interpretation would lead to bugs).  It is recommended
   that back-ends either ignore this message, or use it to disable all
   rings.

The only caller of vhost_user_reset_device() is vhost_user_scsi_reset().
It checks that F_RESET_DEVICE was negotiated before calling it:

  static void vhost_user_scsi_reset(VirtIODevice *vdev)
  {
  VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
  struct vhost_dev *dev = >dev;

  /*
   * Historically, reset was not implemented so only reset devices
   * that are expecting it.
   */
  if (!virtio_has_feature(dev->protocol_features,
  VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
  return;
  }

  if (dev->vhost_ops->vhost_reset_device) {
  dev->vhost_ops->vhost_reset_device(dev);
  }
  }

Therefore VHOST_USER_RESET_OWNER is actually never sent by
vhost_user_reset_device(). Remove the dead code. This effectively moves
the vhost-user protocol specific code from vhost-user-scsi.c into
vhost-user.c where it belongs.

Signed-off-by: Stefan Hajnoczi 
---
 hw/scsi/vhost-user-scsi.c |  9 -
 hw/virtio/vhost-user.c| 13 +
 2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/hw/scsi/vhost-user-scsi.c b/hw/scsi/vhost-user-scsi.c
index ee99b19e7a..8582b2e8ab 100644
--- a/hw/scsi/vhost-user-scsi.c
+++ b/hw/scsi/vhost-user-scsi.c
@@ -71,15 +71,6 @@ static void vhost_user_scsi_reset(VirtIODevice *vdev)
 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
 struct vhost_dev *dev = >dev;
 
-/*
- * Historically, reset was not implemented so only reset devices
- * that are expecting it.
- */
-if (!virtio_has_feature(dev->protocol_features,
-VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
-return;
-}
-
 if (dev->vhost_ops->vhost_reset_device) {
 dev->vhost_ops->vhost_reset_device(dev);
 }
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
index 8dcf049d42..7bed9ad7d5 100644
--- a/hw/virtio/vhost-user.c
+++ b/hw/virtio/vhost-user.c
@@ -1492,12 +1492,17 @@ static int vhost_user_reset_device(struct vhost_dev 
*dev)
 {
 VhostUserMsg msg = {
 .hdr.flags = VHOST_USER_VERSION,
+.hdr.request = VHOST_USER_RESET_DEVICE,
 };
 
-msg.hdr.request = virtio_has_feature(dev->protocol_features,
- VHOST_USER_PROTOCOL_F_RESET_DEVICE)
-? VHOST_USER_RESET_DEVICE
-: VHOST_USER_RESET_OWNER;
+/*
+ * Historically, reset was not implemented so only reset devices
+ * that are expecting it.
+ */
+if (!virtio_has_feature(dev->protocol_features,
+VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
+return -ENOSYS;
+}
 
 return vhost_user_write(dev, , NULL, 0);
 }
-- 
2.41.0




[PATCH v2 3/3] virtio: call ->vhost_reset_device() during reset

2023-10-03 Thread Stefan Hajnoczi
vhost-user-scsi has a VirtioDeviceClass->reset() function that calls
->vhost_reset_device(). The other vhost devices don't notify the vhost
device upon reset.

Stateful vhost devices may need to handle device reset in order to free
resources or prevent stale device state from interfering after reset.

Call ->vhost_device_reset() from virtio_reset() so that that vhost
devices are notified of device reset.

This patch affects behavior as follows:
- vhost-kernel: No change in behavior since ->vhost_reset_device() is
  not implemented.
- vhost-user: back-ends that negotiate
  VHOST_USER_PROTOCOL_F_RESET_DEVICE now receive a
  VHOST_USER_DEVICE_RESET message upon device reset. Otherwise there is
  no change in behavior. DPDK, SPDK, libvhost-user, and the
  vhost-user-backend crate do not negotiate
  VHOST_USER_PROTOCOL_F_RESET_DEVICE automatically.
- vhost-vdpa: an extra SET_STATUS 0 call is made during device reset.

Signed-off-by: Stefan Hajnoczi 
---
 meson.build   |  1 +
 include/hw/virtio/vhost.h | 10 ++
 hw/scsi/vhost-user-scsi.c | 11 ---
 hw/virtio/vhost.c |  9 +
 hw/virtio/virtio.c|  4 
 5 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/meson.build b/meson.build
index 21a1bc03f8..dd5e61915a 100644
--- a/meson.build
+++ b/meson.build
@@ -2143,6 +2143,7 @@ config_host_data.set('CONFIG_TPM', have_tpm)
 config_host_data.set('CONFIG_TSAN', get_option('tsan'))
 config_host_data.set('CONFIG_USB_LIBUSB', libusb.found())
 config_host_data.set('CONFIG_VDE', vde.found())
+config_host_data.set('CONFIG_VHOST', have_vhost)
 config_host_data.set('CONFIG_VHOST_NET', have_vhost_net)
 config_host_data.set('CONFIG_VHOST_NET_USER', have_vhost_net_user)
 config_host_data.set('CONFIG_VHOST_NET_VDPA', have_vhost_net_vdpa)
diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h
index 6a173cb9fa..14621f9e79 100644
--- a/include/hw/virtio/vhost.h
+++ b/include/hw/virtio/vhost.h
@@ -338,4 +338,14 @@ int vhost_dev_set_inflight(struct vhost_dev *dev,
 int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size,
struct vhost_inflight *inflight);
 bool vhost_dev_has_iommu(struct vhost_dev *dev);
+
+#ifdef CONFIG_VHOST
+int vhost_reset_device(struct vhost_dev *hdev);
+#else
+static inline int vhost_reset_device(struct vhost_dev *hdev)
+{
+return -ENOSYS;
+}
+#endif /* CONFIG_VHOST */
+
 #endif
diff --git a/hw/scsi/vhost-user-scsi.c b/hw/scsi/vhost-user-scsi.c
index 8582b2e8ab..6917a748bb 100644
--- a/hw/scsi/vhost-user-scsi.c
+++ b/hw/scsi/vhost-user-scsi.c
@@ -66,16 +66,6 @@ static void vhost_user_scsi_set_status(VirtIODevice *vdev, 
uint8_t status)
 }
 }
 
-static void vhost_user_scsi_reset(VirtIODevice *vdev)
-{
-VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
-struct vhost_dev *dev = >dev;
-
-if (dev->vhost_ops->vhost_reset_device) {
-dev->vhost_ops->vhost_reset_device(dev);
-}
-}
-
 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 {
 }
@@ -195,7 +185,6 @@ static void vhost_user_scsi_class_init(ObjectClass *klass, 
void *data)
 vdc->get_features = vhost_scsi_common_get_features;
 vdc->set_config = vhost_scsi_common_set_config;
 vdc->set_status = vhost_user_scsi_set_status;
-vdc->reset = vhost_user_scsi_reset;
 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
 }
 
diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index e2f6ffb446..6003e50e83 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -2087,3 +2087,12 @@ int vhost_net_set_backend(struct vhost_dev *hdev,
 
 return -ENOSYS;
 }
+
+int vhost_reset_device(struct vhost_dev *hdev)
+{
+if (hdev->vhost_ops->vhost_reset_device) {
+return hdev->vhost_ops->vhost_reset_device(hdev);
+}
+
+return -ENOSYS;
+}
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 4577f3f5b3..d863ffd5d6 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -2121,6 +2121,10 @@ void virtio_reset(void *opaque)
 vdev->device_endian = virtio_default_endian();
 }
 
+if (vdev->vhost_started) {
+vhost_reset_device(k->get_vhost(vdev));
+}
+
 if (k->reset) {
 k->reset(vdev);
 }
-- 
2.41.0




[PATCH v2 2/3] vhost-backend: remove vhost_kernel_reset_device()

2023-10-03 Thread Stefan Hajnoczi
vhost_kernel_reset_device() invokes RESET_OWNER, which disassociates the
owner process from the device. The device is left non-operational since
SET_OWNER is only called once during startup in vhost_dev_init().

vhost_kernel_reset_device() is never called so this latent bug never
appears. Get rid of vhost_kernel_reset_device() for now. If someone
needs it in the future they'll need to implement it correctly.

Signed-off-by: Stefan Hajnoczi 
---
 hw/virtio/vhost-backend.c | 6 --
 1 file changed, 6 deletions(-)

diff --git a/hw/virtio/vhost-backend.c b/hw/virtio/vhost-backend.c
index 8e581575c9..17f3fc6a08 100644
--- a/hw/virtio/vhost-backend.c
+++ b/hw/virtio/vhost-backend.c
@@ -197,11 +197,6 @@ static int vhost_kernel_set_owner(struct vhost_dev *dev)
 return vhost_kernel_call(dev, VHOST_SET_OWNER, NULL);
 }
 
-static int vhost_kernel_reset_device(struct vhost_dev *dev)
-{
-return vhost_kernel_call(dev, VHOST_RESET_OWNER, NULL);
-}
-
 static int vhost_kernel_get_vq_index(struct vhost_dev *dev, int idx)
 {
 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
@@ -322,7 +317,6 @@ const VhostOps kernel_ops = {
 .vhost_get_features = vhost_kernel_get_features,
 .vhost_set_backend_cap = vhost_kernel_set_backend_cap,
 .vhost_set_owner = vhost_kernel_set_owner,
-.vhost_reset_device = vhost_kernel_reset_device,
 .vhost_get_vq_index = vhost_kernel_get_vq_index,
 .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
 .vhost_vsock_set_running = vhost_kernel_vsock_set_running,
-- 
2.41.0




[PATCH v2 0/3] vhost: clean up device reset

2023-10-03 Thread Stefan Hajnoczi
v2:
- Fix compilation error when vhost is not built [Michael]

Stateful vhost devices may need to free resources or clear device state upon
device reset. The vhost-user protocol has a VHOST_USER_RESET_DEVICE message for
this and vDPA has SET_STATUS 0, but only QEMU's vhost-user-scsi device actually
implements this today.

This patch series performs device reset across all device types. When
virtio_reset() is called, the associated vhost_dev's ->vhost_reset_device() is
called. vhost-user-scsi's one-off implementation is obsoleted and removed.

This patch affects behavior as follows:
- vhost-kernel: no change in behavior. No ioctl calls are made.
- vhost-user: back-ends that negotiate
  VHOST_USER_PROTOCOL_F_RESET_DEVICE now receive a
  VHOST_USER_DEVICE_RESET message upon device reset. Otherwise there is
  no change in behavior. DPDK, SPDK, libvhost-user, and the
  vhost-user-backend crate do not negotiate
  VHOST_USER_PROTOCOL_F_RESET_DEVICE automatically.
- vhost-vdpa: an extra SET_STATUS 0 call is made during device reset.

I have tested this series with vhost-net (kernel), vhost-user-blk, and
vhost-user-fs (both Rust and legacy C).

Stefan Hajnoczi (3):
  vhost-user: do not send RESET_OWNER on device reset
  vhost-backend: remove vhost_kernel_reset_device()
  virtio: call ->vhost_reset_device() during reset

 meson.build   |  1 +
 include/hw/virtio/vhost.h | 10 ++
 hw/scsi/vhost-user-scsi.c | 20 
 hw/virtio/vhost-backend.c |  6 --
 hw/virtio/vhost-user.c| 13 +
 hw/virtio/vhost.c |  9 +
 hw/virtio/virtio.c|  4 
 7 files changed, 33 insertions(+), 30 deletions(-)

-- 
2.41.0




Ping: Re: [PULL 4/5] hw/ufs: Support for UFS logical unit

2023-10-03 Thread Jeuk Kim

Dear Paolo

This is a ping for the following.
If you don't mind, could you give me some feedback?
Thank you very much.

Jeuk


On 23. 9. 21. 17:38, Jeuk Kim wrote:

Dear Paolo

Hi. I've been looking into how ufs-lu can share code with scsi-hd.

I have verified that ufs-lu can use scsi-hd's code, and I would like 
to modify it to do so.


I've validated two possible fixes.
I'd like to hear your thoughts.

Option1.
As you mentioned, using ufsbus, which inherits from scsibus, removes 
the ufs-lu device type and use scsi-hd. (like -device ufs,id=ufs0 
-device scsi-hd,bus=ufs0)

I've verified that this method is implementable, except for one problem.
Because we are using the scsi-hd type instead of the ufs-lu type, the 
ufs has to manage all the ufs-lu specific data (such as the unit 
descriptor).
However, since there is no ufs_lu_realize() function, we need a way to 
notify the ufs when a new scsi-hd device is added.
Would there be a way to let the ufs know that a new scsi-hd has been 
added at scsi_hd_realize() time?


Option 2.
Use qdev_new() & qdev_realize() to make ufs-lu have a virtual scsi bus 
and scsi-hd.

The ufs-lu can pass through SCSI commands to the virtual scsi-hd.
This is similar to the method used by the device "usb-storage".

With this method, I can keep the ufs-lu device type (ufs_lu_realize() 
makes it convenient to manage ufs-lu related data) and avoid 
duplicating code with scsi-hd.
So I prefer this approach, but the annotation for "usb-storage" is 
marked with a "Hack alert", so I'm not sure if this is the right way.
The code can be found in usb_msd_storage_realize() 
(hw/usb/dev-storage-classic.c:51).


I am wondering if you could give me some advice on this and your 
preferred direction for fixing it.


Thank you so much.

Jeuk





[PULL 1/2] target/hexagon: move GETPC() calls to top level helpers

2023-10-03 Thread Brian Cain
From: Matheus Tavares Bernardino 

As docs/devel/loads-stores.rst states:

  ``GETPC()`` should be used with great care: calling
  it in other functions that are *not* the top level
  ``HELPER(foo)`` will cause unexpected behavior. Instead, the
  value of ``GETPC()`` should be read from the helper and passed
  if needed to the functions that the helper calls.

Let's fix the GETPC() usage in Hexagon, making sure it's always called
from top level helpers and passed down to the places where it's
needed. There are a few snippets where that is not currently the case:

- probe_store(), which is only called from two helpers, so it's easy to
  move GETPC() up.

- mem_load*() functions, which are also called directly from helpers,
  but through the MEM_LOAD*() set of macros. Note that this are only
  used when compiling with --disable-hexagon-idef-parser.

  In this case, we also take this opportunity to simplify the code,
  unifying the mem_load*() functions.

- HELPER(probe_hvx_stores), when called from another helper, ends up
  using its own GETPC() expansion instead of the top level caller.

Signed-off-by: Matheus Tavares Bernardino 
Reviewed-by: Taylor Simpson 
Message-Id: 
<2c74c3696946edba7cc5b2942cf296a5af532052.1689070412.git.quic_mathb...@quicinc.com>-ne
Reviewed-by: Brian Cain 
Signed-off-by: Brian Cain 
---
 target/hexagon/macros.h| 19 +-
 target/hexagon/op_helper.c | 75 +++---
 target/hexagon/op_helper.h |  9 -
 3 files changed, 38 insertions(+), 65 deletions(-)

diff --git a/target/hexagon/macros.h b/target/hexagon/macros.h
index 5451b061ee..dafa0df6ed 100644
--- a/target/hexagon/macros.h
+++ b/target/hexagon/macros.h
@@ -173,15 +173,6 @@
 #define MEM_STORE8(VA, DATA, SLOT) \
 MEM_STORE8_FUNC(DATA)(cpu_env, VA, DATA, SLOT)
 #else
-#define MEM_LOAD1s(VA) ((int8_t)mem_load1(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD1u(VA) ((uint8_t)mem_load1(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD2s(VA) ((int16_t)mem_load2(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD2u(VA) ((uint16_t)mem_load2(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD4s(VA) ((int32_t)mem_load4(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD4u(VA) ((uint32_t)mem_load4(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD8s(VA) ((int64_t)mem_load8(env, pkt_has_store_s1, slot, VA))
-#define MEM_LOAD8u(VA) ((uint64_t)mem_load8(env, pkt_has_store_s1, slot, VA))
-
 #define MEM_STORE1(VA, DATA, SLOT) log_store32(env, VA, DATA, 1, SLOT)
 #define MEM_STORE2(VA, DATA, SLOT) log_store32(env, VA, DATA, 2, SLOT)
 #define MEM_STORE4(VA, DATA, SLOT) log_store32(env, VA, DATA, 4, SLOT)
@@ -530,8 +521,16 @@ static inline TCGv gen_read_ireg(TCGv result, TCGv val, 
int shift)
 #ifdef QEMU_GENERATE
 #define fLOAD(NUM, SIZE, SIGN, EA, DST) MEM_LOAD##SIZE##SIGN(DST, EA)
 #else
+#define MEM_LOAD1 cpu_ldub_data_ra
+#define MEM_LOAD2 cpu_lduw_data_ra
+#define MEM_LOAD4 cpu_ldl_data_ra
+#define MEM_LOAD8 cpu_ldq_data_ra
+
 #define fLOAD(NUM, SIZE, SIGN, EA, DST) \
-DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE##SIGN(EA)
+do { \
+check_noshuf(env, pkt_has_store_s1, slot, EA, SIZE, GETPC()); \
+DST = (size##SIZE##SIGN##_t)MEM_LOAD##SIZE(env, EA, GETPC()); \
+} while (0)
 #endif
 
 #define fMEMOP(NUM, SIZE, SIGN, EA, FNTYPE, VALUE)
diff --git a/target/hexagon/op_helper.c b/target/hexagon/op_helper.c
index 12967ac21e..8ca3976a65 100644
--- a/target/hexagon/op_helper.c
+++ b/target/hexagon/op_helper.c
@@ -95,9 +95,8 @@ void HELPER(debug_check_store_width)(CPUHexagonState *env, 
int slot, int check)
 }
 }
 
-void HELPER(commit_store)(CPUHexagonState *env, int slot_num)
+static void commit_store(CPUHexagonState *env, int slot_num, uintptr_t ra)
 {
-uintptr_t ra = GETPC();
 uint8_t width = env->mem_log_stores[slot_num].width;
 target_ulong va = env->mem_log_stores[slot_num].va;
 
@@ -119,6 +118,12 @@ void HELPER(commit_store)(CPUHexagonState *env, int 
slot_num)
 }
 }
 
+void HELPER(commit_store)(CPUHexagonState *env, int slot_num)
+{
+uintptr_t ra = GETPC();
+commit_store(env, slot_num, ra);
+}
+
 void HELPER(gather_store)(CPUHexagonState *env, uint32_t addr, int slot)
 {
 mem_gather_store(env, addr, slot);
@@ -467,13 +472,12 @@ int32_t HELPER(cabacdecbin_pred)(int64_t RssV, int64_t 
RttV)
 }
 
 static void probe_store(CPUHexagonState *env, int slot, int mmu_idx,
-bool is_predicated)
+bool is_predicated, uintptr_t retaddr)
 {
 if (!is_predicated || !(env->slot_cancelled & (1 << slot))) {
 size1u_t width = env->mem_log_stores[slot].width;
 target_ulong va = env->mem_log_stores[slot].va;
-uintptr_t ra = GETPC();
-probe_write(env, va, width, mmu_idx, ra);
+probe_write(env, va, width, mmu_idx, retaddr);
 }
 }
 
@@ -494,12 +498,13 @@ void HELPER(probe_pkt_scalar_store_s0)(CPUHexagonState 
*env, int args)
 int mmu_idx = FIELD_EX32(args, 

[PULL 2/2] target/hexagon: fix some occurrences of -Wshadow=local

2023-10-03 Thread Brian Cain
Of the changes in this commit, the changes in `HELPER(commit_hvx_stores)()`
are less obvious.  They are required because of some macro invocations like
SCATTER_OP_WRITE_TO_MEM().

e.g.:

In file included from ../target/hexagon/op_helper.c:31:
../target/hexagon/mmvec/macros.h:205:18: error: declaration of ‘i’ shadows 
a previous local [-Werror=shadow=compatible-local]
  205 | for (int i = 0; i < sizeof(MMVector); i += sizeof(TYPE)) { \
  |  ^
../target/hexagon/op_helper.c:157:17: note: in expansion of macro 
‘SCATTER_OP_WRITE_TO_MEM’
  157 | SCATTER_OP_WRITE_TO_MEM(uint16_t);
  | ^~~
../target/hexagon/op_helper.c:135:9: note: shadowed declaration is here
  135 | int i;
  | ^
In file included from ../target/hexagon/op_helper.c:31:
../target/hexagon/mmvec/macros.h:204:19: error: declaration of ‘ra’ shadows 
a previous local [-Werror=shadow=compatible-local]
  204 | uintptr_t ra = GETPC(); \
  |   ^~
../target/hexagon/op_helper.c:160:17: note: in expansion of macro 
‘SCATTER_OP_WRITE_TO_MEM’
  160 | SCATTER_OP_WRITE_TO_MEM(uint32_t);
  | ^~~
../target/hexagon/op_helper.c:134:15: note: shadowed declaration is here
  134 | uintptr_t ra = GETPC();
  |   ^~

Reviewed-by: Matheus Tavares Bernardino 
Signed-off-by: Brian Cain 
---
 target/hexagon/imported/alu.idef |  6 +++---
 target/hexagon/mmvec/macros.h|  2 +-
 target/hexagon/op_helper.c   |  9 +++--
 target/hexagon/translate.c   | 10 +-
 4 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/target/hexagon/imported/alu.idef b/target/hexagon/imported/alu.idef
index 12d2aac5d4..b855676989 100644
--- a/target/hexagon/imported/alu.idef
+++ b/target/hexagon/imported/alu.idef
@@ -1142,9 +1142,9 @@ 
Q6INSN(A4_cround_rr,"Rd32=cround(Rs32,Rt32)",ATTRIBS(),"Convergent Round", {RdV
 tmp128 = fSHIFTR128(tmp128, SHIFT);\
 DST =  fCAST16S_8S(tmp128);\
 } else {\
-size16s_t rndbit_128 =  fCAST8S_16S((1LL << (SHIFT - 1))); \
-size16s_t src_128 =  fCAST8S_16S(SRC); \
-size16s_t tmp128 = fADD128(src_128, rndbit_128);\
+rndbit_128 =  fCAST8S_16S((1LL << (SHIFT - 1))); \
+src_128 =  fCAST8S_16S(SRC); \
+tmp128 = fADD128(src_128, rndbit_128);\
 tmp128 = fSHIFTR128(tmp128, SHIFT);\
 DST =  fCAST16S_8S(tmp128);\
 }
diff --git a/target/hexagon/mmvec/macros.h b/target/hexagon/mmvec/macros.h
index a655634fd1..1ceb9453ee 100644
--- a/target/hexagon/mmvec/macros.h
+++ b/target/hexagon/mmvec/macros.h
@@ -201,7 +201,7 @@
 } while (0)
 #define SCATTER_OP_WRITE_TO_MEM(TYPE) \
 do { \
-uintptr_t ra = GETPC(); \
+ra = GETPC(); \
 for (int i = 0; i < sizeof(MMVector); i += sizeof(TYPE)) { \
 if (test_bit(i, env->vtcm_log.mask)) { \
 TYPE dst = 0; \
diff --git a/target/hexagon/op_helper.c b/target/hexagon/op_helper.c
index 8ca3976a65..da10ac5847 100644
--- a/target/hexagon/op_helper.c
+++ b/target/hexagon/op_helper.c
@@ -132,10 +132,9 @@ void HELPER(gather_store)(CPUHexagonState *env, uint32_t 
addr, int slot)
 void HELPER(commit_hvx_stores)(CPUHexagonState *env)
 {
 uintptr_t ra = GETPC();
-int i;
 
 /* Normal (possibly masked) vector store */
-for (i = 0; i < VSTORES_MAX; i++) {
+for (int i = 0; i < VSTORES_MAX; i++) {
 if (env->vstore_pending[i]) {
 env->vstore_pending[i] = 0;
 target_ulong va = env->vstore[i].va;
@@ -162,7 +161,7 @@ void HELPER(commit_hvx_stores)(CPUHexagonState *env)
 g_assert_not_reached();
 }
 } else {
-for (i = 0; i < sizeof(MMVector); i++) {
+for (int i = 0; i < sizeof(MMVector); i++) {
 if (test_bit(i, env->vtcm_log.mask)) {
 cpu_stb_data_ra(env, env->vtcm_log.va[i],
 env->vtcm_log.data.ub[i], ra);
@@ -505,10 +504,8 @@ void HELPER(probe_pkt_scalar_store_s0)(CPUHexagonState 
*env, int args)
 static void probe_hvx_stores(CPUHexagonState *env, int mmu_idx,
 uintptr_t retaddr)
 {
-int i;
-
 /* Normal (possibly masked) vector store */
-for (i = 0; i < VSTORES_MAX; i++) {
+for (int i = 0; i < VSTORES_MAX; i++) {
 if (env->vstore_pending[i]) {
 target_ulong va = env->vstore[i].va;
 int size = env->vstore[i].size;
diff --git a/target/hexagon/translate.c b/target/hexagon/translate.c
index c00254e4d5..a1c7cd6f21 100644
--- a/target/hexagon/translate.c
+++ b/target/hexagon/translate.c
@@ -553,7 +553,7 @@ static void gen_start_packet(DisasContext *ctx)
 /* Preload the predicated registers into 

[PULL 0/2] hex queue

2023-10-03 Thread Brian Cain
The following changes since commit 36e9aab3c569d4c9ad780473596e18479838d1aa:

  migration: Move return path cleanup to main migration thread (2023-09-27 
13:58:02 -0400)

are available in the Git repository at:

  https://github.com/quic/qemu tags/pull-hex-20231003

for you to fetch changes up to 53cdd26f1909fc45ad50a5eaa95dcfff06130e5f:

  target/hexagon: fix some occurrences of -Wshadow=local (2023-10-03 17:02:29 
-0700)


Hexagon GETPC() and -Wshadow fixes


Brian Cain (1):
  target/hexagon: fix some occurrences of -Wshadow=local

Matheus Tavares Bernardino (1):
  target/hexagon: move GETPC() calls to top level helpers

 target/hexagon/imported/alu.idef |  6 +--
 target/hexagon/macros.h  | 19 +
 target/hexagon/mmvec/macros.h|  2 +-
 target/hexagon/op_helper.c   | 84 +++-
 target/hexagon/op_helper.h   |  9 -
 target/hexagon/translate.c   | 10 ++---
 6 files changed, 50 insertions(+), 80 deletions(-)


[PULL 51/51] bsd-user: Add stubs for vadvise(), sbrk() and sstk()

2023-10-03 Thread Warner Losh
The above system calls are not supported by qemu.

Signed-off-by: Warner Losh 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-24-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 18 ++
 bsd-user/freebsd/os-syscall.c | 12 
 2 files changed, 30 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index c512a4e3756..c3e72e3b866 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -431,4 +431,22 @@ static inline abi_long do_bsd_shmdt(abi_ulong shmaddr)
 return ret;
 }
 
+static inline abi_long do_bsd_vadvise(void)
+{
+/* See sys_ovadvise() in vm_unix.c */
+return -TARGET_EINVAL;
+}
+
+static inline abi_long do_bsd_sbrk(void)
+{
+/* see sys_sbrk() in vm_mmap.c */
+return -TARGET_EOPNOTSUPP;
+}
+
+static inline abi_long do_bsd_sstk(void)
+{
+/* see sys_sstk() in vm_mmap.c */
+return -TARGET_EOPNOTSUPP;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 39e66312da1..ca2f6fdb66e 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -879,6 +879,18 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_shmdt(arg1);
 break;
 
+case TARGET_FREEBSD_NR_freebsd11_vadvise:
+ret = do_bsd_vadvise();
+break;
+
+case TARGET_FREEBSD_NR_sbrk:
+ret = do_bsd_sbrk();
+break;
+
+case TARGET_FREEBSD_NR_sstk:
+ret = do_bsd_sstk();
+break;
+
 /*
  * Misc
  */
-- 
2.41.0




[PULL 49/51] bsd-user: Implement shmctl(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-22-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 39 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 43 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index c362cc07a30..b82f3eaa253 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -305,4 +305,43 @@ static inline abi_long do_bsd_shmget(abi_long arg1, 
abi_ulong arg2,
 return get_errno(shmget(arg1, arg2, arg3));
 }
 
+/* shmctl(2) */
+static inline abi_long do_bsd_shmctl(abi_long shmid, abi_long cmd,
+abi_ulong buff)
+{
+struct shmid_ds dsarg;
+abi_long ret = -TARGET_EINVAL;
+
+cmd &= 0xff;
+
+switch (cmd) {
+case IPC_STAT:
+if (target_to_host_shmid_ds(, buff)) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(shmctl(shmid, cmd, ));
+if (host_to_target_shmid_ds(buff, )) {
+return -TARGET_EFAULT;
+}
+break;
+
+case IPC_SET:
+if (target_to_host_shmid_ds(, buff)) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(shmctl(shmid, cmd, ));
+break;
+
+case IPC_RMID:
+ret = get_errno(shmctl(shmid, cmd, NULL));
+break;
+
+default:
+ret = -TARGET_EINVAL;
+break;
+}
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 4f67677eb92..0512d41db7c 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -867,6 +867,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_shmget(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_shmctl: /* shmctl(2) */
+ret = do_bsd_shmctl(arg1, arg2, arg3);
+break;
+
 /*
  * Misc
  */
-- 
2.41.0




[PULL 44/51] bsd-user: Implment madvise(2) to match the linux-user implementation.

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-17-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 53 +++
 bsd-user/freebsd/os-syscall.c |  4 +++
 bsd-user/syscall_defs.h   |  2 ++
 3 files changed, 59 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 16c22593bfd..b00ab3aed8e 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -129,6 +129,59 @@ static inline abi_long do_bsd_munlockall(void)
 return get_errno(munlockall());
 }
 
+/* madvise(2) */
+static inline abi_long do_bsd_madvise(abi_long arg1, abi_long arg2,
+abi_long arg3)
+{
+abi_ulong len;
+int ret = 0;
+abi_long start = arg1;
+abi_long len_in = arg2;
+abi_long advice = arg3;
+
+if (start & ~TARGET_PAGE_MASK) {
+return -TARGET_EINVAL;
+}
+if (len_in == 0) {
+return 0;
+}
+len = TARGET_PAGE_ALIGN(len_in);
+if (len == 0 || !guest_range_valid_untagged(start, len)) {
+return -TARGET_EINVAL;
+}
+
+/*
+ * Most advice values are hints, so ignoring and returning success is ok.
+ *
+ * However, some advice values such as MADV_DONTNEED, are not hints and
+ * need to be emulated.
+ *
+ * A straight passthrough for those may not be safe because qemu sometimes
+ * turns private file-backed mappings into anonymous mappings.
+ * If all guest pages have PAGE_PASSTHROUGH set, mappings have the
+ * same semantics for the host as for the guest.
+ *
+ * MADV_DONTNEED is passed through, if possible.
+ * If passthrough isn't possible, we nevertheless (wrongly!) return
+ * success, which is broken but some userspace programs fail to work
+ * otherwise. Completely implementing such emulation is quite complicated
+ * though.
+ */
+mmap_lock();
+switch (advice) {
+case MADV_DONTNEED:
+if (page_check_range(start, len, PAGE_PASSTHROUGH)) {
+ret = get_errno(madvise(g2h_untagged(start), len, advice));
+if (ret == 0) {
+page_reset_target_data(start, start + len - 1);
+}
+}
+}
+mmap_unlock();
+
+return ret;
+}
+
 /* minherit(2) */
 static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
 abi_long inherit)
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7a7ae26793f..b8c44cea0ff 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -831,6 +831,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_munlockall();
 break;
 
+case TARGET_FREEBSD_NR_madvise: /* madvise(2) */
+ret = do_bsd_madvise(arg1, arg2, arg3);
+break;
+
 case TARGET_FREEBSD_NR_minherit: /* minherit(2) */
 ret = do_bsd_minherit(arg1, arg2, arg3);
 break;
diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index ff692814333..52f84d5dd17 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -95,6 +95,8 @@ struct bsd_shm_regions {
 /*
  *  sys/mman.h
  */
+#define TARGET_MADV_DONTNEED4   /* dont need these pages */
+
 #define TARGET_FREEBSD_MAP_RESERVED0080 0x0080  /* previously misimplemented */
 /* MAP_INHERIT */
 #define TARGET_FREEBSD_MAP_RESERVED0100 0x0100  /* previously unimplemented */
-- 
2.41.0




Re: [PATCH v3 10/15] vfio/ccw: Use vfio_[attach/detach]_device

2023-10-03 Thread Matthew Rosato
On 10/3/23 6:14 AM, Eric Auger wrote:
> Let the vfio-ccw device use vfio_attach_device() and
> vfio_detach_device(), hence hiding the details of the used
> IOMMU backend.
> 
> Note that the migration reduces the following trace
> "vfio: subchannel %s has already been attached" (featuring
> cssid.ssid.devid) into "device is already attached"
> 
> Also now all the devices have been migrated to use the new
> vfio_attach_device/vfio_detach_device API, let's turn the
> legacy functions into static functions, local to container.c.
> 
> Signed-off-by: Eric Auger 
> Signed-off-by: Yi Liu 
> Signed-off-by: Zhenzhong Duan 
> 
> ---
> v2 -> v3:
> - Hopefully fix confusion beteen vbasedev->name, mdevid and sysfsdev
>   while keeping into account Matthew's comment
>   
> https://lore.kernel.org/qemu-devel/6e04ab8f-dc84-e9c2-deea-2b6b31678...@linux.ibm.com/

Reviewed-by: Matthew Rosato 

Tested out using some vfio-ccw devices and verified attach/detach worked / 
sysfsdev was correct.

> ---
>  include/hw/vfio/vfio-common.h |   5 --
>  hw/vfio/ccw.c | 122 +-
>  hw/vfio/common.c  |  10 +--
>  3 files changed, 37 insertions(+), 100 deletions(-)
> 
> diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
> index 12fbfbc37d..c486bdef2a 100644
> --- a/include/hw/vfio/vfio-common.h
> +++ b/include/hw/vfio/vfio-common.h
> @@ -202,7 +202,6 @@ typedef struct {
>  hwaddr pages;
>  } VFIOBitmap;
>  
> -void vfio_put_base_device(VFIODevice *vbasedev);
>  void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
>  void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
>  void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
> @@ -220,11 +219,7 @@ void vfio_region_unmap(VFIORegion *region);
>  void vfio_region_exit(VFIORegion *region);
>  void vfio_region_finalize(VFIORegion *region);
>  void vfio_reset_handler(void *opaque);
> -VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);
> -void vfio_put_group(VFIOGroup *group);
>  struct vfio_device_info *vfio_get_device_info(int fd);
> -int vfio_get_device(VFIOGroup *group, const char *name,
> -VFIODevice *vbasedev, Error **errp);
>  int vfio_attach_device(char *name, VFIODevice *vbasedev,
> AddressSpace *as, Error **errp);
>  void vfio_detach_device(VFIODevice *vbasedev);
> diff --git a/hw/vfio/ccw.c b/hw/vfio/ccw.c
> index 1e2fce83b0..84eafb2e87 100644
> --- a/hw/vfio/ccw.c
> +++ b/hw/vfio/ccw.c
> @@ -572,88 +572,15 @@ static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
>  g_free(vcdev->io_region);
>  }
>  
> -static void vfio_ccw_put_device(VFIOCCWDevice *vcdev)
> -{
> -g_free(vcdev->vdev.name);
> -vfio_put_base_device(>vdev);
> -}
> -
> -static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
> -Error **errp)
> -{
> -S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev);
> -char *name = g_strdup_printf("%x.%x.%04x", cdev->hostid.cssid,
> - cdev->hostid.ssid,
> - cdev->hostid.devid);
> -VFIODevice *vbasedev;
> -
> -QLIST_FOREACH(vbasedev, >device_list, next) {
> -if (strcmp(vbasedev->name, name) == 0) {
> -error_setg(errp, "vfio: subchannel %s has already been attached",
> -   name);
> -goto out_err;
> -}
> -}
> -
> -/*
> - * All vfio-ccw devices are believed to operate in a way compatible with
> - * discarding of memory in RAM blocks, ie. pages pinned in the host are
> - * in the current working set of the guest driver and therefore never
> - * overlap e.g., with pages available to the guest balloon driver.  This
> - * needs to be set before vfio_get_device() for vfio common to handle
> - * ram_block_discard_disable().
> - */
> -vcdev->vdev.ram_block_discard_allowed = true;
> -
> -if (vfio_get_device(group, cdev->mdevid, >vdev, errp)) {
> -goto out_err;
> -}
> -
> -vcdev->vdev.ops = _ccw_ops;
> -vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
> -vcdev->vdev.name = name;
> -vcdev->vdev.dev = DEVICE(vcdev);
> -
> -return;
> -
> -out_err:
> -g_free(name);
> -}
> -
> -static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
> -{
> -char *tmp, group_path[PATH_MAX];
> -ssize_t len;
> -int groupid;
> -
> -tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
> -  cdev->hostid.cssid, cdev->hostid.ssid,
> -  cdev->hostid.devid, cdev->mdevid);
> -len = readlink(tmp, group_path, sizeof(group_path));
> -g_free(tmp);
> -
> -if (len <= 0 || len >= sizeof(group_path)) {
> -error_setg(errp, "vfio: no iommu_group found");
> -return NULL;
> -}
> -
> -group_path[len] = 0;
> -
> -if (sscanf(basename(group_path), "%d", ) != 1) {
> -

[PULL 35/51] bsd-user: Add bsd-mem.c to meson.build

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-8-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c   | 0
 bsd-user/meson.build | 1 +
 2 files changed, 1 insertion(+)
 create mode 100644 bsd-user/bsd-mem.c

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
new file mode 100644
index 000..e69de29bb2d
diff --git a/bsd-user/meson.build b/bsd-user/meson.build
index b97fce14722..c6bfd3b2b53 100644
--- a/bsd-user/meson.build
+++ b/bsd-user/meson.build
@@ -7,6 +7,7 @@ bsd_user_ss = ss.source_set()
 common_user_inc += include_directories('include')
 
 bsd_user_ss.add(files(
+  'bsd-mem.c',
   'bsd-proc.c',
   'bsdload.c',
   'elfload.c',
-- 
2.41.0




[PULL 02/51] bsd-user: Define procctl(2) related structs

2023-10-03 Thread Warner Losh
From: Stacey Son 

Implement procctl flags and related structs:
struct target_procctl_reaper_status
struct target_procctl_reaper_pidinfo
struct target_procctl_reaper_pids
struct target_procctl_reaper_kill

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-3-kariem.taha...@gmail.com>
---
 bsd-user/syscall_defs.h | 42 +
 1 file changed, 42 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index ddd38c13e08..a3bc738ff89 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -390,6 +390,48 @@ struct target_freebsd_flock {
 /* user: vfork(2) semantics, clear signals */
 #define TARGET_RFSPAWN (1U << 31)
 
+/*
+ * from sys/procctl.h
+ */
+#define TARGET_PROC_SPROTECT1
+#define TARGET_PROC_REAP_ACQUIRE2
+#define TARGET_PROC_REAP_RELEASE3
+#define TARGET_PROC_REAP_STATUS 4
+#define TARGET_PROC_REAP_GETPIDS5
+#define TARGET_PROC_REAP_KILL   6
+
+struct target_procctl_reaper_status {
+uint32_t rs_flags;
+uint32_t rs_children;
+uint32_t rs_descendants;
+uint32_t rs_reaper;
+uint32_t rs_pid;
+uint32_t rs_pad0[15];
+};
+
+struct target_procctl_reaper_pidinfo {
+uint32_t pi_pid;
+uint32_t pi_subtree;
+uint32_t pi_flags;
+uint32_t pi_pad0[15];
+};
+
+struct target_procctl_reaper_pids {
+uint32_t rp_count;
+uint32_t rp_pad0[15];
+abi_ulong rp_pids;
+};
+
+struct target_procctl_reaper_kill {
+int32_t  rk_sig;
+uint32_t rk_flags;
+uint32_t rk_subtree;
+uint32_t rk_killed;
+uint32_t rk_fpid;
+uint32_t rk_pad0[15];
+};
+
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
-- 
2.41.0




[PULL 40/51] bsd-user: Implement mmap(2) and munmap(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-13-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 20 
 bsd-user/freebsd/os-syscall.c |  9 +
 2 files changed, 29 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index d865e0807d8..76b504f70c5 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -61,4 +61,24 @@ extern struct bsd_shm_regions bsd_shm_regions[];
 extern abi_ulong target_brk;
 extern abi_ulong initial_target_brk;
 
+/* mmap(2) */
+static inline abi_long do_bsd_mmap(void *cpu_env, abi_long arg1, abi_long arg2,
+abi_long arg3, abi_long arg4, abi_long arg5, abi_long arg6, abi_long arg7,
+abi_long arg8)
+{
+if (regpairs_aligned(cpu_env) != 0) {
+arg6 = arg7;
+arg7 = arg8;
+}
+return get_errno(target_mmap(arg1, arg2, arg3,
+ target_to_host_bitmask(arg4, mmap_flags_tbl),
+ arg5, target_arg64(arg6, arg7)));
+}
+
+/* munmap(2) */
+static inline abi_long do_bsd_munmap(abi_long arg1, abi_long arg2)
+{
+return get_errno(target_munmap(arg1, arg2));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7887ad4c0c6..b03837d032a 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -798,6 +798,15 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 /*
  * Memory management system calls.
  */
+case TARGET_FREEBSD_NR_mmap: /* mmap(2) */
+ret = do_bsd_mmap(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6, arg7,
+  arg8);
+break;
+
+case TARGET_FREEBSD_NR_munmap: /* munmap(2) */
+ret = do_bsd_munmap(arg1, arg2);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




Re: [PATCH v2 3/5] hw/i386/apic: Simplify apic_get_class()

2023-10-03 Thread Bernhard Beschow



Am 3. Oktober 2023 08:27:26 UTC schrieb "Philippe Mathieu-Daudé" 
:
>Now than apic_get_class() can not fail, remove its

s/than/that/
s/can not/can't/ (which is stylistically consistent with "neither" below)

Best regards,
Bernhard

>Error** parameter. It can't return NULL neither, so
>simplify x86_cpu_apic_create().
>
>Signed-off-by: Philippe Mathieu-Daudé 
>---
> include/hw/i386/apic_internal.h | 2 +-
> hw/i386/amd_iommu.c | 2 +-
> hw/i386/intel_iommu.c   | 4 ++--
> target/i386/cpu-sysemu.c| 4 ++--
> 4 files changed, 6 insertions(+), 6 deletions(-)
>
>diff --git a/include/hw/i386/apic_internal.h b/include/hw/i386/apic_internal.h
>index 5f2ba24bfc..e61ad04769 100644
>--- a/include/hw/i386/apic_internal.h
>+++ b/include/hw/i386/apic_internal.h
>@@ -225,6 +225,6 @@ static inline int apic_get_bit(uint32_t *tab, int index)
> return !!(tab[i] & mask);
> }
> 
>-APICCommonClass *apic_get_class(Error **errp);
>+APICCommonClass *apic_get_class(void);
> 
> #endif /* QEMU_APIC_INTERNAL_H */
>diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
>index c98a3c6e11..0a95025ab7 100644
>--- a/hw/i386/amd_iommu.c
>+++ b/hw/i386/amd_iommu.c
>@@ -1368,7 +1368,7 @@ static MemTxResult amdvi_mem_ir_write(void *opaque, 
>hwaddr addr,
> return MEMTX_ERROR;
> }
> 
>-apic_get_class(NULL)->send_msi();
>+apic_get_class()->send_msi();
> 
> trace_amdvi_mem_ir_write(to.address, to.data);
> return MEMTX_OK;
>diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
>index 2c832ab68b..dffe3583bd 100644
>--- a/hw/i386/intel_iommu.c
>+++ b/hw/i386/intel_iommu.c
>@@ -397,7 +397,7 @@ static void vtd_generate_interrupt(IntelIOMMUState *s, 
>hwaddr mesg_addr_reg,
> 
> trace_vtd_irq_generate(msi.address, msi.data);
> 
>-apic_get_class(NULL)->send_msi();
>+apic_get_class()->send_msi();
> }
> 
> /* Generate a fault event to software via MSI if conditions are met.
>@@ -3554,7 +3554,7 @@ static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr 
>addr,
> return MEMTX_ERROR;
> }
> 
>-apic_get_class(NULL)->send_msi();
>+apic_get_class()->send_msi();
> 
> return MEMTX_OK;
> }
>diff --git a/target/i386/cpu-sysemu.c b/target/i386/cpu-sysemu.c
>index 6a228c9178..9038c65267 100644
>--- a/target/i386/cpu-sysemu.c
>+++ b/target/i386/cpu-sysemu.c
>@@ -247,7 +247,7 @@ void x86_cpu_machine_reset_cb(void *opaque)
> cpu_reset(CPU(cpu));
> }
> 
>-APICCommonClass *apic_get_class(Error **errp)
>+APICCommonClass *apic_get_class(void)
> {
> const char *apic_type = "apic";
> 
>@@ -266,7 +266,7 @@ APICCommonClass *apic_get_class(Error **errp)
> void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
> {
> APICCommonState *apic;
>-APICCommonClass *apic_class = apic_get_class(errp);
>+APICCommonClass *apic_class = apic_get_class();
> 
> cpu->apic_state = DEVICE(object_new_with_class(OBJECT_CLASS(apic_class)));
> object_property_add_child(OBJECT(cpu), "lapic",



[PULL 31/51] bsd-user: Declarations for ipc_perm and shmid_ds conversion functions

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-4-kariem.taha...@gmail.com>
---
 bsd-user/qemu-bsd.h | 20 
 1 file changed, 20 insertions(+)

diff --git a/bsd-user/qemu-bsd.h b/bsd-user/qemu-bsd.h
index b93a0b7fd5b..ffc64bb244a 100644
--- a/bsd-user/qemu-bsd.h
+++ b/bsd-user/qemu-bsd.h
@@ -22,6 +22,16 @@
 
 #include 
 #include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
 /* bsd-proc.c */
 int target_to_host_resource(int code);
@@ -35,4 +45,14 @@ int host_to_target_waitstatus(int status);
 void h2g_rusage(const struct rusage *rusage,
 struct target_freebsd_rusage *target_rusage);
 
+/* bsd-mem.c */
+void target_to_host_ipc_perm__locked(struct ipc_perm *host_ip,
+struct target_ipc_perm *target_ip);
+void host_to_target_ipc_perm__locked(struct target_ipc_perm *target_ip,
+struct ipc_perm *host_ip);
+abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
+abi_ulong target_addr);
+abi_long host_to_target_shmid_ds(abi_ulong target_addr,
+struct shmid_ds *host_sd);
+
 #endif /* QEMU_BSD_H */
-- 
2.41.0




[PULL 28/51] bsd-user: Implement pdfork(2) system call.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Acked-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-29-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 32 
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 36 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 0a3cd0ef57c..d6418780344 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -258,4 +258,36 @@ static inline abi_long do_freebsd_rfork(void *cpu_env, 
abi_long flags)
 
 }
 
+/* pdfork(2) */
+static inline abi_long do_freebsd_pdfork(void *cpu_env, abi_ulong target_fdp,
+abi_long flags)
+{
+abi_long ret;
+abi_ulong child_flag;
+int fd;
+
+fork_start();
+ret = pdfork(, flags);
+if (ret == 0) {
+/* child */
+child_flag = 1;
+target_cpu_clone_regs(cpu_env, 0);
+} else {
+/* parent */
+child_flag = 0;
+if (put_user_s32(fd, target_fdp)) {
+return -TARGET_EFAULT;
+}
+}
+
+/*
+ * The fork system call sets a child flag in the second return
+ * value: 0 for parent process, 1 for child process.
+ */
+set_second_rval(cpu_env, child_flag);
+fork_end(child_flag);
+
+return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 4c4e773d1d3..d04712f0a7e 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -238,6 +238,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_freebsd_rfork(cpu_env, arg1);
 break;
 
+case TARGET_FREEBSD_NR_pdfork: /* pdfork(2) */
+ret = do_freebsd_pdfork(cpu_env, arg1, arg2);
+break;
+
 case TARGET_FREEBSD_NR_execve: /* execve(2) */
 ret = do_freebsd_execve(arg1, arg2, arg3);
 break;
-- 
2.41.0




[PULL 27/51] bsd-user: Implement rfork(2) system call.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-28-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 39 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 43 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 7b2e6a9f796..0a3cd0ef57c 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -219,4 +219,43 @@ static inline abi_long do_freebsd_vfork(void *cpu_env)
 return do_freebsd_fork(cpu_env);
 }
 
+/* rfork(2) */
+static inline abi_long do_freebsd_rfork(void *cpu_env, abi_long flags)
+{
+abi_long ret;
+abi_ulong child_flag;
+
+/*
+ * XXX We need to handle RFMEM here, as well.  Neither are safe to execute
+ * as-is on x86 hosts because they'll split memory but not the stack,
+ * wreaking havoc on host architectures that use the stack to store the
+ * return address as both threads try to pop it off.  Rejecting RFSPAWN
+ * entirely for now is ok, the only consumer at the moment is posix_spawn
+ * and it will fall back to classic vfork(2) if we return EINVAL.
+ */
+if ((flags & TARGET_RFSPAWN) != 0) {
+return -TARGET_EINVAL;
+}
+fork_start();
+ret = rfork(flags);
+if (ret == 0) {
+/* child */
+child_flag = 1;
+target_cpu_clone_regs(cpu_env, 0);
+} else {
+/* parent */
+child_flag = 0;
+}
+
+/*
+ * The fork system call sets a child flag in the second return
+ * value: 0 for parent process, 1 for child process.
+ */
+set_second_rval(cpu_env, child_flag);
+fork_end(child_flag);
+
+return ret;
+
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index cb9425c9bab..4c4e773d1d3 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -234,6 +234,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_freebsd_vfork(cpu_env);
 break;
 
+case TARGET_FREEBSD_NR_rfork: /* rfork(2) */
+ret = do_freebsd_rfork(cpu_env, arg1);
+break;
+
 case TARGET_FREEBSD_NR_execve: /* execve(2) */
 ret = do_freebsd_execve(arg1, arg2, arg3);
 break;
-- 
2.41.0




[PULL 46/51] bsd-user: Implement do_obreak function

2023-10-03 Thread Warner Losh
From: Stacey Son 

Match linux-user, by manually applying the following commits, in order:

d28b3c90cfad1a7e211ae2bce36ecb9071086129   linux-user: Make sure initial brk(0) 
is page-aligned
15ad98536ad9410fb32ddf1ff09389b677643faa   linux-user: Fix qemu brk() to not 
zero bytes on current page
dfe49864afb06e7e452a4366051697bc4fcfc1a5   linux-user: Prohibit brk() to to 
shrink below initial heap address
eac78a4b0b7da4de2c0a297f4d528ca9cc6256a3   linux-user: Fix signed math overflow 
in brk() syscall
c6cc059eca18d9f6e4e26bb8b6d1135ddb35d81a   linux-user: Do not call get_errno() 
in do_brk()
e69e032d1a8ee8d754ca119009a3c2c997f8bb30   linux-user: Use MAP_FIXED_NOREPLACE 
for do_brk()
cb9d5d1fda0bc2312fc0c779b4ea1d7bf826f31f   linux-user: Do nothing if too small 
brk is specified
2aea137a425a87b930a33590177b04368fd7cc12   linux-user: Do not align brk with 
host page size

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-19-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 45 +++
 bsd-user/freebsd/os-syscall.c |  7 ++
 2 files changed, 52 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 0c8d96d9a43..b296c5c6f0a 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -212,4 +212,49 @@ static inline abi_long do_bsd_mincore(abi_ulong 
target_addr, abi_ulong len,
 return ret;
 }
 
+/* do_brk() must return target values and target errnos. */
+static inline abi_long do_obreak(abi_ulong brk_val)
+{
+abi_long mapped_addr;
+abi_ulong new_brk;
+abi_ulong old_brk;
+
+/* brk pointers are always untagged */
+
+/* do not allow to shrink below initial brk value */
+if (brk_val < initial_target_brk) {
+return target_brk;
+}
+
+new_brk = TARGET_PAGE_ALIGN(brk_val);
+old_brk = TARGET_PAGE_ALIGN(target_brk);
+
+/* new and old target_brk might be on the same page */
+if (new_brk == old_brk) {
+target_brk = brk_val;
+return target_brk;
+}
+
+/* Release heap if necesary */
+if (new_brk < old_brk) {
+target_munmap(new_brk, old_brk - new_brk);
+
+target_brk = brk_val;
+return target_brk;
+}
+
+mapped_addr = target_mmap(old_brk, new_brk - old_brk,
+  PROT_READ | PROT_WRITE,
+  MAP_FIXED | MAP_EXCL | MAP_ANON | MAP_PRIVATE,
+  -1, 0);
+
+if (mapped_addr == old_brk) {
+target_brk = brk_val;
+return target_brk;
+}
+
+/* For everything else, return the previous break. */
+return target_brk;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index f054241cd62..92793ab1fb3 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -855,6 +855,13 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 break;
 #endif
 
+/*
+ * Misc
+ */
+case TARGET_FREEBSD_NR_break:
+ret = do_obreak(arg1);
+break;
+
 /*
  * sys{ctl, arch, call}
  */
-- 
2.41.0




[PULL 19/51] bsd-user: Implement get_filename_from_fd.

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-20-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/meson.build |  1 +
 bsd-user/freebsd/os-proc.c   | 82 
 2 files changed, 83 insertions(+)
 create mode 100644 bsd-user/freebsd/os-proc.c

diff --git a/bsd-user/freebsd/meson.build b/bsd-user/freebsd/meson.build
index f2f047cca31..8fd6c7cfb82 100644
--- a/bsd-user/freebsd/meson.build
+++ b/bsd-user/freebsd/meson.build
@@ -1,5 +1,6 @@
 bsd_user_ss.add(files(
   'os-stat.c',
+  'os-proc.c',
   'os-sys.c',
   'os-syscall.c',
 ))
diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
new file mode 100644
index 000..2603c5c6538
--- /dev/null
+++ b/bsd-user/freebsd/os-proc.c
@@ -0,0 +1,82 @@
+/*
+ *  FreeBSD process related emulation code
+ *
+ *  Copyright (c) 2013-15 Stacey D. Son
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, see .
+ */
+#include "qemu/osdep.h"
+
+#include 
+#include 
+#include 
+struct kinfo_proc;
+#include 
+
+#include "qemu.h"
+
+/*
+ * Get the filename for the given file descriptor.
+ * Note that this may return NULL (fail) if no longer cached in the kernel.
+ */
+char *
+get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len);
+char *
+get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
+{
+char *ret = NULL;
+unsigned int cnt;
+struct procstat *procstat = NULL;
+struct kinfo_proc *kp = NULL;
+struct filestat_list *head = NULL;
+struct filestat *fst;
+
+procstat = procstat_open_sysctl();
+if (procstat == NULL) {
+goto out;
+}
+
+kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, );
+if (kp == NULL) {
+goto out;
+}
+
+head = procstat_getfiles(procstat, kp, 0);
+if (head == NULL) {
+goto out;
+}
+
+STAILQ_FOREACH(fst, head, next) {
+if (fd == fst->fs_fd) {
+if (fst->fs_path != NULL) {
+(void)strlcpy(filename, fst->fs_path, len);
+ret = filename;
+}
+break;
+}
+}
+
+out:
+if (head != NULL) {
+procstat_freefiles(procstat, head);
+}
+if (kp != NULL) {
+procstat_freeprocs(procstat, kp);
+}
+if (procstat != NULL) {
+procstat_close(procstat);
+}
+return ret;
+}
+
-- 
2.41.0




[PULL 30/51] bsd-user: Implement struct target_shmid_ds

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-3-kariem.taha...@gmail.com>
---
 bsd-user/syscall_defs.h | 20 
 1 file changed, 20 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 0e54d7df690..ff692814333 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -72,6 +72,26 @@ struct target_ipc_perm {
 #define TARGET_IPC_SET  1   /* set options */
 #define TARGET_IPC_STAT 2   /* get options */
 
+/*
+ * sys/shm.h
+ */
+struct target_shmid_ds {
+struct  target_ipc_perm shm_perm; /* peration permission structure */
+abi_ulong   shm_segsz;  /* size of segment in bytes */
+int32_t shm_lpid;   /* process ID of last shared memory op */
+int32_t shm_cpid;   /* process ID of creator */
+int32_t shm_nattch; /* number of current attaches */
+target_time_t shm_atime;  /* time of last shmat() */
+target_time_t shm_dtime;  /* time of last shmdt() */
+target_time_t shm_ctime;  /* time of last change by shmctl() */
+};
+
+#define N_BSD_SHM_REGIONS   32
+struct bsd_shm_regions {
+abi_long start;
+abi_long size;
+};
+
 /*
  *  sys/mman.h
  */
-- 
2.41.0




[PULL 15/51] bsd-user: Implement several get/set system calls:

2023-10-03 Thread Warner Losh
From: Stacey Son 

getpid(2), getppid(2), getpgrp(2)
setreuid(2), setregid(2)
getuid(2), geteuid(2), getgid(2), getegid(2), getpgid(2)
setuid(2), seteuid(2), setgid(2), setegid(2), setpgid(2)

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-16-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 90 +++
 bsd-user/freebsd/os-syscall.c | 60 +++
 2 files changed, 150 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 38d1324034c..6ff07c0ac36 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -196,4 +196,94 @@ static inline abi_long do_bsd_setrlimit(abi_long arg1, 
abi_ulong arg2)
 return ret;
 }
 
+/* getpid(2) */
+static inline abi_long do_bsd_getpid(void)
+{
+return get_errno(getpid());
+}
+
+/* getppid(2) */
+static inline abi_long do_bsd_getppid(void)
+{
+return get_errno(getppid());
+}
+
+/* getuid(2) */
+static inline abi_long do_bsd_getuid(void)
+{
+return get_errno(getuid());
+}
+
+/* geteuid(2) */
+static inline abi_long do_bsd_geteuid(void)
+{
+return get_errno(geteuid());
+}
+
+/* getgid(2) */
+static inline abi_long do_bsd_getgid(void)
+{
+return get_errno(getgid());
+}
+
+/* getegid(2) */
+static inline abi_long do_bsd_getegid(void)
+{
+return get_errno(getegid());
+}
+
+/* setuid(2) */
+static inline abi_long do_bsd_setuid(abi_long arg1)
+{
+return get_errno(setuid(arg1));
+}
+
+/* seteuid(2) */
+static inline abi_long do_bsd_seteuid(abi_long arg1)
+{
+return get_errno(seteuid(arg1));
+}
+
+/* setgid(2) */
+static inline abi_long do_bsd_setgid(abi_long arg1)
+{
+return get_errno(setgid(arg1));
+}
+
+/* setegid(2) */
+static inline abi_long do_bsd_setegid(abi_long arg1)
+{
+return get_errno(setegid(arg1));
+}
+
+/* getpgid(2) */
+static inline abi_long do_bsd_getpgid(pid_t pid)
+{
+return get_errno(getpgid(pid));
+}
+
+/* setpgid(2) */
+static inline abi_long do_bsd_setpgid(int pid, int pgrp)
+{
+return get_errno(setpgid(pid, pgrp));
+}
+
+/* getpgrp(2) */
+static inline abi_long do_bsd_getpgrp(void)
+{
+return get_errno(getpgrp());
+}
+
+/* setreuid(2) */
+static inline abi_long do_bsd_setreuid(abi_long arg1, abi_long arg2)
+{
+return get_errno(setreuid(arg1, arg2));
+}
+
+/* setregid(2) */
+static inline abi_long do_bsd_setregid(abi_long arg1, abi_long arg2)
+{
+return get_errno(setregid(arg1, arg2));
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 5cb60862303..7565e69e76d 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -255,6 +255,66 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_setrlimit(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_getpid: /* getpid(2) */
+ret = do_bsd_getpid();
+break;
+
+case TARGET_FREEBSD_NR_getppid: /* getppid(2) */
+ret = do_bsd_getppid();
+break;
+
+case TARGET_FREEBSD_NR_getuid: /* getuid(2) */
+ret = do_bsd_getuid();
+break;
+
+case TARGET_FREEBSD_NR_geteuid: /* geteuid(2) */
+ret = do_bsd_geteuid();
+break;
+
+case TARGET_FREEBSD_NR_getgid: /* getgid(2) */
+ret = do_bsd_getgid();
+break;
+
+case TARGET_FREEBSD_NR_getegid: /* getegid(2) */
+ret = do_bsd_getegid();
+break;
+
+case TARGET_FREEBSD_NR_setuid: /* setuid(2) */
+ret = do_bsd_setuid(arg1);
+break;
+
+case TARGET_FREEBSD_NR_seteuid: /* seteuid(2) */
+ret = do_bsd_seteuid(arg1);
+break;
+
+case TARGET_FREEBSD_NR_setgid: /* setgid(2) */
+ret = do_bsd_setgid(arg1);
+break;
+
+case TARGET_FREEBSD_NR_setegid: /* setegid(2) */
+ret = do_bsd_setegid(arg1);
+break;
+
+case TARGET_FREEBSD_NR_getpgrp: /* getpgrp(2) */
+ret = do_bsd_getpgrp();
+break;
+
+case TARGET_FREEBSD_NR_getpgid: /* getpgid(2) */
+ ret = do_bsd_getpgid(arg1);
+ break;
+
+case TARGET_FREEBSD_NR_setpgid: /* setpgid(2) */
+ ret = do_bsd_setpgid(arg1, arg2);
+ break;
+
+case TARGET_FREEBSD_NR_setreuid: /* setreuid(2) */
+ret = do_bsd_setreuid(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_setregid: /* setregid(2) */
+ret = do_bsd_setregid(arg1, arg2);
+break;
+
 
 /*
  * File system calls.
-- 
2.41.0




[PULL 25/51] bsd-user: Implement pdgetpid(2) and the undocumented setugid.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-26-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 23 +++
 bsd-user/freebsd/os-syscall.c |  8 
 2 files changed, 31 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 2eaba141dcd..42bdd61904b 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -34,6 +34,8 @@ pid_t safe_wait4(pid_t wpid, int *status, int options, struct 
rusage *rusage);
 pid_t safe_wait6(idtype_t idtype, id_t id, int *status, int options,
 struct __wrusage *wrusage, siginfo_t *infop);
 
+extern int __setugid(int flag);
+
 /* execve(2) */
 static inline abi_long do_freebsd_execve(abi_ulong path_or_fd, abi_ulong argp,
 abi_ulong envp)
@@ -162,4 +164,25 @@ static inline abi_long do_freebsd_getloginclass(abi_ulong 
arg1, abi_ulong arg2)
 return ret;
 }
 
+/* pdgetpid(2) */
+static inline abi_long do_freebsd_pdgetpid(abi_long fd, abi_ulong target_pidp)
+{
+abi_long ret;
+pid_t pid;
+
+ret = get_errno(pdgetpid(fd, ));
+if (!is_error(ret)) {
+if (put_user_u32(pid, target_pidp)) {
+return -TARGET_EFAULT;
+}
+}
+return ret;
+}
+
+/* undocumented __setugid */
+static inline abi_long do_freebsd___setugid(abi_long arg1)
+{
+return -TARGET_ENOSYS;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index d614409e694..99af0f6b156 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -383,6 +383,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_freebsd_getloginclass(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_pdgetpid: /* pdgetpid(2) */
+ret = do_freebsd_pdgetpid(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR___setugid: /* undocumented */
+ret = do_freebsd___setugid(arg1);
+break;
+
 case TARGET_FREEBSD_NR_utrace: /* utrace(2) */
 ret = do_bsd_utrace(arg1, arg2);
 break;
-- 
2.41.0




[PULL 47/51] bsd-user: Implement shm_open(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Co-authored-by: Kyle Evans 

Signed-off-by: Stacey Son 
Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-20-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 25 +
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 29 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index b296c5c6f0a..f8dc943c234 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -257,4 +257,29 @@ static inline abi_long do_obreak(abi_ulong brk_val)
 return target_brk;
 }
 
+/* shm_open(2) */
+static inline abi_long do_bsd_shm_open(abi_ulong arg1, abi_long arg2,
+abi_long arg3)
+{
+int ret;
+void *p;
+
+if (arg1 == (uintptr_t)SHM_ANON) {
+p = SHM_ANON;
+} else {
+p = lock_user_string(arg1);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+}
+ret = get_errno(shm_open(p, target_to_host_bitmask(arg2, fcntl_flags_tbl),
+ arg3));
+
+if (p != SHM_ANON) {
+unlock_user(p, arg1, 0);
+}
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 92793ab1fb3..0d4c3118f0d 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -843,6 +843,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_mincore(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_freebsd12_shm_open: /* shm_open(2) */
+ret = do_bsd_shm_open(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 16/51] bsd-user: Implement get/set[resuid/resgid/sid] and issetugid.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-17-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 76 +++
 bsd-user/freebsd/os-syscall.c | 28 +
 2 files changed, 104 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 6ff07c0ac36..a5f301c72ff 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -286,4 +286,80 @@ static inline abi_long do_bsd_setregid(abi_long arg1, 
abi_long arg2)
 return get_errno(setregid(arg1, arg2));
 }
 
+/* setresgid(2) */
+static inline abi_long do_bsd_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
+{
+return get_errno(setresgid(rgid, egid, sgid));
+}
+
+/* setresuid(2) */
+static inline abi_long do_bsd_setresuid(uid_t ruid, uid_t euid, uid_t suid)
+{
+return get_errno(setresuid(ruid, euid, suid));
+}
+
+/* getresuid(2) */
+static inline abi_long do_bsd_getresuid(abi_ulong arg1, abi_ulong arg2,
+abi_ulong arg3)
+{
+abi_long ret;
+uid_t ruid, euid, suid;
+
+ret = get_errno(getresuid(, , ));
+if (is_error(ret)) {
+return ret;
+}
+if (put_user_s32(ruid, arg1)) {
+return -TARGET_EFAULT;
+}
+if (put_user_s32(euid, arg2)) {
+return -TARGET_EFAULT;
+}
+if (put_user_s32(suid, arg3)) {
+return -TARGET_EFAULT;
+}
+return ret;
+}
+
+/* getresgid(2) */
+static inline abi_long do_bsd_getresgid(abi_ulong arg1, abi_ulong arg2,
+abi_ulong arg3)
+{
+abi_long ret;
+uid_t ruid, euid, suid;
+
+ret = get_errno(getresgid(, , ));
+if (is_error(ret)) {
+return ret;
+}
+if (put_user_s32(ruid, arg1)) {
+return -TARGET_EFAULT;
+}
+if (put_user_s32(euid, arg2)) {
+return -TARGET_EFAULT;
+}
+if (put_user_s32(suid, arg3)) {
+return -TARGET_EFAULT;
+}
+return ret;
+}
+
+/* getsid(2) */
+static inline abi_long do_bsd_getsid(abi_long arg1)
+{
+return get_errno(getsid(arg1));
+}
+
+/* setsid(2) */
+static inline abi_long do_bsd_setsid(void)
+{
+return get_errno(setsid());
+}
+
+/* issetugid(2) */
+static inline abi_long do_bsd_issetugid(void)
+{
+return get_errno(issetugid());
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7565e69e76d..7b51f4f16e4 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -315,6 +315,34 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_setregid(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_getresuid: /* getresuid(2) */
+ret = do_bsd_getresuid(arg1, arg2, arg3);
+break;
+
+case TARGET_FREEBSD_NR_getresgid: /* getresgid(2) */
+ret = do_bsd_getresgid(arg1, arg2, arg3);
+break;
+
+case TARGET_FREEBSD_NR_setresuid: /* setresuid(2) */
+ret = do_bsd_setresuid(arg1, arg2, arg3);
+break;
+
+case TARGET_FREEBSD_NR_setresgid: /* setresgid(2) */
+ret = do_bsd_setresgid(arg1, arg2, arg3);
+break;
+
+case TARGET_FREEBSD_NR_getsid: /* getsid(2) */
+ret = do_bsd_getsid(arg1);
+break;
+
+case TARGET_FREEBSD_NR_setsid: /* setsid(2) */
+ret = do_bsd_setsid();
+break;
+
+case TARGET_FREEBSD_NR_issetugid: /* issetugid(2) */
+ret = do_bsd_issetugid();
+break;
+
 
 /*
  * File system calls.
-- 
2.41.0




[PULL 08/51] bsd-user: Implement host_to_target_rusage and host_to_target_wrusage.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-9-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c | 54 +
 1 file changed, 54 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 19e39a2f764..aa386ff4820 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -48,3 +48,57 @@ abi_llong host_to_target_rlim(rlim_t rlim)
 return tswap64(rlim);
 }
 
+void h2g_rusage(const struct rusage *rusage,
+struct target_freebsd_rusage *target_rusage)
+{
+__put_user(rusage->ru_utime.tv_sec, _rusage->ru_utime.tv_sec);
+__put_user(rusage->ru_utime.tv_usec, _rusage->ru_utime.tv_usec);
+
+__put_user(rusage->ru_stime.tv_sec, _rusage->ru_stime.tv_sec);
+__put_user(rusage->ru_stime.tv_usec, _rusage->ru_stime.tv_usec);
+
+__put_user(rusage->ru_maxrss, _rusage->ru_maxrss);
+__put_user(rusage->ru_idrss, _rusage->ru_idrss);
+__put_user(rusage->ru_idrss, _rusage->ru_idrss);
+__put_user(rusage->ru_isrss, _rusage->ru_isrss);
+__put_user(rusage->ru_minflt, _rusage->ru_minflt);
+__put_user(rusage->ru_majflt, _rusage->ru_majflt);
+__put_user(rusage->ru_nswap, _rusage->ru_nswap);
+__put_user(rusage->ru_inblock, _rusage->ru_inblock);
+__put_user(rusage->ru_oublock, _rusage->ru_oublock);
+__put_user(rusage->ru_msgsnd, _rusage->ru_msgsnd);
+__put_user(rusage->ru_msgrcv, _rusage->ru_msgrcv);
+__put_user(rusage->ru_nsignals, _rusage->ru_nsignals);
+__put_user(rusage->ru_nvcsw, _rusage->ru_nvcsw);
+__put_user(rusage->ru_nivcsw, _rusage->ru_nivcsw);
+}
+
+abi_long host_to_target_rusage(abi_ulong target_addr,
+const struct rusage *rusage)
+{
+struct target_freebsd_rusage *target_rusage;
+
+if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) {
+return -TARGET_EFAULT;
+}
+h2g_rusage(rusage, target_rusage);
+unlock_user_struct(target_rusage, target_addr, 1);
+
+return 0;
+}
+
+abi_long host_to_target_wrusage(abi_ulong target_addr,
+const struct __wrusage *wrusage)
+{
+struct target_freebsd__wrusage *target_wrusage;
+
+if (!lock_user_struct(VERIFY_WRITE, target_wrusage, target_addr, 0)) {
+return -TARGET_EFAULT;
+}
+h2g_rusage(>wru_self, _wrusage->wru_self);
+h2g_rusage(>wru_children, _wrusage->wru_children);
+unlock_user_struct(target_wrusage, target_addr, 1);
+
+return 0;
+}
+
-- 
2.41.0




[PULL 33/51] bsd-user: Implement shm_open2(2) system call

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-6-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-misc.h| 46 +++
 bsd-user/freebsd/os-syscall.c | 22 -
 2 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/bsd-user/freebsd/os-misc.h b/bsd-user/freebsd/os-misc.h
index 8436ccb2f7d..d5e8b5484c8 100644
--- a/bsd-user/freebsd/os-misc.h
+++ b/bsd-user/freebsd/os-misc.h
@@ -24,5 +24,51 @@
 #include 
 #include 
 
+/*
+ * shm_open2 isn't exported, but the __sys_ alias is. We can use either for the
+ * static version, but to dynamically link we have to use the sys version.
+ */
+int __sys_shm_open2(const char *path, int flags, mode_t mode, int shmflags,
+const char *);
+
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
+/* shm_open2(2) */
+static inline abi_long do_freebsd_shm_open2(abi_ulong pathptr, abi_ulong flags,
+abi_long mode, abi_ulong shmflags, abi_ulong nameptr)
+{
+int ret;
+void *uname, *upath;
+
+if (pathptr == (uintptr_t)SHM_ANON) {
+upath = SHM_ANON;
+} else {
+upath = lock_user_string(pathptr);
+if (upath == NULL) {
+return -TARGET_EFAULT;
+}
+}
+
+uname = NULL;
+if (nameptr != 0) {
+uname = lock_user_string(nameptr);
+if (uname == NULL) {
+unlock_user(upath, pathptr, 0);
+return -TARGET_EFAULT;
+}
+}
+ret = get_errno(__sys_shm_open2(upath,
+target_to_host_bitmask(flags, fcntl_flags_tbl), mode,
+target_to_host_bitmask(shmflags, shmflag_flags_tbl), uname));
+
+if (upath != SHM_ANON) {
+unlock_user(upath, pathptr, 0);
+}
+if (uname != NULL) {
+unlock_user(uname, nameptr, 0);
+}
+return ret;
+}
+#endif /* __FreeBSD_version >= 1300048 */
+
 
 #endif /* OS_MISC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index d04712f0a7e..122e186b501 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -33,18 +33,14 @@
 #include "signal-common.h"
 #include "user/syscall-trace.h"
 
+/* BSD independent syscall shims */
 #include "bsd-file.h"
 #include "bsd-proc.h"
 
 /* BSD dependent syscall shims */
 #include "os-stat.h"
 #include "os-proc.h"
-
-/* used in os-proc */
-safe_syscall4(pid_t, wait4, pid_t, wpid, int *, status, int, options,
-struct rusage *, rusage);
-safe_syscall6(pid_t, wait6, idtype_t, idtype, id_t, id, int *, status, int,
-options, struct __wrusage *, wrusage, siginfo_t *, infop);
+#include "os-misc.h"
 
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
@@ -65,6 +61,12 @@ safe_syscall3(ssize_t, writev, int, fd, const struct iovec 
*, iov, int, iovcnt);
 safe_syscall4(ssize_t, pwritev, int, fd, const struct iovec *, iov, int, 
iovcnt,
 off_t, offset);
 
+/* used in os-proc */
+safe_syscall4(pid_t, wait4, pid_t, wpid, int *, status, int, options,
+struct rusage *, rusage);
+safe_syscall6(pid_t, wait6, idtype_t, idtype, id_t, id, int *, status, int,
+options, struct __wrusage *, wrusage, siginfo_t *, infop);
+
 void target_set_brk(abi_ulong new_brk)
 {
 }
@@ -796,6 +798,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_freebsd_fcntl(arg1, arg2, arg3);
 break;
 
+/*
+ * Memory management system calls.
+ */
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
+case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
+ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
+break;
+#endif
 
 /*
  * sys{ctl, arch, call}
-- 
2.41.0




[PULL 36/51] bsd-user: Implement target_set_brk function in bsd-mem.c instead of os-syscall.c

2023-10-03 Thread Warner Losh
From: Stacey Son 

The definitions and variables names matches the corresponding ones in
linux-user/syscall.c, for making later implementation of do_obreak easier

Co-authored-by: Mikaël Urankar 
Signed-off-by: Mikaël Urankar 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-9-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c| 32 
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
index e69de29bb2d..8834ab2e588 100644
--- a/bsd-user/bsd-mem.c
+++ b/bsd-user/bsd-mem.c
@@ -0,0 +1,32 @@
+/*
+ *  memory management system conversion routines
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, see .
+ */
+#include "qemu/osdep.h"
+#include "qemu.h"
+#include "qemu-bsd.h"
+
+struct bsd_shm_regions bsd_shm_regions[N_BSD_SHM_REGIONS];
+
+abi_ulong target_brk;
+abi_ulong initial_target_brk;
+
+void target_set_brk(abi_ulong new_brk)
+{
+target_brk = TARGET_PAGE_ALIGN(new_brk);
+initial_target_brk = target_brk;
+}
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 5fb42b2c218..c9d34b59bbe 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -67,10 +67,6 @@ safe_syscall4(pid_t, wait4, pid_t, wpid, int *, status, int, 
options,
 safe_syscall6(pid_t, wait6, idtype_t, idtype, id_t, id, int *, status, int,
 options, struct __wrusage *, wrusage, siginfo_t *, infop);
 
-void target_set_brk(abi_ulong new_brk)
-{
-}
-
 /*
  * errno conversion.
  */
-- 
2.41.0




[PULL 24/51] bsd-user: Implement setloginclass(2) and getloginclass(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-25-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 32 
 bsd-user/freebsd/os-syscall.c |  8 
 2 files changed, 40 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 04bce755e58..2eaba141dcd 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -130,4 +130,36 @@ static inline abi_long do_freebsd_wait6(void *cpu_env, 
abi_long idtype,
 return ret;
 }
 
+/* setloginclass(2) */
+static inline abi_long do_freebsd_setloginclass(abi_ulong arg1)
+{
+abi_long ret;
+void *p;
+
+p = lock_user_string(arg1);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(setloginclass(p));
+unlock_user(p, arg1, 0);
+
+return ret;
+}
+
+/* getloginclass(2) */
+static inline abi_long do_freebsd_getloginclass(abi_ulong arg1, abi_ulong arg2)
+{
+abi_long ret;
+void *p;
+
+p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(getloginclass(p, arg2));
+unlock_user(p, arg1, arg2);
+
+return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 55e68e48159..d614409e694 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -375,6 +375,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_ktrace(arg1, arg2, arg3, arg4);
 break;
 
+case TARGET_FREEBSD_NR_setloginclass: /* setloginclass(2) */
+ret = do_freebsd_setloginclass(arg1);
+break;
+
+case TARGET_FREEBSD_NR_getloginclass: /* getloginclass(2) */
+ret = do_freebsd_getloginclass(arg1, arg2);
+break;
+
 case TARGET_FREEBSD_NR_utrace: /* utrace(2) */
 ret = do_bsd_utrace(arg1, arg2);
 break;
-- 
2.41.0




[PULL 48/51] bsd-user: Implement shm_unlink(2) and shmget(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-21-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 23 +++
 bsd-user/freebsd/os-syscall.c |  8 
 2 files changed, 31 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index f8dc943c234..c362cc07a30 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -282,4 +282,27 @@ static inline abi_long do_bsd_shm_open(abi_ulong arg1, 
abi_long arg2,
 return ret;
 }
 
+/* shm_unlink(2) */
+static inline abi_long do_bsd_shm_unlink(abi_ulong arg1)
+{
+int ret;
+void *p;
+
+p = lock_user_string(arg1);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(shm_unlink(p)); /* XXX path(p)? */
+unlock_user(p, arg1, 0);
+
+return ret;
+}
+
+/* shmget(2) */
+static inline abi_long do_bsd_shmget(abi_long arg1, abi_ulong arg2,
+abi_long arg3)
+{
+return get_errno(shmget(arg1, arg2, arg3));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 0d4c3118f0d..4f67677eb92 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -859,6 +859,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 break;
 #endif
 
+case TARGET_FREEBSD_NR_shm_unlink: /* shm_unlink(2) */
+ret = do_bsd_shm_unlink(arg1);
+break;
+
+case TARGET_FREEBSD_NR_shmget: /* shmget(2) */
+ret = do_bsd_shmget(arg1, arg2, arg3);
+break;
+
 /*
  * Misc
  */
-- 
2.41.0




[PULL 05/51] bsd-user: add extern declarations for bsd-proc.c conversion functions

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-6-kariem.taha...@gmail.com>
---
 bsd-user/qemu-bsd.h | 38 ++
 1 file changed, 38 insertions(+)
 create mode 100644 bsd-user/qemu-bsd.h

diff --git a/bsd-user/qemu-bsd.h b/bsd-user/qemu-bsd.h
new file mode 100644
index 000..b93a0b7fd5b
--- /dev/null
+++ b/bsd-user/qemu-bsd.h
@@ -0,0 +1,38 @@
+/*
+ *  BSD conversion extern declarations
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, see .
+ */
+
+#ifndef QEMU_BSD_H
+#define QEMU_BSD_H
+
+#include 
+#include 
+
+/* bsd-proc.c */
+int target_to_host_resource(int code);
+rlim_t target_to_host_rlim(abi_llong target_rlim);
+abi_llong host_to_target_rlim(rlim_t rlim);
+abi_long host_to_target_rusage(abi_ulong target_addr,
+const struct rusage *rusage);
+abi_long host_to_target_wrusage(abi_ulong target_addr,
+const struct __wrusage *wrusage);
+int host_to_target_waitstatus(int status);
+void h2g_rusage(const struct rusage *rusage,
+struct target_freebsd_rusage *target_rusage);
+
+#endif /* QEMU_BSD_H */
-- 
2.41.0




[PULL 39/51] bsd-user: Introduce bsd-mem.h to the source tree

2023-10-03 Thread Warner Losh
From: Stacey Son 

Preserve the copyright notice and help with the 'Author' info for
subsequent changes to the file.

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-12-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 64 +++
 bsd-user/freebsd/os-syscall.c |  1 +
 2 files changed, 65 insertions(+)
 create mode 100644 bsd-user/bsd-mem.h

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
new file mode 100644
index 000..d865e0807d8
--- /dev/null
+++ b/bsd-user/bsd-mem.h
@@ -0,0 +1,64 @@
+/*
+ *  memory management system call shims and definitions
+ *
+ *  Copyright (c) 2013-15 Stacey D. Son
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, see .
+ */
+
+/*
+ * Copyright (c) 1982, 1986, 1993
+ *  The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ *may be used to endorse or promote products derived from this software
+ *without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef BSD_USER_BSD_MEM_H
+#define BSD_USER_BSD_MEM_H
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "qemu-bsd.h"
+
+extern struct bsd_shm_regions bsd_shm_regions[];
+extern abi_ulong target_brk;
+extern abi_ulong initial_target_brk;
+
+#endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index c9d34b59bbe..7887ad4c0c6 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -35,6 +35,7 @@
 
 /* BSD independent syscall shims */
 #include "bsd-file.h"
+#include "bsd-mem.h"
 #include "bsd-proc.h"
 
 /* BSD dependent syscall shims */
-- 
2.41.0




Re: [PATCH v3 08/14] Introduce machine property "audiodev"

2023-10-03 Thread Bernhard Beschow



Am 29. September 2023 08:51:01 UTC schrieb Paolo Bonzini :
>From: Martin Kletzander 
>
>Many machine types have default audio devices with no way to set the underlying
>audiodev.  Instead of adding an option for each and every one of them, this new
>property can be used as a default during machine initialisation when creating
>such devices.
>
>Signed-off-by: Martin Kletzander 
>[Make the property optional, instead of including it in all machines. - Paolo]
>Signed-off-by: Paolo Bonzini 
>---
> hw/core/machine.c   | 33 +
> include/hw/boards.h |  9 +
> 2 files changed, 42 insertions(+)
>
>diff --git a/hw/core/machine.c b/hw/core/machine.c
>index cb38b8cf4cb..6aa49c8d4f1 100644
>--- a/hw/core/machine.c
>+++ b/hw/core/machine.c
>@@ -39,6 +39,7 @@
> #include "hw/virtio/virtio.h"
> #include "hw/virtio/virtio-pci.h"
> #include "hw/virtio/virtio-net.h"
>+#include "audio/audio.h"
> 
> GlobalProperty hw_compat_8_1[] = {};
> const size_t hw_compat_8_1_len = G_N_ELEMENTS(hw_compat_8_1);
>@@ -686,6 +687,26 @@ bool device_type_is_dynamic_sysbus(MachineClass *mc, 
>const char *type)
> return allowed;
> }
> 
>+static char *machine_get_audiodev(Object *obj, Error **errp)
>+{
>+MachineState *ms = MACHINE(obj);
>+
>+return g_strdup(ms->audiodev);
>+}
>+
>+static void machine_set_audiodev(Object *obj, const char *value,
>+ Error **errp)
>+{
>+MachineState *ms = MACHINE(obj);
>+
>+if (!audio_state_by_name(value, errp)) {
>+return;
>+}
>+
>+g_free(ms->audiodev);
>+ms->audiodev = g_strdup(value);
>+}
>+
> HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
> {
> int i;
>@@ -931,6 +952,17 @@ out_free:
> qapi_free_BootConfiguration(config);
> }
> 
>+void machine_add_audiodev_property(MachineClass *mc)
>+{
>+ObjectClass *oc = OBJECT_CLASS(mc);
>+
>+object_class_property_add_str(oc, "audiodev",
>+  machine_get_audiodev,
>+  machine_set_audiodev);
>+object_class_property_set_description(oc, "audiodev",
>+  "Audiodev to use for default 
>machine devices");
>+}
>+
> static void machine_class_init(ObjectClass *oc, void *data)
> {
> MachineClass *mc = MACHINE_CLASS(oc);
>@@ -1136,6 +1168,7 @@ static void machine_finalize(Object *obj)
> g_free(ms->device_memory);
> g_free(ms->nvdimms_state);
> g_free(ms->numa_state);
>+g_free(ms->audiodev);
> }
> 
> bool machine_usb(MachineState *machine)
>diff --git a/include/hw/boards.h b/include/hw/boards.h
>index 6c67af196a3..55a64a13fdf 100644
>--- a/include/hw/boards.h
>+++ b/include/hw/boards.h
>@@ -24,6 +24,7 @@ OBJECT_DECLARE_TYPE(MachineState, MachineClass, MACHINE)
> 
> extern MachineState *current_machine;
> 
>+void machine_add_audiodev_property(MachineClass *mc);

I'm a bit late now since the code is already in master but I wonder if this 
function should've been named machine_*class_*add_audiodev_property(). At least 
similar functions in this header suggest so.

Best regards,
Bernhard

> void machine_run_board_init(MachineState *machine, const char *mem_path, 
> Error **errp);
> bool machine_usb(MachineState *machine);
> int machine_phandle_start(MachineState *machine);
>@@ -358,6 +359,14 @@ struct MachineState {
> MemoryRegion *ram;
> DeviceMemoryState *device_memory;
> 
>+/*
>+ * Included in MachineState for simplicity, but not supported
>+ * unless machine_add_audiodev_property is called.  Boards
>+ * that have embedded audio devices can call it from the
>+ * machine init function and forward the property to the device.
>+ */
>+char *audiodev;
>+
> ram_addr_t ram_size;
> ram_addr_t maxram_size;
> uint64_t   ram_slots;



[PULL 32/51] bsd-user: Introduce freebsd/os-misc.h to the source tree

2023-10-03 Thread Warner Losh
From: Stacey Son 

To preserve the copyright notice and help with the 'Author' info for
subsequent changes to the file.

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-5-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-misc.h | 28 
 1 file changed, 28 insertions(+)
 create mode 100644 bsd-user/freebsd/os-misc.h

diff --git a/bsd-user/freebsd/os-misc.h b/bsd-user/freebsd/os-misc.h
new file mode 100644
index 000..8436ccb2f7d
--- /dev/null
+++ b/bsd-user/freebsd/os-misc.h
@@ -0,0 +1,28 @@
+/*
+ *  miscellaneous FreeBSD system call shims
+ *
+ *  Copyright (c) 2013-14 Stacey D. Son
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, see .
+ */
+
+#ifndef OS_MISC_H
+#define OS_MISC_H
+
+#include 
+#include 
+#include 
+
+
+#endif /* OS_MISC_H */
-- 
2.41.0




[PULL 03/51] bsd-user: Implement host_to_target_siginfo.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Used in wait6 system call

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-4-kariem.taha...@gmail.com>
---
 bsd-user/signal-common.h | 1 +
 bsd-user/signal.c| 6 ++
 2 files changed, 7 insertions(+)

diff --git a/bsd-user/signal-common.h b/bsd-user/signal-common.h
index c044e811653..77d7c7a78b7 100644
--- a/bsd-user/signal-common.h
+++ b/bsd-user/signal-common.h
@@ -35,6 +35,7 @@ int do_sigaction(int sig, const struct target_sigaction *act,
 abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
 long do_sigreturn(CPUArchState *env, abi_ulong addr);
 void force_sig_fault(int sig, int code, abi_ulong addr);
+void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
 int host_to_target_signal(int sig);
 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s);
 void process_pending_signals(CPUArchState *env);
diff --git a/bsd-user/signal.c b/bsd-user/signal.c
index b6beab659e1..ea82241b70b 100644
--- a/bsd-user/signal.c
+++ b/bsd-user/signal.c
@@ -311,6 +311,12 @@ static void tswap_siginfo(target_siginfo_t *tinfo, const 
target_siginfo_t *info)
 }
 }
 
+void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
+{
+host_to_target_siginfo_noswap(tinfo, info);
+tswap_siginfo(tinfo, tinfo);
+}
+
 int block_signals(void)
 {
 TaskState *ts = (TaskState *)thread_cpu->opaque;
-- 
2.41.0




[PULL 10/51] bsd-user: Get number of cpus.

2023-10-03 Thread Warner Losh
From: Kyle Evans 

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-11-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c | 24 
 bsd-user/bsd-proc.h |  2 ++
 2 files changed, 26 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 19f6efe1f78..ca3c1bf94f4 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -119,3 +119,27 @@ int host_to_target_waitstatus(int status)
 return status;
 }
 
+int bsd_get_ncpu(void)
+{
+int ncpu = -1;
+cpuset_t mask;
+
+CPU_ZERO();
+
+if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
+   ) == 0) {
+ncpu = CPU_COUNT();
+}
+
+if (ncpu == -1) {
+ncpu = sysconf(_SC_NPROCESSORS_ONLN);
+}
+
+if (ncpu == -1) {
+gemu_log("XXX Missing bsd_get_ncpu() implementation\n");
+ncpu = 1;
+}
+
+return ncpu;
+}
+
diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 048773a75dd..b6225e520ea 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -26,6 +26,8 @@
 #include "gdbstub/syscalls.h"
 #include "qemu/plugin.h"
 
+int bsd_get_ncpu(void);
+
 /* exit(2) */
 static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
 {
-- 
2.41.0




[PULL 21/51] bsd-user: Implement procctl(2) along with necessary conversion functions.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Implement t2h_procctl_cmd, h2t_reaper_status, h2t_reaper_pidinfo and h2t/t2h 
reaper_kill conversion functions.

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-22-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.c| 223 ++
 bsd-user/freebsd/os-syscall.c |   3 +
 2 files changed, 226 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index 12d78b7fc98..4e67ae4d56c 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -255,3 +255,226 @@ execve_end:
 return ret;
 }
 
+#include 
+
+static abi_long
+t2h_procctl_cmd(int target_cmd, int *host_cmd)
+{
+switch (target_cmd) {
+case TARGET_PROC_SPROTECT:
+*host_cmd = PROC_SPROTECT;
+break;
+
+case TARGET_PROC_REAP_ACQUIRE:
+*host_cmd = PROC_REAP_ACQUIRE;
+break;
+
+case TARGET_PROC_REAP_RELEASE:
+*host_cmd = PROC_REAP_RELEASE;
+break;
+
+case TARGET_PROC_REAP_STATUS:
+*host_cmd = PROC_REAP_STATUS;
+break;
+
+case TARGET_PROC_REAP_KILL:
+*host_cmd = PROC_REAP_KILL;
+break;
+
+default:
+return -TARGET_EINVAL;
+}
+
+return 0;
+}
+
+static abi_long
+h2t_reaper_status(struct procctl_reaper_status *host_rs,
+abi_ulong target_rs_addr)
+{
+struct target_procctl_reaper_status *target_rs;
+
+if (!lock_user_struct(VERIFY_WRITE, target_rs, target_rs_addr, 0)) {
+return -TARGET_EFAULT;
+}
+__put_user(host_rs->rs_flags, _rs->rs_flags);
+__put_user(host_rs->rs_children, _rs->rs_children);
+__put_user(host_rs->rs_descendants, _rs->rs_descendants);
+__put_user(host_rs->rs_reaper, _rs->rs_reaper);
+__put_user(host_rs->rs_pid, _rs->rs_pid);
+unlock_user_struct(target_rs, target_rs_addr, 1);
+
+return 0;
+}
+
+static abi_long
+t2h_reaper_kill(abi_ulong target_rk_addr, struct procctl_reaper_kill *host_rk)
+{
+struct target_procctl_reaper_kill *target_rk;
+
+if (!lock_user_struct(VERIFY_READ, target_rk, target_rk_addr, 1)) {
+return -TARGET_EFAULT;
+}
+__get_user(host_rk->rk_sig, _rk->rk_sig);
+__get_user(host_rk->rk_flags, _rk->rk_flags);
+__get_user(host_rk->rk_subtree, _rk->rk_subtree);
+__get_user(host_rk->rk_killed, _rk->rk_killed);
+__get_user(host_rk->rk_fpid, _rk->rk_fpid);
+unlock_user_struct(target_rk, target_rk_addr, 0);
+
+return 0;
+}
+
+static abi_long
+h2t_reaper_kill(struct procctl_reaper_kill *host_rk, abi_ulong target_rk_addr)
+{
+struct target_procctl_reaper_kill *target_rk;
+
+if (!lock_user_struct(VERIFY_WRITE, target_rk, target_rk_addr, 0)) {
+return -TARGET_EFAULT;
+}
+__put_user(host_rk->rk_sig, _rk->rk_sig);
+__put_user(host_rk->rk_flags, _rk->rk_flags);
+__put_user(host_rk->rk_subtree, _rk->rk_subtree);
+__put_user(host_rk->rk_killed, _rk->rk_killed);
+__put_user(host_rk->rk_fpid, _rk->rk_fpid);
+unlock_user_struct(target_rk, target_rk_addr, 1);
+
+return 0;
+}
+
+static abi_long
+h2t_procctl_reaper_pidinfo(struct procctl_reaper_pidinfo *host_pi,
+abi_ulong target_pi_addr)
+{
+struct target_procctl_reaper_pidinfo *target_pi;
+
+if (!lock_user_struct(VERIFY_WRITE, target_pi, target_pi_addr, 0)) {
+return -TARGET_EFAULT;
+}
+__put_user(host_pi->pi_pid, _pi->pi_pid);
+__put_user(host_pi->pi_subtree, _pi->pi_subtree);
+__put_user(host_pi->pi_flags, _pi->pi_flags);
+unlock_user_struct(target_pi, target_pi_addr, 1);
+
+return 0;
+}
+
+abi_long
+do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2, abi_ulong arg3,
+   abi_ulong arg4, abi_ulong arg5, abi_ulong arg6)
+{
+abi_long error = 0, target_rp_pids;
+void *data;
+int host_cmd, flags;
+uint32_t u, target_rp_count;
+g_autofree union {
+struct procctl_reaper_status rs;
+struct procctl_reaper_pids rp;
+struct procctl_reaper_kill rk;
+} host;
+struct target_procctl_reaper_pids *target_rp;
+id_t id; /* 64-bit */
+int target_cmd;
+abi_ulong target_arg;
+
+#if TARGET_ABI_BITS == 32
+/* See if we need to align the register pairs. */
+if (regpairs_aligned(cpu_env)) {
+id = (id_t)target_arg64(arg3, arg4);
+target_cmd = (int)arg5;
+target_arg = arg6;
+} else {
+id = (id_t)target_arg64(arg2, arg3);
+target_cmd = (int)arg4;
+target_arg = arg5;
+}
+#else
+id = (id_t)arg2;
+target_cmd = (int)arg3;
+target_arg = arg4;
+#endif
+
+error = t2h_procctl_cmd(target_cmd, _cmd);
+if (error) {
+return error;
+}
+switch (host_cmd) {
+case PROC_SPROTECT:
+data = 
+break;
+
+case PROC_REAP_ACQUIRE:
+case PROC_REAP_RELEASE:
+if (target_arg == 0) {
+data = NULL;
+} else {
+error = -TARGET_EINVAL;
+

[PULL 34/51] bsd-user: Implement shm_rename(2) system call

2023-10-03 Thread Warner Losh
From: Kyle Evans 

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-7-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-misc.h| 24 
 bsd-user/freebsd/os-syscall.c |  6 ++
 2 files changed, 30 insertions(+)

diff --git a/bsd-user/freebsd/os-misc.h b/bsd-user/freebsd/os-misc.h
index d5e8b5484c8..71145764a4d 100644
--- a/bsd-user/freebsd/os-misc.h
+++ b/bsd-user/freebsd/os-misc.h
@@ -70,5 +70,29 @@ static inline abi_long do_freebsd_shm_open2(abi_ulong 
pathptr, abi_ulong flags,
 }
 #endif /* __FreeBSD_version >= 1300048 */
 
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300049
+/* shm_rename(2) */
+static inline abi_long do_freebsd_shm_rename(abi_ulong fromptr, abi_ulong 
toptr,
+abi_ulong flags)
+{
+int ret;
+void *ufrom, *uto;
+
+ufrom = lock_user_string(fromptr);
+if (ufrom == NULL) {
+return -TARGET_EFAULT;
+}
+uto = lock_user_string(toptr);
+if (uto == NULL) {
+unlock_user(ufrom, fromptr, 0);
+return -TARGET_EFAULT;
+}
+ret = get_errno(shm_rename(ufrom, uto, flags));
+unlock_user(ufrom, fromptr, 0);
+unlock_user(uto, toptr, 0);
+
+return ret;
+}
+#endif /* __FreeBSD_version >= 1300049 */
 
 #endif /* OS_MISC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 122e186b501..5fb42b2c218 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -807,6 +807,12 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 break;
 #endif
 
+#if defined(__FreeBSD_version) && __FreeBSD_version >= 1300049
+case TARGET_FREEBSD_NR_shm_rename: /* shm_rename(2) */
+ret = do_freebsd_shm_rename(arg1, arg2, arg3);
+break;
+#endif
+
 /*
  * sys{ctl, arch, call}
  */
-- 
2.41.0




[PULL 13/51] bsd-user: Implement getrusage(2).

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-14-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 13 +
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 17 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index cb7c69acb0c..133c1b0eaf8 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -124,4 +124,17 @@ static inline abi_long do_bsd_getlogin(abi_long arg1, 
abi_long arg2)
 return ret;
 }
 
+/* getrusage(2) */
+static inline abi_long do_bsd_getrusage(abi_long who, abi_ulong target_addr)
+{
+abi_long ret;
+struct rusage rusage;
+
+ret = get_errno(getrusage(who, ));
+if (!is_error(ret)) {
+host_to_target_rusage(target_addr, );
+}
+return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 44cbf52f087..5d8693ed550 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -243,6 +243,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_getlogin(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_getrusage: /* getrusage(2) */
+ret = do_bsd_getrusage(arg1, arg2);
+break;
+
 
 /*
  * File system calls.
-- 
2.41.0




[PULL 37/51] bsd-user: Implement ipc_perm conversion between host and target.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-10-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c | 25 +
 1 file changed, 25 insertions(+)

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
index 8834ab2e588..46cda8eb5ce 100644
--- a/bsd-user/bsd-mem.c
+++ b/bsd-user/bsd-mem.c
@@ -30,3 +30,28 @@ void target_set_brk(abi_ulong new_brk)
 target_brk = TARGET_PAGE_ALIGN(new_brk);
 initial_target_brk = target_brk;
 }
+
+void target_to_host_ipc_perm__locked(struct ipc_perm *host_ip,
+ struct target_ipc_perm *target_ip)
+{
+__get_user(host_ip->cuid, _ip->cuid);
+__get_user(host_ip->cgid, _ip->cgid);
+__get_user(host_ip->uid,  _ip->uid);
+__get_user(host_ip->gid,  _ip->gid);
+__get_user(host_ip->mode, _ip->mode);
+__get_user(host_ip->seq,  _ip->seq);
+__get_user(host_ip->key,  _ip->key);
+}
+
+void host_to_target_ipc_perm__locked(struct target_ipc_perm *target_ip,
+ struct ipc_perm *host_ip)
+{
+__put_user(host_ip->cuid, _ip->cuid);
+__put_user(host_ip->cgid, _ip->cgid);
+__put_user(host_ip->uid,  _ip->uid);
+__put_user(host_ip->gid,  _ip->gid);
+__put_user(host_ip->mode, _ip->mode);
+__put_user(host_ip->seq,  _ip->seq);
+__put_user(host_ip->key,  _ip->key);
+}
+
-- 
2.41.0




[PULL 07/51] bsd-user: Implement target_to_host_rlim and host_to_target_rlim conversion.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-8-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 68410a0aa9d..19e39a2f764 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -38,3 +38,13 @@ int target_to_host_resource(int code)
 return code;
 }
 
+rlim_t target_to_host_rlim(abi_llong target_rlim)
+{
+return tswap64(target_rlim);
+}
+
+abi_llong host_to_target_rlim(rlim_t rlim)
+{
+return tswap64(rlim);
+}
+
-- 
2.41.0




[PULL 12/51] bsd-user: Implement umask(2), setlogin(2) and getlogin(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-13-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 39 +++
 bsd-user/freebsd/os-syscall.c | 12 +++
 2 files changed, 51 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 7b25aa19829..cb7c69acb0c 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -26,6 +26,7 @@
 #include "gdbstub/syscalls.h"
 #include "qemu/plugin.h"
 
+extern int _getlogin(char*, int);
 int bsd_get_ncpu(void);
 
 /* exit(2) */
@@ -85,4 +86,42 @@ static inline abi_long do_bsd_setgroups(abi_long gidsetsize, 
abi_long arg2)
 return get_errno(setgroups(gidsetsize, grouplist));
 }
 
+/* umask(2) */
+static inline abi_long do_bsd_umask(abi_long arg1)
+{
+return get_errno(umask(arg1));
+}
+
+/* setlogin(2) */
+static inline abi_long do_bsd_setlogin(abi_long arg1)
+{
+abi_long ret;
+void *p;
+
+p = lock_user_string(arg1);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(setlogin(p));
+unlock_user(p, arg1, 0);
+
+return ret;
+}
+
+/* getlogin(2) */
+static inline abi_long do_bsd_getlogin(abi_long arg1, abi_long arg2)
+{
+abi_long ret;
+void *p;
+
+p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(_getlogin(p, arg2));
+unlock_user(p, arg1, arg2);
+
+return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 535e6287bde..44cbf52f087 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -231,6 +231,18 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_setgroups(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_umask: /* umask(2) */
+ret = do_bsd_umask(arg1);
+break;
+
+case TARGET_FREEBSD_NR_setlogin: /* setlogin(2) */
+ret = do_bsd_setlogin(arg1);
+break;
+
+case TARGET_FREEBSD_NR_getlogin: /* getlogin(2) */
+ret = do_bsd_getlogin(arg1, arg2);
+break;
+
 
 /*
  * File system calls.
-- 
2.41.0




[PULL 38/51] bsd-user: Implement shmid_ds conversion between host and target.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-11-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.c | 47 ++
 1 file changed, 47 insertions(+)

diff --git a/bsd-user/bsd-mem.c b/bsd-user/bsd-mem.c
index 46cda8eb5ce..2ab1334b700 100644
--- a/bsd-user/bsd-mem.c
+++ b/bsd-user/bsd-mem.c
@@ -43,6 +43,30 @@ void target_to_host_ipc_perm__locked(struct ipc_perm 
*host_ip,
 __get_user(host_ip->key,  _ip->key);
 }
 
+abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
+ abi_ulong target_addr)
+{
+struct target_shmid_ds *target_sd;
+
+if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1)) {
+return -TARGET_EFAULT;
+}
+
+target_to_host_ipc_perm__locked(&(host_sd->shm_perm),
+&(target_sd->shm_perm));
+
+__get_user(host_sd->shm_segsz,  _sd->shm_segsz);
+__get_user(host_sd->shm_lpid,   _sd->shm_lpid);
+__get_user(host_sd->shm_cpid,   _sd->shm_cpid);
+__get_user(host_sd->shm_nattch, _sd->shm_nattch);
+__get_user(host_sd->shm_atime,  _sd->shm_atime);
+__get_user(host_sd->shm_dtime,  _sd->shm_dtime);
+__get_user(host_sd->shm_ctime,  _sd->shm_ctime);
+unlock_user_struct(target_sd, target_addr, 0);
+
+return 0;
+}
+
 void host_to_target_ipc_perm__locked(struct target_ipc_perm *target_ip,
  struct ipc_perm *host_ip)
 {
@@ -55,3 +79,26 @@ void host_to_target_ipc_perm__locked(struct target_ipc_perm 
*target_ip,
 __put_user(host_ip->key,  _ip->key);
 }
 
+abi_long host_to_target_shmid_ds(abi_ulong target_addr,
+ struct shmid_ds *host_sd)
+{
+struct target_shmid_ds *target_sd;
+
+if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0)) {
+return -TARGET_EFAULT;
+}
+
+host_to_target_ipc_perm__locked(&(target_sd->shm_perm),
+&(host_sd->shm_perm));
+
+__put_user(host_sd->shm_segsz,  _sd->shm_segsz);
+__put_user(host_sd->shm_lpid,   _sd->shm_lpid);
+__put_user(host_sd->shm_cpid,   _sd->shm_cpid);
+__put_user(host_sd->shm_nattch, _sd->shm_nattch);
+__put_user(host_sd->shm_atime,  _sd->shm_atime);
+__put_user(host_sd->shm_dtime,  _sd->shm_dtime);
+__put_user(host_sd->shm_ctime,  _sd->shm_ctime);
+unlock_user_struct(target_sd, target_addr, 1);
+
+return 0;
+}
-- 
2.41.0




[PULL 06/51] bsd-user: Implement target_to_host_resource conversion function

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-7-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c  | 40 
 bsd-user/bsd-proc.h  |  4 
 bsd-user/meson.build |  6 ++
 3 files changed, 50 insertions(+)
 create mode 100644 bsd-user/bsd-proc.c

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
new file mode 100644
index 000..68410a0aa9d
--- /dev/null
+++ b/bsd-user/bsd-proc.c
@@ -0,0 +1,40 @@
+/*
+ *  BSD process related system call helpers
+ *
+ *  Copyright (c) 2013-14 Stacey D. Son
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, see .
+ */
+#include "qemu/osdep.h"
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "qemu.h"
+#include "qemu-bsd.h"
+#include "signal-common.h"
+
+#include "bsd-proc.h"
+
+/*
+ * resource/rusage conversion
+ */
+int target_to_host_resource(int code)
+{
+return code;
+}
+
diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index a1061bffb8f..048773a75dd 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -22,6 +22,10 @@
 
 #include 
 
+#include "qemu-bsd.h"
+#include "gdbstub/syscalls.h"
+#include "qemu/plugin.h"
+
 /* exit(2) */
 static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
 {
diff --git a/bsd-user/meson.build b/bsd-user/meson.build
index 5243122fc56..b97fce14722 100644
--- a/bsd-user/meson.build
+++ b/bsd-user/meson.build
@@ -7,6 +7,7 @@ bsd_user_ss = ss.source_set()
 common_user_inc += include_directories('include')
 
 bsd_user_ss.add(files(
+  'bsd-proc.c',
   'bsdload.c',
   'elfload.c',
   'main.c',
@@ -16,6 +17,11 @@ bsd_user_ss.add(files(
   'uaccess.c',
 ))
 
+elf = cc.find_library('elf', required: true)
+procstat = cc.find_library('procstat', required: true)
+kvm = cc.find_library('kvm', required: true)
+bsd_user_ss.add(elf, procstat, kvm)
+
 # Pull in the OS-specific build glue, if any
 subdir(targetos)
 
-- 
2.41.0




[PULL 50/51] bsd-user: Implement shmat(2) and shmdt(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Use `WITH_MMAP_LOCK_GUARD` instead of mmap_lock() and mmap_unlock(),
to match linux-user implementation, according to the following commits:

69fa2708a216df715ba5102a0f98468b540a464e linux-user: Use WITH_MMAP_LOCK_GUARD 
in target_{shmat,shmdt}
ceda5688b650646248f269a992c06b11148c5759 linux-user: Fix shmdt

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Message-Id: <20230925182709.4834-23-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 87 +++
 bsd-user/freebsd/os-syscall.c |  8 
 bsd-user/mmap.c   |  2 +-
 bsd-user/qemu.h   |  1 +
 4 files changed, 97 insertions(+), 1 deletion(-)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index b82f3eaa253..c512a4e3756 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -344,4 +344,91 @@ static inline abi_long do_bsd_shmctl(abi_long shmid, 
abi_long cmd,
 return ret;
 }
 
+/* shmat(2) */
+static inline abi_long do_bsd_shmat(int shmid, abi_ulong shmaddr, int shmflg)
+{
+abi_ulong raddr;
+abi_long ret;
+struct shmid_ds shm_info;
+
+/* Find out the length of the shared memory segment. */
+ret = get_errno(shmctl(shmid, IPC_STAT, _info));
+if (is_error(ret)) {
+/* Can't get the length */
+return ret;
+}
+
+if (!guest_range_valid_untagged(shmaddr, shm_info.shm_segsz)) {
+return -TARGET_EINVAL;
+}
+
+WITH_MMAP_LOCK_GUARD() {
+void *host_raddr;
+
+if (shmaddr) {
+host_raddr = shmat(shmid, (void *)g2h_untagged(shmaddr), shmflg);
+} else {
+abi_ulong mmap_start;
+
+mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
+
+if (mmap_start == -1) {
+return -TARGET_ENOMEM;
+}
+host_raddr = shmat(shmid, g2h_untagged(mmap_start),
+   shmflg | SHM_REMAP);
+}
+
+if (host_raddr == (void *)-1) {
+return get_errno(-1);
+}
+raddr = h2g(host_raddr);
+
+page_set_flags(raddr, raddr + shm_info.shm_segsz - 1,
+   PAGE_VALID | PAGE_RESET | PAGE_READ |
+   (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE));
+
+for (int i = 0; i < N_BSD_SHM_REGIONS; i++) {
+if (bsd_shm_regions[i].start == 0) {
+bsd_shm_regions[i].start = raddr;
+bsd_shm_regions[i].size = shm_info.shm_segsz;
+break;
+}
+}
+}
+
+return raddr;
+}
+
+/* shmdt(2) */
+static inline abi_long do_bsd_shmdt(abi_ulong shmaddr)
+{
+abi_long ret;
+
+WITH_MMAP_LOCK_GUARD() {
+int i;
+
+for (i = 0; i < N_BSD_SHM_REGIONS; ++i) {
+if (bsd_shm_regions[i].start == shmaddr) {
+break;
+}
+}
+
+if (i == N_BSD_SHM_REGIONS) {
+return -TARGET_EINVAL;
+}
+
+ret = get_errno(shmdt(g2h_untagged(shmaddr)));
+if (ret == 0) {
+abi_ulong size = bsd_shm_regions[i].size;
+
+bsd_shm_regions[i].start = 0;
+page_set_flags(shmaddr, shmaddr + size - 1, 0);
+mmap_reserve(shmaddr, size);
+}
+}
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 0512d41db7c..39e66312da1 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -871,6 +871,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_shmctl(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_shmat: /* shmat(2) */
+ret = do_bsd_shmat(arg1, arg2, arg3);
+break;
+
+case TARGET_FREEBSD_NR_shmdt: /* shmdt(2) */
+ret = do_bsd_shmdt(arg1);
+break;
+
 /*
  * Misc
  */
diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index 8e148a2ea3e..3ef11b28079 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -636,7 +636,7 @@ fail:
 return -1;
 }
 
-static void mmap_reserve(abi_ulong start, abi_ulong size)
+void mmap_reserve(abi_ulong start, abi_ulong size)
 {
 abi_ulong real_start;
 abi_ulong real_end;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 6047805ae38..dc842fffa7d 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -233,6 +233,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong 
old_size,
 int target_msync(abi_ulong start, abi_ulong len, int flags);
 extern abi_ulong mmap_next_start;
 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
+void mmap_reserve(abi_ulong start, abi_ulong size);
 void TSA_NO_TSA mmap_fork_start(void);
 void TSA_NO_TSA mmap_fork_end(int child);
 
-- 
2.41.0




[PULL 42/51] bsd-user: Implement msync(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Co-authored-by: Kyle Evans 
Signed-off-by: Stacey Son 
Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-15-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 11 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 15 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 0f9e4a1d4be..5e885823a79 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -88,4 +88,15 @@ static inline abi_long do_bsd_mprotect(abi_long arg1, 
abi_long arg2,
 return get_errno(target_mprotect(arg1, arg2, arg3));
 }
 
+/* msync(2) */
+static inline abi_long do_bsd_msync(abi_long addr, abi_long len, abi_long 
flags)
+{
+if (!guest_range_valid_untagged(addr, len)) {
+/* It seems odd, but POSIX wants this to be ENOMEM */
+return -TARGET_ENOMEM;
+}
+
+return get_errno(msync(g2h_untagged(addr), len, flags));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 2d8f1a953b2..2525e0bc316 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -811,6 +811,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_mprotect(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_msync: /* msync(2) */
+ret = do_bsd_msync(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 41/51] bsd-user: Implement mprotect(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-14-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 7 +++
 bsd-user/freebsd/os-syscall.c | 4 
 2 files changed, 11 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 76b504f70c5..0f9e4a1d4be 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -81,4 +81,11 @@ static inline abi_long do_bsd_munmap(abi_long arg1, abi_long 
arg2)
 return get_errno(target_munmap(arg1, arg2));
 }
 
+/* mprotect(2) */
+static inline abi_long do_bsd_mprotect(abi_long arg1, abi_long arg2,
+abi_long arg3)
+{
+return get_errno(target_mprotect(arg1, arg2, arg3));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index b03837d032a..2d8f1a953b2 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -807,6 +807,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_munmap(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_mprotect: /* mprotect(2) */
+ret = do_bsd_mprotect(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 20/51] bsd-user: Implement freebsd_exec_common, used in implementing execve/fexecve.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-21-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.c | 181 -
 bsd-user/main.c|   2 +-
 bsd-user/qemu.h|   1 +
 3 files changed, 180 insertions(+), 4 deletions(-)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index 2603c5c6538..12d78b7fc98 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -30,9 +30,7 @@ struct kinfo_proc;
  * Get the filename for the given file descriptor.
  * Note that this may return NULL (fail) if no longer cached in the kernel.
  */
-char *
-get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len);
-char *
+static char *
 get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
 {
 char *ret = NULL;
@@ -80,3 +78,180 @@ out:
 return ret;
 }
 
+/*
+ * execve/fexecve
+ */
+abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
+abi_ulong guest_envp, int do_fexec)
+{
+char **argp, **envp, **qargp, **qarg1, **qarg0, **qargend;
+int argc, envc;
+abi_ulong gp;
+abi_ulong addr;
+char **q;
+int total_size = 0;
+void *p;
+abi_long ret;
+
+argc = 0;
+for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
+if (get_user_ual(addr, gp)) {
+return -TARGET_EFAULT;
+}
+if (!addr) {
+break;
+}
+argc++;
+}
+envc = 0;
+for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
+if (get_user_ual(addr, gp)) {
+return -TARGET_EFAULT;
+}
+if (!addr) {
+break;
+}
+envc++;
+}
+
+qarg0 = argp = g_new0(char *, argc + 9);
+/* save the first agrument for the emulator */
+*argp++ = (char *)getprogname();
+qargp = argp;
+*argp++ = (char *)getprogname();
+qarg1 = argp;
+envp = g_new0(char *, envc + 1);
+for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
+if (get_user_ual(addr, gp)) {
+ret = -TARGET_EFAULT;
+goto execve_end;
+}
+if (!addr) {
+break;
+}
+*q = lock_user_string(addr);
+if (*q == NULL) {
+ret = -TARGET_EFAULT;
+goto execve_end;
+}
+total_size += strlen(*q) + 1;
+}
+*q++ = NULL;
+qargend = q;
+
+for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
+if (get_user_ual(addr, gp)) {
+ret = -TARGET_EFAULT;
+goto execve_end;
+}
+if (!addr) {
+break;
+}
+*q = lock_user_string(addr);
+if (*q == NULL) {
+ret = -TARGET_EFAULT;
+goto execve_end;
+}
+total_size += strlen(*q) + 1;
+}
+*q = NULL;
+
+/*
+ * This case will not be caught by the host's execve() if its
+ * page size is bigger than the target's.
+ */
+if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
+ret = -TARGET_E2BIG;
+goto execve_end;
+}
+
+if (do_fexec) {
+if (((int)path_or_fd > 0 &&
+is_target_elf_binary((int)path_or_fd)) == 1) {
+char execpath[PATH_MAX];
+
+/*
+ * The executable is an elf binary for the target
+ * arch.  execve() it using the emulator if we can
+ * determine the filename path from the fd.
+ */
+if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath,
+sizeof(execpath)) != NULL) {
+memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
+qarg1[1] = qarg1[0];
+qarg1[0] = (char *)"-0";
+qarg1 += 2;
+qargend += 2;
+*qarg1 = execpath;
+#ifndef DONT_INHERIT_INTERP_PREFIX
+memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
+*qarg1++ = (char *)"-L";
+*qarg1++ = (char *)interp_prefix;
+#endif
+ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
+} else {
+/* Getting the filename path failed. */
+ret = -TARGET_EBADF;
+goto execve_end;
+}
+} else {
+ret = get_errno(fexecve((int)path_or_fd, argp, envp));
+}
+} else {
+int fd;
+
+p = lock_user_string(path_or_fd);
+if (p == NULL) {
+ret = -TARGET_EFAULT;
+goto execve_end;
+}
+
+/*
+ * Check the header and see if it a target elf binary.  If so
+ * then execute using qemu user mode emulator.
+ */
+fd = open(p, O_RDONLY | O_CLOEXEC);
+if (fd > 0 && is_target_elf_binary(fd) == 1) {
+close(fd);
+/* execve() as a target 

[PULL 18/51] bsd-user: Implement getpriority(2) and setpriority(2).

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-19-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 24 
 bsd-user/freebsd/os-syscall.c |  8 
 2 files changed, 32 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 2c1a9ae22fa..9a8912361f6 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -390,4 +390,28 @@ static inline abi_long do_bsd_ptrace(abi_long arg1, 
abi_long arg2,
 return -TARGET_ENOSYS;
 }
 
+/* getpriority(2) */
+static inline abi_long do_bsd_getpriority(abi_long which, abi_long who)
+{
+abi_long ret;
+/*
+ * Note that negative values are valid for getpriority, so we must
+ * differentiate based on errno settings.
+ */
+errno = 0;
+ret = getpriority(which, who);
+if (ret == -1 && errno != 0) {
+return -host_to_target_errno(errno);
+}
+
+return ret;
+}
+
+/* setpriority(2) */
+static inline abi_long do_bsd_setpriority(abi_long which, abi_long who,
+  abi_long prio)
+{
+return get_errno(setpriority(which, who, prio));
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 1a760b13808..71a2657dd0f 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -359,6 +359,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_ptrace(arg1, arg2, arg3, arg4);
 break;
 
+case TARGET_FREEBSD_NR_getpriority: /* getpriority(2) */
+ret = do_bsd_getpriority(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_setpriority: /* setpriority(2) */
+ret = do_bsd_setpriority(arg1, arg2, arg3);
+break;
+
 
 /*
  * File system calls.
-- 
2.41.0




[PULL 45/51] bsd-user: Implement mincore(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-18-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 23 +++
 bsd-user/freebsd/os-syscall.c |  4 
 2 files changed, 27 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index b00ab3aed8e..0c8d96d9a43 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -189,4 +189,27 @@ static inline abi_long do_bsd_minherit(abi_long addr, 
abi_long len,
 return get_errno(minherit(g2h_untagged(addr), len, inherit));
 }
 
+/* mincore(2) */
+static inline abi_long do_bsd_mincore(abi_ulong target_addr, abi_ulong len,
+abi_ulong target_vec)
+{
+abi_long ret;
+void *p;
+abi_ulong vec_len = DIV_ROUND_UP(len, TARGET_PAGE_SIZE);
+
+if (!guest_range_valid_untagged(target_addr, len)
+|| !page_check_range(target_addr, len, PAGE_VALID)) {
+return -TARGET_EFAULT;
+}
+
+p = lock_user(VERIFY_WRITE, target_vec, vec_len, 0);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+ret = get_errno(mincore(g2h_untagged(target_addr), len, p));
+unlock_user(p, target_vec, vec_len);
+
+return ret;
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index b8c44cea0ff..f054241cd62 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -839,6 +839,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_minherit(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_mincore: /* mincore(2) */
+ret = do_bsd_mincore(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 26/51] bsd-user: Implement fork(2) and vfork(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-27-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 34 ++
 bsd-user/freebsd/os-syscall.c |  8 
 2 files changed, 42 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 42bdd61904b..7b2e6a9f796 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -185,4 +185,38 @@ static inline abi_long do_freebsd___setugid(abi_long arg1)
 return -TARGET_ENOSYS;
 }
 
+/* fork(2) */
+static inline abi_long do_freebsd_fork(void *cpu_env)
+{
+abi_long ret;
+abi_ulong child_flag;
+
+fork_start();
+ret = fork();
+if (ret == 0) {
+/* child */
+child_flag = 1;
+target_cpu_clone_regs(cpu_env, 0);
+} else {
+/* parent */
+child_flag = 0;
+}
+
+/*
+ * The fork system call sets a child flag in the second return
+ * value: 0 for parent process, 1 for child process.
+ */
+set_second_rval(cpu_env, child_flag);
+
+fork_end(child_flag);
+
+return ret;
+}
+
+/* vfork(2) */
+static inline abi_long do_freebsd_vfork(void *cpu_env)
+{
+return do_freebsd_fork(cpu_env);
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 99af0f6b156..cb9425c9bab 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -226,6 +226,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 /*
  * process system calls
  */
+case TARGET_FREEBSD_NR_fork: /* fork(2) */
+ret = do_freebsd_fork(cpu_env);
+break;
+
+case TARGET_FREEBSD_NR_vfork: /* vfork(2) */
+ret = do_freebsd_vfork(cpu_env);
+break;
+
 case TARGET_FREEBSD_NR_execve: /* execve(2) */
 ret = do_freebsd_execve(arg1, arg2, arg3);
 break;
-- 
2.41.0




[PULL 01/51] bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics, and fix RLIM_INFINITY

2023-10-03 Thread Warner Losh
From: Karim Taha 

RLIM_INFINITY on FreeBSD, OpenBSD and NetBSD has value of ~(1<<63), caculated
one way or another.

Signed-off-by: Kyle Evans 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-2-kariem.taha...@gmail.com>
---
 bsd-user/syscall_defs.h | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 9c90616baae..ddd38c13e08 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -130,11 +130,7 @@ struct target_freebsd_timeval {
 /*
  *  sys/resource.h
  */
-#if defined(__FreeBSD__)
 #define TARGET_RLIM_INFINITYRLIM_INFINITY
-#else
-#define TARGET_RLIM_INFINITY((abi_ulong)-1)
-#endif
 
 #define TARGET_RLIMIT_CPU   0
 #define TARGET_RLIMIT_FSIZE 1
@@ -390,6 +386,10 @@ struct target_freebsd_flock {
 int32_t l_sysid;
 } QEMU_PACKED;
 
+/* sys/unistd.h */
+/* user: vfork(2) semantics, clear signals */
+#define TARGET_RFSPAWN (1U << 31)
+
 #define safe_syscall0(type, name) \
 type safe_##name(void) \
 { \
-- 
2.41.0




[PULL 04/51] bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-5-kariem.taha...@gmail.com>
---
 bsd-user/qemu.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index d9507137cca..41c7bd31d3c 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -249,6 +249,12 @@ abi_long get_errno(abi_long ret);
 bool is_error(abi_long ret);
 int host_to_target_errno(int err);
 
+/* os-proc.c */
+abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
+abi_ulong guest_envp, int do_fexec);
+abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
+abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
+
 /* os-sys.c */
 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
 abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
-- 
2.41.0




[PULL 43/51] bsd-user: Implement mlock(2), munlock(2), mlockall(2), munlockall(2), minherit(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Message-Id: <20230925182709.4834-16-kariem.taha...@gmail.com>
---
 bsd-user/bsd-mem.h| 37 +++
 bsd-user/freebsd/os-syscall.c | 20 +++
 2 files changed, 57 insertions(+)

diff --git a/bsd-user/bsd-mem.h b/bsd-user/bsd-mem.h
index 5e885823a79..16c22593bfd 100644
--- a/bsd-user/bsd-mem.h
+++ b/bsd-user/bsd-mem.h
@@ -99,4 +99,41 @@ static inline abi_long do_bsd_msync(abi_long addr, abi_long 
len, abi_long flags)
 return get_errno(msync(g2h_untagged(addr), len, flags));
 }
 
+/* mlock(2) */
+static inline abi_long do_bsd_mlock(abi_long arg1, abi_long arg2)
+{
+if (!guest_range_valid_untagged(arg1, arg2)) {
+return -TARGET_EINVAL;
+}
+return get_errno(mlock(g2h_untagged(arg1), arg2));
+}
+
+/* munlock(2) */
+static inline abi_long do_bsd_munlock(abi_long arg1, abi_long arg2)
+{
+if (!guest_range_valid_untagged(arg1, arg2)) {
+return -TARGET_EINVAL;
+}
+return get_errno(munlock(g2h_untagged(arg1), arg2));
+}
+
+/* mlockall(2) */
+static inline abi_long do_bsd_mlockall(abi_long arg1)
+{
+return get_errno(mlockall(arg1));
+}
+
+/* munlockall(2) */
+static inline abi_long do_bsd_munlockall(void)
+{
+return get_errno(munlockall());
+}
+
+/* minherit(2) */
+static inline abi_long do_bsd_minherit(abi_long addr, abi_long len,
+abi_long inherit)
+{
+return get_errno(minherit(g2h_untagged(addr), len, inherit));
+}
+
 #endif /* BSD_USER_BSD_MEM_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 2525e0bc316..7a7ae26793f 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -815,6 +815,26 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_msync(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_mlock: /* mlock(2) */
+ret = do_bsd_mlock(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_munlock: /* munlock(2) */
+ret = do_bsd_munlock(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_mlockall: /* mlockall(2) */
+ret = do_bsd_mlockall(arg1);
+break;
+
+case TARGET_FREEBSD_NR_munlockall: /* munlockall(2) */
+ret = do_bsd_munlockall();
+break;
+
+case TARGET_FREEBSD_NR_minherit: /* minherit(2) */
+ret = do_bsd_minherit(arg1, arg2, arg3);
+break;
+
 #if defined(__FreeBSD_version) && __FreeBSD_version >= 1300048
 case TARGET_FREEBSD_NR_shm_open2: /* shm_open2(2) */
 ret = do_freebsd_shm_open2(arg1, arg2, arg3, arg4, arg5);
-- 
2.41.0




[PULL 29/51] bsd-user: Implement struct target_ipc_perm

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182709.4834-2-kariem.taha...@gmail.com>
---
 bsd-user/syscall_defs.h | 17 +
 1 file changed, 17 insertions(+)

diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index a3bc738ff89..0e54d7df690 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -55,6 +55,23 @@ struct target_iovec {
 abi_long iov_len;   /* Number of bytes */
 };
 
+/*
+ * sys/ipc.h
+ */
+struct target_ipc_perm {
+uint32_tcuid;   /* creator user id */
+uint32_tcgid;   /* creator group id */
+uint32_tuid;/* user id */
+uint32_tgid;/* group id */
+uint16_tmode;   /* r/w permission */
+uint16_tseq;/* sequence # */
+abi_longkey;/* user specified msg/sem/shm key */
+};
+
+#define TARGET_IPC_RMID 0   /* remove identifier */
+#define TARGET_IPC_SET  1   /* set options */
+#define TARGET_IPC_STAT 2   /* get options */
+
 /*
  *  sys/mman.h
  */
-- 
2.41.0




[PULL 09/51] bsd-user: Implement host_to_target_waitstatus conversion.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-10-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.c | 17 +
 1 file changed, 17 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index aa386ff4820..19f6efe1f78 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -102,3 +102,20 @@ abi_long host_to_target_wrusage(abi_ulong target_addr,
 return 0;
 }
 
+/*
+ * wait status conversion.
+ *
+ * Map host to target signal numbers for the wait family of syscalls.
+ * Assume all other status bits are the same.
+ */
+int host_to_target_waitstatus(int status)
+{
+if (WIFSIGNALED(status)) {
+return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
+}
+if (WIFSTOPPED(status)) {
+return (host_to_target_signal(WSTOPSIG(status)) << 8) | (status & 
0xff);
+}
+return status;
+}
+
-- 
2.41.0




[PULL 00/51] Bsd user mmap patches

2023-10-03 Thread Warner Losh
The following changes since commit da1034094d375afe9e3d8ec8980550ea0f06f7e0:

  Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging 
(2023-10-03 07:43:44 -0400)

are available in the Git repository at:

  g...@gitlab.com:bsdimp/qemu.git tags/bsd-user-mmap-pull-request

for you to fetch changes up to dfa1d915756b2d9d22946cbd7d2587f30cdcb7a3:

  bsd-user: Add stubs for vadvise(), sbrk() and sstk() (2023-10-03 17:14:07 
-0600)


bsd-user mmap and exec branches from gsoc

This pull request represents the mmap and exec changes from Karim Taha
for his GSoC project.

They represent all the mmap and exec related system calls and get bsd-user to
the point that a dynamic hello-world works (at least for armv7).

There are a couple of patch check errors, but they are the lessor evil: I made
purposely bad style choices to ensure all the commits compiled (and i undid the
style choices in subsequent commits).

I pushed an earlier version to gitlab, and all but the riscv64 pipelines were
green.  Since bsd-user doesn't change anything related to ricsv64 (there's no
support in qemu-project repo, though we do have it in the bsd-user fork: coming
soon).

I think this is good to go.

https://gitlab.com/bsdimp/qemu.git

Warner
-BEGIN PGP SIGNATURE-
Comment: GPGTools - https://gpgtools.org

iQIzBAABCgAdFiEEIDX4lLAKo898zeG3bBzRKH2wEQAFAmUcpC4ACgkQbBzRKH2w
EQDD9xAA3Rg0AnfnFrd+AoWRb/1/gOuO0v+dEGXj50qnGY8OmHeYtg3XecYPArBq
EicZzL/OG7UZKMl5OfrmGP9tbr32yfeRUTe3AGGHfmnSb11q0yeSaEFZI7felLHj
9nlq4H/2EDRrY+7EnG1TWqtnuqDJAJf/7M0giiVxIk77XGX+USUNPOSG4NP/yc8E
D5p2GN23pUsvnI0jBZkyP3gyeXVNCNG5+KobwqJM3r6OjEiTRmLEVBw98YzG12bh
OY9ekMtVUKHi4Cvsf+2TtkDGRya0wX4uqm4UB1TtV1VUDoCWhYgEKBHp3ozCoVjB
J+ygbx7/jNfY53cpgEpKUBFH7rnOq1yQQ+ad5Ap5hbp4j6WSvPwdp1N3RCnkZzd/
L50VIaySd+P6enAgPO5Mbt3kMMVd/eDGhQDWdzNToIjyhXBb5hUNfumg9AgdEwTh
rW/kKT39YLYWLO123hIJCy2CKU9nvoea9588ExkKb22v0ltrtDcAlWfCbZvZYxNN
wRzh+MFBt7Cd/bqk7HaJ0J/YyPToqImoUjNuBnBSDPqZQP2H4U8v/FoICQ0mm5kR
jZCmGLMEP1PiDlusjUjaW0iamHvXiSP8KEzaAbIxx5UUiTWTTkQm4CKY/xPxC9VQ
0ygJqJVrKHlNrAY9u6ggJAXtorVwmC55z4ZqIVQH6cbzUYFMuJU=
=WpL4
-END PGP SIGNATURE-



Karim Taha (6):
  bsd-user: define TARGET_RFSPAWN for rfork to use vfork(2) semantics,
and fix RLIM_INFINITY
  bsd-user: Implement get_filename_from_fd.
  bsd-user: Implement execve(2) and fexecve(2) system calls.
  bsd-user: Implement shm_open2(2) system call
  bsd-user: Add bsd-mem.c to meson.build
  bsd-user: Implment madvise(2) to match the linux-user implementation.

Kyle Evans (2):
  bsd-user: Get number of cpus.
  bsd-user: Implement shm_rename(2) system call

Stacey Son (42):
  bsd-user: Define procctl(2) related structs
  bsd-user: Implement host_to_target_siginfo.
  bsd-user: Add freebsd_exec_common and do_freebsd_procctl to qemu.h.
  bsd-user: add extern declarations for bsd-proc.c conversion functions
  bsd-user: Implement target_to_host_resource conversion function
  bsd-user: Implement target_to_host_rlim and host_to_target_rlim
conversion.
  bsd-user: Implement host_to_target_rusage and host_to_target_wrusage.
  bsd-user: Implement host_to_target_waitstatus conversion.
  bsd-user: Implement getgroups(2) and setgroups(2) system calls.
  bsd-user: Implement umask(2), setlogin(2) and getlogin(2)
  bsd-user: Implement getrusage(2).
  bsd-user: Implement getrlimit(2) and setrlimit(2)
  bsd-user: Implement several get/set system calls:
  bsd-user: Implement get/set[resuid/resgid/sid] and issetugid.
  bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2).
  bsd-user: Implement getpriority(2) and setpriority(2).
  bsd-user: Implement freebsd_exec_common, used in implementing
execve/fexecve.
  bsd-user: Implement procctl(2) along with necessary conversion
functions.
  bsd-user: Implement wait4(2) and wait6(2) system calls.
  bsd-user: Implement setloginclass(2) and getloginclass(2) system
calls.
  bsd-user: Implement pdgetpid(2) and the undocumented setugid.
  bsd-user: Implement fork(2) and vfork(2) system calls.
  bsd-user: Implement rfork(2) system call.
  bsd-user: Implement pdfork(2) system call.
  bsd-user: Implement struct target_ipc_perm
  bsd-user: Implement struct target_shmid_ds
  bsd-user: Declarations for ipc_perm and shmid_ds conversion functions
  bsd-user: Introduce freebsd/os-misc.h to the source tree
  bsd-user: Implement target_set_brk function in bsd-mem.c instead of
os-syscall.c
  bsd-user: Implement ipc_perm conversion between host and target.
  bsd-user: Implement shmid_ds conversion between host and target.
  bsd-user: Introduce bsd-mem.h to the source tree
  bsd-user: Implement mmap(2) and munmap(2)
  bsd-user: Implement mprotect(2)
  bsd-user: Implement msync(2)
  bsd-user: Implement mlock(2), munlock(2), mlockall(2), munlockall(2),
minherit(2)
  bsd-user: Implement mincore(2)
  bsd-user: Implement do_obreak function
  

[PULL 17/51] bsd-user: Add stubs for profil(2), ktrace(2), utrace(2) and ptrace(2).

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-18-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 28 
 bsd-user/freebsd/os-syscall.c | 16 
 2 files changed, 44 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index a5f301c72ff..2c1a9ae22fa 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -362,4 +362,32 @@ static inline abi_long do_bsd_issetugid(void)
 return get_errno(issetugid());
 }
 
+/* profil(2) */
+static inline abi_long do_bsd_profil(abi_long arg1, abi_long arg2,
+ abi_long arg3, abi_long arg4)
+{
+return -TARGET_ENOSYS;
+}
+
+/* ktrace(2) */
+static inline abi_long do_bsd_ktrace(abi_long arg1, abi_long arg2,
+ abi_long arg3, abi_long arg4)
+{
+return -TARGET_ENOSYS;
+}
+
+/* utrace(2) */
+static inline abi_long do_bsd_utrace(abi_long arg1, abi_long arg2)
+{
+return -TARGET_ENOSYS;
+}
+
+
+/* ptrace(2) */
+static inline abi_long do_bsd_ptrace(abi_long arg1, abi_long arg2,
+abi_long arg3, abi_long arg4)
+{
+return -TARGET_ENOSYS;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7b51f4f16e4..1a760b13808 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -343,6 +343,22 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_issetugid();
 break;
 
+case TARGET_FREEBSD_NR_profil: /* profil(2) */
+ret = do_bsd_profil(arg1, arg2, arg3, arg4);
+break;
+
+case TARGET_FREEBSD_NR_ktrace: /* ktrace(2) */
+ret = do_bsd_ktrace(arg1, arg2, arg3, arg4);
+break;
+
+case TARGET_FREEBSD_NR_utrace: /* utrace(2) */
+ret = do_bsd_utrace(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_ptrace: /* ptrace(2) */
+ret = do_bsd_ptrace(arg1, arg2, arg3, arg4);
+break;
+
 
 /*
  * File system calls.
-- 
2.41.0




[PULL 11/51] bsd-user: Implement getgroups(2) and setgroups(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-12-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 44 +++
 bsd-user/freebsd/os-syscall.c |  9 +++
 2 files changed, 53 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index b6225e520ea..7b25aa19829 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -41,4 +41,48 @@ static inline abi_long do_bsd_exit(void *cpu_env, abi_long 
arg1)
 return 0;
 }
 
+/* getgroups(2) */
+static inline abi_long do_bsd_getgroups(abi_long gidsetsize, abi_long arg2)
+{
+abi_long ret;
+uint32_t *target_grouplist;
+g_autofree gid_t *grouplist;
+int i;
+
+grouplist = g_try_new(gid_t, gidsetsize);
+ret = get_errno(getgroups(gidsetsize, grouplist));
+if (gidsetsize != 0) {
+if (!is_error(ret)) {
+target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 
0);
+if (!target_grouplist) {
+return -TARGET_EFAULT;
+}
+for (i = 0; i < ret; i++) {
+target_grouplist[i] = tswap32(grouplist[i]);
+}
+unlock_user(target_grouplist, arg2, gidsetsize * 2);
+}
+}
+return ret;
+}
+
+/* setgroups(2) */
+static inline abi_long do_bsd_setgroups(abi_long gidsetsize, abi_long arg2)
+{
+uint32_t *target_grouplist;
+g_autofree gid_t *grouplist;
+int i;
+
+grouplist = g_try_new(gid_t, gidsetsize);
+target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
+if (!target_grouplist) {
+return -TARGET_EFAULT;
+}
+for (i = 0; i < gidsetsize; i++) {
+grouplist[i] = tswap32(target_grouplist[i]);
+}
+unlock_user(target_grouplist, arg2, 0);
+return get_errno(setgroups(gidsetsize, grouplist));
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index fa60df529ef..535e6287bde 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -223,6 +223,15 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_exit(cpu_env, arg1);
 break;
 
+case TARGET_FREEBSD_NR_getgroups: /* getgroups(2) */
+ret = do_bsd_getgroups(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_setgroups: /* setgroups(2) */
+ret = do_bsd_setgroups(arg1, arg2);
+break;
+
+
 /*
  * File system calls.
  */
-- 
2.41.0




[PULL 14/51] bsd-user: Implement getrlimit(2) and setrlimit(2)

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-15-kariem.taha...@gmail.com>
---
 bsd-user/bsd-proc.h   | 59 +++
 bsd-user/freebsd/os-syscall.c |  8 +
 2 files changed, 67 insertions(+)

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 133c1b0eaf8..38d1324034c 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -137,4 +137,63 @@ static inline abi_long do_bsd_getrusage(abi_long who, 
abi_ulong target_addr)
 return ret;
 }
 
+/* getrlimit(2) */
+static inline abi_long do_bsd_getrlimit(abi_long arg1, abi_ulong arg2)
+{
+abi_long ret;
+int resource = target_to_host_resource(arg1);
+struct target_rlimit *target_rlim;
+struct rlimit rlim;
+
+switch (resource) {
+case RLIMIT_STACK:
+rlim.rlim_cur = target_dflssiz;
+rlim.rlim_max = target_maxssiz;
+ret = 0;
+break;
+
+case RLIMIT_DATA:
+rlim.rlim_cur = target_dfldsiz;
+rlim.rlim_max = target_maxdsiz;
+ret = 0;
+break;
+
+default:
+ret = get_errno(getrlimit(resource, ));
+break;
+}
+if (!is_error(ret)) {
+if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) {
+return -TARGET_EFAULT;
+}
+target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
+target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
+unlock_user_struct(target_rlim, arg2, 1);
+}
+return ret;
+}
+
+/* setrlimit(2) */
+static inline abi_long do_bsd_setrlimit(abi_long arg1, abi_ulong arg2)
+{
+abi_long ret;
+int resource = target_to_host_resource(arg1);
+struct target_rlimit *target_rlim;
+struct rlimit rlim;
+
+if (RLIMIT_STACK == resource) {
+/* XXX We should, maybe, allow the stack size to shrink */
+ret = -TARGET_EPERM;
+} else {
+if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) {
+return -TARGET_EFAULT;
+}
+rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
+rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
+unlock_user_struct(target_rlim, arg2, 0);
+ret = get_errno(setrlimit(resource, ));
+}
+return ret;
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 5d8693ed550..5cb60862303 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -247,6 +247,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_bsd_getrusage(arg1, arg2);
 break;
 
+case TARGET_FREEBSD_NR_getrlimit: /* getrlimit(2) */
+ret = do_bsd_getrlimit(arg1, arg2);
+break;
+
+case TARGET_FREEBSD_NR_setrlimit: /* setrlimit(2) */
+ret = do_bsd_setrlimit(arg1, arg2);
+break;
+
 
 /*
  * File system calls.
-- 
2.41.0




[PULL 22/51] bsd-user: Implement execve(2) and fexecve(2) system calls.

2023-10-03 Thread Warner Losh
From: Karim Taha 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Richard Henderson 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-23-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 49 +++
 bsd-user/freebsd/os-syscall.c | 11 +++-
 2 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 bsd-user/freebsd/os-proc.h

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
new file mode 100644
index 000..75ed39f8ddd
--- /dev/null
+++ b/bsd-user/freebsd/os-proc.h
@@ -0,0 +1,49 @@
+/*
+ *  process related system call shims and definitions
+ *
+ *  Copyright (c) 2013-14 Stacey D. Son
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, see .
+ */
+
+#ifndef BSD_USER_FREEBSD_OS_PROC_H
+#define BSD_USER_FREEBSD_OS_PROC_H
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "target_arch_cpu.h"
+
+/* execve(2) */
+static inline abi_long do_freebsd_execve(abi_ulong path_or_fd, abi_ulong argp,
+abi_ulong envp)
+{
+
+return freebsd_exec_common(path_or_fd, argp, envp, 0);
+}
+
+/* fexecve(2) */
+static inline abi_long do_freebsd_fexecve(abi_ulong path_or_fd, abi_ulong argp,
+abi_ulong envp)
+{
+
+return freebsd_exec_common(path_or_fd, argp, envp, 1);
+}
+
+#endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index b7bd0b92a65..515eaaf31f1 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -36,8 +36,9 @@
 #include "bsd-file.h"
 #include "bsd-proc.h"
 
-/* *BSD dependent syscall shims */
+/* BSD dependent syscall shims */
 #include "os-stat.h"
+#include "os-proc.h"
 
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
@@ -219,6 +220,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 /*
  * process system calls
  */
+case TARGET_FREEBSD_NR_execve: /* execve(2) */
+ret = do_freebsd_execve(arg1, arg2, arg3);
+break;
+
+case TARGET_FREEBSD_NR_fexecve: /* fexecve(2) */
+ret = do_freebsd_fexecve(arg1, arg2, arg3);
+break;
+
 case TARGET_FREEBSD_NR_exit: /* exit(2) */
 ret = do_bsd_exit(cpu_env, arg1);
 break;
-- 
2.41.0




[PULL 23/51] bsd-user: Implement wait4(2) and wait6(2) system calls.

2023-10-03 Thread Warner Losh
From: Stacey Son 

Signed-off-by: Stacey Son 
Signed-off-by: Karim Taha 
Reviewed-by: Warner Losh 
Message-Id: <20230925182425.3163-24-kariem.taha...@gmail.com>
---
 bsd-user/freebsd/os-proc.h| 84 +++
 bsd-user/freebsd/os-syscall.c | 15 +++
 2 files changed, 99 insertions(+)

diff --git a/bsd-user/freebsd/os-proc.h b/bsd-user/freebsd/os-proc.h
index 75ed39f8ddd..04bce755e58 100644
--- a/bsd-user/freebsd/os-proc.h
+++ b/bsd-user/freebsd/os-proc.h
@@ -30,6 +30,10 @@
 
 #include "target_arch_cpu.h"
 
+pid_t safe_wait4(pid_t wpid, int *status, int options, struct rusage *rusage);
+pid_t safe_wait6(idtype_t idtype, id_t id, int *status, int options,
+struct __wrusage *wrusage, siginfo_t *infop);
+
 /* execve(2) */
 static inline abi_long do_freebsd_execve(abi_ulong path_or_fd, abi_ulong argp,
 abi_ulong envp)
@@ -46,4 +50,84 @@ static inline abi_long do_freebsd_fexecve(abi_ulong 
path_or_fd, abi_ulong argp,
 return freebsd_exec_common(path_or_fd, argp, envp, 1);
 }
 
+/* wait4(2) */
+static inline abi_long do_freebsd_wait4(abi_long arg1, abi_ulong target_status,
+abi_long arg3, abi_ulong target_rusage)
+{
+abi_long ret;
+int status;
+struct rusage rusage, *rusage_ptr = NULL;
+
+if (target_rusage) {
+rusage_ptr = 
+}
+ret = get_errno(safe_wait4(arg1, , arg3, rusage_ptr));
+
+if (ret < 0) {
+return ret;
+}
+if (target_status != 0) {
+status = host_to_target_waitstatus(status);
+if (put_user_s32(status, target_status) != 0) {
+return -TARGET_EFAULT;
+}
+}
+if (target_rusage != 0) {
+host_to_target_rusage(target_rusage, );
+}
+return ret;
+}
+
+/* wait6(2) */
+static inline abi_long do_freebsd_wait6(void *cpu_env, abi_long idtype,
+abi_long id1, abi_long id2,
+abi_ulong target_status, abi_long options, abi_ulong target_wrusage,
+abi_ulong target_infop, abi_ulong pad1)
+{
+abi_long ret;
+int status;
+struct __wrusage wrusage, *wrusage_ptr = NULL;
+siginfo_t info;
+void *p;
+
+if (regpairs_aligned(cpu_env) != 0) {
+/* printf("shifting args\n"); */
+/* 64-bit id is aligned, so shift all the arguments over by one */
+id1 = id2;
+id2 = target_status;
+target_status = options;
+options = target_wrusage;
+target_wrusage = target_infop;
+target_infop = pad1;
+}
+
+if (target_wrusage) {
+wrusage_ptr = 
+}
+ret = get_errno(safe_wait6(idtype, target_arg64(id1, id2),
+   , options, wrusage_ptr, ));
+
+if (ret < 0) {
+return ret;
+}
+if (target_status != 0) {
+status = host_to_target_waitstatus(status);
+if (put_user_s32(status, target_status) != 0) {
+return -TARGET_EFAULT;
+}
+}
+if (target_wrusage != 0) {
+host_to_target_wrusage(target_wrusage, );
+}
+if (target_infop != 0) {
+p = lock_user(VERIFY_WRITE, target_infop, sizeof(target_siginfo_t), 0);
+if (p == NULL) {
+return -TARGET_EFAULT;
+}
+host_to_target_siginfo(p, );
+unlock_user(p, target_infop, sizeof(target_siginfo_t));
+}
+return ret;
+}
+
 #endif /* BSD_USER_FREEBSD_OS_PROC_H */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 515eaaf31f1..55e68e48159 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -40,6 +40,12 @@
 #include "os-stat.h"
 #include "os-proc.h"
 
+/* used in os-proc */
+safe_syscall4(pid_t, wait4, pid_t, wpid, int *, status, int, options,
+struct rusage *, rusage);
+safe_syscall6(pid_t, wait6, idtype_t, idtype, id_t, id, int *, status, int,
+options, struct __wrusage *, wrusage, siginfo_t *, infop);
+
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
 safe_syscall4(int, openat, int, fd, const char *, path, int, flags, mode_t,
@@ -228,6 +234,15 @@ static abi_long freebsd_syscall(void *cpu_env, int num, 
abi_long arg1,
 ret = do_freebsd_fexecve(arg1, arg2, arg3);
 break;
 
+case TARGET_FREEBSD_NR_wait4: /* wait4(2) */
+ret = do_freebsd_wait4(arg1, arg2, arg3, arg4);
+break;
+
+case TARGET_FREEBSD_NR_wait6: /* wait6(2) */
+ret = do_freebsd_wait6(cpu_env, arg1, arg2, arg3,
+   arg4, arg5, arg6, arg7, arg8);
+break;
+
 case TARGET_FREEBSD_NR_exit: /* exit(2) */
 ret = do_bsd_exit(cpu_env, arg1);
 break;
-- 
2.41.0




Re: [PATCH v2 2/5] hw/i386/apic: Defer error check from apic_get_class to kvm_apic_realize

2023-10-03 Thread Bernhard Beschow



Am 3. Oktober 2023 08:27:25 UTC schrieb "Philippe Mathieu-Daudé" 
:
>apic_get_class() isn't supposed to fail. kvm_apic_realize() is
>DeviceRealize() handler, which can fail. Defer the error check
>to the latter.
>
>Signed-off-by: Philippe Mathieu-Daudé 
>---
> hw/i386/kvm/apic.c   | 5 +
> target/i386/cpu-sysemu.c | 8 
> 2 files changed, 5 insertions(+), 8 deletions(-)
>
>diff --git a/hw/i386/kvm/apic.c b/hw/i386/kvm/apic.c
>index 1e89ca0899..4883308247 100644
>--- a/hw/i386/kvm/apic.c
>+++ b/hw/i386/kvm/apic.c
>@@ -228,6 +228,11 @@ static void kvm_apic_realize(DeviceState *dev, Error 
>**errp)
> {
> APICCommonState *s = APIC_COMMON(dev);
> 
>+if (!kvm_irqchip_in_kernel()) {
>+error_setg(errp, "KVM does not support userspace APIC");
>+return;
>+}
>+
> memory_region_init_io(>io_memory, OBJECT(s), _apic_io_ops, s,
>   "kvm-apic-msi", APIC_SPACE_SIZE);
> 
>diff --git a/target/i386/cpu-sysemu.c b/target/i386/cpu-sysemu.c
>index 2375e48178..6a228c9178 100644
>--- a/target/i386/cpu-sysemu.c
>+++ b/target/i386/cpu-sysemu.c
>@@ -253,10 +253,6 @@ APICCommonClass *apic_get_class(Error **errp)
> 
> /* TODO: in-kernel irqchip for hvf */
> if (kvm_enabled()) {
>-if (!kvm_irqchip_in_kernel()) {
>-error_setg(errp, "KVM does not support userspace APIC");
>-return NULL;
>-}
> apic_type = "kvm-apic";
> } else if (xen_enabled()) {
> apic_type = "xen-apic";
>@@ -272,10 +268,6 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
> APICCommonState *apic;
> APICCommonClass *apic_class = apic_get_class(errp);
> 
>-if (!apic_class) {
>-return;
>-}
>-

Did you intend to remove these lines in the next commit? There you're writing 
to simplify x86_cpu_apic_create() which you're doing here already.

Best regards,
Bernhard

> cpu->apic_state = DEVICE(object_new_with_class(OBJECT_CLASS(apic_class)));
> object_property_add_child(OBJECT(cpu), "lapic",
>   OBJECT(cpu->apic_state));



Re: [PATCH v3 09/15] vfio/ap: Use vfio_[attach/detach]_device

2023-10-03 Thread Matthew Rosato
On 10/3/23 11:25 AM, Cédric Le Goater wrote:
> On 10/3/23 12:14, Eric Auger wrote:
>> Let the vfio-ap device use vfio_attach_device() and
>> vfio_detach_device(), hence hiding the details of the used
>> IOMMU backend.
>>
>> We take the opportunity to use g_path_get_basename() which
>> is prefered, as suggested by
>> 3e015d815b ("use g_path_get_basename instead of basename")
>>
>> Signed-off-by: Eric Auger 
>> Signed-off-by: Yi Liu 
>> Signed-off-by: Zhenzhong Duan 
>>
>> ---
>>
>> v2 -> v3:
>> - Mention g_path_get_basename in commit message and properly free
>>    vbasedev->name, call vfio_detach_device
>> ---
>>   hw/vfio/ap.c | 70 ++--
>>   1 file changed, 13 insertions(+), 57 deletions(-)
>>
>> diff --git a/hw/vfio/ap.c b/hw/vfio/ap.c
>> index 6e21d1da5a..d0b587b3b1 100644
>> --- a/hw/vfio/ap.c
>> +++ b/hw/vfio/ap.c
>> @@ -53,40 +53,6 @@ struct VFIODeviceOps vfio_ap_ops = {
>>   .vfio_compute_needs_reset = vfio_ap_compute_needs_reset,
>>   };
>>   -static void vfio_ap_put_device(VFIOAPDevice *vapdev)
>> -{
>> -    g_free(vapdev->vdev.name);
>> -    vfio_put_base_device(>vdev);
>> -}
>> -
>> -static VFIOGroup *vfio_ap_get_group(VFIOAPDevice *vapdev, Error **errp)
>> -{
>> -    GError *gerror = NULL;
>> -    char *symlink, *group_path;
>> -    int groupid;
>> -
>> -    symlink = g_strdup_printf("%s/iommu_group", vapdev->vdev.sysfsdev);
>> -    group_path = g_file_read_link(symlink, );
>> -    g_free(symlink);
>> -
>> -    if (!group_path) {
>> -    error_setg(errp, "%s: no iommu_group found for %s: %s",
>> -   TYPE_VFIO_AP_DEVICE, vapdev->vdev.sysfsdev, 
>> gerror->message);
>> -    g_error_free(gerror);
>> -    return NULL;
>> -    }
>> -
>> -    if (sscanf(basename(group_path), "%d", ) != 1) {
>> -    error_setg(errp, "vfio: failed to read %s", group_path);
>> -    g_free(group_path);
>> -    return NULL;
>> -    }
>> -
>> -    g_free(group_path);
>> -
>> -    return vfio_get_group(groupid, _space_memory, errp);
>> -}
>> -
>>   static void vfio_ap_req_notifier_handler(void *opaque)
>>   {
>>   VFIOAPDevice *vapdev = opaque;
>> @@ -189,22 +155,15 @@ static void 
>> vfio_ap_unregister_irq_notifier(VFIOAPDevice *vapdev,
>>   static void vfio_ap_realize(DeviceState *dev, Error **errp)
>>   {
>>   int ret;
>> -    char *mdevid;
>>   Error *err = NULL;
>> -    VFIOGroup *vfio_group;
>>   APDevice *apdev = AP_DEVICE(dev);
>>   VFIOAPDevice *vapdev = VFIO_AP_DEVICE(apdev);
>> +    VFIODevice *vbasedev = >vdev;
>>   -    vfio_group = vfio_ap_get_group(vapdev, errp);
>> -    if (!vfio_group) {
>> -    return;
>> -    }
>> -
>> -    vapdev->vdev.ops = _ap_ops;
>> -    vapdev->vdev.type = VFIO_DEVICE_TYPE_AP;
>> -    mdevid = basename(vapdev->vdev.sysfsdev);
>> -    vapdev->vdev.name = g_strdup_printf("%s", mdevid);
>> -    vapdev->vdev.dev = dev;
>> +    vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
>> +    vbasedev->ops = _ap_ops;
>> +    vbasedev->type = VFIO_DEVICE_TYPE_AP;
>> +    vbasedev->dev = dev;
>>     /*
>>    * vfio-ap devices operate in a way compatible with discarding of
>> @@ -214,9 +173,11 @@ static void vfio_ap_realize(DeviceState *dev, Error 
>> **errp)
>>    */
>>   vapdev->vdev.ram_block_discard_allowed = true;
>>   -    ret = vfio_get_device(vfio_group, mdevid, >vdev, errp);
>> +    ret = vfio_attach_device(vbasedev->name, vbasedev,
>> + _space_memory, errp);
>>   if (ret) {
>> -    goto out_get_dev_err;
>> +    g_free(vbasedev->name);
>> +    return;
>>   }
>>     vfio_ap_register_irq_notifier(vapdev, VFIO_AP_REQ_IRQ_INDEX, );
>> @@ -225,25 +186,20 @@ static void vfio_ap_realize(DeviceState *dev, Error 
>> **errp)
>>    * Report this error, but do not make it a failing condition.
>>    * Lack of this IRQ in the host does not prevent normal operation.
>>    */
>> +    vfio_detach_device(vbasedev);
>>   error_report_err(err);
>> +    g_free(vbasedev->name);

This patch overall looks good to me and passes basic tests with vfio-ap 
devices.  But I note that this addition of detach+free here runs counter to 
what the comment block above it states and prior behavior (where we did not 
goto out_get_dev_err for this case and expect the realize to complete 
successfully despite this error).  

In this error case, we only report the local 'err' contents and nothing is 
propagated into 'errp' -- which means that to the caller dc->realize() should 
be viewed as successful (errp is NULL) and so we should be able to assume a 
subsequent dc->unrealize() will do this g_free+detach later. 

>>   }
>> -
>> -    return;
>> -
>> -out_get_dev_err:
>> -    vfio_ap_put_device(vapdev);
>> -    vfio_put_group(vfio_group);
>>   }
> 
> 
> To be consistent with vfio_(pci)_realize(), I would introduce the same
> failure path at the end the routine :
> 
>   out_detach:
>   

Re: [PATCH v6 08/19] linux-user: Add gen-vdso tool

2023-10-03 Thread Richard Henderson

On 10/3/23 06:00, Philippe Mathieu-Daudé wrote:

On 30/9/23 04:15, Richard Henderson wrote:

This tool will be used for post-processing the linked vdso image,
turning it into something that is easy to include into elfload.c.

Signed-off-by: Richard Henderson 
---
  linux-user/gen-vdso.c  | 223 
  linux-user/gen-vdso-elfn.c.inc | 307 +
  linux-user/meson.build |   6 +-
  3 files changed, 535 insertions(+), 1 deletion(-)
  create mode 100644 linux-user/gen-vdso.c
  create mode 100644 linux-user/gen-vdso-elfn.c.inc




+static void output_reloc(FILE *outf, void *buf, void *loc)
+{
+    fprintf(outf, "    0x%08lx,\n", (unsigned long)(loc - buf));


uintptr_t? Otherwise nice!


uintptr_t would require more complex printf.

Actually, I should just use %tx for the ptrdiff_t one gets from pointer 
subtraction.


r~



Re: [PATCH v6 18/19] linux-user/s390x: Add vdso

2023-10-03 Thread Richard Henderson

On 10/3/23 06:07, Philippe Mathieu-Daudé wrote:

+++ b/linux-user/s390x/vdso.ld
@@ -0,0 +1,69 @@
+/*
+ * Linker script for linux x86-64 replacement vdso.
+ *
+ * Copyright 2023 Linaro, Ltd.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+VERSION {
+    LINUX_2.6.29 {
+    global:
+    __kernel_gettimeofday;
+    __kernel_clock_gettime;
+    __kernel_clock_getres;
+    __kernel_getcpu;
+    __kernel_restart_syscall;


Where is __kernel_restart_syscall defined?


It isn't, and I guess isn't needed for qemu (we have QEMU_ERESTARTSYS).
I'm not sure why it's an exported symbol for the s390x vdso for the kernel, even if they 
do require some user-space code.


Anyway, I'll remove the symbol here.


r~



Re: [PATCH 0/3] target/m68k: Fix a few semihosting bugs

2023-10-03 Thread Richard Henderson

On 8/2/23 09:19, Keith Packard via wrote:

The first two patches mirror similar patches I recently sent for nios2.

  1. Use correct parameter for EXIT (d1 instead of d0)
  2. Fix use of deposit64 in LSEEK (argument order was incorrect)

The second patch has also been submitted by Peter Maydell, it's
included here because it was required to get things working.

The final patch adds semihosting support for non-ColdFire processors
(which don't support the HALT instruction) by using BKPT #0 instead
(as per the m68k semihosting docs).

All of these have been tested using picolibc (patches for m68k support
there are moving upstream as well).



Queued patch 1 to m68k-next.  Patch 2 has already been committed, and I've just sent a 
replacement for patch 3.



r~


PS: I hadn't noticed the picolibc note here at the time; I'll give that a try 
later today.



[PATCH v2 2/2] target/m68k: Support semihosting on non-ColdFire targets

2023-10-03 Thread Richard Henderson
According to the m68k semihosting spec:

"The instruction used to trigger a semihosting request depends on the
 m68k processor variant.  On ColdFire, "halt" is used; on other processors
 (which don't implement "halt"), "bkpt #0" may be used."

Add support for non-CodeFire processors by matching BKPT #0 instructions.

Signed-off-by: Keith Packard 
[rth: Use semihosting_test()]
Signed-off-by: Richard Henderson 
---
 target/m68k/translate.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/target/m68k/translate.c b/target/m68k/translate.c
index e07b0b659d..54c3ff1218 100644
--- a/target/m68k/translate.c
+++ b/target/m68k/translate.c
@@ -2663,6 +2663,11 @@ DISAS_INSN(bkpt)
 #if defined(CONFIG_USER_ONLY)
 gen_exception(s, s->base.pc_next, EXCP_DEBUG);
 #else
+/* BKPT #0 is the alternate semihosting instruction. */
+if ((insn & 7) == 0 && semihosting_test(s)) {
+gen_exception(s, s->pc, EXCP_SEMIHOSTING);
+return;
+}
 gen_exception(s, s->base.pc_next, EXCP_ILLEGAL);
 #endif
 }
-- 
2.34.1




[PATCH v2 1/2] target/m68k: Perform the semihosting test during translate

2023-10-03 Thread Richard Henderson
Replace EXCP_HALT_INSN by EXCP_SEMIHOSTING.  Perform the pre-
and post-insn tests during translate, leaving only the actual
semihosting operation for the exception.

Signed-off-by: Richard Henderson 
---
 target/m68k/cpu.h   |  2 +-
 target/m68k/op_helper.c | 14 ++---
 target/m68k/translate.c | 45 +
 3 files changed, 44 insertions(+), 17 deletions(-)

diff --git a/target/m68k/cpu.h b/target/m68k/cpu.h
index 10697120f6..4549c28452 100644
--- a/target/m68k/cpu.h
+++ b/target/m68k/cpu.h
@@ -66,7 +66,7 @@
 #define EXCP_MMU_ACCESS 58  /* MMU Access Level Violation Error */
 
 #define EXCP_RTE0x100
-#define EXCP_HALT_INSN  0x101
+#define EXCP_SEMIHOSTING0x101
 
 #define M68K_DTTR0   0
 #define M68K_DTTR1   1
diff --git a/target/m68k/op_helper.c b/target/m68k/op_helper.c
index 1ce850bbc5..38eb85b8e7 100644
--- a/target/m68k/op_helper.c
+++ b/target/m68k/op_helper.c
@@ -202,18 +202,8 @@ static void cf_interrupt_all(CPUM68KState *env, int is_hw)
 /* Return from an exception.  */
 cf_rte(env);
 return;
-case EXCP_HALT_INSN:
-if (semihosting_enabled((env->sr & SR_S) == 0)
-&& (env->pc & 3) == 0
-&& cpu_lduw_code(env, env->pc - 4) == 0x4e71
-&& cpu_ldl_code(env, env->pc) == 0x4e7bf000) {
-env->pc += 4;
-do_m68k_semihosting(env, env->dregs[0]);
-return;
-}
-cs->halted = 1;
-cs->exception_index = EXCP_HLT;
-cpu_loop_exit(cs);
+case EXCP_SEMIHOSTING:
+do_m68k_semihosting(env, env->dregs[0]);
 return;
 }
 }
diff --git a/target/m68k/translate.c b/target/m68k/translate.c
index ea7af25d50..e07b0b659d 100644
--- a/target/m68k/translate.c
+++ b/target/m68k/translate.c
@@ -26,12 +26,11 @@
 #include "qemu/log.h"
 #include "qemu/qemu-print.h"
 #include "exec/translator.h"
-
 #include "exec/helper-proto.h"
 #include "exec/helper-gen.h"
-
 #include "exec/log.h"
 #include "fpu/softfloat.h"
+#include "semihosting/semihost.h"
 
 #define HELPER_H "helper.h"
 #include "exec/helper-info.c.inc"
@@ -1418,6 +1417,40 @@ static void gen_jmp_tb(DisasContext *s, int n, 
target_ulong dest,
 s->base.is_jmp = DISAS_NORETURN;
 }
 
+#ifndef CONFIG_USER_ONLY
+static bool semihosting_test(DisasContext *s)
+{
+uint32_t test;
+
+if (!semihosting_enabled(IS_USER(s))) {
+return false;
+}
+
+/*
+ * "The semihosting instruction is immediately preceded by a
+ * nop aligned to a 4-byte boundary..."
+ * The preceding 2-byte (aligned) nop plus the 2-byte halt/bkpt
+ * means that we have advanced 4 bytes from the required nop.
+ */
+if (s->pc % 4 != 0) {
+return false;
+}
+test = cpu_lduw_code(s->env, s->pc - 4);
+if (test != 0x4e71) {
+return false;
+}
+/* "... and followed by an invalid sentinel instruction movec %sp,0." */
+test = translator_ldl(s->env, >base, s->pc);
+if (test != 0x4e7bf000) {
+return false;
+}
+
+/* Consume the sentinel. */
+s->pc += 4;
+return true;
+}
+#endif /* !CONFIG_USER_ONLY */
+
 DISAS_INSN(scc)
 {
 DisasCompare c;
@@ -4482,8 +4515,12 @@ DISAS_INSN(halt)
 gen_exception(s, s->base.pc_next, EXCP_PRIVILEGE);
 return;
 }
-
-gen_exception(s, s->pc, EXCP_HALT_INSN);
+if (semihosting_test(s)) {
+gen_exception(s, s->pc, EXCP_SEMIHOSTING);
+return;
+}
+tcg_gen_movi_i32(cpu_halted, 1);
+gen_exception(s, s->pc, EXCP_HLT);
 }
 
 DISAS_INSN(stop)
-- 
2.34.1




[PATCH v2 0/2] target/m68k: Support semihosting on non-ColdFire targets

2023-10-03 Thread Richard Henderson
Hi Keith,

Sorry this has languished for a few months.  I wasn't happy with
the frobbing done by EXCP_HALT_INSN, and less keen to replicate
it with EXCP_BKPT_INSN, so I've re-organized a bit.

So far only compile-tested.  I guess gcc testing with m68k-elf
might exercise libgloss and thence semihosting?


r~


Richard Henderson (2):
  target/m68k: Perform the semihosting test during translate
  target/m68k: Support semihosting on non-ColdFire targets

 target/m68k/cpu.h   |  2 +-
 target/m68k/op_helper.c | 14 ++--
 target/m68k/translate.c | 50 +
 3 files changed, 49 insertions(+), 17 deletions(-)

-- 
2.34.1




[PATCH] hw/isa/vt82c686: Respect SCI interrupt assignment

2023-10-03 Thread Bernhard Beschow
According to the datasheet, SCI interrupts of the power management function
aren't triggered through the PCI pins but rather directly to the integrated PIC.
The routing is configurable through the ACPI interrupt select register at offset
42 in the PCI configuration space of the ISA function.

Signed-off-by: Bernhard Beschow 
---
 hw/isa/vt82c686.c | 43 +++
 1 file changed, 31 insertions(+), 12 deletions(-)

diff --git a/hw/isa/vt82c686.c b/hw/isa/vt82c686.c
index 57bdfb4e78..2988ad1eda 100644
--- a/hw/isa/vt82c686.c
+++ b/hw/isa/vt82c686.c
@@ -46,6 +46,8 @@ struct ViaPMState {
 ACPIREGS ar;
 APMState apm;
 PMSMBus smb;
+
+qemu_irq irq;
 };
 
 static void pm_io_space_update(ViaPMState *s)
@@ -148,18 +150,7 @@ static void pm_update_sci(ViaPMState *s)
ACPI_BITMASK_POWER_BUTTON_ENABLE |
ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
ACPI_BITMASK_TIMER_ENABLE)) != 0);
-if (pci_get_byte(s->dev.config + PCI_INTERRUPT_PIN)) {
-/*
- * FIXME:
- * Fix device model that realizes this PM device and remove
- * this work around.
- * The device model should wire SCI and setup
- * PCI_INTERRUPT_PIN properly.
- * If PIN# = 0(interrupt pin isn't used), don't raise SCI as
- * work around.
- */
-pci_set_irq(>dev, sci_level);
-}
+qemu_set_irq(s->irq, sci_level);
 /* schedule a timer interruption if needed */
 acpi_pm_tmr_update(>ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) 
&&
!(pmsts & ACPI_BITMASK_TIMER_STATUS));
@@ -213,6 +204,13 @@ static void via_pm_realize(PCIDevice *dev, Error **errp)
 acpi_pm1_cnt_init(>ar, >io, false, false, 2, false);
 }
 
+static void via_pm_init(Object *obj)
+{
+ViaPMState *s = VIA_PM(obj);
+
+qdev_init_gpio_out(DEVICE(obj), >irq, 1);
+}
+
 typedef struct via_pm_init_info {
 uint16_t device_id;
 } ViaPMInitInfo;
@@ -238,6 +236,7 @@ static void via_pm_class_init(ObjectClass *klass, void 
*data)
 static const TypeInfo via_pm_info = {
 .name  = TYPE_VIA_PM,
 .parent= TYPE_PCI_DEVICE,
+.instance_init = via_pm_init,
 .instance_size = sizeof(ViaPMState),
 .abstract  = true,
 .interfaces = (InterfaceInfo[]) {
@@ -568,9 +567,25 @@ static const VMStateDescription vmstate_via = {
 }
 };
 
+static void via_isa_set_pm_irq(void *opaque, int n, int level)
+{
+ViaISAState *s = opaque;
+uint8_t irq = pci_get_byte(s->pm.dev.config + 0x42) & 0xf;
+
+if (irq == 2) {
+qemu_log_mask(LOG_GUEST_ERROR, "IRQ 2 for PM controller is reserved");
+return;
+}
+
+if (irq != 0) {
+qemu_set_irq(s->isa_irqs_in[irq], level);
+}
+}
+
 static void via_isa_init(Object *obj)
 {
 ViaISAState *s = VIA_ISA(obj);
+DeviceState *dev = DEVICE(s);
 
 object_initialize_child(obj, "rtc", >rtc, TYPE_MC146818_RTC);
 object_initialize_child(obj, "ide", >ide, TYPE_VIA_IDE);
@@ -578,6 +593,8 @@ static void via_isa_init(Object *obj)
 object_initialize_child(obj, "uhci2", >uhci[1], 
TYPE_VT82C686B_USB_UHCI);
 object_initialize_child(obj, "ac97", >ac97, TYPE_VIA_AC97);
 object_initialize_child(obj, "mc97", >mc97, TYPE_VIA_MC97);
+
+qdev_init_gpio_in_named(dev, via_isa_set_pm_irq, "sci", 1);
 }
 
 static const TypeInfo via_isa_info = {
@@ -704,6 +721,8 @@ static void via_isa_realize(PCIDevice *d, Error **errp)
 if (!qdev_realize(DEVICE(>pm), BUS(pci_bus), errp)) {
 return;
 }
+qdev_connect_gpio_out(DEVICE(>pm), 0,
+  qdev_get_gpio_in_named(DEVICE(d), "sci", 0));
 
 /* Function 5: AC97 Audio */
 qdev_prop_set_int32(DEVICE(>ac97), "addr", d->devfn + 5);
-- 
2.42.0




Re: [PATCH v3] hw/i386/acpi-build: Remove build-time assertion on PIIX/ICH9 reset registers being identical

2023-10-03 Thread Bernhard Beschow
The iteration in the subject should have been 1, not 3...

Am 3. Oktober 2023 21:16:58 UTC schrieb Bernhard Beschow :
>Commit 6103451aeb74 ("hw/i386: Build-time assertion on pc/q35 reset register
>being identical.") introduced a build-time check where the addresses of the
>reset registers are expected to be equal. Back then the code to generate AML 
>for
>the reset register in the FADT was common. However, since commit 937d1b58714b
>("pc: acpi: isolate FADT specific data into AcpiFadtData structure") the AML
>gets generated for ICH9 only. There is no need any loger for the assertion, so
>remove it.
>
>Signed-off-by: Bernhard Beschow 
>---
> hw/i386/acpi-build.c | 5 -
> 1 file changed, 5 deletions(-)
>
>diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
>index 95199c8900..6fff1901f5 100644
>--- a/hw/i386/acpi-build.c
>+++ b/hw/i386/acpi-build.c
>@@ -56,7 +56,6 @@
> 
> /* Supported chipsets: */
> #include "hw/southbridge/ich9.h"
>-#include "hw/southbridge/piix.h"
> #include "hw/acpi/pcihp.h"
> #include "hw/i386/fw_cfg.h"
> #include "hw/i386/pc.h"
>@@ -242,10 +241,6 @@ static void acpi_get_pm_info(MachineState *machine, 
>AcpiPmInfo *pm)
> pm->pcihp_io_len =
> object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
> 
>-/* The above need not be conditional on machine type because the reset 
>port
>- * happens to be the same on PIIX (pc) and ICH9 (q35). */
>-QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != PIIX_RCR_IOPORT);
>-
> /* Fill in optional s3/s4 related properties */
> o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
> if (o) {



Re: [PATCH] target/m68k: Map FPU exceptions to FPSR register

2023-10-03 Thread Richard Henderson

On 8/2/23 20:52, Keith Packard via wrote:

Add helpers for reading/writing the 68881 FPSR register so that
changes in floating point exception state can be seen by the
application.

Call these helpers in pre_load/post_load hooks to synchronize
exception state.

Signed-off-by: Keith Packard
---
  target/m68k/cpu.c| 12 +++
  target/m68k/cpu.h|  2 ++
  target/m68k/fpu_helper.c | 72 
  target/m68k/helper.c |  4 +--
  target/m68k/helper.h |  2 ++
  target/m68k/translate.c  |  4 +--
  6 files changed, 92 insertions(+), 4 deletions(-)


Queued to m68k-next.

r~



Re: [PATCH for-8.1] vfio/display: Fix missing update to set backing fields

2023-10-03 Thread Cédric Le Goater

On 10/3/23 19:03, Michael Tokarev wrote:

22.09.2023 12:38, Cédric Le Goater:

On 9/13/23 21:18, Alex Williamson wrote:


Hi Gerd,

Some consultation would be appreciated on this thread to get this patch
out of limbo.  Is there a better solution that what I've proposed?


This does fix a regression reproducible on systems with an Intel Gen 8,
my T480 laptop for instance.

Tested-by: Cédric Le Goater 

Also, queuing it in vfio-next.


Cédric, can we get this in time for 8.1.2 please
(which I'm planning for Oct-14 for now)?  Looks like
it hit quite some people already.


yes. I would like to include this series [1] from Eric in my next PR.
It doesn't need much, an Ack from the s390 team mostly, the rest is
aesthetic. So, I hope this week, else I will send a smaller PR.

Thanks,

C.

[1] 
https://lore.kernel.org/qemu-devel/20231003101530.288864-1-eric.au...@redhat.com/




I dunno what are your plans for vfio-next, maybe this
one (which missed 8.1.0 already) can be pushed as a
bugfix?

Thank you!

/mjt






[PATCH v3] hw/i386/acpi-build: Remove build-time assertion on PIIX/ICH9 reset registers being identical

2023-10-03 Thread Bernhard Beschow
Commit 6103451aeb74 ("hw/i386: Build-time assertion on pc/q35 reset register
being identical.") introduced a build-time check where the addresses of the
reset registers are expected to be equal. Back then the code to generate AML for
the reset register in the FADT was common. However, since commit 937d1b58714b
("pc: acpi: isolate FADT specific data into AcpiFadtData structure") the AML
gets generated for ICH9 only. There is no need any loger for the assertion, so
remove it.

Signed-off-by: Bernhard Beschow 
---
 hw/i386/acpi-build.c | 5 -
 1 file changed, 5 deletions(-)

diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index 95199c8900..6fff1901f5 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -56,7 +56,6 @@
 
 /* Supported chipsets: */
 #include "hw/southbridge/ich9.h"
-#include "hw/southbridge/piix.h"
 #include "hw/acpi/pcihp.h"
 #include "hw/i386/fw_cfg.h"
 #include "hw/i386/pc.h"
@@ -242,10 +241,6 @@ static void acpi_get_pm_info(MachineState *machine, 
AcpiPmInfo *pm)
 pm->pcihp_io_len =
 object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
 
-/* The above need not be conditional on machine type because the reset port
- * happens to be the same on PIIX (pc) and ICH9 (q35). */
-QEMU_BUILD_BUG_ON(ICH9_RST_CNT_IOPORT != PIIX_RCR_IOPORT);
-
 /* Fill in optional s3/s4 related properties */
 o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
 if (o) {
-- 
2.42.0




Re: [PATCH 0/3] vhost: clean up device reset

2023-10-03 Thread Stefan Hajnoczi
On Tue, 3 Oct 2023 at 17:01, Michael S. Tsirkin  wrote:
>
> On Wed, Sep 27, 2023 at 03:27:34PM -0400, Stefan Hajnoczi wrote:
> > Stateful vhost devices may need to free resources or clear device state upon
> > device reset. The vhost-user protocol has a VHOST_USER_RESET_DEVICE message 
> > for
> > this and vDPA has SET_STATUS 0, but only QEMU's vhost-user-scsi device 
> > actually
> > implements this today.
> >
> > This patch series performs device reset across all device types. When
> > virtio_reset() is called, the associated vhost_dev's ->vhost_reset_device() 
> > is
> > called. vhost-user-scsi's one-off implementation is obsoleted and removed.
> >
> > This patch affects behavior as follows:
> > - vhost-kernel: no change in behavior. No ioctl calls are made.
> > - vhost-user: back-ends that negotiate
> >   VHOST_USER_PROTOCOL_F_RESET_DEVICE now receive a
> >   VHOST_USER_DEVICE_RESET message upon device reset. Otherwise there is
> >   no change in behavior. DPDK, SPDK, libvhost-user, and the
> >   vhost-user-backend crate do not negotiate
> >   VHOST_USER_PROTOCOL_F_RESET_DEVICE automatically.
> > - vhost-vdpa: an extra SET_STATUS 0 call is made during device reset.
> >
> > I have tested this series with vhost-net (kernel), vhost-user-blk, and
> > vhost-user-fs (both Rust and legacy C).
> >
> > Stefan Hajnoczi (3):
> >   vhost-user: do not send RESET_OWNER on device reset
> >   vhost-backend: remove vhost_kernel_reset_device()
> >   virtio: call ->vhost_reset_device() during reset
>
> Build failure:
> https://gitlab.com/mstredhat/qemu/-/jobs/5215049540

Sorry about that, will fix.

Stefan

> >  include/hw/virtio/vhost.h |  3 +++
> >  hw/scsi/vhost-user-scsi.c | 20 
> >  hw/virtio/vhost-backend.c |  6 --
> >  hw/virtio/vhost-user.c| 13 +
> >  hw/virtio/vhost.c |  9 +
> >  hw/virtio/virtio.c|  4 
> >  6 files changed, 25 insertions(+), 30 deletions(-)
> >
> > --
> > 2.41.0
>
>



Re: [PATCH] analyze-migration: ignore RAM_SAVE_FLAG_MULTIFD_FLUSH

2023-10-03 Thread Peter Xu
On Wed, Sep 27, 2023 at 02:32:37PM +0400, Marc-André Lureau wrote:
> On Tue, Sep 26, 2023 at 12:19 PM  wrote:
> >
> > From: Marc-André Lureau 
> >
> > Traceback (most recent call last):
> >   File "scripts/analyze-migration.py", line 605, in 
> > dump.read(dump_memory = args.memory)
> >   File "scripts/analyze-migration.py", line 542, in read
> > section.read()
> >   File "scripts/analyze-migration.py", line 214, in read
> > raise Exception("Unknown RAM flags: %x" % flags)
> > Exception: Unknown RAM flags: 200
> >
> > See commit 77c259a4cb ("multifd: Create property 
> > multifd-flush-after-each-section")
> >
> > Signed-off-by: Marc-André Lureau 
> 
> cc Peter and Juan

Sorry to be late..

It's actually a bug to have MULTIFD_FLUSH in the stream in the first place,
though..  the fix to make that MULTIFD_FLUSH disappear should be in Juan's
next pull.

IIUC logically analyze-migration.py shouldn't be able to understand
multifd, because it relies on a solo stream file, while multifd is already
against it.  We plan to add "file:" support for multifd, but even with that
I doubt whether there'll be MULTIFD_FLUSH kept in the final file because
it's only used to sync threads.

Thanks,

> 
> > ---
> >  scripts/analyze-migration.py | 4 
> >  1 file changed, 4 insertions(+)
> >
> > diff --git a/scripts/analyze-migration.py b/scripts/analyze-migration.py
> > index b82a1b0c58..082424558b 100755
> > --- a/scripts/analyze-migration.py
> > +++ b/scripts/analyze-migration.py
> > @@ -111,6 +111,8 @@ class RamSection(object):
> >  RAM_SAVE_FLAG_CONTINUE = 0x20
> >  RAM_SAVE_FLAG_XBZRLE   = 0x40
> >  RAM_SAVE_FLAG_HOOK = 0x80
> > +RAM_SAVE_FLAG_COMPRESS_PAGE = 0x100
> > +RAM_SAVE_FLAG_MULTIFD_FLUSH = 0x200
> >
> >  def __init__(self, file, version_id, ramargs, section_key):
> >  if version_id != 4:
> > @@ -205,6 +207,8 @@ def read(self):
> >  raise Exception("XBZRLE RAM compression is not supported 
> > yet")
> >  elif flags & self.RAM_SAVE_FLAG_HOOK:
> >  raise Exception("RAM hooks don't make sense with files")
> > +if flags & self.RAM_SAVE_FLAG_MULTIFD_FLUSH:
> > +continue
> >
> >  # End of RAM section
> >  if flags & self.RAM_SAVE_FLAG_EOS:
> > --
> > 2.41.0
> >
> >
> 
> 
> -- 
> Marc-André Lureau
> 

-- 
Peter Xu




Re: [PULL 38/47] linux-user: Rename mmap_reserve to mmap_reserve_or_unmap

2023-10-03 Thread Richard Henderson

On 9/18/23 01:35, Andreas Schwab wrote:

On Jul 15 2023, Richard Henderson wrote:


@@ -776,9 +776,15 @@ static void mmap_reserve(abi_ulong start, abi_ulong len)
  real_len = real_last - real_start + 1;
  host_start = g2h_untagged(real_start);
  
-ptr = mmap(host_start, real_len, PROT_NONE,

-   MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
-assert(ptr == host_start);
+if (reserved_va) {
+void *ptr = mmap(host_start, real_len, PROT_NONE,
+ MAP_FIXED | MAP_ANONYMOUS
+ | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
+assert(ptr == host_start);
+} else {
+int ret = munmap(host_start, real_len);
+assert(ret == 0);


munmap can fail if vm.max_map_count is exceeded.  See the attached test
(must be run from the current directory):

$ qemu-riscv64 ./test-free
qemu-riscv64: ../linux-user/mmap.c:801: mmap_reserve_or_unmap: Assertion `ret 
== 0' failed.
**
ERROR:../accel/tcg/cpu-exec.c:532:cpu_exec_longjmp_cleanup: assertion failed: 
(cpu == current_cpu)
Bail out! ERROR:../accel/tcg/cpu-exec.c:532:cpu_exec_longjmp_cleanup: assertion 
failed: (cpu == current_cpu)



Thanks.  Sent a fix.


r~



[PATCH] linux-user: Propagate failure in mmap_reserve_or_unmap back to target_munmap

2023-10-03 Thread Richard Henderson
Do not assert success, but return any failure received.
Additionally, fix the method of earlier error return in target_munmap.

Reported-by: Andreas Schwab 
Signed-off-by: Richard Henderson 
---
 linux-user/mmap.c | 30 +-
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index 8eaf57b208..2d8b7842d1 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -778,7 +778,7 @@ fail:
 return -1;
 }
 
-static void mmap_reserve_or_unmap(abi_ulong start, abi_ulong len)
+static int mmap_reserve_or_unmap(abi_ulong start, abi_ulong len)
 {
 abi_ulong real_start;
 abi_ulong real_last;
@@ -807,7 +807,7 @@ static void mmap_reserve_or_unmap(abi_ulong start, 
abi_ulong len)
 prot |= page_get_flags(a + 1);
 }
 if (prot != 0) {
-return;
+return 0;
 }
 } else {
 for (prot = 0, a = real_start; a < start; a += TARGET_PAGE_SIZE) {
@@ -825,7 +825,7 @@ static void mmap_reserve_or_unmap(abi_ulong start, 
abi_ulong len)
 }
 
 if (real_last < real_start) {
-return;
+return 0;
 }
 }
 
@@ -836,32 +836,36 @@ static void mmap_reserve_or_unmap(abi_ulong start, 
abi_ulong len)
 void *ptr = mmap(host_start, real_len, PROT_NONE,
  MAP_FIXED | MAP_ANONYMOUS
  | MAP_PRIVATE | MAP_NORESERVE, -1, 0);
-assert(ptr == host_start);
-} else {
-int ret = munmap(host_start, real_len);
-assert(ret == 0);
+return ptr == host_start ? 0 : -1;
 }
+return munmap(host_start, real_len);
 }
 
 int target_munmap(abi_ulong start, abi_ulong len)
 {
+int ret;
+
 trace_target_munmap(start, len);
 
 if (start & ~TARGET_PAGE_MASK) {
-return -TARGET_EINVAL;
+errno = EINVAL;
+return -1;
 }
 len = TARGET_PAGE_ALIGN(len);
 if (len == 0 || !guest_range_valid_untagged(start, len)) {
-return -TARGET_EINVAL;
+errno = EINVAL;
+return -1;
 }
 
 mmap_lock();
-mmap_reserve_or_unmap(start, len);
-page_set_flags(start, start + len - 1, 0);
-shm_region_rm_complete(start, start + len - 1);
+ret = mmap_reserve_or_unmap(start, len);
+if (likely(ret == 0)) {
+page_set_flags(start, start + len - 1, 0);
+shm_region_rm_complete(start, start + len - 1);
+}
 mmap_unlock();
 
-return 0;
+return ret;
 }
 
 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
-- 
2.34.1




Re: [PATCH 0/3] vhost: clean up device reset

2023-10-03 Thread Michael S. Tsirkin
On Wed, Sep 27, 2023 at 03:27:34PM -0400, Stefan Hajnoczi wrote:
> Stateful vhost devices may need to free resources or clear device state upon
> device reset. The vhost-user protocol has a VHOST_USER_RESET_DEVICE message 
> for
> this and vDPA has SET_STATUS 0, but only QEMU's vhost-user-scsi device 
> actually
> implements this today.
> 
> This patch series performs device reset across all device types. When
> virtio_reset() is called, the associated vhost_dev's ->vhost_reset_device() is
> called. vhost-user-scsi's one-off implementation is obsoleted and removed.
> 
> This patch affects behavior as follows:
> - vhost-kernel: no change in behavior. No ioctl calls are made.
> - vhost-user: back-ends that negotiate
>   VHOST_USER_PROTOCOL_F_RESET_DEVICE now receive a
>   VHOST_USER_DEVICE_RESET message upon device reset. Otherwise there is
>   no change in behavior. DPDK, SPDK, libvhost-user, and the
>   vhost-user-backend crate do not negotiate
>   VHOST_USER_PROTOCOL_F_RESET_DEVICE automatically.
> - vhost-vdpa: an extra SET_STATUS 0 call is made during device reset.
> 
> I have tested this series with vhost-net (kernel), vhost-user-blk, and
> vhost-user-fs (both Rust and legacy C).
> 
> Stefan Hajnoczi (3):
>   vhost-user: do not send RESET_OWNER on device reset
>   vhost-backend: remove vhost_kernel_reset_device()
>   virtio: call ->vhost_reset_device() during reset

Build failure:
https://gitlab.com/mstredhat/qemu/-/jobs/5215049540


>  include/hw/virtio/vhost.h |  3 +++
>  hw/scsi/vhost-user-scsi.c | 20 
>  hw/virtio/vhost-backend.c |  6 --
>  hw/virtio/vhost-user.c| 13 +
>  hw/virtio/vhost.c |  9 +
>  hw/virtio/virtio.c|  4 
>  6 files changed, 25 insertions(+), 30 deletions(-)
> 
> -- 
> 2.41.0




Re: [PATCH v2 4/5] hw/intc/apic: Rename x86_cpu_apic_create() -> x86_cpu_apic_new()

2023-10-03 Thread Peter Xu
On Tue, Oct 03, 2023 at 10:27:27AM +0200, Philippe Mathieu-Daudé wrote:
> x86_cpu_apic_create()'s Error** parameter is unused, drop it.
> 
> While there is no convention, QDev methods are usually named
> with _new() / _realize() suffixes. Rename as appropriate.
> 
> Signed-off-by: Philippe Mathieu-Daudé 

Reviewed-by: Peter Xu 

-- 
Peter Xu




Re: [PATCH v2 3/5] hw/i386/apic: Simplify apic_get_class()

2023-10-03 Thread Peter Xu
On Tue, Oct 03, 2023 at 10:27:26AM +0200, Philippe Mathieu-Daudé wrote:
> Now than apic_get_class() can not fail, remove its
> Error** parameter. It can't return NULL neither, so
> simplify x86_cpu_apic_create().
> 
> Signed-off-by: Philippe Mathieu-Daudé 

Reviewed-by: Peter Xu 

-- 
Peter Xu




Re: [PATCH v2 1/5] hw/intc/apic: Use ERRP_GUARD() in apic_common_realize()

2023-10-03 Thread Peter Xu
On Tue, Oct 03, 2023 at 10:27:24AM +0200, Philippe Mathieu-Daudé wrote:
> APICCommonClass::realize() is a DeviceRealize() handler which
> take an Error** parameter and can fail. Do not proceed further
> on failure.
> 
> Signed-off-by: Philippe Mathieu-Daudé 

Reviewed-by: Peter Xu 

-- 
Peter Xu




Re: [PATCH v2 2/5] hw/i386/apic: Defer error check from apic_get_class to kvm_apic_realize

2023-10-03 Thread Peter Xu
On Tue, Oct 03, 2023 at 10:27:25AM +0200, Philippe Mathieu-Daudé wrote:
> apic_get_class() isn't supposed to fail. kvm_apic_realize() is
> DeviceRealize() handler, which can fail. Defer the error check
> to the latter.
> 
> Signed-off-by: Philippe Mathieu-Daudé 
> ---
>  hw/i386/kvm/apic.c   | 5 +
>  target/i386/cpu-sysemu.c | 8 
>  2 files changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/i386/kvm/apic.c b/hw/i386/kvm/apic.c
> index 1e89ca0899..4883308247 100644
> --- a/hw/i386/kvm/apic.c
> +++ b/hw/i386/kvm/apic.c
> @@ -228,6 +228,11 @@ static void kvm_apic_realize(DeviceState *dev, Error 
> **errp)
>  {
>  APICCommonState *s = APIC_COMMON(dev);
>  
> +if (!kvm_irqchip_in_kernel()) {
> +error_setg(errp, "KVM does not support userspace APIC");
> +return;
> +}

IMO the code reads a bit weird, that we are already trying to create
kvm-apic even if !kvm_irqchip_in_kernel()..

Would it be better to check this in i386's kvm_arch_irqchip_create()?

> +
>  memory_region_init_io(>io_memory, OBJECT(s), _apic_io_ops, s,
>"kvm-apic-msi", APIC_SPACE_SIZE);
>  
> diff --git a/target/i386/cpu-sysemu.c b/target/i386/cpu-sysemu.c
> index 2375e48178..6a228c9178 100644
> --- a/target/i386/cpu-sysemu.c
> +++ b/target/i386/cpu-sysemu.c
> @@ -253,10 +253,6 @@ APICCommonClass *apic_get_class(Error **errp)
>  
>  /* TODO: in-kernel irqchip for hvf */
>  if (kvm_enabled()) {
> -if (!kvm_irqchip_in_kernel()) {
> -error_setg(errp, "KVM does not support userspace APIC");
> -return NULL;
> -}
>  apic_type = "kvm-apic";
>  } else if (xen_enabled()) {
>  apic_type = "xen-apic";
> @@ -272,10 +268,6 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
>  APICCommonState *apic;
>  APICCommonClass *apic_class = apic_get_class(errp);
>  
> -if (!apic_class) {
> -return;
> -}
> -
>  cpu->apic_state = 
> DEVICE(object_new_with_class(OBJECT_CLASS(apic_class)));
>  object_property_add_child(OBJECT(cpu), "lapic",
>OBJECT(cpu->apic_state));
> -- 
> 2.41.0
> 

-- 
Peter Xu




[PATCH v15 9/9] docs/system: add basic virtio-gpu documentation

2023-10-03 Thread Gurchetan Singh
This adds basic documentation for virtio-gpu.

Suggested-by: Akihiko Odaki 
Signed-off-by: Gurchetan Singh 
Tested-by: Alyssa Ross 
Tested-by: Emmanouil Pitsidianakis 
Tested-by: Akihiko Odaki 
Reviewed-by: Emmanouil Pitsidianakis 
Reviewed-by: Antonio Caggiano 
Reviewed-by: Akihiko Odaki 
---
 docs/system/device-emulation.rst   |   1 +
 docs/system/devices/virtio-gpu.rst | 112 +
 2 files changed, 113 insertions(+)
 create mode 100644 docs/system/devices/virtio-gpu.rst

diff --git a/docs/system/device-emulation.rst b/docs/system/device-emulation.rst
index 4491c4cbf7..1167f3a9f2 100644
--- a/docs/system/device-emulation.rst
+++ b/docs/system/device-emulation.rst
@@ -91,6 +91,7 @@ Emulated Devices
devices/nvme.rst
devices/usb.rst
devices/vhost-user.rst
+   devices/virtio-gpu.rst
devices/virtio-pmem.rst
devices/vhost-user-rng.rst
devices/canokey.rst
diff --git a/docs/system/devices/virtio-gpu.rst 
b/docs/system/devices/virtio-gpu.rst
new file mode 100644
index 00..cb73dd7998
--- /dev/null
+++ b/docs/system/devices/virtio-gpu.rst
@@ -0,0 +1,112 @@
+..
+   SPDX-License-Identifier: GPL-2.0-or-later
+
+virtio-gpu
+==
+
+This document explains the setup and usage of the virtio-gpu device.
+The virtio-gpu device paravirtualizes the GPU and display controller.
+
+Linux kernel support
+
+
+virtio-gpu requires a guest Linux kernel built with the
+``CONFIG_DRM_VIRTIO_GPU`` option.
+
+QEMU virtio-gpu variants
+
+
+QEMU virtio-gpu device variants come in the following form:
+
+ * ``virtio-vga[-BACKEND]``
+ * ``virtio-gpu[-BACKEND][-INTERFACE]``
+ * ``vhost-user-vga``
+ * ``vhost-user-pci``
+
+**Backends:** QEMU provides a 2D virtio-gpu backend, and two accelerated
+backends: virglrenderer ('gl' device label) and rutabaga_gfx ('rutabaga'
+device label).  There is a vhost-user backend that runs the graphics stack
+in a separate process for improved isolation.
+
+**Interfaces:** QEMU further categorizes virtio-gpu device variants based
+on the interface exposed to the guest. The interfaces can be classified
+into VGA and non-VGA variants. The VGA ones are prefixed with virtio-vga
+or vhost-user-vga while the non-VGA ones are prefixed with virtio-gpu or
+vhost-user-gpu.
+
+The VGA ones always use the PCI interface, but for the non-VGA ones, the
+user can further pick between MMIO or PCI. For MMIO, the user can suffix
+the device name with -device, though vhost-user-gpu does not support MMIO.
+For PCI, the user can suffix it with -pci. Without these suffixes, the
+platform default will be chosen.
+
+virtio-gpu 2d
+-
+
+The default 2D backend only performs 2D operations. The guest needs to
+employ a software renderer for 3D graphics.
+
+Typically, the software renderer is provided by `Mesa`_ or `SwiftShader`_.
+Mesa's implementations (LLVMpipe, Lavapipe and virgl below) work out of box
+on typical modern Linux distributions.
+
+.. parsed-literal::
+-device virtio-gpu
+
+.. _Mesa: https://www.mesa3d.org/
+.. _SwiftShader: https://github.com/google/swiftshader
+
+virtio-gpu virglrenderer
+
+
+When using virgl accelerated graphics mode in the guest, OpenGL API calls
+are translated into an intermediate representation (see `Gallium3D`_). The
+intermediate representation is communicated to the host and the
+`virglrenderer`_ library on the host translates the intermediate
+representation back to OpenGL API calls.
+
+.. parsed-literal::
+-device virtio-gpu-gl
+
+.. _Gallium3D: https://www.freedesktop.org/wiki/Software/gallium/
+.. _virglrenderer: https://gitlab.freedesktop.org/virgl/virglrenderer/
+
+virtio-gpu rutabaga
+---
+
+virtio-gpu can also leverage rutabaga_gfx to provide `gfxstream`_
+rendering and `Wayland display passthrough`_.  With the gfxstream rendering
+mode, GLES and Vulkan calls are forwarded to the host with minimal
+modification.
+
+The crosvm book provides directions on how to build a `gfxstream-enabled
+rutabaga`_ and launch a `guest Wayland proxy`_.
+
+This device does require host blob support (``hostmem`` field below). The
+``hostmem`` field specifies the size of virtio-gpu host memory window.
+This is typically between 256M and 8G.
+
+At least one virtio-gpu capability set ("capset") must be specified when
+starting the device.  The currently capsets supported are ``gfxstream-vulkan``
+and ``cross-domain`` for Linux guests. For Android guests, the experimental
+``x-gfxstream-gles`` and ``x-gfxstream-composer`` capsets are also supported.
+
+The device will try to auto-detect the wayland socket path if the
+``cross-domain`` capset name is set.  The user may optionally specify
+``wayland-socket-path`` for non-standard paths.
+
+The ``wsi`` option can be set to ``surfaceless`` or ``headless``.
+Surfaceless doesn't create a native window surface, but does copy from the
+render target to the Pixman buffer if a virtio-gpu 2D hypercall is 

[PATCH v15 8/9] gfxstream + rutabaga: enable rutabaga

2023-10-03 Thread Gurchetan Singh
This change enables rutabaga to receive virtio-gpu-3d hypercalls
when it is active.

Signed-off-by: Gurchetan Singh 
Tested-by: Alyssa Ross 
Tested-by: Emmanouil Pitsidianakis 
Tested-by: Akihiko Odaki 
Reviewed-by: Antonio Caggiano 
Reviewed-by: Emmanouil Pitsidianakis 
Reviewed-by: Akihiko Odaki 
---
 hw/display/virtio-gpu-base.c | 3 ++-
 hw/display/virtio-gpu.c  | 5 +++--
 softmmu/qdev-monitor.c   | 3 +++
 softmmu/vl.c | 1 +
 4 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/hw/display/virtio-gpu-base.c b/hw/display/virtio-gpu-base.c
index 4f2b0ba1f3..50c5373b65 100644
--- a/hw/display/virtio-gpu-base.c
+++ b/hw/display/virtio-gpu-base.c
@@ -223,7 +223,8 @@ virtio_gpu_base_get_features(VirtIODevice *vdev, uint64_t 
features,
 {
 VirtIOGPUBase *g = VIRTIO_GPU_BASE(vdev);
 
-if (virtio_gpu_virgl_enabled(g->conf)) {
+if (virtio_gpu_virgl_enabled(g->conf) ||
+virtio_gpu_rutabaga_enabled(g->conf)) {
 features |= (1 << VIRTIO_GPU_F_VIRGL);
 }
 if (virtio_gpu_edid_enabled(g->conf)) {
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index be16efbd38..6efd15b6ae 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -1363,8 +1363,9 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error 
**errp)
 VirtIOGPU *g = VIRTIO_GPU(qdev);
 
 if (virtio_gpu_blob_enabled(g->parent_obj.conf)) {
-if (!virtio_gpu_have_udmabuf()) {
-error_setg(errp, "cannot enable blob resources without udmabuf");
+if (!virtio_gpu_rutabaga_enabled(g->parent_obj.conf) &&
+!virtio_gpu_have_udmabuf()) {
+error_setg(errp, "need rutabaga or udmabuf for blob resources");
 return;
 }
 
diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
index 74f4e41338..1b8005ae55 100644
--- a/softmmu/qdev-monitor.c
+++ b/softmmu/qdev-monitor.c
@@ -86,6 +86,9 @@ static const QDevAlias qdev_alias_table[] = {
 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI },
 { "virtio-gpu-gl-device", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_MMIO },
 { "virtio-gpu-gl-pci", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_PCI },
+{ "virtio-gpu-rutabaga-device", "virtio-gpu-rutabaga",
+  QEMU_ARCH_VIRTIO_MMIO },
+{ "virtio-gpu-rutabaga-pci", "virtio-gpu-rutabaga", QEMU_ARCH_VIRTIO_PCI },
 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO },
 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW },
 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI },
diff --git a/softmmu/vl.c b/softmmu/vl.c
index 98e071e63b..dd82c6eb13 100644
--- a/softmmu/vl.c
+++ b/softmmu/vl.c
@@ -215,6 +215,7 @@ static struct {
 { .driver = "ati-vga",  .flag = _vga   },
 { .driver = "vhost-user-vga",   .flag = _vga   },
 { .driver = "virtio-vga-gl",.flag = _vga   },
+{ .driver = "virtio-vga-rutabaga",  .flag = _vga   },
 };
 
 static QemuOptsList qemu_rtc_opts = {
-- 
2.42.0.582.g8ccd20d70d-goog




[PATCH v15 2/9] virtio-gpu: CONTEXT_INIT feature

2023-10-03 Thread Gurchetan Singh
From: Antonio Caggiano 

The feature can be enabled when a backend wants it.

Signed-off-by: Antonio Caggiano 
Signed-off-by: Gurchetan Singh 
Tested-by: Alyssa Ross 
Tested-by: Akihiko Odaki 
Tested-by: Huang Rui 
Acked-by: Huang Rui 
Reviewed-by: Marc-André Lureau 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Akihiko Odaki 
---
 hw/display/virtio-gpu-base.c   | 3 +++
 include/hw/virtio/virtio-gpu.h | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/hw/display/virtio-gpu-base.c b/hw/display/virtio-gpu-base.c
index ca1fb7b16f..4f2b0ba1f3 100644
--- a/hw/display/virtio-gpu-base.c
+++ b/hw/display/virtio-gpu-base.c
@@ -232,6 +232,9 @@ virtio_gpu_base_get_features(VirtIODevice *vdev, uint64_t 
features,
 if (virtio_gpu_blob_enabled(g->conf)) {
 features |= (1 << VIRTIO_GPU_F_RESOURCE_BLOB);
 }
+if (virtio_gpu_context_init_enabled(g->conf)) {
+features |= (1 << VIRTIO_GPU_F_CONTEXT_INIT);
+}
 
 return features;
 }
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index 390c4642b8..8377c365ef 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -93,6 +93,7 @@ enum virtio_gpu_base_conf_flags {
 VIRTIO_GPU_FLAG_EDID_ENABLED,
 VIRTIO_GPU_FLAG_DMABUF_ENABLED,
 VIRTIO_GPU_FLAG_BLOB_ENABLED,
+VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED,
 };
 
 #define virtio_gpu_virgl_enabled(_cfg) \
@@ -105,6 +106,8 @@ enum virtio_gpu_base_conf_flags {
 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_DMABUF_ENABLED))
 #define virtio_gpu_blob_enabled(_cfg) \
 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_BLOB_ENABLED))
+#define virtio_gpu_context_init_enabled(_cfg) \
+(_cfg.flags & (1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED))
 
 struct virtio_gpu_base_conf {
 uint32_t max_outputs;
-- 
2.42.0.582.g8ccd20d70d-goog




[PATCH v15 6/9] gfxstream + rutabaga: add initial support for gfxstream

2023-10-03 Thread Gurchetan Singh
This adds initial support for gfxstream and cross-domain.  Both
features rely on virtio-gpu blob resources and context types, which
are also implemented in this patch.

gfxstream has a long and illustrious history in Android graphics
paravirtualization.  It has been powering graphics in the Android
Studio Emulator for more than a decade, which is the main developer
platform.

Originally conceived by Jesse Hall, it was first known as "EmuGL" [a].
The key design characteristic was a 1:1 threading model and
auto-generation, which fit nicely with the OpenGLES spec.  It also
allowed easy layering with ANGLE on the host, which provides the GLES
implementations on Windows or MacOS enviroments.

gfxstream has traditionally been maintained by a single engineer, and
between 2015 to 2021, the goldfish throne passed to Frank Yang.
Historians often remark this glorious reign ("pax gfxstreama" is the
academic term) was comparable to that of Augustus and both Queen
Elizabeths.  Just to name a few accomplishments in a resplendent
panoply: higher versions of GLES, address space graphics, snapshot
support and CTS compliant Vulkan [b].

One major drawback was the use of out-of-tree goldfish drivers.
Android engineers didn't know much about DRM/KMS and especially TTM so
a simple guest to host pipe was conceived.

Luckily, virtio-gpu 3D started to emerge in 2016 due to the work of
the Mesa/virglrenderer communities.  In 2018, the initial virtio-gpu
port of gfxstream was done by Cuttlefish enthusiast Alistair Delva.
It was a symbol compatible replacement of virglrenderer [c] and named
"AVDVirglrenderer".  This implementation forms the basis of the
current gfxstream host implementation still in use today.

cross-domain support follows a similar arc.  Originally conceived by
Wayland aficionado David Reveman and crosvm enjoyer Zach Reizner in
2018, it initially relied on the downstream "virtio-wl" device.

In 2020 and 2021, virtio-gpu was extended to include blob resources
and multiple timelines by yours truly, features gfxstream/cross-domain
both require to function correctly.

Right now, we stand at the precipice of a truly fantastic possibility:
the Android Emulator powered by upstream QEMU and upstream Linux
kernel.  gfxstream will then be packaged properfully, and app
developers can even fix gfxstream bugs on their own if they encounter
them.

It's been quite the ride, my friends.  Where will gfxstream head next,
nobody really knows.  I wouldn't be surprised if it's around for
another decade, maintained by a new generation of Android graphics
enthusiasts.

Technical details:
  - Very simple initial display integration: just used Pixman
  - Largely, 1:1 mapping of virtio-gpu hypercalls to rutabaga function
calls

Next steps for Android VMs:
  - The next step would be improving display integration and UI interfaces
with the goal of the QEMU upstream graphics being in an emulator
release [d].

Next steps for Linux VMs for display virtualization:
  - For widespread distribution, someone needs to package Sommelier or the
wayland-proxy-virtwl [e] ideally into Debian main. In addition, newer
versions of the Linux kernel come with DRM_VIRTIO_GPU_KMS option,
which allows disabling KMS hypercalls.  If anyone cares enough, it'll
probably be possible to build a custom VM variant that uses this display
virtualization strategy.

[a] https://android-review.googlesource.com/c/platform/development/+/34470
[b] 
https://android-review.googlesource.com/q/topic:%22vulkan-hostconnection-start%22
[c] 
https://android-review.googlesource.com/c/device/generic/goldfish-opengl/+/761927
[d] https://developer.android.com/studio/releases/emulator
[e] https://github.com/talex5/wayland-proxy-virtwl

Signed-off-by: Gurchetan Singh 
Tested-by: Alyssa Ross 
Tested-by: Emmanouil Pitsidianakis 
Tested-by: Akihiko Odaki 
Reviewed-by: Emmanouil Pitsidianakis 
Reviewed-by: Antonio Caggiano 
Reviewed-by: Akihiko Odaki 
---
 hw/display/virtio-gpu-pci-rutabaga.c |   47 ++
 hw/display/virtio-gpu-rutabaga.c | 1122 ++
 hw/display/virtio-vga-rutabaga.c |   50 ++
 3 files changed, 1219 insertions(+)
 create mode 100644 hw/display/virtio-gpu-pci-rutabaga.c
 create mode 100644 hw/display/virtio-gpu-rutabaga.c
 create mode 100644 hw/display/virtio-vga-rutabaga.c

diff --git a/hw/display/virtio-gpu-pci-rutabaga.c 
b/hw/display/virtio-gpu-pci-rutabaga.c
new file mode 100644
index 00..c96729e198
--- /dev/null
+++ b/hw/display/virtio-gpu-pci-rutabaga.c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/module.h"
+#include "hw/pci/pci.h"
+#include "hw/qdev-properties.h"
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-bus.h"
+#include "hw/virtio/virtio-gpu-pci.h"
+#include "qom/object.h"
+
+#define TYPE_VIRTIO_GPU_RUTABAGA_PCI "virtio-gpu-rutabaga-pci"
+OBJECT_DECLARE_SIMPLE_TYPE(VirtIOGPURutabagaPCI, 

[PATCH v15 1/9] virtio: Add shared memory capability

2023-10-03 Thread Gurchetan Singh
From: "Dr. David Alan Gilbert" 

Define a new capability type 'VIRTIO_PCI_CAP_SHARED_MEMORY_CFG' to allow
defining shared memory regions with sizes and offsets of 2^32 and more.
Multiple instances of the capability are allowed and distinguished
by a device-specific 'id'.

Signed-off-by: Dr. David Alan Gilbert 
Signed-off-by: Antonio Caggiano 
Signed-off-by: Gurchetan Singh 
Tested-by: Alyssa Ross 
Tested-by: Huang Rui 
Tested-by: Akihiko Odaki 
Acked-by: Huang Rui 
Reviewed-by: Gurchetan Singh 
Reviewed-by: Akihiko Odaki 
---
 hw/virtio/virtio-pci.c | 18 ++
 include/hw/virtio/virtio-pci.h |  4 
 2 files changed, 22 insertions(+)

diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index edbc0daa18..da8c9ea12d 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -1435,6 +1435,24 @@ static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
 return offset;
 }
 
+int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy,
+   uint8_t bar, uint64_t offset, uint64_t length,
+   uint8_t id)
+{
+struct virtio_pci_cap64 cap = {
+.cap.cap_len = sizeof cap,
+.cap.cfg_type = VIRTIO_PCI_CAP_SHARED_MEMORY_CFG,
+};
+
+cap.cap.bar = bar;
+cap.cap.length = cpu_to_le32(length);
+cap.length_hi = cpu_to_le32(length >> 32);
+cap.cap.offset = cpu_to_le32(offset);
+cap.offset_hi = cpu_to_le32(offset >> 32);
+cap.cap.id = id;
+return virtio_pci_add_mem_cap(proxy, );
+}
+
 static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
unsigned size)
 {
diff --git a/include/hw/virtio/virtio-pci.h b/include/hw/virtio/virtio-pci.h
index ab2051b64b..5a3f182f99 100644
--- a/include/hw/virtio/virtio-pci.h
+++ b/include/hw/virtio/virtio-pci.h
@@ -264,4 +264,8 @@ unsigned virtio_pci_optimal_num_queues(unsigned 
fixed_queues);
 void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue 
*vq,
   int n, bool assign,
   bool with_irqfd);
+
+int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset,
+   uint64_t length, uint8_t id);
+
 #endif
-- 
2.42.0.582.g8ccd20d70d-goog




[PATCH v15 3/9] virtio-gpu: hostmem

2023-10-03 Thread Gurchetan Singh
From: Gerd Hoffmann 

Use VIRTIO_GPU_SHM_ID_HOST_VISIBLE as id for virtio-gpu.

Signed-off-by: Antonio Caggiano 
Tested-by: Alyssa Ross 
Tested-by: Akihiko Odaki 
Tested-by: Huang Rui 
Acked-by: Huang Rui 
Acked-by: Michael S. Tsirkin 
Reviewed-by: Akihiko Odaki 
---
 hw/display/virtio-gpu-pci.c| 14 ++
 hw/display/virtio-gpu.c|  1 +
 hw/display/virtio-vga.c| 33 -
 include/hw/virtio/virtio-gpu.h |  5 +
 4 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/hw/display/virtio-gpu-pci.c b/hw/display/virtio-gpu-pci.c
index 93f214ff58..da6a99f038 100644
--- a/hw/display/virtio-gpu-pci.c
+++ b/hw/display/virtio-gpu-pci.c
@@ -33,6 +33,20 @@ static void virtio_gpu_pci_base_realize(VirtIOPCIProxy 
*vpci_dev, Error **errp)
 DeviceState *vdev = DEVICE(g);
 int i;
 
+if (virtio_gpu_hostmem_enabled(g->conf)) {
+vpci_dev->msix_bar_idx = 1;
+vpci_dev->modern_mem_bar_idx = 2;
+memory_region_init(>hostmem, OBJECT(g), "virtio-gpu-hostmem",
+   g->conf.hostmem);
+pci_register_bar(_dev->pci_dev, 4,
+ PCI_BASE_ADDRESS_SPACE_MEMORY |
+ PCI_BASE_ADDRESS_MEM_PREFETCH |
+ PCI_BASE_ADDRESS_MEM_TYPE_64,
+ >hostmem);
+virtio_pci_add_shm_cap(vpci_dev, 4, 0, g->conf.hostmem,
+   VIRTIO_GPU_SHM_ID_HOST_VISIBLE);
+}
+
 virtio_pci_force_virtio_1(vpci_dev);
 if (!qdev_realize(vdev, BUS(_dev->bus), errp)) {
 return;
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 93857ad523..5585558855 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -1511,6 +1511,7 @@ static Property virtio_gpu_properties[] = {
  256 * MiB),
 DEFINE_PROP_BIT("blob", VirtIOGPU, parent_obj.conf.flags,
 VIRTIO_GPU_FLAG_BLOB_ENABLED, false),
+DEFINE_PROP_SIZE("hostmem", VirtIOGPU, parent_obj.conf.hostmem, 0),
 DEFINE_PROP_END_OF_LIST(),
 };
 
diff --git a/hw/display/virtio-vga.c b/hw/display/virtio-vga.c
index e6fb0aa876..c8552ff760 100644
--- a/hw/display/virtio-vga.c
+++ b/hw/display/virtio-vga.c
@@ -115,17 +115,32 @@ static void virtio_vga_base_realize(VirtIOPCIProxy 
*vpci_dev, Error **errp)
 pci_register_bar(_dev->pci_dev, 0,
  PCI_BASE_ADDRESS_MEM_PREFETCH, >vram);
 
-/*
- * Configure virtio bar and regions
- *
- * We use bar #2 for the mmio regions, to be compatible with stdvga.
- * virtio regions are moved to the end of bar #2, to make room for
- * the stdvga mmio registers at the start of bar #2.
- */
-vpci_dev->modern_mem_bar_idx = 2;
-vpci_dev->msix_bar_idx = 4;
 vpci_dev->modern_io_bar_idx = 5;
 
+if (!virtio_gpu_hostmem_enabled(g->conf)) {
+/*
+ * Configure virtio bar and regions
+ *
+ * We use bar #2 for the mmio regions, to be compatible with stdvga.
+ * virtio regions are moved to the end of bar #2, to make room for
+ * the stdvga mmio registers at the start of bar #2.
+ */
+vpci_dev->modern_mem_bar_idx = 2;
+vpci_dev->msix_bar_idx = 4;
+} else {
+vpci_dev->msix_bar_idx = 1;
+vpci_dev->modern_mem_bar_idx = 2;
+memory_region_init(>hostmem, OBJECT(g), "virtio-gpu-hostmem",
+   g->conf.hostmem);
+pci_register_bar(_dev->pci_dev, 4,
+ PCI_BASE_ADDRESS_SPACE_MEMORY |
+ PCI_BASE_ADDRESS_MEM_PREFETCH |
+ PCI_BASE_ADDRESS_MEM_TYPE_64,
+ >hostmem);
+virtio_pci_add_shm_cap(vpci_dev, 4, 0, g->conf.hostmem,
+   VIRTIO_GPU_SHM_ID_HOST_VISIBLE);
+}
+
 if (!(vpci_dev->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ)) {
 /*
  * with page-per-vq=off there is no padding space we can use
diff --git a/include/hw/virtio/virtio-gpu.h b/include/hw/virtio/virtio-gpu.h
index 8377c365ef..de4f624e94 100644
--- a/include/hw/virtio/virtio-gpu.h
+++ b/include/hw/virtio/virtio-gpu.h
@@ -108,12 +108,15 @@ enum virtio_gpu_base_conf_flags {
 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_BLOB_ENABLED))
 #define virtio_gpu_context_init_enabled(_cfg) \
 (_cfg.flags & (1 << VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED))
+#define virtio_gpu_hostmem_enabled(_cfg) \
+(_cfg.hostmem > 0)
 
 struct virtio_gpu_base_conf {
 uint32_t max_outputs;
 uint32_t flags;
 uint32_t xres;
 uint32_t yres;
+uint64_t hostmem;
 };
 
 struct virtio_gpu_ctrl_command {
@@ -137,6 +140,8 @@ struct VirtIOGPUBase {
 int renderer_blocked;
 int enable;
 
+MemoryRegion hostmem;
+
 struct virtio_gpu_scanout scanout[VIRTIO_GPU_MAX_SCANOUTS];
 
 int enabled_output_bitmask;
-- 
2.42.0.582.g8ccd20d70d-goog




[PATCH v15 7/9] gfxstream + rutabaga: meson support

2023-10-03 Thread Gurchetan Singh
- Add meson detection of rutabaga_gfx
- Build virtio-gpu-rutabaga.c + associated vga/pci files when
  present

Signed-off-by: Gurchetan Singh 
Tested-by: Alyssa Ross 
Tested-by: Emmanouil Pitsidianakis 
Tested-by: Akihiko Odaki 
Reviewed-by: Emmanouil Pitsidianakis 
Reviewed-by: Antonio Caggiano 
Reviewed-by: Akihiko Odaki 
---
 hw/display/meson.build| 22 ++
 meson.build   |  7 +++
 meson_options.txt |  2 ++
 scripts/meson-buildoptions.sh |  3 +++
 4 files changed, 34 insertions(+)

diff --git a/hw/display/meson.build b/hw/display/meson.build
index 413ba4ab24..e362d625dd 100644
--- a/hw/display/meson.build
+++ b/hw/display/meson.build
@@ -79,6 +79,13 @@ if config_all_devices.has_key('CONFIG_VIRTIO_GPU')
  if_true: [files('virtio-gpu-gl.c', 
'virtio-gpu-virgl.c'), pixman, virgl])
 hw_display_modules += {'virtio-gpu-gl': virtio_gpu_gl_ss}
   endif
+
+  if rutabaga.found()
+virtio_gpu_rutabaga_ss = ss.source_set()
+virtio_gpu_rutabaga_ss.add(when: ['CONFIG_VIRTIO_GPU', rutabaga],
+   if_true: [files('virtio-gpu-rutabaga.c'), 
pixman])
+hw_display_modules += {'virtio-gpu-rutabaga': virtio_gpu_rutabaga_ss}
+  endif
 endif
 
 if config_all_devices.has_key('CONFIG_VIRTIO_PCI')
@@ -95,6 +102,12 @@ if config_all_devices.has_key('CONFIG_VIRTIO_PCI')
  if_true: [files('virtio-gpu-pci-gl.c'), pixman])
 hw_display_modules += {'virtio-gpu-pci-gl': virtio_gpu_pci_gl_ss}
   endif
+  if rutabaga.found()
+virtio_gpu_pci_rutabaga_ss = ss.source_set()
+virtio_gpu_pci_rutabaga_ss.add(when: ['CONFIG_VIRTIO_GPU', 
'CONFIG_VIRTIO_PCI', rutabaga],
+   if_true: 
[files('virtio-gpu-pci-rutabaga.c'), pixman])
+hw_display_modules += {'virtio-gpu-pci-rutabaga': 
virtio_gpu_pci_rutabaga_ss}
+  endif
 endif
 
 if config_all_devices.has_key('CONFIG_VIRTIO_VGA')
@@ -113,6 +126,15 @@ if config_all_devices.has_key('CONFIG_VIRTIO_VGA')
   virtio_vga_gl_ss.add(when: 'CONFIG_ACPI', if_true: files('acpi-vga.c'),
 if_false: files('acpi-vga-stub.c'))
   hw_display_modules += {'virtio-vga-gl': virtio_vga_gl_ss}
+
+  if rutabaga.found()
+virtio_vga_rutabaga_ss = ss.source_set()
+virtio_vga_rutabaga_ss.add(when: ['CONFIG_VIRTIO_VGA', rutabaga],
+   if_true: [files('virtio-vga-rutabaga.c'), 
pixman])
+virtio_vga_rutabaga_ss.add(when: 'CONFIG_ACPI', if_true: 
files('acpi-vga.c'),
+if_false: 
files('acpi-vga-stub.c'))
+hw_display_modules += {'virtio-vga-rutabaga': virtio_vga_rutabaga_ss}
+  endif
 endif
 
 system_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_lcdc.c'))
diff --git a/meson.build b/meson.build
index 21a1bc03f8..1228690880 100644
--- a/meson.build
+++ b/meson.build
@@ -1061,6 +1061,12 @@ if not get_option('virglrenderer').auto() or have_system 
or have_vhost_user_gpu
dependencies: virgl))
   endif
 endif
+rutabaga = not_found
+if not get_option('rutabaga_gfx').auto() or have_system or have_vhost_user_gpu
+  rutabaga = dependency('rutabaga_gfx_ffi',
+ method: 'pkg-config',
+ required: get_option('rutabaga_gfx'))
+endif
 blkio = not_found
 if not get_option('blkio').auto() or have_block
   blkio = dependency('blkio',
@@ -4299,6 +4305,7 @@ summary_info += {'libtasn1':  tasn1}
 summary_info += {'PAM':   pam}
 summary_info += {'iconv support': iconv}
 summary_info += {'virgl support': virgl}
+summary_info += {'rutabaga support':  rutabaga}
 summary_info += {'blkio support': blkio}
 summary_info += {'curl support':  curl}
 summary_info += {'Multipath support': mpathpersist}
diff --git a/meson_options.txt b/meson_options.txt
index 57e265c871..aabc706b6d 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -230,6 +230,8 @@ option('vmnet', type : 'feature', value : 'auto',
description: 'vmnet.framework network backend support')
 option('virglrenderer', type : 'feature', value : 'auto',
description: 'virgl rendering support')
+option('rutabaga_gfx', type : 'feature', value : 'auto',
+   description: 'rutabaga_gfx support')
 option('png', type : 'feature', value : 'auto',
description: 'PNG support with libpng')
 option('vnc', type : 'feature', value : 'auto',
diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh
index e4b46d5715..8efbc9851c 100644
--- a/scripts/meson-buildoptions.sh
+++ b/scripts/meson-buildoptions.sh
@@ -157,6 +157,7 @@ meson_options_help() {
   printf "%s\n" '  rbd Ceph block device driver'
   printf "%s\n" '  rdmaEnable RDMA-based migration'
   printf "%s\n" '  replication replication support'
+  printf "%s\n" '  rutabaga-gfxrutabaga_gfx support'
   printf "%s\n" '  sdl SDL 

[PATCH v15 0/9] rutabaga_gfx + gfxstream

2023-10-03 Thread Gurchetan Singh
From: Gurchetan Singh 

Prior version:

https://lists.gnu.org/archive/html/qemu-devel/2023-09/msg07402.html

Branch containing changes:

https://gitlab.com/gurchetansingh/qemu/-/commits/qemu-gfxstream-v15

Changes since v14:

- Thomas Huth suggested the use of the "x-" prefix for experimental
  properties. Now, we have "x-gfxstream-gles" and "x-gfxstream-composer"
  since those capsets are only runnable with Android guests and we're
  not sure if anybody else will use them.

https://lists.gnu.org/archive/html/qemu-devel/2023-09/msg07465.html

What still needs to happen:

- As mentioned in v14:
* AEMU: d6e6b99 "Delete VpxFrameParser.cpp"
* gfxstream: 2131f78d Merge "gfxstream: add egl & gles deps.."

are the proposed v.0.1.2 release points.  If those commits are sufficient
for packaging AEMU + gfxstream, let me know and I'll have official release
tags made.  If additional changes are required for packaging, let me know
as well.

Antonio Caggiano (2):
  virtio-gpu: CONTEXT_INIT feature
  virtio-gpu: blob prep

Dr. David Alan Gilbert (1):
  virtio: Add shared memory capability

Gerd Hoffmann (1):
  virtio-gpu: hostmem

Gurchetan Singh (5):
  gfxstream + rutabaga prep: added need defintions, fields, and options
  gfxstream + rutabaga: add initial support for gfxstream
  gfxstream + rutabaga: meson support
  gfxstream + rutabaga: enable rutabaga
  docs/system: add basic virtio-gpu documentation

 docs/system/device-emulation.rst |1 +
 docs/system/devices/virtio-gpu.rst   |  112 +++
 hw/display/meson.build   |   22 +
 hw/display/virtio-gpu-base.c |6 +-
 hw/display/virtio-gpu-pci-rutabaga.c |   47 ++
 hw/display/virtio-gpu-pci.c  |   14 +
 hw/display/virtio-gpu-rutabaga.c | 1122 ++
 hw/display/virtio-gpu.c  |   16 +-
 hw/display/virtio-vga-rutabaga.c |   50 ++
 hw/display/virtio-vga.c  |   33 +-
 hw/virtio/virtio-pci.c   |   18 +
 include/hw/virtio/virtio-gpu-bswap.h |   15 +
 include/hw/virtio/virtio-gpu.h   |   40 +
 include/hw/virtio/virtio-pci.h   |4 +
 meson.build  |7 +
 meson_options.txt|2 +
 scripts/meson-buildoptions.sh|3 +
 softmmu/qdev-monitor.c   |3 +
 softmmu/vl.c |1 +
 19 files changed, 1497 insertions(+), 19 deletions(-)
 create mode 100644 docs/system/devices/virtio-gpu.rst
 create mode 100644 hw/display/virtio-gpu-pci-rutabaga.c
 create mode 100644 hw/display/virtio-gpu-rutabaga.c
 create mode 100644 hw/display/virtio-vga-rutabaga.c

-- 
2.42.0.582.g8ccd20d70d-goog




  1   2   3   4   5   >