Home | History | Annotate | only in /src/sys/arch/xen/xenbus
History log of /src/sys/arch/xen/xenbus
RevisionDateAuthorComments
 1.2 06-Mar-2006  bouyer Adapt for NetBSD and add glue for config(9). Enough to get guest domain
virtual devices probed and attached, domain0 and xenstore watch needs to
be done.
 1.1 06-Mar-2006  bouyer branches: 1.1.1;
Initial revision
 1.1.1.1 06-Mar-2006  bouyer Import xenbus sources, from linux sparse tree in the 20060107 xen-3.0
snapshot.
 1.17 07-Apr-2020  jdolecek convert from malloc() to kmem_alloc()
 1.16 07-Apr-2020  jdolecek convert the node watch code to use kmem_alloc() instead of malloc()
 1.15 07-Apr-2020  jdolecek make xenbus_watch_path() static, it's not used outside xenbus_client.c
 1.14 10-Nov-2019  chs in many device attach paths, allocate memory with M_WAITOK instead of M_NOWAIT
and remove code to handle failures that can no longer happen.
 1.13 21-Sep-2014  bouyer branches: 1.13.20;
Make Xen kernels compile without DIAGNOSTIC
 1.12 27-Mar-2014  christos branches: 1.12.4;
correct/add protection against snprintf overflow.
 1.11 17-Jul-2011  joerg branches: 1.11.2; 1.11.12; 1.11.16;
Retire varargs.h support. Move machine/stdarg.h logic into MI
sys/stdarg.h and expect compiler to provide proper builtins, defaulting
to the GCC interface. lint still has a special fallback.
Reduce abuse of _BSD_VA_LIST_ by defining __va_list by default and
derive va_list as required by standards.
 1.10 28-Apr-2009  cegger sprintf -> snprintf
 1.9 05-Sep-2008  tron branches: 1.9.8;
Compile NetBSD/amd64 kernels with "-Wextra". Patches contributed by
Juan RP in PR port-amd64/39266.
 1.8 15-Dec-2007  perry branches: 1.8.6; 1.8.10; 1.8.12; 1.8.16;
__FUNCTION__ -> __func__
 1.7 22-Nov-2007  bouyer branches: 1.7.2; 1.7.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.6 14-Mar-2007  dogcow branches: 1.6.14; 1.6.16; 1.6.20; 1.6.22;
unb0rk build.
 1.5 25-Jun-2006  bouyer branches: 1.5.6; 1.5.12; 1.5.16; 1.5.18;
Add a 'base' argument to xenbus_read_ul, so that we can read number is base
other than 10.
 1.4 16-Mar-2006  bouyer branches: 1.4.2; 1.4.4; 1.4.8; 1.4.10;
Add a xenbus_read_ul() which reads a node and convert it to unsigned long.
Make xenbus_switch_state() and xenbus_read_driver_state() do something usefull.
 1.3 15-Mar-2006  bouyer branches: 1.3.2;
Implement watch of xenstore nodes, and install watches for otherend'state
node.
 1.2 06-Mar-2006  bouyer branches: 1.2.2; 1.2.4;
Adapt for NetBSD and add glue for config(9). Enough to get guest domain
virtual devices probed and attached, domain0 and xenstore watch needs to
be done.
 1.1 06-Mar-2006  bouyer branches: 1.1.1;
Initial revision
 1.1.1.1 06-Mar-2006  bouyer Import xenbus sources, from linux sparse tree in the 20060107 xen-3.0
snapshot.
 1.2.4.4 26-Jun-2006  yamt sync with head.
 1.2.4.3 01-Apr-2006  yamt sync with head.
 1.2.4.2 13-Mar-2006  yamt sync with head.
 1.2.4.1 06-Mar-2006  yamt file xenbus_client.c was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.2.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.3.2.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.4.10.6 21-Jan-2008  yamt sync with head
 1.4.10.5 07-Dec-2007  yamt sync with head
 1.4.10.4 03-Sep-2007  yamt sync with head.
 1.4.10.3 30-Dec-2006  yamt sync with head.
 1.4.10.2 21-Jun-2006  yamt sync with head.
 1.4.10.1 16-Mar-2006  yamt file xenbus_client.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.4.8.1 13-Jul-2006  gdamore Merge from HEAD.
 1.4.4.2 22-Apr-2006  simonb Sync with head.
 1.4.4.1 16-Mar-2006  simonb file xenbus_client.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.4.2.2 07-Apr-2006  tron Apply patch (requested by bouyer in ticket #1245):
Pull up Xen3 domU support. This adds support for the Xen-3 memory bootstrap,
xenstore, and block and network device frontend. Xen-3 support is turned
on by 'options XEN3', which disable Xen-2 support.
Changes affecting non-xen3 specific code:
- xbd and xennet at hypervisor now attaches with xbd_hypervisor and
xennet_hypervisor
- x86_atomic_* renamed to xen_atomic_*
- use genassim.cf to pull in more constant from include files for assembly
- Map the shared info page from locore.S instead of the 0xffffffff hack
in xen_machdep.c
- remove some unused code
- some __asm__ __volatile__ -> __asm volatile and __inline__ -> inline
- more debug code
 1.4.2.1 16-Mar-2006  tron file xenbus_client.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.5.18.1 11-Jul-2007  mjf Sync with head.
 1.5.16.2 03-Dec-2007  ad Sync with HEAD.
 1.5.16.1 10-Apr-2007  ad Sync with head.
 1.5.12.1 24-Mar-2007  yamt sync with head.
 1.5.6.2 09-Sep-2006  rpaulo sync with head
 1.5.6.1 25-Jun-2006  rpaulo file xenbus_client.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.6.22.2 27-Dec-2007  mjf Sync with HEAD.
 1.6.22.1 08-Dec-2007  mjf Sync with HEAD.
 1.6.20.1 17-Oct-2007  bouyer Prepare for xenamd64:
- kill xen/i386/identcpu.c, use i386/i386/identcpu.c instead (with a few
#ifndef XEN)
- move some files that can be shared between i386 and amd64 from
xen/i386 to xen/x86 (or to xen/xen for non-cpu-specific code)
- split assembly out of xen/include/hypervisor.h to xen/include/hypercalls.h
- use <xen/...> instead of <machine/...> for cpu-independant include files.

more work needed here, i386-specific files should got out of arch/xen to
arch/xeni386, and more code shared with arch/i386.
 1.6.16.1 09-Jan-2008  matt sync with HEAD
 1.6.14.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.7.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.7.2.1 26-Dec-2007  ad Sync with head.
 1.8.16.1 19-Oct-2008  haad Sync with HEAD.
 1.8.12.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.8.10.1 04-May-2009  yamt sync with head.
 1.8.6.1 28-Sep-2008  mjf Sync with HEAD.
 1.9.8.3 27-Aug-2011  jym Sync with HEAD. Most notably: uvm/pmap work done by rmind@, and MP Xen
work of cherry@.

No regression observed on suspend/restore.
 1.9.8.2 01-Nov-2009  jym Sync with HEAD.
 1.9.8.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.11.16.1 18-May-2014  rmind sync with head
 1.11.12.2 03-Dec-2017  jdolecek update from HEAD
 1.11.12.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.11.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.12.4.1 22-Sep-2014  martin Pull up following revision(s) (requested by bouyer in ticket #115):
sys/arch/xen/x86/hypervisor_machdep.c: revision 1.28
sys/arch/xen/xenbus/xenbus_client.c: revision 1.13
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.60
sys/arch/xen/xen/clock.c: revision 1.63
Make Xen kernels compile without DIAGNOSTIC
 1.13.20.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.26 25-Feb-2023  riastradh xenbus_comms.c: Nix trailing whitespace.

No functional change intended.
 1.25 25-Feb-2023  riastradh xenbus_comms.c: Membar audit.

This had the sense of membars reversed, presumably because xen_rmb
and xen_wmb had gotten reversed at some point.
 1.24 13-May-2020  jdolecek branches: 1.24.20;
don't reinitialize mutexes/cv on resume

part of PR port-xen/55207
 1.23 06-May-2020  bouyer Convert remaining splfoo/splx and tsleep/wakeup to mutex and condvar.
Mark kernel threads and interrupt handlers MPSAFE.
 1.22 25-Apr-2020  bouyer Merge the bouyer-xenpvh branch, bringing in Xen PV drivers support under HVM
guests in GENERIC.
Xen support can be disabled at runtime with
boot -c
disable hypervisor
 1.21 24-Dec-2018  cherry branches: 1.21.10;
Bifurcate the interrupt establish functions between XEN and non-XEN

Thus intr_establish_xname() becomes xen_intr_establish_xname() etc.

One consequence of this is that dom0 devices expect the native
function calls to be available and we thus provide weak aliasing for
dom0 builds to succeed. XEN and non-XEN devices are distinguished by
the PIC they are established on. XEN interrupts are exclusively
established on xen_pic, while dom0 interrupts are established on
natively available PICs.

This allows us an orthogonal path to xen device management (eg:
xenstore events) in XENPVHVM, without having to worry about unifying
the vector entry paths, etc., which is quite challenging.
 1.20 26-Oct-2018  cherry Decompose hypervisor_enable_event() into functional steps.

The hypervisor_unmask_event() step is relevant for any event.

The pirq related step is only relevant for pirq bound events.

Prune blanket usage of this, so that usage is semantically appropriate.
 1.19 24-Oct-2018  cherry When using the intr_establish_xname() interface to register
XEN events, follow established x86/intr.c conventions - set
the 'legacy' irq value to -1, to indicate that the pic, pin
combination (&xen_pic, port) is used for registration.
 1.18 24-Jun-2018  jdolecek branches: 1.18.2;
add support for kern.intr.list aka intrctl(8) 'list' for xen

event_set_handler() and pirq_establish() now have extra intrname
parameter; shared intr_create_intrid() is used to provide the value

xen drivers were changed to pass the specific driver instance
name as the xname, e.g. 'vcpu0 clock' instead just 'clock', or
'xencons0' instead of 'xencons'

associated evcnt is now changed to use intrname - this matches native x86
 1.17 13-Nov-2017  riastradh branches: 1.17.2;
Missed a spot: preserve known_mpsafe = (level != IPL_VM).

Noted by kre -- sorry!
 1.16 06-Nov-2017  cherry Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.15 07-Jul-2016  msaitoh KNF. Remove extra spaces. No functional change.
 1.14 20-Sep-2011  jym branches: 1.14.12; 1.14.30;
Merge jym-xensuspend branch in -current. ok bouyer@.

Goal: save/restore support in NetBSD domUs, for i386, i386 PAE and amd64.

Executive summary:
- split all Xen drivers (xenbus(4), grant tables, xbd(4), xennet(4))
in two parts: suspend and resume, and hook them to pmf(9).
- modify pmap so that Xen hypervisor does not cry out loud in case
it finds "unexpected" recursive memory mappings
- provide a sysctl(7), machdep.xen.suspend, to command suspend from
userland via powerd(8). Note: a suspend can only be handled correctly
when dom0 requested it, so provide a mechanism that will prevent
kernel to blindly validate user's commands

The code is still in experimental state, use at your own risk: restore
can corrupt backend communications rings; this can completely thrash
dom0 as it will loop at a high interrupt level trying to honor
all domU requests.

XXX PAE suspend does not work in amd64 currently, due to (yet again!)
page validation issues with hypervisor. Will fix.

XXX secondary CPUs are not suspended, I will write the handlers
in sync with cherry's Xen MP work.

Tested under i386 and amd64, bear in mind ring corruption though.

No build break expected, GENERICs and XEN* kernels should be fine.
./build.sh distribution still running. In any case: sorry if it does
break for you, contact me directly for reports.
 1.13 02-Jul-2011  jym Remove all return error checks for event_set_handler(...). It either
succeeds or end in panic.
 1.12 16-Jan-2009  jym branches: 1.12.2;
Replace x86 memory fences in Xen drivers by their Xen equivalents, to reduce
MD dependency:

x86_lfence() => xen_rmb()
x86_sfence() => xen_wmb()
x86_mfence() => xen_mb()

Discussed in
http://mail-index.netbsd.org/port-xen/2009/01/15/msg004655.html

Ok by bouyer@.
 1.11 18-Dec-2008  cegger remove unused malloc.h
 1.10 29-Oct-2008  cegger include <xen/xen.h> for xendomain_is_dom0()
 1.9 24-Oct-2008  jym branches: 1.9.2;
- printf -> aprint_*
- fix and add comments
- make some panic/error messages more relevant
- remove last '\n' in DPRINTK() macros, not required as it is already part of format string.

No functional changes.
 1.8 21-Oct-2008  cegger introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.7 16-Apr-2008  cegger branches: 1.7.4; 1.7.10;
device_t / softc split
reviewed, tested and approved by bouyer
 1.6 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.5 22-Nov-2007  bouyer branches: 1.5.14;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.4 04-Mar-2007  christos branches: 1.4.2; 1.4.18; 1.4.20; 1.4.24; 1.4.26;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.3 23-May-2006  bouyer branches: 1.3.4; 1.3.10; 1.3.16;
Add needed framework for backend drivers.
As we want some control on the name the backend driver will have we
can't use autoconf(9) here. Instead backend drivers registers to
xenbus, which will call a create callback when a new device is there.
Backend devices won't have a "struct device" in xenbus, use a void pointer
instead.
 1.2 06-Mar-2006  bouyer branches: 1.2.4; 1.2.6; 1.2.8; 1.2.10; 1.2.12;
Adapt for NetBSD and add glue for config(9). Enough to get guest domain
virtual devices probed and attached, domain0 and xenstore watch needs to
be done.
 1.1 06-Mar-2006  bouyer branches: 1.1.1;
Initial revision
 1.1.1.1 06-Mar-2006  bouyer Import xenbus sources, from linux sparse tree in the 20060107 xen-3.0
snapshot.
 1.2.12.1 19-Jun-2006  chap Sync with head.
 1.2.10.3 01-Jun-2006  kardel Sync with head.
 1.2.10.2 22-Apr-2006  simonb Sync with head.
 1.2.10.1 06-Mar-2006  simonb file xenbus_comms.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.2.8.2 07-Apr-2006  tron Apply patch (requested by bouyer in ticket #1245):
Pull up Xen3 domU support. This adds support for the Xen-3 memory bootstrap,
xenstore, and block and network device frontend. Xen-3 support is turned
on by 'options XEN3', which disable Xen-2 support.
Changes affecting non-xen3 specific code:
- xbd and xennet at hypervisor now attaches with xbd_hypervisor and
xennet_hypervisor
- x86_atomic_* renamed to xen_atomic_*
- use genassim.cf to pull in more constant from include files for assembly
- Map the shared info page from locore.S instead of the 0xffffffff hack
in xen_machdep.c
- remove some unused code
- some __asm__ __volatile__ -> __asm volatile and __inline__ -> inline
- more debug code
 1.2.8.1 06-Mar-2006  tron file xenbus_comms.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.2.6.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.2.4.3 24-May-2006  yamt sync with head.
 1.2.4.2 13-Mar-2006  yamt sync with head.
 1.2.4.1 06-Mar-2006  yamt file xenbus_comms.c was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.3.16.1 12-Mar-2007  rmind Sync with HEAD.
 1.3.10.2 09-Sep-2006  rpaulo sync with head
 1.3.10.1 23-May-2006  rpaulo file xenbus_comms.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:06 +0000
 1.3.4.4 07-Dec-2007  yamt sync with head
 1.3.4.3 03-Sep-2007  yamt sync with head.
 1.3.4.2 21-Jun-2006  yamt sync with head.
 1.3.4.1 23-May-2006  yamt file xenbus_comms.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.4.26.1 08-Dec-2007  mjf Sync with HEAD.
 1.4.24.1 17-Oct-2007  bouyer Prepare for xenamd64:
- kill xen/i386/identcpu.c, use i386/i386/identcpu.c instead (with a few
#ifndef XEN)
- move some files that can be shared between i386 and amd64 from
xen/i386 to xen/x86 (or to xen/xen for non-cpu-specific code)
- split assembly out of xen/include/hypervisor.h to xen/include/hypercalls.h
- use <xen/...> instead of <machine/...> for cpu-independant include files.

more work needed here, i386-specific files should got out of arch/xen to
arch/xeni386, and more code shared with arch/i386.
 1.4.20.1 09-Jan-2008  matt sync with HEAD
 1.4.18.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.4.2.1 03-Dec-2007  ad Sync with HEAD.
 1.5.14.2 17-Jan-2009  mjf Sync with HEAD.
 1.5.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.7.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.7.4.1 04-May-2009  yamt sync with head.
 1.9.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.12.2.5 27-Aug-2011  jym Sync with HEAD. Most notably: uvm/pmap work done by rmind@, and MP Xen
work of cherry@.

No regression observed on suspend/restore.
 1.12.2.4 25-Jul-2011  jym Pull-up to my branch some of the improvements I committed to HEAD, but
forgot to reflect here.

Improvements in the attachement routines: in case of error, don't forget
to free() the allocated rings. Should not happen anyway, more a matter
of staying clean.
 1.12.2.3 01-Nov-2009  jym - Upgrade suspend/resume code to comply with Xen2 removal.
- Add support for PAE domUs suspend/resume.
- Fix an issue regarding initialization of the xbd ring I/O that could end
badly during resume, with invalid block operations submitted to dom0 backend.

NetBSD supports PAE under x86_32 by considering the L2 page as being
4 pages long instead of 1.

Xen validates the page types during resume. Sadly, the hypervisor handles
alternative recursive mappings (== PG/PD entries pointing to pages other
than self) inadequately, which could lead to incorrect page pinning.

As a result, the important change with this patch is to clear these alternative
mappings during suspend, and reset them back to their former self upon
resume. For PAE, approx. all 4 PDIR_SLOT_PTEs could be considered as
alternative recursive mappings.

See comments in pmap.c for further details.

Now, let the testing and bug hunting begin.
 1.12.2.2 01-Nov-2009  jym Sync with HEAD.
 1.12.2.1 09-Feb-2009  jym Initial code for xen save/restore/migrate facilities.

- split the attach code of frontends in two half: one that is only needed
during autoconf(9) attach/detach phases, and one used at each save/restore
of device state (between suspend and resume).

Applies to hypervisor, xencons, xenbus, xbd, and xennet.

- add a rwlock(9) ("ptom_lock") to protect the different parts in the kernel
that manipulate MFNs (which could change between a suspend and a resume,
without the kernel noticing it). Parts that require MFNs acquire a reader lock,
while suspend code will acquire a writer lock to ensure that no-other parts
in kernel still use MFNs.

- integrate the suspend code with sysmon.

- various things in pmap(9), and clock.

TODO:
- factorize code a bit more inside frontends drivers.
- remove all alternative recursive (APDP_PDE) mappings found in PD/PT during
suspend, as Xen does not support them.
- abstract the ptom_lock locking, it is only required when kernel preemption
is enabled, or on MP systems.

Current code works mostly. You may experience difficulties in some corner
cases (dom0 warnings about xennet interface errors, and Xen tools failing to
validate NetBSD's alternative pmaps).
 1.14.30.1 09-Jul-2016  skrll Sync with HEAD
 1.14.12.1 03-Dec-2017  jdolecek update from HEAD
 1.17.2.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.17.2.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.17.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.18.2.1 10-Jun-2019  christos Sync with HEAD
 1.21.10.1 16-Apr-2020  bouyer Reorganise sources to make it possible to include Xen PVHVM support in
native kernels. Among others:
- move xen/include/amd64/hypercall.h to amd64/include/xen and
xen/include/i386/hypercall.h to i386/include/xen
- exclude some native files from the build for xenpv
- add xen to "machine" config statement for amd64 and i386
- split arch/xen/conf/files.xen to arch/xen/conf/files.xen (for pv drivers)
and arch/xen/conf/files.xen.pv (for full pv support)
- add GENERIC_XENHVM kernel config which includes GENERIC and add Xen PV
drivers.
 1.24.20.1 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #268):

sys/arch/xen/xenbus/xenbus_comms.c: revision 1.25
sys/arch/xen/xenbus/xenbus_comms.c: revision 1.26
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.110
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.111
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.112
sys/arch/xen/x86/cpu.c: revision 1.144
sys/arch/xen/x86/cpu.c: revision 1.145
sys/arch/xen/include/hypervisor.h: revision 1.56
sys/arch/xen/include/hypervisor.h: revision 1.57
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.102
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.103
sys/arch/xen/include/xenring.h: revision 1.7
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.109
sys/arch/xen/xen/xengnt.c: revision 1.40
sys/arch/xen/xen/xengnt.c: revision 1.41
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.129
sys/arch/xen/xen/xencons.c: revision 1.51
sys/arch/xen/xen/xencons.c: revision 1.52
sys/arch/xen/xen/xencons.c: revision 1.53
sys/arch/xen/xen/xbd_xenbus.c: revision 1.130 (patch)
sys/arch/xen/xen/xbd_xenbus.c: revision 1.131 (patch)

xen: Fix sense of xen_rmb/wmb to make sense.

Use membar_acquire and membar_release, not membar_consumer and
membar_producer, out of paranoia -- that better matches Linux's
rmb/wmb (at least for non-I/O loads and stores).

Proposed on port-xen:
https://mail-index.netbsd.org/port-xen/2022/07/13/msg010248.html

xen/x86/cpu.c: Membar audit.

I see no reason for store-before-load ordering here; as far as I'm
aware, evtchn_upcall_mask is only shared between a (v)CPU and its
(hypervisor) interrupts, not other (v)CPUs.

xennet(4): Membar audit.
- xennet_tx_complete: Other side owns rsp_prod, giving us responses
to tx commands. We own rsp_cons, recording which responess we've
processed already.
1. Other side initializes responses before advancing rsp_prod, so
we must observe rsp_prod before trying to examine the responses.
Hence load from rsp_prod must be followed by xen_rmb.
(Can this just use atomic_load_acquire?)
2. As soon as other side observes rsp_event, it may start to
overwrite now-unused response slots, so we must finish using the
response before advancing rsp_cons. Hence we must issue xen_wmb
before store to rsp_event.
(Can this just use atomic_store_release?)
(Should this use RING_FINAL_CHECK_FOR_RESPONSES?)
3. When loop is done and we set rsp_event, we must ensure the other
side has had a chance to see that we want more before we check
whether there is more to consume; otherwise the other side might
not bother to send us an interrupt. Hence after setting
rsp_event, we must issue xen_mb (store-before-load) before
re-checking rsp_prod.
- xennet_handler (rx): Same deal, except the xen_mb is buried in
RING_FINAL_CHECK_FOR_RESPONSES. Unclear why xennet_tx_complete has
this open-coded while xennet_handler (rx) uses the macro.

xbd(4): Membar audit.
After consuming slots, must issue xen_wmb before notifying the other
side that we've consumed them in RING_FINAL_CHECK_FOR_RESPONSES.
xbdback(4): Membar audit.

After consuming request slots, must issue xen_wmb notifying the other
side that we've consumed them in RING_FINAL_CHECK_FOR_REQUESTS.

xencons(4): Membar audit.
- xenconscn_getc: Once we have consumed an input slot, it is clearer
to issue xen_wmb (release, i.e., load/store-before-store) before
advancing in_cons so that the update becomes a store-release
freeing the input slot for the other side to reuse.
- xenconscn_putc: After filling an output slot, must issue xen_wmb
(release, i.e., load/store-before-store) before advancing out_prod,
and another one before notifying the other side of the advance.

xencons(4): Reduce unnecessary membars.
- xencons_handler: After advancing in_cons, only need one xen_wmb
before notifying the hypervisor that we're ready for more.
(XXX Should this do xen_mb and re-check in_prod at that point, or
does hypervisor_notify_via_evtchn obviate the need for this?)
- xenvonscn_getc: After reading in_prod, only need one xen_rmb before
using the slots it is telling us are now ready.

xengnt(4): Membar audit.
This had the sense of membars reversed, presumably because xen_rmb
and xen_wmb had gotten reversed at some point.
xenbus_comms.c: Membar audit.

This had the sense of membars reversed, presumably because xen_rmb
and xen_wmb had gotten reversed at some point.

xennetback(4): Fix xennetback_evthandler loop.
- After observing the other side has produced pending tx requests by
reading sring->req_prod, must issue xen_rmb before touching them.
Despite all the effort to use the heavy-weight
RING_FINAL_CHECK_FOR_REQUESTS on each request in the loop, this
barrier was missing.
- No need to update req_cons at each iteration in the loop. It's
private. Just update it once at the end.
- After consuming requests, must issue xen_wmb before releasing the
slots with RING_FINAL_CHECK_FOR_REQUEST for the other side to
reuse.

xennetback(4): Fix membars in xennetback_rx_copy_process.
- No need for barrier around touching req_cons and rsp_prod_pvt,
which are private.
- RING_PUSH_RESPONSES_AND_CHECK_NOTIFY already issues xen_wmb, no
need to add one explicitly.
- After pushing responses, must issue xen_wmb (not xen_rmb) before
hypervisor_notify_via_evtchn.

xennetback(4): Omit needless membars in xennetback_connect.
xneti is a private data structure to which we have exclusive access
here; ordering the stores doesn't make sense.

xen/hypervisor.h: Nix trailing whitespace.
No functional change intended.

xen/x86/cpu.c: Nix trailing whitespace.
No functional change intended.

xbd(4): Nix trailing whitespace.

xbdback(4): Nix trailing whitespace.
No functional change intended.

xencons(4): Nix trailing whitespace.
No functional change intended.

xengnt(4): Nix trailing whitespace.
No functional change intended.

xenbus_comms.c: Nix trailing whitespace.
No functional change intended.

xennetback(4): Nix trailing whitespace.
No functional change intended.
 1.8 13-May-2020  jdolecek don't reinitialize mutexes/cv on resume

part of PR port-xen/55207
 1.7 06-May-2020  bouyer Convert remaining splfoo/splx and tsleep/wakeup to mutex and condvar.
Mark kernel threads and interrupt handlers MPSAFE.
 1.6 20-Sep-2011  jym Merge jym-xensuspend branch in -current. ok bouyer@.

Goal: save/restore support in NetBSD domUs, for i386, i386 PAE and amd64.

Executive summary:
- split all Xen drivers (xenbus(4), grant tables, xbd(4), xennet(4))
in two parts: suspend and resume, and hook them to pmf(9).
- modify pmap so that Xen hypervisor does not cry out loud in case
it finds "unexpected" recursive memory mappings
- provide a sysctl(7), machdep.xen.suspend, to command suspend from
userland via powerd(8). Note: a suspend can only be handled correctly
when dom0 requested it, so provide a mechanism that will prevent
kernel to blindly validate user's commands

The code is still in experimental state, use at your own risk: restore
can corrupt backend communications rings; this can completely thrash
dom0 as it will loop at a high interrupt level trying to honor
all domU requests.

XXX PAE suspend does not work in amd64 currently, due to (yet again!)
page validation issues with hypervisor. Will fix.

XXX secondary CPUs are not suspended, I will write the handlers
in sync with cherry's Xen MP work.

Tested under i386 and amd64, bear in mind ring corruption though.

No build break expected, GENERICs and XEN* kernels should be fine.
./build.sh distribution still running. In any case: sorry if it does
break for you, contact me directly for reports.
 1.5 24-Oct-2008  jym branches: 1.5.8;
- rename init_events() to events_init(), to better reflect netbsd semantics

- change unbind_[pv]irq_from_evtch() so that they now return the event
channel the [PV]IRQ was bound to. It reflects the opposite behaviour of the
bind_[pv]irq_to_evtch() functions.

- remove xenbus_suspend() and xenbus_resume() prototypes, as they are not
used anywhere else, and will conflict with the xenbus pmf(9) handlers.

- make start_info aligned on a page boundary, as Xen expects it to be so.

- mask event channel during xbd detach before removing its handler (can
avoid spurious events).

- add the "protocol" entry in xenstore during xbd initialization. Normally
created during domU's boot by xentools, it is under domU's responsibility
in all other cases (save/restore, hot plugging, etc.).

- modifications to xs_init(), so that it can properly return an error.

Reviewed by Christoph (cegger@).
 1.4 16-Apr-2008  cegger branches: 1.4.4; 1.4.10;
device_t / softc split
reviewed, tested and approved by bouyer
 1.3 09-Apr-2006  bouyer branches: 1.3.2; 1.3.8; 1.3.14; 1.3.68;
start xenbus support for domain0: allocate the page and the event channel,
and export theses via /kern/xen/xsd_mfn and /kern/xen/xsd_port.
 1.2 06-Mar-2006  bouyer branches: 1.2.2; 1.2.4; 1.2.6; 1.2.8;
Adapt for NetBSD and add glue for config(9). Enough to get guest domain
virtual devices probed and attached, domain0 and xenstore watch needs to
be done.
 1.1 06-Mar-2006  bouyer branches: 1.1.1;
Initial revision
 1.1.1.1 06-Mar-2006  bouyer Import xenbus sources, from linux sparse tree in the 20060107 xen-3.0
snapshot.
 1.2.8.2 07-Apr-2006  tron Apply patch (requested by bouyer in ticket #1245):
Pull up Xen3 domU support. This adds support for the Xen-3 memory bootstrap,
xenstore, and block and network device frontend. Xen-3 support is turned
on by 'options XEN3', which disable Xen-2 support.
Changes affecting non-xen3 specific code:
- xbd and xennet at hypervisor now attaches with xbd_hypervisor and
xennet_hypervisor
- x86_atomic_* renamed to xen_atomic_*
- use genassim.cf to pull in more constant from include files for assembly
- Map the shared info page from locore.S instead of the 0xffffffff hack
in xen_machdep.c
- remove some unused code
- some __asm__ __volatile__ -> __asm volatile and __inline__ -> inline
- more debug code
 1.2.8.1 06-Mar-2006  tron file xenbus_comms.h was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.2.6.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.2.4.3 11-Apr-2006  yamt sync with head
 1.2.4.2 13-Mar-2006  yamt sync with head.
 1.2.4.1 06-Mar-2006  yamt file xenbus_comms.h was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.2.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.3.68.2 17-Jan-2009  mjf Sync with HEAD.
 1.3.68.1 02-Jun-2008  mjf Sync with HEAD.
 1.3.14.2 09-Sep-2006  rpaulo sync with head
 1.3.14.1 09-Apr-2006  rpaulo file xenbus_comms.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:06 +0000
 1.3.8.2 21-Jun-2006  yamt sync with head.
 1.3.8.1 09-Apr-2006  yamt file xenbus_comms.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.3.2.2 22-Apr-2006  simonb Sync with head.
 1.3.2.1 09-Apr-2006  simonb file xenbus_comms.h was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.4.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.4.4.1 04-May-2009  yamt sync with head.
 1.5.8.3 01-Nov-2009  jym - Upgrade suspend/resume code to comply with Xen2 removal.
- Add support for PAE domUs suspend/resume.
- Fix an issue regarding initialization of the xbd ring I/O that could end
badly during resume, with invalid block operations submitted to dom0 backend.

NetBSD supports PAE under x86_32 by considering the L2 page as being
4 pages long instead of 1.

Xen validates the page types during resume. Sadly, the hypervisor handles
alternative recursive mappings (== PG/PD entries pointing to pages other
than self) inadequately, which could lead to incorrect page pinning.

As a result, the important change with this patch is to clear these alternative
mappings during suspend, and reset them back to their former self upon
resume. For PAE, approx. all 4 PDIR_SLOT_PTEs could be considered as
alternative recursive mappings.

See comments in pmap.c for further details.

Now, let the testing and bug hunting begin.
 1.5.8.2 01-Nov-2009  jym Sync with HEAD.
 1.5.8.1 09-Feb-2009  jym Initial code for xen save/restore/migrate facilities.

- split the attach code of frontends in two half: one that is only needed
during autoconf(9) attach/detach phases, and one used at each save/restore
of device state (between suspend and resume).

Applies to hypervisor, xencons, xenbus, xbd, and xennet.

- add a rwlock(9) ("ptom_lock") to protect the different parts in the kernel
that manipulate MFNs (which could change between a suspend and a resume,
without the kernel noticing it). Parts that require MFNs acquire a reader lock,
while suspend code will acquire a writer lock to ensure that no-other parts
in kernel still use MFNs.

- integrate the suspend code with sysmon.

- various things in pmap(9), and clock.

TODO:
- factorize code a bit more inside frontends drivers.
- remove all alternative recursive (APDP_PDE) mappings found in PD/PT during
suspend, as Xen does not support them.
- abstract the ptom_lock locking, it is only required when kernel preemption
is enabled, or on MP systems.

Current code works mostly. You may experience difficulties in some corner
cases (dom0 warnings about xennet interface errors, and Xen tools failing to
validate NetBSD's alternative pmaps).
 1.19 09-Feb-2024  andvar fix spelling mistakes, mainly in comments and log messages.
 1.18 25-Apr-2020  bouyer Merge the bouyer-xenpvh branch, bringing in Xen PV drivers support under HVM
guests in GENERIC.
Xen support can be disabled at runtime with
boot -c
disable hypervisor
 1.17 07-Apr-2020  jdolecek branches: 1.17.2;
add wrapper to free response from xenbus_dev_request_and_reply(), so
that call in xenbus_dev_write() wouldn't need to know how it's allocated
 1.16 07-Apr-2020  jdolecek mostly convert to kmem_alloc()

doing this, remove check for failed allocation with KM_SLEEP, and make
sure to not hold mutex during the call
 1.15 07-Apr-2020  jdolecek switch KERNFS_ALLOCENTRY() to use kmem_zalloc() instead of malloc()
 1.14 27-Mar-2017  bouyer branches: 1.14.14;
Avoid variable reassignement; reported by dcb314@hotmail.com in
PR port-xen/52112
 1.13 23-Mar-2017  bouyer xlwp->mtx will never be used in interrupt context; set to IPL_NONE.
avoids a KASSERT when sleeping for the reply.
 1.12 22-Mar-2017  bouyer On second through, it may be possible to have a NULL kfs_v in read and write
(if we're not the LWP which did the open).
Add the appropriate locks and checks.
 1.11 22-Mar-2017  bouyer Fix /kern/xen/xenbus handling. It's badly broken and will do bad things
if more than one thread tries to use it at the same time (hang, memory leak,
panic).
In a kernfs node, the fileops (open, close, read, write) gets a vnode,
but no way to know the file descriptor from which the request comes from.
As /kern/xen/xenbus interface is statefull (write sends a command and read
gets the response), a way to track "clients" is needed.
This commit implement states using the lwp pointer from the caller as a key.
It will fail (with an error) if a kernfs file descriptor is reused by a child
after a fork(), or if a file descriptor is shared by two threads of the same
process but fortunably the xen tools using this interface don't do this.

This should fixes occasional hangs of the Xen tools (one in "xbrd" state,
others in tstile) reported on port-xen by Alaric Snell-Pym.
 1.10 07-Jul-2016  msaitoh branches: 1.10.2; 1.10.4;
KNF. Remove extra spaces. No functional change.
 1.9 22-Sep-2011  jym branches: 1.9.12; 1.9.30;
Expose Xen kernfs entries inside a domU. Patch originally from sborrill@,
slightly modified by me to profit from runtime checks for dom0 privileges
instead of using compile time macros (DOM0OPS).

It should now be possible to use pkgsrc's sysutils/xentools inside
a domU to query XenStore entries (or even modify part of it if the domain
has enough rights).
 1.8 16-Mar-2009  cegger ansify function definitions
 1.7 27-Nov-2007  pooka branches: 1.7.18; 1.7.26; 1.7.28; 1.7.32;
a_l -> curlwp
 1.6 22-Nov-2007  bouyer Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.5 07-May-2006  bouyer branches: 1.5.6; 1.5.12; 1.5.22; 1.5.40; 1.5.42; 1.5.46; 1.5.48;
Fix read for /kern/xen/xenbus: ignore offset.
 1.4 07-May-2006  bouyer Add dom0 operation support for Xen3. Probably buggy, but it's enouth to have
xend and xenstored starting; xm info and xm list works.
 1.3 11-Apr-2006  bouyer branches: 1.3.2;
Centralize all xenbus /kern entries in xenbus_dev.c, and call
xenbus_kernfs_init() for domain0. Now /kern/xen/xenbus is also present
on domain0.
 1.2 06-Mar-2006  bouyer branches: 1.2.2; 1.2.4; 1.2.6; 1.2.8;
Adapt for NetBSD and add glue for config(9). Enough to get guest domain
virtual devices probed and attached, domain0 and xenstore watch needs to
be done.
 1.1 06-Mar-2006  bouyer branches: 1.1.1;
Initial revision
 1.1.1.1 06-Mar-2006  bouyer Import xenbus sources, from linux sparse tree in the 20060107 xen-3.0
snapshot.
 1.2.8.2 07-Apr-2006  tron Apply patch (requested by bouyer in ticket #1245):
Pull up Xen3 domU support. This adds support for the Xen-3 memory bootstrap,
xenstore, and block and network device frontend. Xen-3 support is turned
on by 'options XEN3', which disable Xen-2 support.
Changes affecting non-xen3 specific code:
- xbd and xennet at hypervisor now attaches with xbd_hypervisor and
xennet_hypervisor
- x86_atomic_* renamed to xen_atomic_*
- use genassim.cf to pull in more constant from include files for assembly
- Map the shared info page from locore.S instead of the 0xffffffff hack
in xen_machdep.c
- remove some unused code
- some __asm__ __volatile__ -> __asm volatile and __inline__ -> inline
- more debug code
 1.2.8.1 06-Mar-2006  tron file xenbus_dev.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.2.6.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.2.4.3 24-May-2006  yamt sync with head.
 1.2.4.2 13-Mar-2006  yamt sync with head.
 1.2.4.1 06-Mar-2006  yamt file xenbus_dev.c was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.2.2.2 11-May-2006  elad sync with head
 1.2.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.3.2.3 01-Jun-2006  kardel Sync with head.
 1.3.2.2 22-Apr-2006  simonb Sync with head.
 1.3.2.1 11-Apr-2006  simonb file xenbus_dev.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.5.48.1 08-Dec-2007  mjf Sync with HEAD.
 1.5.46.2 16-Nov-2007  bouyer Initial domain0 support for xenamd64. The kernel boots multiuser, but
xen tools have not been tried yet.
In this process, cleanup some more the page table bootstrap, and properly
handle event counters for soft interrupts.
 1.5.46.1 17-Oct-2007  bouyer Prepare for xenamd64:
- kill xen/i386/identcpu.c, use i386/i386/identcpu.c instead (with a few
#ifndef XEN)
- move some files that can be shared between i386 and amd64 from
xen/i386 to xen/x86 (or to xen/xen for non-cpu-specific code)
- split assembly out of xen/include/hypervisor.h to xen/include/hypercalls.h
- use <xen/...> instead of <machine/...> for cpu-independant include files.

more work needed here, i386-specific files should got out of arch/xen to
arch/xeni386, and more code shared with arch/i386.
 1.5.42.1 09-Jan-2008  matt sync with HEAD
 1.5.40.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.5.22.1 03-Dec-2007  ad Sync with HEAD.
 1.5.12.2 09-Sep-2006  rpaulo sync with head
 1.5.12.1 07-May-2006  rpaulo file xenbus_dev.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:06 +0000
 1.5.6.3 07-Dec-2007  yamt sync with head
 1.5.6.2 21-Jun-2006  yamt sync with head.
 1.5.6.1 07-May-2006  yamt file xenbus_dev.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:24 +0000
 1.7.32.2 01-Nov-2009  jym Sync with HEAD.
 1.7.32.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.7.28.1 23-Sep-2011  sborrill Pull up the following revisions(s) (requested by jym in ticket #1672):
sys/arch/xen/conf/files.xen: revision 1.123 via patch
sys/arch/xen/xen/hypervisor.c: revision 1.58 via patch
sys/arch/xen/xenbus/xenbus_dev.c: revision 1.9
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.35

Expose Xen kernfs entries inside a domU to make it possible to use pkgsrc's
sysutils/xentools inside a domU to query XenStore entries (or even modify
part of it if the domain has enough rights).
 1.7.26.1 28-Apr-2009  skrll Sync with HEAD.
 1.7.18.1 04-May-2009  yamt sync with head.
 1.9.30.2 28-Aug-2017  skrll Sync with HEAD
 1.9.30.1 09-Jul-2016  skrll Sync with HEAD
 1.9.12.1 03-Dec-2017  jdolecek update from HEAD
 1.10.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.10.2.1 26-Apr-2017  pgoyette Sync with HEAD
 1.14.14.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.17.2.1 16-Apr-2020  bouyer Reorganise sources to make it possible to include Xen PVHVM support in
native kernels. Among others:
- move xen/include/amd64/hypercall.h to amd64/include/xen and
xen/include/i386/hypercall.h to i386/include/xen
- exclude some native files from the build for xenpv
- add xen to "machine" config statement for amd64 and i386
- split arch/xen/conf/files.xen to arch/xen/conf/files.xen (for pv drivers)
and arch/xen/conf/files.xen.pv (for full pv support)
- add GENERIC_XENHVM kernel config which includes GENERIC and add Xen PV
drivers.
 1.62 06-Feb-2025  bouyer Ignore cdroms only for VM_GUEST_XENPVHVM guests, not for all !XENPV kernels
(we can also have PVH guests with !XENPV kernels)
 1.61 06-Feb-2025  sborrill cdroms as xbd devices are skipped in PVHVM mode to avoid hangs if they
are not ready, i.e. do not contain an ISO image (that they hang is a bug
that should be fixed in itself). They are instead detected as an emulated
cdX which does deal with not being ready.

This leads to a regression in pure PV mode where xbd devices are hot-plugged
when an ISO is mounted and there are no emulated block devices such as cdX.
The xbd devices never appear and thus ISO images are not accessible.

Revert to pre-10 behaviour on pure PV kernels.
 1.60 17-Oct-2023  bouyer branches: 1.60.6;
xenbus: if dom0 support is not compiled in, panic with a usefull message
instead of waiting for an event which will never happen
 1.59 01-Aug-2023  mrg don't test arrays against NULL.

found by GCC 12.
 1.58 07-Aug-2021  thorpej branches: 1.58.6;
Merge thorpej-cfargs2.
 1.57 16-Jun-2021  bouyer branches: 1.57.2;
Grab KERNEL_LOCK before calling config_found()
 1.56 24-Apr-2021  thorpej branches: 1.56.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).
 1.55 26-May-2020  bouyer branches: 1.55.4;
Add need-flags for kernfs.
Compile Xen kernfs support only if kernfs is compiled in the kernel.
Should fix MODULAR build.
 1.54 14-May-2020  jdolecek fix compile when DPRINK() is defined
 1.53 13-May-2020  jdolecek don't reinitialize mutexes/cv on resume

part of PR port-xen/55207
 1.52 06-May-2020  bouyer Convert remaining splfoo/splx and tsleep/wakeup to mutex and condvar.
Mark kernel threads and interrupt handlers MPSAFE.
 1.51 28-Apr-2020  bouyer Skip block device with device-type "cdrom", as their emulation can't be
disabled; and the backend driver doesn't handle them either.
Fix hang when booting with 'ioemu:hdc:cdrom' type disks.
While there convert some printf to aprint_error()
 1.50 25-Apr-2020  bouyer Merge the bouyer-xenpvh branch, bringing in Xen PV drivers support under HVM
guests in GENERIC.
Xen support can be disabled at runtime with
boot -c
disable hypervisor
 1.49 11-Apr-2020  jdolecek put xenbus dmat into xenbus_device so it's available also for backend
devices which don't use autoconfig, remove from attach args
 1.48 10-Apr-2020  jdolecek add and pass dma tag to PV drivers attached to xenbus, so thay can
use bus_dmamap_load_mbuf() et.al.

due to XENPV override, _BUS_BUS_TO_PHYS() dmamap segment ds_addr
gets filled with ma, so value can be directly used for e.g. grant calls
 1.47 10-Apr-2020  jdolecek g/c unused xenwatch_mutex extern, and streq()
 1.46 07-Apr-2020  jdolecek branches: 1.46.2;
add a small wrapper xenbus_directory_free() to free result of
xenbus_directory(), so that caller doesn't need to be aware how the memory
was allocated
 1.45 07-Apr-2020  jdolecek partially convert to kmem_alloc()

plug memory leak in one xenbus_probe_device_type() error path
when read_backend_details() fails
 1.44 07-Apr-2020  jdolecek convert the node watch code to use kmem_alloc() instead of malloc()
 1.43 07-Apr-2020  jdolecek revert the watch.node change, xenbus_watch_path() and xenbus_watch_path2()
need to use non-constant path
 1.42 07-Apr-2020  jdolecek change xenbus_read() interface so that caller supplies the buffer and it's
size, caller doesn't free(9) the returned value any more
 1.41 07-Apr-2020  jdolecek no need to malloc()+copy watch.node, it's constant string - either "device"
or "backend"; just use the strings direct
 1.40 26-Feb-2019  joerg Compute storage size for a string correctly.
 1.39 07-Jul-2016  msaitoh branches: 1.39.18;
KNF. Remove extra spaces. No functional change.
 1.38 13-Oct-2013  riz branches: 1.38.6;
Catch up to recent changes in config_pending_{incr,decr}().
 1.37 07-Jun-2012  sborrill branches: 1.37.2; 1.37.4;
Fix problem where devices with ID 0 were skipped as invalid as it didn't
distinguish between numerical zero and invalid numeric string.
 1.36 05-Jun-2012  sborrill Sort vif and vbd device IDs numerically so that attach order does not depend
on the order they are passed in through xenstore. While this works for
hand-crafted Xen configuration files, it does not work for XenServer, XCP or
EC2 instances. This means that adding an extra virtual disk can make the
domU unbootable.

ID is actually based on the Linux device major/minor so this approach isn't
entirely correct (for instance, you can specify devices to be non-contiguous
which doesn't fit too well with our autoconf approach), but it works as a
first approximation.

Tested by me on XenServer and riz@ on EC2. OK bouyer@
 1.35 22-Sep-2011  jym branches: 1.35.2; 1.35.8;
Expose Xen kernfs entries inside a domU. Patch originally from sborrill@,
slightly modified by me to profit from runtime checks for dom0 privileges
instead of using compile time macros (DOM0OPS).

It should now be possible to use pkgsrc's sysutils/xentools inside
a domU to query XenStore entries (or even modify part of it if the domain
has enough rights).
 1.34 20-Sep-2011  jym Merge jym-xensuspend branch in -current. ok bouyer@.

Goal: save/restore support in NetBSD domUs, for i386, i386 PAE and amd64.

Executive summary:
- split all Xen drivers (xenbus(4), grant tables, xbd(4), xennet(4))
in two parts: suspend and resume, and hook them to pmf(9).
- modify pmap so that Xen hypervisor does not cry out loud in case
it finds "unexpected" recursive memory mappings
- provide a sysctl(7), machdep.xen.suspend, to command suspend from
userland via powerd(8). Note: a suspend can only be handled correctly
when dom0 requested it, so provide a mechanism that will prevent
kernel to blindly validate user's commands

The code is still in experimental state, use at your own risk: restore
can corrupt backend communications rings; this can completely thrash
dom0 as it will loop at a high interrupt level trying to honor
all domU requests.

XXX PAE suspend does not work in amd64 currently, due to (yet again!)
page validation issues with hypervisor. Will fix.

XXX secondary CPUs are not suspended, I will write the handlers
in sync with cherry's Xen MP work.

Tested under i386 and amd64, bear in mind ring corruption though.

No build break expected, GENERICs and XEN* kernels should be fine.
./build.sh distribution still running. In any case: sorry if it does
break for you, contact me directly for reports.
 1.33 17-Jul-2011  joerg Retire varargs.h support. Move machine/stdarg.h logic into MI
sys/stdarg.h and expect compiler to provide proper builtins, defaulting
to the GCC interface. lint still has a special fallback.
Reduce abuse of _BSD_VA_LIST_ by defining __va_list by default and
derive va_list as required by standards.
 1.32 18-Apr-2011  jym Large rewrite of the balloon driver. This one:

- turns balloon into a driver that attaches to xenbus(4). This allows to
disable the functionality either at compile time or boot time via
userconf(4). Driver can implement detach or pmf(9) hooks if deemed
necessary.

- keeps Cherry's locking model, but simplify it a bit. There is now
only one target value serialized inside balloon, we do not feedback
alternative value to Xenstore (clients are not expected to see its value
evolve behind their back, and can't do much about that either)

- implements min threshold; this is an admin-settable value that tells
driver to "not balloon below this threshold." This can be used by domain
to keep memory reservations, useful if activity is expected in the near
future.

- in addition to min threshold, the driver implements internally a
safeguard value (uvmexp.freemin + 1MiB), so that admin cannot
inadvertently set min to a very low value forcing domain into heavy
memory pressure and swapping.

- create the sysctl(8) kern.xen.balloon tree. 4 nodes are actually present
(values are in KiB):
- min: (rw) an admin-settable value that prevents ballooning below this
mark
- max: (ro) the maximum size for reservation, as set by xm(1) mem-max.
- current: (ro) the current reservation for domain.
- target: (rw) the targetted reservation for domain.

- fix a few limitations here and there, most notably the max_reservation
hypercall, and KiB vs pages representations at interfaces.

The driver is still turned off by default. Enabling it would need more
approval, especially from bouyer@, cherry@ and cegger@.

FWIW: tested it two days long, from amd64 dom0 (with dom0 ballooning
enabled for xend), and bunch of domUs. Did not notice anything suspicious.

XXX it still has one big limitation: it cannot hotplug memory pages in
uvm(9) if they were not present beforehand. Example: ballooning above
physmem will give more pages to domain but it won't use it to serve
allocations, unless we teach uvm(9) how to handle the extra pages.
 1.31 12-Apr-2011  cegger previous fix does not work if there is exactly only one entry where continue
exits the loop.
Apply fix from Konrad Wilke on port-xen@
That makes NetBSD DomU boot on Linux Dom0 with xl.
 1.30 11-Apr-2011  cegger Continue scanning for other frontends when initialization
of one frontend failed. Bug reported by Konrad Wilk on port-xen@.
Fix this for all error pathes within the loop.
 1.29 30-Mar-2011  jym (purely cosmetic changes)

- Use free_otherend_details() instead of calling free() on xbusd_otherend.
- rename talk_to_otherend() to watch_otherend(). We register a watch for
changes in the otherend device "state"; we are not really talking to it.
- add missing prototypes.
 1.28 06-Jul-2010  cherry branches: 1.28.2;
The Xen balloon driver enables growing and shrinking
PV domains on the fly, by collaborating with UVM and the hypervisor
 1.27 09-Jan-2009  jym branches: 1.27.2; 1.27.4; 1.27.6;
Do not probe for console when configuring xenbus devices, as it is
attached earlier during boot, when initializing hypervisor.

This avoids the "unknown type console at xenbus0 id 0 not configured"
autoconf(9) messages, which are misleading during domU's boot.

See also http://mail-index.netbsd.org/port-xen/2009/01/05/msg004621.html

Ok by bouyer@ in private mail.
 1.26 29-Oct-2008  cegger branches: 1.26.2;
include <xen/xen.h> for xendomain_is_dom0()
 1.25 24-Oct-2008  jym branches: 1.25.2;
- rename init_events() to events_init(), to better reflect netbsd semantics

- change unbind_[pv]irq_from_evtch() so that they now return the event
channel the [PV]IRQ was bound to. It reflects the opposite behaviour of the
bind_[pv]irq_to_evtch() functions.

- remove xenbus_suspend() and xenbus_resume() prototypes, as they are not
used anywhere else, and will conflict with the xenbus pmf(9) handlers.

- make start_info aligned on a page boundary, as Xen expects it to be so.

- mask event channel during xbd detach before removing its handler (can
avoid spurious events).

- add the "protocol" entry in xenstore during xbd initialization. Normally
created during domU's boot by xentools, it is under domU's responsibility
in all other cases (save/restore, hot plugging, etc.).

- modifications to xs_init(), so that it can properly return an error.

Reviewed by Christoph (cegger@).
 1.24 21-Oct-2008  cegger The 'sc' in xenbus_sc implies a softc, but it is actually a device_t. Since the device_t/softc split you can't cast a softc out of a device and vice versa.
Therefore rename xenbus_sc to xenbus_dev to prevent any possible confusions.
 1.23 21-Oct-2008  cegger xenbus_probe_init():
- use xendomain_is_dom0() (from jym@)
- replace return with kthread_exit(0) (from jym@)
- cleanup error handling
- plug memory leak in error path
- use aprint_error_dev for error messages (from jym@)

xenbus_attach():
- use aprint_error_dev for error message
 1.22 05-Sep-2008  tron Compile NetBSD/amd64 kernels with "-Wextra". Patches contributed by
Juan RP in PR port-amd64/39266.
 1.21 02-Jun-2008  cegger branches: 1.21.4;
Dom0: Add a delay before registering event handlers. Wait 1s per request from bouyer.

Fixes "Hotplug scripts not working" problems when launching guests.
Reviewed by bouyer.
 1.20 16-Apr-2008  cegger branches: 1.20.2; 1.20.4; 1.20.6;
device_t / softc split
reviewed, tested and approved by bouyer
 1.19 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.18 15-Dec-2007  perry branches: 1.18.6;
__FUNCTION__ -> __func__
 1.17 22-Nov-2007  bouyer branches: 1.17.2; 1.17.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.16 17-Oct-2007  garbled branches: 1.16.2;
Merge the ppcoea-renovation branch to HEAD.

This branch was a major cleanup and rototill of many of the various OEA
cpu based PPC ports that focused on sharing as much code as possible
between the various ports to eliminate near-identical copies of files in
every tree. Additionally there is a new PIC system that unifies the
interface to interrupt code for all different OEA ppc arches. The work
for this branch was done by a variety of people, too long to list here.

TODO:
bebox still needs work to complete the transition to -renovation.
ofppc still needs a bunch of work, which I will be looking at.
ev64260 still needs to be renovated
amigappc was not attempted.

NOTES:
pmppc was removed as an arch, and moved to a evbppc target.
 1.15 09-Jul-2007  ad branches: 1.15.8; 1.15.10; 1.15.14;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.14 29-Sep-2006  christos branches: 1.14.8; 1.14.10; 1.14.16;
Coverity CID 3786: Fix memory leak (from Arnaud Lacombe)
 1.13 11-Aug-2006  yamt branches: 1.13.2; 1.13.4; 1.13.6;
"xm shutdown" support for xen3. ok'ed by Manuel Bouyer.
 1.12 25-Jun-2006  bouyer branches: 1.12.2;
Add a 'base' argument to xenbus_read_ul, so that we can read number is base
other than 10.
 1.11 23-May-2006  bouyer branches: 1.11.2; 1.11.4;
Add needed framework for backend drivers.
As we want some control on the name the backend driver will have we
can't use autoconf(9) here. Instead backend drivers registers to
xenbus, which will call a create callback when a new device is there.
Backend devices won't have a "struct device" in xenbus, use a void pointer
instead.
 1.10 07-May-2006  bouyer branches: 1.10.2;
Call xb_init_comms() once the event channel has been allocated for dom0.
 1.9 11-Apr-2006  bouyer branches: 1.9.2;
Centralize all xenbus /kern entries in xenbus_dev.c, and call
xenbus_kernfs_init() for domain0. Now /kern/xen/xenbus is also present
on domain0.
 1.8 09-Apr-2006  bouyer Move xenkernfs_init() back in hypervisor.c so that /kern/xen/privcmd can
also register properly.
 1.7 09-Apr-2006  bouyer start xenbus support for domain0: allocate the page and the event channel,
and export theses via /kern/xen/xsd_mfn and /kern/xen/xsd_port.
 1.6 09-Apr-2006  bouyer #if out read_frontend_details() for now, so that it compiles with DOM0OPS.
 1.5 26-Mar-2006  bouyer branches: 1.5.2;
Properly handle dynamic attach/detach of device:
- keep a linked list of xenbus_device in the xenbus_infrastructure, and
keep a pointer to struct device for each xenbus_device
- xenbus_probe_device_type(): check that the device is not already attached
- when we get a frontend_changed callback, call xenbus_probe_device_type()
- When a device changes to state XenbusStateClosed, config_detach() it
and free the structures.
While there, move xbusd_path[] to the end of struct xenbus_device, and
allocate only the space needed to store the path. Garbage-collect
struct xenbus_driver, it's not needed.
 1.4 25-Mar-2006  bouyer Initialize memory to 0 when allocating a new xenbus device structure.
Prevents trying to unregister a nonexistant node in talk_to_otherend().
 1.3 15-Mar-2006  bouyer branches: 1.3.2;
Implement watch of xenstore nodes, and install watches for otherend'state
node.
 1.2 06-Mar-2006  bouyer branches: 1.2.2; 1.2.4;
Adapt for NetBSD and add glue for config(9). Enough to get guest domain
virtual devices probed and attached, domain0 and xenstore watch needs to
be done.
 1.1 06-Mar-2006  bouyer branches: 1.1.1;
Initial revision
 1.1.1.1 06-Mar-2006  bouyer Import xenbus sources, from linux sparse tree in the 20060107 xen-3.0
snapshot.
 1.2.4.7 11-Aug-2006  yamt sync with head
 1.2.4.6 26-Jun-2006  yamt sync with head.
 1.2.4.5 24-May-2006  yamt sync with head.
 1.2.4.4 11-Apr-2006  yamt sync with head
 1.2.4.3 01-Apr-2006  yamt sync with head.
 1.2.4.2 13-Mar-2006  yamt sync with head.
 1.2.4.1 06-Mar-2006  yamt file xenbus_probe.c was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.2.2.2 11-May-2006  elad sync with head
 1.2.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.3.2.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.3.2.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.5.2.3 08-Sep-2006  ghen Pull up following revision(s) (requested by jld in ticket #1499):
sys/arch/xen/conf/files.xen: revision 1.46
sys/arch/xen/xen/hypervisor.c: revision 1.26
sys/arch/xen/xen/shutdown_xenbus.c: revision 1.1
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.13
sys/arch/xen/include/shutdown_xenbus.h: revision 1.1
"xm shutdown" support for xen3. ok'ed by Manuel Bouyer.
 1.5.2.2 07-Apr-2006  tron Apply patch (requested by bouyer in ticket #1245):
Pull up Xen3 domU support. This adds support for the Xen-3 memory bootstrap,
xenstore, and block and network device frontend. Xen-3 support is turned
on by 'options XEN3', which disable Xen-2 support.
Changes affecting non-xen3 specific code:
- xbd and xennet at hypervisor now attaches with xbd_hypervisor and
xennet_hypervisor
- x86_atomic_* renamed to xen_atomic_*
- use genassim.cf to pull in more constant from include files for assembly
- Map the shared info page from locore.S instead of the 0xffffffff hack
in xen_machdep.c
- remove some unused code
- some __asm__ __volatile__ -> __asm volatile and __inline__ -> inline
- more debug code
 1.5.2.1 26-Mar-2006  tron file xenbus_probe.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.9.2.3 01-Jun-2006  kardel Sync with head.
 1.9.2.2 22-Apr-2006  simonb Sync with head.
 1.9.2.1 11-Apr-2006  simonb file xenbus_probe.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.10.2.1 19-Jun-2006  chap Sync with head.
 1.11.4.6 21-Jan-2008  yamt sync with head
 1.11.4.5 07-Dec-2007  yamt sync with head
 1.11.4.4 03-Sep-2007  yamt sync with head.
 1.11.4.3 30-Dec-2006  yamt sync with head.
 1.11.4.2 21-Jun-2006  yamt sync with head.
 1.11.4.1 23-May-2006  yamt file xenbus_probe.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:24 +0000
 1.11.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.12.2.1 08-Sep-2006  rpaulo Pull up following revision(s) (requested by jld in ticket #129):
sys/arch/xen/conf/files.xen: revision 1.46
sys/arch/xen/xen/hypervisor.c: revision 1.26
sys/arch/xen/xen/shutdown_xenbus.c: revision 1.1
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.13
sys/arch/xen/include/shutdown_xenbus.h: revision 1.1
"xm shutdown" support for xen3. ok'ed by Manuel Bouyer.
 1.13.6.1 22-Oct-2006  yamt sync with head
 1.13.4.2 09-Sep-2006  rpaulo sync with head
 1.13.4.1 11-Aug-2006  rpaulo file xenbus_probe.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:06 +0000
 1.13.2.1 18-Nov-2006  ad Sync with head.
 1.14.16.1 03-Oct-2007  garbled Sync with HEAD
 1.14.10.1 11-Jul-2007  mjf Sync with head.
 1.14.8.2 03-Dec-2007  ad Sync with HEAD.
 1.14.8.1 15-Jul-2007  ad Sync with head.
 1.15.14.1 17-Oct-2007  bouyer Prepare for xenamd64:
- kill xen/i386/identcpu.c, use i386/i386/identcpu.c instead (with a few
#ifndef XEN)
- move some files that can be shared between i386 and amd64 from
xen/i386 to xen/x86 (or to xen/xen for non-cpu-specific code)
- split assembly out of xen/include/hypervisor.h to xen/include/hypercalls.h
- use <xen/...> instead of <machine/...> for cpu-independant include files.

more work needed here, i386-specific files should got out of arch/xen to
arch/xeni386, and more code shared with arch/i386.
 1.15.10.2 09-Jan-2008  matt sync with HEAD
 1.15.10.1 06-Nov-2007  matt sync with HEAD
 1.15.8.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.16.2.2 27-Dec-2007  mjf Sync with HEAD.
 1.16.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.17.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.17.2.1 26-Dec-2007  ad Sync with head.
 1.18.6.4 17-Jan-2009  mjf Sync with HEAD.
 1.18.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.18.6.2 05-Jun-2008  mjf Sync with HEAD.

Also fix build.
 1.18.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.20.6.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.20.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.20.4.2 11-Aug-2010  yamt sync with head.
 1.20.4.1 04-May-2009  yamt sync with head.
 1.20.2.1 04-Jun-2008  yamt sync with head
 1.21.4.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.21.4.1 19-Oct-2008  haad Sync with HEAD.
 1.25.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.26.2.5 12-Jun-2012  riz Pull up following revision(s) (requested by sborrill in ticket #1768):
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.36
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.37
Sort vif and vbd device IDs numerically so that attach order does not depend
on the order they are passed in through xenstore. While this works for
hand-crafted Xen configuration files, it does not work for XenServer, XCP or
EC2 instances. This means that adding an extra virtual disk can make the
domU unbootable.
ID is actually based on the Linux device major/minor so this approach isn't
entirely correct (for instance, you can specify devices to be non-contiguous
which doesn't fit too well with our autoconf approach), but it works as a
first approximation.
Tested by me on XenServer and riz@ on EC2. OK bouyer@
Fix problem where devices with ID 0 were skipped as invalid as it didn't
distinguish between numerical zero and invalid numeric string.
 1.26.2.4 23-Sep-2011  sborrill Pull up the following revisions(s) (requested by jym in ticket #1672):
sys/arch/xen/conf/files.xen: revision 1.123 via patch
sys/arch/xen/xen/hypervisor.c: revision 1.58 via patch
sys/arch/xen/xenbus/xenbus_dev.c: revision 1.9
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.35

Expose Xen kernfs entries inside a domU to make it possible to use pkgsrc's
sysutils/xentools inside a domU to query XenStore entries (or even modify
part of it if the domain has enough rights).
 1.26.2.3 24-Apr-2011  riz Pull up following revision(s) (requested by cegger in ticket #1599):
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.31
previous fix does not work if there is exactly only one entry where continue
exits the loop.
Apply fix from Konrad Wilke on port-xen@
That makes NetBSD DomU boot on Linux Dom0 with xl.
 1.26.2.2 24-Apr-2011  riz Pull up following revision(s) (requested by cegger in ticket #1598):
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.30
Continue scanning for other frontends when initialization
of one frontend failed. Bug reported by Konrad Wilk on port-xen@.
Fix this for all error pathes within the loop.
 1.26.2.1 16-Jan-2009  snj Pull up following revision(s) (requested by jym in ticket #251):
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.27
Do not probe for console when configuring xenbus devices, as it is
attached earlier during boot, when initializing hypervisor.
This avoids the "unknown type console at xenbus0 id 0 not configured"
autoconf(9) messages, which are misleading during domU's boot.
See also http://mail-index.netbsd.org/port-xen/2009/01/05/msg004621.html
Ok by bouyer@ in private mail.
 1.27.6.2 21-Apr-2011  rmind sync with head
 1.27.6.1 05-Mar-2011  rmind sync with head
 1.27.4.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.27.2.9 27-Aug-2011  jym Sync with HEAD. Most notably: uvm/pmap work done by rmind@, and MP Xen
work of cherry@.

No regression observed on suspend/restore.
 1.27.2.8 07-May-2011  jym KNF.
 1.27.2.7 02-May-2011  jym Sync with head.
 1.27.2.6 30-Mar-2011  jym Sync with my commits in HEAD.
 1.27.2.5 24-Oct-2010  jym Sync with HEAD
 1.27.2.4 01-Nov-2009  jym - Upgrade suspend/resume code to comply with Xen2 removal.
- Add support for PAE domUs suspend/resume.
- Fix an issue regarding initialization of the xbd ring I/O that could end
badly during resume, with invalid block operations submitted to dom0 backend.

NetBSD supports PAE under x86_32 by considering the L2 page as being
4 pages long instead of 1.

Xen validates the page types during resume. Sadly, the hypervisor handles
alternative recursive mappings (== PG/PD entries pointing to pages other
than self) inadequately, which could lead to incorrect page pinning.

As a result, the important change with this patch is to clear these alternative
mappings during suspend, and reset them back to their former self upon
resume. For PAE, approx. all 4 PDIR_SLOT_PTEs could be considered as
alternative recursive mappings.

See comments in pmap.c for further details.

Now, let the testing and bug hunting begin.
 1.27.2.3 01-Nov-2009  jym Sync with HEAD.
 1.27.2.2 31-May-2009  jym Modifications for the Xen suspend/migrate/resume branch:

- introduce xenbus_device_{suspend,resume}() functions. These are routines
used to suspend/resume MI parts of the Xenbus device interfaces, like updating
frontend/backend devices' paths found in XenStore.

- introduce HYPERVISOR_sysctl(), an hypercall used only by Xentools to obtain
information from hypervisor (listing VMs, printing console, etc.). I use it
to query xenconsole from ddb(), as a last resort in case of a panic() in
dom0 (xm being not available). Currently unused in the branch; could be, if
requested.

- disable the rwlock(9) used to protect code that could use transient MFNs.
It could trigger nasty context switches in place it should not to.

- fix some bugs in the xennet/xbd suspend/resume pmf(9) handlers.

- following XenSource's design, talk_to_otherend() is now called
watch_otherend(), and free_otherend_details() is used by Xenbus device
suspend/resume routines.

- some slight modifications in pmap regarding APDP. Introduce an inline
function (pmap_unmap_apdp_pde()) that clears APDP entry for the current pmap.

- similarly, implement pmap_unmap_all_apdp_pdes() that iterates through all
pmaps and tears down APDP, as Xen does not handle them properly.

TODO/XXX:

- pmap_unmap_apdp_pde() does not handle APDP shadow entry of PAE. It will,
once I figure out how PAE uses it.

- revisit the pmap locking issue regarding transient MFNs. As NetBSD does not
use kernel preemption and MP for Xen, this could be skipped momentarily. See
http://mail-index.netbsd.org/port-xen/2009/04/27/msg004903.html for details.

- fix a bug regarding grant tables which could technically DoS a dom0 if
ridiculously high consumer/producer indexes are passed down in the ring during
a resume.

All in all, once the grant table index issue and APDP PAE are fixed, next step
is to torture test this branch.

Tested under i386 PAE and non-PAE, Xen3 dom0 and domU. amd64 is only compile
tested.
 1.27.2.1 09-Feb-2009  jym Initial code for xen save/restore/migrate facilities.

- split the attach code of frontends in two half: one that is only needed
during autoconf(9) attach/detach phases, and one used at each save/restore
of device state (between suspend and resume).

Applies to hypervisor, xencons, xenbus, xbd, and xennet.

- add a rwlock(9) ("ptom_lock") to protect the different parts in the kernel
that manipulate MFNs (which could change between a suspend and a resume,
without the kernel noticing it). Parts that require MFNs acquire a reader lock,
while suspend code will acquire a writer lock to ensure that no-other parts
in kernel still use MFNs.

- integrate the suspend code with sysmon.

- various things in pmap(9), and clock.

TODO:
- factorize code a bit more inside frontends drivers.
- remove all alternative recursive (APDP_PDE) mappings found in PD/PT during
suspend, as Xen does not support them.
- abstract the ptom_lock locking, it is only required when kernel preemption
is enabled, or on MP systems.

Current code works mostly. You may experience difficulties in some corner
cases (dom0 warnings about xennet interface errors, and Xen tools failing to
validate NetBSD's alternative pmaps).
 1.28.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.35.8.1 12-Jun-2012  riz Pull up following revision(s) (requested by sborrill in ticket #313):
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.36
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.37
Sort vif and vbd device IDs numerically so that attach order does not depend
on the order they are passed in through xenstore. While this works for
hand-crafted Xen configuration files, it does not work for XenServer, XCP or
EC2 instances. This means that adding an extra virtual disk can make the
domU unbootable.
ID is actually based on the Linux device major/minor so this approach isn't
entirely correct (for instance, you can specify devices to be non-contiguous
which doesn't fit too well with our autoconf approach), but it works as a
first approximation.
Tested by me on XenServer and riz@ on EC2. OK bouyer@
Fix problem where devices with ID 0 were skipped as invalid as it didn't
distinguish between numerical zero and invalid numeric string.
 1.35.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.35.2.1 30-Oct-2012  yamt sync with head
 1.37.4.1 18-May-2014  rmind sync with head
 1.37.2.2 03-Dec-2017  jdolecek update from HEAD
 1.37.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.38.6.1 09-Jul-2016  skrll Sync with HEAD
 1.39.18.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.39.18.1 10-Jun-2019  christos Sync with HEAD
 1.46.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.55.4.3 23-Apr-2021  thorpej No need to be explicit about interface attributes here.
 1.55.4.2 02-Apr-2021  thorpej config_found_ia() -> config_found() w/ CFARG_IATTR.
 1.55.4.1 23-Mar-2021  thorpej Convert config_found_ia() call sites where the device only carries
a single interface attribute to bare config_found() calls.
 1.56.2.1 17-Jun-2021  thorpej Sync w/ HEAD.
 1.57.2.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.58.6.2 20-Feb-2025  martin Pull up following revision(s) (requested by sborrill in ticket #1050):

sys/arch/xen/xenbus/xenbus_probe.c: revision 1.61
sys/arch/xen/xenbus/xenbus_probe.c: revision 1.62

cdroms as xbd devices are skipped in PVHVM mode to avoid hangs if they
are not ready, i.e. do not contain an ISO image (that they hang is a bug
that should be fixed in itself). They are instead detected as an emulated
cdX which does deal with not being ready.

This leads to a regression in pure PV mode where xbd devices are hot-plugged
when an ISO is mounted and there are no emulated block devices such as cdX.

The xbd devices never appear and thus ISO images are not accessible.

Revert to pre-10 behaviour on pure PV kernels.

Ignore cdroms only for VM_GUEST_XENPVHVM guests, not for all !XENPV kernels
(we can also have PVH guests with !XENPV kernels)
 1.58.6.1 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #426):

sys/arch/xen/xenbus/xenbus_probe.c: revision 1.60

xenbus: if dom0 support is not compiled in, panic with a usefull message
instead of waiting for an event which will never happen
 1.60.6.1 02-Aug-2025  perseant Sync with HEAD
 1.28 01-Sep-2022  bouyer process_msg() is called from thread context, so malloc() can wait for
memory. Should avoids occasional ENOMEM reading messages
 1.27 06-May-2020  bouyer Convert remaining splfoo/splx and tsleep/wakeup to mutex and condvar.
Mark kernel threads and interrupt handlers MPSAFE.
 1.26 07-Apr-2020  jdolecek add wrapper to free response from xenbus_dev_request_and_reply(), so
that call in xenbus_dev_write() wouldn't need to know how it's allocated
 1.25 07-Apr-2020  jdolecek add a small wrapper xenbus_directory_free() to free result of
xenbus_directory(), so that caller doesn't need to be aware how the memory
was allocated
 1.24 07-Apr-2020  jdolecek change xenbus_read() interface so that caller supplies the buffer and it's
size, caller doesn't free(9) the returned value any more
 1.23 28-Nov-2012  royger branches: 1.23.38;
xen: prevent adding duplicate xenwatches

When a xenstore watch triggers, the event is processed on process_msg
and if a valid handle it's found the handler is queued for execution
on the pending xen watches queue (watch_events).

This may present a problem if we trigger a xenwatch several times and
then disconnect the device. If several xenwatch events are added to
the watch_events queue and the device is disconnected afterwards, the
first processed xenwatch event will disconnect the device, remove the
watch and free all resources. This triggers a panic if there are
pending xenwatch events for that device already queued in the local
queue of the function xenwatch_thread, since when the next watch that
has the same handler tries to execute we get a panic due to the fact
that the device is already disconnected and all resources had been
freed:

xenbus_watch: 0xffffa0000b7cd1d0
xbw_callback: 0xffffffff80755dd4
otherend_changed: backend/vif/1/0
backend/vif/1/0/state 6
backend/vif/1/0 -> Closed
backend/vif/1/0 -> backend_device, b_detach: 0xffffffff8075a2bf
xenbus_watch: 0xffffa0000b7cd1d0
xbw_callback: 0xfc5ec02183e547a8
fatal protection fault in supervisor mode
trap type 4 code 0 rip ffffffff80756596 cs e030 rflags 10246 cr2
7f7ff7b4c020 ilevel 0 rsp ffffa000e6d82c50
curlwp 0xffffa0000a72d580 pid 0 lid 36 lowest kstack
0xffffa000e6d7f000
kernel: protection fault trap, code=0
Stopped in pid 0.36 (system) at netbsd:xenwatch_thread+0xc7: call
*10(%rax
)
xenwatch_thread() at netbsd:xenwatch_thread+0xc7
ds f
es 5987
fs 2c40
gs 1460
rdi ffffa0000b7cd1d0
rsi ffffa0000a5477f0
rbp ffffa000e6d82c70
rbx ffffa0000b7c14c0
rdx 2
rcx f
rax ffffa0000b7cd1d0
r8 78
r9 ffffffef
r10 deadbeef
r11 1
r12 ffffa000e6d82c50
r13 ffffa0000a72d580
r14 ffffa0000a72d580
r15 0
rip ffffffff80756596 xenwatch_thread+0xc7
cs e030
rflags 10246
rsp ffffa000e6d82c50
ss e02b
netbsd:xenwatch_thread+0xc7: call *10(%rax)
 1.22 27-Jul-2011  matt branches: 1.22.2; 1.22.8; 1.22.12; 1.22.14;
Change a cast to appease gcc4.5
 1.21 17-Jul-2011  joerg Retire varargs.h support. Move machine/stdarg.h logic into MI
sys/stdarg.h and expect compiler to provide proper builtins, defaulting
to the GCC interface. lint still has a special fallback.
Reduce abuse of _BSD_VA_LIST_ by defining __va_list by default and
derive va_list as required by standards.
 1.20 07-Jun-2011  bouyer check that the list is empty before calling cv_wait(). Otherwise
we may sleep waiting for an event which is already in the queue.
 1.19 22-May-2011  rmind branches: 1.19.2;
- Replace uses of simple_lock and ltsleep with mutex and condvar.
- Improve some parts of the code to be more MP-friendly.

Tested by jakllsch@.
 1.18 28-Apr-2009  cegger branches: 1.18.4; 1.18.6;
sprintf -> snprintf
 1.17 29-Oct-2008  cegger branches: 1.17.6;
include <xen/xen.h> for xendomain_is_dom0()
 1.16 24-Oct-2008  jym branches: 1.16.2;
- rename init_events() to events_init(), to better reflect netbsd semantics

- change unbind_[pv]irq_from_evtch() so that they now return the event
channel the [PV]IRQ was bound to. It reflects the opposite behaviour of the
bind_[pv]irq_to_evtch() functions.

- remove xenbus_suspend() and xenbus_resume() prototypes, as they are not
used anywhere else, and will conflict with the xenbus pmf(9) handlers.

- make start_info aligned on a page boundary, as Xen expects it to be so.

- mask event channel during xbd detach before removing its handler (can
avoid spurious events).

- add the "protocol" entry in xenstore during xbd initialization. Normally
created during domU's boot by xentools, it is under domU's responsibility
in all other cases (save/restore, hot plugging, etc.).

- modifications to xs_init(), so that it can properly return an error.

Reviewed by Christoph (cegger@).
 1.15 24-Oct-2008  jym - printf -> aprint_*
- fix and add comments
- make some panic/error messages more relevant
- remove last '\n' in DPRINTK() macros, not required as it is already part of format string.

No functional changes.
 1.14 13-Mar-2008  bouyer branches: 1.14.4; 1.14.10;
Add swedge support to xbd backed, and make the virtual block device handle
sizes larger than 4TB. Tested by Jukka Marin.
Should fix kern/37370.
 1.13 11-Jan-2008  bouyer branches: 1.13.2; 1.13.6;
Merge the bouyer-xeni386 branch to head, at tag bouyer-xeni386-merge1 (the
branch is still active and will see i386PAE support developement).
Sumary of changes:
- switch xeni386 to the x86/x86/pmap.c, and the xen/x86/x86_xpmap.c
pmap bootstrap.
- merge back most of xen/i386/ to i386/i386
- change the build to reduce diffs between i386 and amd64 in file locations
- remove include files that were identical to the i386/amd64 counterparts,
the build will find them via the xen-ma/machine link.
 1.12 15-Dec-2007  perry __FUNCTION__ -> __func__
 1.11 06-Dec-2007  ad branches: 1.11.4;
lockmgr -> mutex
 1.10 22-Nov-2007  bouyer branches: 1.10.2;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.9 17-Oct-2007  garbled branches: 1.9.2;
Merge the ppcoea-renovation branch to HEAD.

This branch was a major cleanup and rototill of many of the various OEA
cpu based PPC ports that focused on sharing as much code as possible
between the various ports to eliminate near-identical copies of files in
every tree. Additionally there is a new PIC system that unifies the
interface to interrupt code for all different OEA ppc arches. The work
for this branch was done by a variety of people, too long to list here.

TODO:
bebox still needs work to complete the transition to -renovation.
ofppc still needs a bunch of work, which I will be looking at.
ev64260 still needs to be renovated
amigappc was not attempted.

NOTES:
pmppc was removed as an arch, and moved to a evbppc target.
 1.8 11-Jul-2007  dogcow branches: 1.8.8; 1.8.10; 1.8.14;
Looks like this was part of a botched merge; apply he@'s patch to make
everything compile/link again.
 1.7 09-Jul-2007  ad Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.6 25-Jun-2006  bouyer branches: 1.6.6; 1.6.10; 1.6.16; 1.6.18; 1.6.20; 1.6.24; 1.6.26;
Add a 'base' argument to xenbus_read_ul, so that we can read number is base
other than 10.
 1.5 02-Apr-2006  bouyer branches: 1.5.2; 1.5.4; 1.5.8; 1.5.10;
unregister_xenbus_watch(): Ops, correct code to remove pending watch events
so that it's not an infinite loop.
 1.4 16-Mar-2006  bouyer Add a xenbus_read_ul() which reads a node and convert it to unsigned long.
Make xenbus_switch_state() and xenbus_read_driver_state() do something usefull.
 1.3 15-Mar-2006  bouyer branches: 1.3.2;
Implement watch of xenstore nodes, and install watches for otherend'state
node.
 1.2 06-Mar-2006  bouyer branches: 1.2.2; 1.2.4;
Adapt for NetBSD and add glue for config(9). Enough to get guest domain
virtual devices probed and attached, domain0 and xenstore watch needs to
be done.
 1.1 06-Mar-2006  bouyer branches: 1.1.1;
Initial revision
 1.1.1.1 06-Mar-2006  bouyer Import xenbus sources, from linux sparse tree in the 20060107 xen-3.0
snapshot.
 1.2.4.5 26-Jun-2006  yamt sync with head.
 1.2.4.4 11-Apr-2006  yamt sync with head
 1.2.4.3 01-Apr-2006  yamt sync with head.
 1.2.4.2 13-Mar-2006  yamt sync with head.
 1.2.4.1 06-Mar-2006  yamt file xenbus_xs.c was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.2.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.3.2.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.3.2.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.5.10.7 17-Mar-2008  yamt sync with head.
 1.5.10.6 21-Jan-2008  yamt sync with head
 1.5.10.5 07-Dec-2007  yamt sync with head
 1.5.10.4 03-Sep-2007  yamt sync with head.
 1.5.10.3 30-Dec-2006  yamt sync with head.
 1.5.10.2 21-Jun-2006  yamt sync with head.
 1.5.10.1 02-Apr-2006  yamt file xenbus_xs.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:24 +0000
 1.5.8.1 13-Jul-2006  gdamore Merge from HEAD.
 1.5.4.2 22-Apr-2006  simonb Sync with head.
 1.5.4.1 02-Apr-2006  simonb file xenbus_xs.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.5.2.2 07-Apr-2006  tron Apply patch (requested by bouyer in ticket #1245):
Pull up Xen3 domU support. This adds support for the Xen-3 memory bootstrap,
xenstore, and block and network device frontend. Xen-3 support is turned
on by 'options XEN3', which disable Xen-2 support.
Changes affecting non-xen3 specific code:
- xbd and xennet at hypervisor now attaches with xbd_hypervisor and
xennet_hypervisor
- x86_atomic_* renamed to xen_atomic_*
- use genassim.cf to pull in more constant from include files for assembly
- Map the shared info page from locore.S instead of the 0xffffffff hack
in xen_machdep.c
- remove some unused code
- some __asm__ __volatile__ -> __asm volatile and __inline__ -> inline
- more debug code
 1.5.2.1 02-Apr-2006  tron file xenbus_xs.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.6.26.1 04-Sep-2008  skrll Sync with netbsd-4.
 1.6.24.1 03-Oct-2007  garbled Sync with HEAD
 1.6.20.1 09-Dec-2007  reinoud Pullup to HEAD
 1.6.18.1 11-Jul-2007  mjf Sync with head.
 1.6.16.2 03-Dec-2007  ad Sync with HEAD.
 1.6.16.1 15-Jul-2007  ad Sync with head.
 1.6.10.1 31-Aug-2008  jdc Pull up revisions:
sys/arch/xen/include/xenbus.h:1.8
sys/arch/xen/xen/xbd_xenbus.c:1.24
sys/arch/xen/xen/xbdback_xenbus.c:1.15
sys/arch/xen/xenbus/xenbus_xs.c:1.14
via patch (requested by bouyer in ticket #1149).
 1.6.6.2 09-Sep-2006  rpaulo sync with head
 1.6.6.1 25-Jun-2006  rpaulo file xenbus_xs.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:06 +0000
 1.8.14.1 17-Oct-2007  bouyer Prepare for xenamd64:
- kill xen/i386/identcpu.c, use i386/i386/identcpu.c instead (with a few
#ifndef XEN)
- move some files that can be shared between i386 and amd64 from
xen/i386 to xen/x86 (or to xen/xen for non-cpu-specific code)
- split assembly out of xen/include/hypervisor.h to xen/include/hypercalls.h
- use <xen/...> instead of <machine/...> for cpu-independant include files.

more work needed here, i386-specific files should got out of arch/xen to
arch/xeni386, and more code shared with arch/i386.
 1.8.10.3 23-Mar-2008  matt sync with HEAD
 1.8.10.2 09-Jan-2008  matt sync with HEAD
 1.8.10.1 06-Nov-2007  matt sync with HEAD
 1.8.8.2 09-Dec-2007  jmcneill Sync with HEAD.
 1.8.8.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.9.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.9.2.2 27-Dec-2007  mjf Sync with HEAD.
 1.9.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.10.2.2 26-Dec-2007  ad Sync with head.
 1.10.2.1 08-Dec-2007  ad Sync with head.
 1.11.4.2 08-Jan-2008  bouyer Make XEN kernels build again.
 1.11.4.1 02-Jan-2008  bouyer Sync with HEAD
 1.13.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.13.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.13.2.1 24-Mar-2008  keiichi sync with head.
 1.14.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.14.4.1 04-May-2009  yamt sync with head.
 1.16.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.17.6.3 27-Aug-2011  jym Sync with HEAD. Most notably: uvm/pmap work done by rmind@, and MP Xen
work of cherry@.

No regression observed on suspend/restore.
 1.17.6.2 01-Nov-2009  jym Sync with HEAD.
 1.17.6.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.18.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.18.4.2 12-Jun-2011  rmind sync with head
 1.18.4.1 31-May-2011  rmind sync with head
 1.19.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.22.14.1 30-Nov-2012  msaitoh Pull up following revision(s) (requested by royger in ticket #728):
sys/arch/xen/xenbus/xenbus_xs.c: revision 1.23
xen: prevent adding duplicate xenwatches
When a xenstore watch triggers, the event is processed on process_msg
and if a valid handle it's found the handler is queued for execution
on the pending xen watches queue (watch_events).
This may present a problem if we trigger a xenwatch several times and
then disconnect the device. If several xenwatch events are added to
the watch_events queue and the device is disconnected afterwards, the
first processed xenwatch event will disconnect the device, remove the
watch and free all resources. This triggers a panic if there are
pending xenwatch events for that device already queued in the local
queue of the function xenwatch_thread, since when the next watch that
has the same handler tries to execute we get a panic due to the fact
that the device is already disconnected and all resources had been
freed:
xenbus_watch: 0xffffa0000b7cd1d0
xbw_callback: 0xffffffff80755dd4
otherend_changed: backend/vif/1/0
backend/vif/1/0/state 6
backend/vif/1/0 -> Closed
backend/vif/1/0 -> backend_device, b_detach: 0xffffffff8075a2bf
xenbus_watch: 0xffffa0000b7cd1d0
xbw_callback: 0xfc5ec02183e547a8
fatal protection fault in supervisor mode
trap type 4 code 0 rip ffffffff80756596 cs e030 rflags 10246 cr2
7f7ff7b4c020 ilevel 0 rsp ffffa000e6d82c50
curlwp 0xffffa0000a72d580 pid 0 lid 36 lowest kstack
0xffffa000e6d7f000
kernel: protection fault trap, code=0
Stopped in pid 0.36 (system) at netbsd:xenwatch_thread+0xc7: call
*10(%rax
)
xenwatch_thread() at netbsd:xenwatch_thread+0xc7
ds f
es 5987
fs 2c40
gs 1460
rdi ffffa0000b7cd1d0
rsi ffffa0000a5477f0
rbp ffffa000e6d82c70
rbx ffffa0000b7c14c0
rdx 2
rcx f
rax ffffa0000b7cd1d0
r8 78
r9 ffffffef
r10 deadbeef
r11 1
r12 ffffa000e6d82c50
r13 ffffa0000a72d580
r14 ffffa0000a72d580
r15 0
rip ffffffff80756596 xenwatch_thread+0xc7
cs e030
rflags 10246
rsp ffffa000e6d82c50
ss e02b
netbsd:xenwatch_thread+0xc7: call *10(%rax)
 1.22.12.1 25-Feb-2013  tls resync with head
 1.22.8.1 30-Nov-2012  msaitoh Pull up following revision(s) (requested by royger in ticket #728):
sys/arch/xen/xenbus/xenbus_xs.c: revision 1.23
xen: prevent adding duplicate xenwatches
When a xenstore watch triggers, the event is processed on process_msg
and if a valid handle it's found the handler is queued for execution
on the pending xen watches queue (watch_events).
This may present a problem if we trigger a xenwatch several times and
then disconnect the device. If several xenwatch events are added to
the watch_events queue and the device is disconnected afterwards, the
first processed xenwatch event will disconnect the device, remove the
watch and free all resources. This triggers a panic if there are
pending xenwatch events for that device already queued in the local
queue of the function xenwatch_thread, since when the next watch that
has the same handler tries to execute we get a panic due to the fact
that the device is already disconnected and all resources had been
freed:
xenbus_watch: 0xffffa0000b7cd1d0
xbw_callback: 0xffffffff80755dd4
otherend_changed: backend/vif/1/0
backend/vif/1/0/state 6
backend/vif/1/0 -> Closed
backend/vif/1/0 -> backend_device, b_detach: 0xffffffff8075a2bf
xenbus_watch: 0xffffa0000b7cd1d0
xbw_callback: 0xfc5ec02183e547a8
fatal protection fault in supervisor mode
trap type 4 code 0 rip ffffffff80756596 cs e030 rflags 10246 cr2
7f7ff7b4c020 ilevel 0 rsp ffffa000e6d82c50
curlwp 0xffffa0000a72d580 pid 0 lid 36 lowest kstack
0xffffa000e6d7f000
kernel: protection fault trap, code=0
Stopped in pid 0.36 (system) at netbsd:xenwatch_thread+0xc7: call
*10(%rax
)
xenwatch_thread() at netbsd:xenwatch_thread+0xc7
ds f
es 5987
fs 2c40
gs 1460
rdi ffffa0000b7cd1d0
rsi ffffa0000a5477f0
rbp ffffa000e6d82c70
rbx ffffa0000b7c14c0
rdx 2
rcx f
rax ffffa0000b7cd1d0
r8 78
r9 ffffffef
r10 deadbeef
r11 1
r12 ffffa000e6d82c50
r13 ffffa0000a72d580
r14 ffffa0000a72d580
r15 0
rip ffffffff80756596 xenwatch_thread+0xc7
cs e030
rflags 10246
rsp ffffa000e6d82c50
ss e02b
netbsd:xenwatch_thread+0xc7: call *10(%rax)
 1.22.2.1 16-Jan-2013  yamt sync with (a bit old) head
 1.23.38.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411

RSS XML Feed