Home | History | Annotate | only in /src/sys/arch/xen
History log of /src/sys/arch/xen
RevisionDateAuthorComments
 1.9 31-Mar-2010  dyoung Include more sources in the tags target. Don't compute tags on some
non-existent assembly-language files.
 1.8 18-Aug-2009  dyoung branches: 1.8.2; 1.8.4;
Make the 'tags' target work by borrowing stuff from sys/arch/i386/Makefile.
 1.7 20-May-2009  dyoung Revert part of last: .include <bsd.own.mk> for TOOL_SED / TOOL_AWK
definitions instead of using :Used / :Uawk alternates. Now,
'nbmake-i386 tags' works.
 1.6 20-May-2009  dyoung If TOOL_AWK or TOOL_SED are undefined, then use awk or sed,
respectively.

Use ${FINDCOMM} to avoid too-long arguments lists.
 1.5 25-Oct-2008  apb branches: 1.5.8;
Use ${TOOL_SED} instead if plain sed in Makefiles.
 1.4 19-Oct-2008  apb Use ${TOOL_AWK} instead of ${AWK} or plain "awk" in make commands.
Pass AWK=${TOOL_AWK:Q} to shell scripts that use awk.
 1.3 11-Dec-2005  christos branches: 1.3.74; 1.3.78; 1.3.84;
merge ktrace-lwp.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 12-May-2004  cl branches: 1.1.2; 1.1.4; 1.1.8; 1.1.10; 1.1.12;
``build.sh -m xen-i386 release'' now builds a release for NetBSD/xen
for i386. The resulting release consists of:
- NetBSD/xen for i386 kernel, loader and docuemntation
- NetBSD/i386 userland sets
 1.1.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.10.1 29-Apr-2005  kent sync with -current
 1.1.8.1 09-Mar-2005  bouyer stand is gone.
 1.1.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 12-May-2004  skrll file Makefile was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 12-May-2004  he file Makefile was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.3.84.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.3.78.4 11-Aug-2010  yamt sync with head.
 1.3.78.3 19-Aug-2009  yamt sync with head.
 1.3.78.2 20-Jun-2009  yamt sync with head
 1.3.78.1 04-May-2009  yamt sync with head.
 1.3.74.1 17-Jan-2009  mjf Sync with HEAD.
 1.5.8.3 29-Mar-2011  jym More sync fixes. And add the mbr_gpt files.
 1.5.8.2 01-Nov-2009  jym Sync with HEAD.
 1.5.8.1 31-May-2009  jym Sync with HEAD.
 1.8.4.1 30-May-2010  rmind sync with head
 1.8.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.2 11-Dec-2005  christos merge ktrace-lwp.
 1.1 11-Mar-2004  cl branches: 1.1.4;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file Makefile was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.25 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.24 25-Feb-2005  simonb Add COMPAT_20 (and COMPAT_16 in some cases) to kernel config files
that didn't have those options but had other earlier compat options.
 1.23 18-Feb-2005  dsl Invert FFS_SNAPSHOT to FFS_NO_SNAPSHOT
 1.22 11-Feb-2005  dsl Add 'option FFS_SNAPSHOT' to most of the config files.
Commented out for kernels that appear to hace space constraints.
 1.21 31-Jan-2005  hannken Add file system snapshots to kernel configs.

- Ffs internal snapshots get compiled in unconditionally.

- File system snapshot device fss(4) added to all kernel configs that
have a disk. Device is commented out on all non-GENERIC kernels.

Reviewed by: Jason Thorpe <thorpej@netbsd.org>
 1.20 28-Jan-2005  rumble Sprinkle options UFS_DIRHASH on GENERIC kernels. It's presently
commented out and labeled experimental pending further review and
testing.
 1.19 17-Jan-2005  cube branches: 1.19.2;
Add tap(4) support to a random^Wcarefully chosen set of kernel configs.
All those kernels have a line for both tun and bridge, and if either is
commented out, tap is commented out also. With the exception of i386's
GENERIC_TINY.

XXX: we _need_ some way of making this more simple.
 1.18 10-Nov-2004  christos branches: 1.18.4; 1.18.6;
Add COMPAT_BSDPTY to the rest of the config files.
 1.17 10-Nov-2004  christos Add COMPAT_BSDPTY on all the kernels that have COMPAT options.
 1.16 23-Sep-2004  tls Changes from Christian Limpach to improve timekeeping on NetBSD/xen by
actually adjusting the time correctly (calling hardclock as needed, not
just blindly every time Xen schedules us) based on Xen's idea of the
time in the shared page.

Xen source repo change info:
ChangeSet
2004/09/22 13:47:22+01:00 cl349@freefall.cl.cam.ac.uk
Fix time.

netbsd-2.0-xen-sparse/sys/arch/xen/xen/clock.c
2004/09/22 13:47:21+01:00 cl349@freefall.cl.cam.ac.uk +28 -3
Don't call hardclock on spurious timer interrupt and call hardclock
for missed interrupts.

netbsd-2.0-xen-sparse/sys/arch/xen/conf/XEN
2004/09/22 13:47:21+01:00 cl349@freefall.cl.cam.ac.uk +0 -1
Don't need custom HZ value any longer.

: ----------------------------------------------------------------------
 1.15 04-Sep-2004  manu IPv4 PIM support, based on a submission from Pavlin Radoslavov posted on
tech-net@
 1.14 15-Jul-2004  atatat branches: 1.14.2;
Add "options SYSCTL_INCLUDE_DESCR" to a lot of configs, but commented
out in most of them.
 1.13 26-Jun-2004  abs Add (commented out) ALTQ options to all GENERIC-like files
 1.12 18-Jun-2004  christos ptm is now mandatory, depends on pty, and can be disabled with -DNO_DEV_PTM
 1.11 16-Jun-2004  christos Add pseudo-device ptm on all the generic flavored kernels.
 1.10 07-May-2004  cl Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.9 26-Apr-2004  cl Add mouse support.
 1.8 24-Apr-2004  cl Enable keyboard and vga display as console when running as domain-0.
 1.7 24-Apr-2004  cl Add keyboard support and wscons config options.
 1.6 24-Apr-2004  cl Add vga display support.
 1.5 24-Apr-2004  cl Allow the block device driver to impersonate wd/sd/cd devices. This allows
for most system seamless migration from a NetBSD/i386 setup to a NetBSD/xen
setup without changing /etc/fstab or creating additional device nodes.
 1.4 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.3 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.2 17-Apr-2004  cl add block device driver
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.5 11-Aug-2007  bouyer Pull up following revision(s) (requested by adrianp in ticket #11356):
sys/arch/acorn32/conf/GENERIC 1.80 via patch
sys/arch/amd64/conf/GENERIC 1.154 via patch
sys/arch/amiga/conf/GENERIC.in 1.61 via patch
sys/arch/arc/conf/GENERIC 1.148 via patch
sys/arch/atari/conf/GENERIC.in 1.68 via patch
sys/arch/cats/conf/GENERIC 1.116 via patch
sys/arch/hp300/conf/GENERIC 1.141 via patch
sys/arch/hp700/conf/GENERIC patch
sys/arch/i386/conf/GENERIC 1.840 via patch
sys/arch/i386/conf/GENERIC_LAPTOP 1.239 via patch
sys/arch/i386/conf/GENERIC.MPACPI patch
sys/arch/mac68k/conf/GENERIC 1.182 via patch
sys/arch/sgimips/conf/GENERIC32_IP2x 1.68 via patch
sys/arch/sgimips/conf/GENERIC32_IP3x 1.66 via patch
sys/arch/sparc/conf/GENERIC 1.200 via patch
sys/arch/sparc64/conf/GENERIC32 patch
sys/arch/xen/conf/GENERIC patch
Remove iso(4) from GENERIC kernels, as discussed on tech-net@
Ok'ed by core@
 1.1.2.4 23-Sep-2004  jmc branches: 1.1.2.4.2; 1.1.2.4.4;
Pullup rev 1.16 (requested by tls in ticket #871)

Changes to improve timekeeping on NetBSD/xen by actually adjusting the time
correctly (calling hardclock as needed, not just blindly every time Xen
schedules us) based on Xens idea of the time in the shared page.
 1.1.2.3 15-Jul-2004  he Pull up revision 1.14 (requested by atatat in ticket #663):
Add "options SYSCTL_INCLUDE_DESCR" to a lot of configurations,
but commented out in most of them.
 1.1.2.2 30-Jun-2004  jdc Pull up revision 1.13 (requested by abs in ticket #567).

Add (commented out) ALTQ options to all GENERIC-like files
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.10 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.4.4.1 11-Aug-2007  bouyer Pull up following revision(s) (requested by adrianp in ticket #11356):
sys/arch/acorn32/conf/GENERIC 1.80 via patch
sys/arch/amd64/conf/GENERIC 1.154 via patch
sys/arch/amiga/conf/GENERIC.in 1.61 via patch
sys/arch/arc/conf/GENERIC 1.148 via patch
sys/arch/atari/conf/GENERIC.in 1.68 via patch
sys/arch/cats/conf/GENERIC 1.116 via patch
sys/arch/hp300/conf/GENERIC 1.141 via patch
sys/arch/hp700/conf/GENERIC patch
sys/arch/i386/conf/GENERIC 1.840 via patch
sys/arch/i386/conf/GENERIC_LAPTOP 1.239 via patch
sys/arch/i386/conf/GENERIC.MPACPI patch
sys/arch/mac68k/conf/GENERIC 1.182 via patch
sys/arch/sgimips/conf/GENERIC32_IP2x 1.68 via patch
sys/arch/sgimips/conf/GENERIC32_IP3x 1.66 via patch
sys/arch/sparc/conf/GENERIC 1.200 via patch
sys/arch/sparc64/conf/GENERIC32 patch
sys/arch/xen/conf/GENERIC patch
Remove iso(4) from GENERIC kernels, as discussed on tech-net@
Ok'ed by core@
 1.1.2.4.2.1 11-Aug-2007  bouyer Pull up following revision(s) (requested by adrianp in ticket #11356):
sys/arch/acorn32/conf/GENERIC 1.80 via patch
sys/arch/amd64/conf/GENERIC 1.154 via patch
sys/arch/amiga/conf/GENERIC.in 1.61 via patch
sys/arch/arc/conf/GENERIC 1.148 via patch
sys/arch/atari/conf/GENERIC.in 1.68 via patch
sys/arch/cats/conf/GENERIC 1.116 via patch
sys/arch/hp300/conf/GENERIC 1.141 via patch
sys/arch/hp700/conf/GENERIC patch
sys/arch/i386/conf/GENERIC 1.840 via patch
sys/arch/i386/conf/GENERIC_LAPTOP 1.239 via patch
sys/arch/i386/conf/GENERIC.MPACPI patch
sys/arch/mac68k/conf/GENERIC 1.182 via patch
sys/arch/sgimips/conf/GENERIC32_IP2x 1.68 via patch
sys/arch/sgimips/conf/GENERIC32_IP3x 1.66 via patch
sys/arch/sparc/conf/GENERIC 1.200 via patch
sys/arch/sparc64/conf/GENERIC32 patch
sys/arch/xen/conf/GENERIC patch
Remove iso(4) from GENERIC kernels, as discussed on tech-net@
Ok'ed by core@
 1.14.2.11 01-Apr-2005  skrll Sync with HEAD.
 1.14.2.10 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.14.2.9 15-Feb-2005  skrll Sync with HEAD.
 1.14.2.8 04-Feb-2005  skrll Sync with HEAD.
 1.14.2.7 24-Jan-2005  skrll Sync with HEAD.
 1.14.2.6 14-Nov-2004  skrll Sync with HEAD.
 1.14.2.5 24-Sep-2004  skrll Sync with HEAD.
 1.14.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.14.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.14.2.2 03-Aug-2004  skrll Sync with HEAD
 1.14.2.1 15-Jul-2004  skrll file GENERIC was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.18.6.1 29-Apr-2005  kent sync with -current
 1.18.4.1 09-Mar-2005  bouyer out of date; use XEN0 or XEN.
 1.19.2.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.19.2.1 12-Feb-2005  yamt sync with head.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 11-Mar-2004  cl branches: 1.1.4; 1.1.8; 1.1.12; 1.1.14;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.14.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.12.1 29-Apr-2005  kent sync with -current
 1.1.8.1 09-Mar-2005  bouyer out of date; use XEN0 or XEN.
 1.1.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file GENERIC.local was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.3 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.2 11-Dec-2005  christos branches: 1.2.30; 1.2.48; 1.2.50; 1.2.54; 1.2.56;
merge ktrace-lwp.
 1.1 12-May-2004  cl branches: 1.1.2; 1.1.4; 1.1.18;
``build.sh -m xen-i386 release'' now builds a release for NetBSD/xen
for i386. The resulting release consists of:
- NetBSD/xen for i386 kernel, loader and docuemntation
- NetBSD/i386 userland sets
 1.1.18.1 07-Dec-2007  yamt sync with head
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 12-May-2004  skrll file Makefile.arch.inc was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 12-May-2004  he file Makefile.arch.inc was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.2.56.1 08-Dec-2007  mjf Sync with HEAD.
 1.2.54.2 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 1: empty xenamd64/conf
- move xenamd64/conf/std.xen to amd64/conf/
- define arch to amd64 amd64/conf/std.xen, and i386 in xen/conf/std.xen
(machine is xen)
- enclose content of amd64/conf/files.amd64 and i386/conf/files.i386 in
ifndef xen/endif, so it can be transparently inclued for xen kernels
- support MACHINE_ARCH=x86_64 in xen/conf/Makefile*
 1.2.54.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.2.50.1 09-Jan-2008  matt sync with HEAD
 1.2.48.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.30.1 03-Dec-2007  ad Sync with HEAD.
 1.50 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.49 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.48 02-Feb-2019  cherry branches: 1.48.10;
Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.47 22-Sep-2018  rin - Determine KERN_AS automatically depending on whether OPT_MODULAR is
set or not, in the same way as libcompat.

- Specify OPT_MODULAR in the port Makefile instead of KERN_AS.

Now, KERN_AS=library is used for kernels without module(7) for all ports.

OK christos
 1.46 02-Jun-2018  christos branches: 1.46.2;
disable sanitizers and relro
 1.45 21-Jan-2018  maxv branches: 1.45.2;
Fix the build, on Xen too amd64_trap.S needs to be compiled independently.
 1.44 12-Dec-2017  pgoyette Remove trailing '&&' from shell script to fix the build.

From a-rin on https://gist.github.com/a-rin/940abb80d45eb7ac75b8110b8466f149
 1.43 10-Dec-2017  christos - Allow multiple .BEGIN targets
- Make their protection consistent
 1.42 09-Aug-2017  maxv Remove several dead entries from the x86 makefiles. Looks like people (me
included) regularly forget to take care of this.
 1.41 02-Feb-2017  maxv branches: 1.41.6;
Increase KERNTEXTOFF from 1MB to 2MB on amd64. [1MB; 2MB[ is now handled
by UVM, so there is no physical loss.

On amd64 we always remap the kernel text with 2MB pages, and because of the
1MB start address we were forced to map [0MB; 2MB[ inside the first large
page. The problem is, the lower half is used by UVM to allocate physical
pages, and it is possible that some of these could be used by userland. We
could end up with userland-controllable data mapped into the kernel text on
a privileged page, which is far from being a good idea from a security pov.

I am not fixing i386 yet, because the large page size depends on PAE, and
we probably don't want to have a text located at 4MB on low-memory systems.

(note: I didn't introduce this issue, it was already there when I came in)
 1.40 24-Aug-2015  uebayasi branches: 1.40.2; 1.40.4;
Define ${LINKSCRIPT} in one place.
 1.39 18-Aug-2015  uebayasi ${KERN_LDSCRIPT} -> ${KERNLDSCRIPT}
 1.38 15-Nov-2014  uebayasi branches: 1.38.2;
Use LINKSCRIPT.
 1.37 20-Jul-2014  joerg Restrict -mno-fp-ret-in-387 to GCC.
 1.36 17-Jul-2014  bouyer Sync DEFCOPTS and CFLAGS with i386 and amd64 counterparts.
Especially -fno-omit-frame-pointer
 1.35 19-Dec-2011  joerg branches: 1.35.6; 1.35.20;
Move Clang-warning flags for ah_regdomain.c into sys/conf, they are
platform independent. Use the selective -Wno-* flags instead of
-Wno-error.
 1.34 17-Aug-2011  joerg branches: 1.34.2; 1.34.6;
Works with clang's integrated assembler now.
 1.33 30-May-2011  joerg Disable -Werror for ah_regdomain.c if building with clang as workaround
for http://llvm.org/bugs/show_bug.cgi?id=10030.
 1.32 20-May-2011  joerg LLVM's assembler parser doesn't support .code32 yet, so disable it as
needed.
 1.31 12-Jan-2011  joerg branches: 1.31.2;
Allow use of traditional CPP to be set on a per platform base in sys.mk.
Honour this for dependency processing in bsd.dep.mk. Switch i386 and
amd64 assembly to use ISO C90 preprocessor concat and drop the
-traditional-cpp on this platform.
 1.30 06-Jul-2010  mrg remove almost all the ability to build netbsd with an a.out target.
we're ELF now, and there are many missing checks against OBJECT_FMT.
if we ever consider switching, the we can figure out what new ones
we need but for now it's just clutter.

this doesn't remove any of the support for exec_aout or any actually
required-for-boot a.out support, only the ability to build a netbsd
release in a.out format. ie, most of this code has been dead for
over a decade.

i've tested builds on vax, amd64, i386, mac68k, macppc, sparc, atari,
amiga, shark, cats, dreamcast, landisk, mmeye and x68k. this covers
the 5 MACHINE_ARCH's affected, and all the other arch code touched.
it also includes some actual run-time testing of sparc, i386 and
shark, and i performed binary comparison upon amiga and x68k as well.


some minor details relevant:
- move shlib.[ch] from ld.aout_so into ldconfig proper, and cut them
down to only the parts ldconfig needs
- remove various unused source files
- switch amiga bootblocks to using elf2bb.h instead of aout2bb.h
 1.29 25-Nov-2009  tron branches: 1.29.2; 1.29.4;
Enable SSP (Stack Smash Protection) in x86 kernels by default (except
in i386 *TINY kernels). The NetBSD/i386 "ALL" kernel is unconditionally
compiled with SSP enabled.

Change approved by the core team.
 1.28 11-Dec-2008  alc branches: 1.28.2;
Clean-up makefile stub used to include in the build the binary HAL object
 1.27 30-Oct-2008  joerg branches: 1.27.2; 1.27.8;
New assym.h dependency.
 1.26 25-Jan-2008  joerg branches: 1.26.6; 1.26.10; 1.26.16; 1.26.18;
Refactor in_cksum/in4_cksum/in6_cksum implementations:
- All three functions are included in the kernel by default.
They call a backend function cpu_in_cksum after possibly
computing the checksum of the pseudo header.
- cpu_in_cksum is the core to implement the one-complement sum.
The default implementation is moderate fast on most platforms
and provides a 32bit accumulator with 16bit addends for L32 platforms
and a 64bit accumulator with 32bit addends for L64 platforms.
It handles edge cases like very large mbuf chains (could happen with
native IPv6 in the future) and provides a good base for new native
implementations.
- Modify i386 and amd64 assembly to use the new interface.

This disables the MD implementations on !x86 until the conversion is
done. For Alpha, the portable version is faster.
 1.25 17-Jan-2008  lukem Remove unnecessary references to config_time.h.
 1.24 11-Jan-2008  bouyer 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.23 03-Jan-2008  joerg Add assym.h dependency.
 1.22 02-Jan-2008  joerg Add some missing dependencies on assym.h.
 1.21 01-Jan-2008  yamt add some dependencies on assym.h.
 1.20 22-Nov-2007  bouyer branches: 1.20.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.19 09-Feb-2007  ad branches: 1.19.6; 1.19.22; 1.19.24; 1.19.28; 1.19.30;
Merge newlock2 to head.
 1.18 28-May-2006  pavel branches: 1.18.6;
Give Xen its own linker script, which does not set the physical load address
specially. In Xen, the system is already in virtual mode when the kernel is
started, so the old behavior of setting physical address to virtual address
was actually correct, and the previous change made NetBSD/Xen kernels
unbootable.

Pointed out, explained and tested by <bouyer>.
 1.17 14-May-2006  elad branches: 1.17.2;
integrate kauth.
 1.16 04-Apr-2006  gdamore Convert existing ath users to new Makefile, except for amd64, which needs
the rules due to needing to conditionally postprocess the HAL object file.

Macppc needs a a non-ELF HAL (EABI) object, so take care of that by default
in the atheros include file.
 1.15 02-Apr-2006  gdamore Reorganize ath layout as requested by sam@ and suggested by dyoung@ in
http://mail-index.netbsd.org/tech-net/2006/03/15/0000.html.

The new layout almost precisely matches FreeBSD, and should make
future imports much easier.

At the same time, import the current 0.9.16.16 HAL from FreeBSD. According
to sam@, this is the proper version we should be using.
 1.14 08-Mar-2006  dyoung branches: 1.14.2;
config(8) creates opt_ah.h, no need to do so here.
 1.13 03-Mar-2006  he branches: 1.13.2; 1.13.4;
Before symlinking opt_ah.h, make sure the target doesn't already exist.
This fixes a build problem with UPDATE set.
 1.12 11-Dec-2005  christos branches: 1.12.4; 1.12.6;
merge ktrace-lwp.
 1.11 08-Nov-2005  yamt copy.S should belong to MD_SFILES, not MD_CFILES.
 1.10 07-Nov-2005  yamt some assym cleanup.
- move copyin and friends from locore.S to their own file, copy.S.
share it between i386 and xen.
- defparam KERNBASE and kill KERNBASE_LOCORE hack.
- add more symbols to assym.h and use it where appropriate.
 1.9 22-Jun-2005  dyoung branches: 1.9.2;
Resolve conflicts in importation of 18-May-2005 ath(4) / net80211(9)
from FreeBSD. Introduce compatibility shims (sys/dev/ic/ath_netbsd.[ch],
sys/net80211/ieee80211_netbsd.[ch]). Update drivers (an, atu, atw,
awi, ipw, iwi, rtw, wi) for the new net80211(9) API.
 1.8 19-Jun-2005  chs copy the athhal glue from Makefile.i386.
 1.7 07-Jun-2005  sjg Sometimes .BEGIN target is not wanted - eg in sub-make's.
 1.6 31-May-2005  christos s/GENASSYM/GENASSYM_CONF/ so we can use "GENASSYM" as the program name.
 1.5 21-Jun-2004  jmc branches: 1.5.2; 1.5.14;
Ignore errors on some rm -rf's for platforms that aren't quite
POSIX compliant
and return errors on r/o source (FreeBSD) for -rf. Fixes PR#25022
 1.4 04-Jun-2004  thorpej Add the %MAKEOPTIONSAPPEND token at the end of the file, after the
common Makefile.kern.inc has been included.
 1.3 14-May-2004  cl use relative links: using $S fails if $S is relative to another dir
than the one where it's used
 1.2 12-May-2004  cl ``build.sh -m xen-i386 release'' now builds a release for NetBSD/xen
for i386. The resulting release consists of:
- NetBSD/xen for i386 kernel, loader and docuemntation
- NetBSD/i386 userland sets
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.2 22-Jun-2004  tron branches: 1.1.2.2.2;
Pull up revision 1.5 (requested by jmc in ticket #531):
Ignore errors on some rm -rf's for platforms that aren't quite
POSIX compliant
and return errors on r/o source (FreeBSD) for -rf. Fixes PR#25022
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.2.2.1 06-Feb-2005  jmc Pull up patch (requested by martti in ticket #1086)
Move ipf to sys/dist/ipf and sync w. trunk
 1.5.14.1 29-Mar-2006  tron Apply patch (requested by bouyer in ticket #1231):
Add support for ath at pci for XEN domain0.
 1.5.2.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.5.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.5.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.5.2.2 03-Aug-2004  skrll Sync with HEAD
 1.5.2.1 21-Jun-2004  skrll file Makefile.xen was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.9.2.5 04-Feb-2008  yamt sync with head.
 1.9.2.4 21-Jan-2008  yamt sync with head
 1.9.2.3 07-Dec-2007  yamt sync with head
 1.9.2.2 26-Feb-2007  yamt sync with head.
 1.9.2.1 21-Jun-2006  yamt sync with head.
 1.12.6.2 01-Jun-2006  kardel Sync with head.
 1.12.6.1 22-Apr-2006  simonb Sync with head.
 1.12.4.1 09-Sep-2006  rpaulo sync with head
 1.13.4.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.13.2.3 26-Jun-2006  yamt sync with head.
 1.13.2.2 11-Apr-2006  yamt sync with head
 1.13.2.1 13-Mar-2006  yamt sync with head.
 1.14.2.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.17.2.1 19-Jun-2006  chap Sync with head.
 1.18.6.1 28-Jan-2007  ad xen MD changes.
 1.19.30.2 18-Feb-2008  mjf Sync with HEAD.
 1.19.30.1 08-Dec-2007  mjf Sync with HEAD.
 1.19.28.2 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.19.28.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 1: empty xenamd64/conf
- move xenamd64/conf/std.xen to amd64/conf/
- define arch to amd64 amd64/conf/std.xen, and i386 in xen/conf/std.xen
(machine is xen)
- enclose content of amd64/conf/files.amd64 and i386/conf/files.i386 in
ifndef xen/endif, so it can be transparently inclued for xen kernels
- support MACHINE_ARCH=x86_64 in xen/conf/Makefile*
 1.19.24.2 23-Mar-2008  matt sync with HEAD
 1.19.24.1 09-Jan-2008  matt sync with HEAD
 1.19.22.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.19.6.1 03-Dec-2007  ad Sync with HEAD.
 1.20.6.7 19-Jan-2008  bouyer Sync with HEAD
 1.20.6.6 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.20.6.5 09-Jan-2008  bouyer Merge Xen suport in i386/i386/spl.S.
 1.20.6.4 09-Jan-2008  bouyer Merge Xen support back to i386/i386/vector.S
 1.20.6.3 08-Jan-2008  bouyer Sync with HEAD
 1.20.6.2 06-Jan-2008  bouyer Merge needed changes to genassym.cf and locore.S for xeni386 back to
arch/i386. Switch xeni386 to use the arch/i386 cpu.h.
 1.20.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.26.18.1 19-Jan-2009  skrll Sync with HEAD.
 1.26.16.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.26.10.3 11-Aug-2010  yamt sync with head.
 1.26.10.2 11-Mar-2010  yamt sync with head
 1.26.10.1 04-May-2009  yamt sync with head.
 1.26.6.1 17-Jan-2009  mjf Sync with HEAD.
 1.27.8.1 21-Apr-2010  matt sync to netbsd-5
 1.27.2.1 07-Aug-2009  snj Pull up following revision(s) (requested by sborrill in ticket #905):
sys/arch/alpha/conf/Makefile.alpha: revision 1.81
sys/arch/amd64/conf/Makefile.amd64: revision 1.26
sys/arch/i386/conf/Makefile.i386: revision 1.162
sys/arch/macppc/conf/Makefile.macppc: revision 1.31
sys/arch/mips/conf/Makefile.mips: revision 1.52
sys/arch/sparc64/conf/Makefile.sparc64: revision 1.68
sys/arch/xen/conf/Makefile.xen: revision 1.28
Clean-up makefile stub used to include in the build the binary HAL object
 1.28.2.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.28.2.2 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.28.2.1 24-Oct-2010  jym Sync with HEAD
 1.29.4.2 31-May-2011  rmind sync with head
 1.29.4.1 05-Mar-2011  rmind sync with head
 1.29.2.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.31.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.34.6.1 18-Feb-2012  mrg merge to -current.
 1.34.2.1 17-Apr-2012  yamt sync with head
 1.35.20.1 10-Aug-2014  tls Rebase.
 1.35.6.2 03-Dec-2017  jdolecek update from HEAD
 1.35.6.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.38.2.3 28-Aug-2017  skrll Sync with HEAD
 1.38.2.2 05-Feb-2017  skrll Sync with HEAD
 1.38.2.1 22-Sep-2015  skrll Sync with HEAD
 1.40.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.40.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.41.6.1 07-Mar-2018  martin Pull up the following revisions (via patch), requested by maxv in ticket #610:

sys/arch/amd64/amd64/amd64_trap.S 1.8,1.10,1.12 (partial),1.13-1.15,
1.19 (partial),1.20,1.21,1.22,1.24
(via patch)
sys/arch/amd64/amd64/locore.S 1.129 (partial),1.132 (via patch)
sys/arch/amd64/amd64/trap.c 1.97 (partial),1.111 (via patch)
sys/arch/amd64/amd64/vector.S 1.54,1.55 (via patch)
sys/arch/amd64/include/frameasm.h 1.21,1.23 (via patch)
sys/arch/x86/x86/cpu.c 1.138 (via patch)
sys/arch/xen/conf/Makefile.xen 1.45 (via patch)

Rename and reorder several things in amd64_trap.S.
Compile amd64_trap.S as a file.
Introduce nmitrap and doubletrap.
Have the CPU clear PSL_D automatically in the syscall entry point.
 1.45.2.2 30-Sep-2018  pgoyette Ssync with HEAD
 1.45.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.46.2.1 10-Jun-2019  christos Sync with HEAD
 1.48.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.21 11-Mar-2005  bouyer Move Xen kernel config files to arch/i386, so that they can be added to
the i386 release process. Make them include archi/i386/conf/GENERIC.local,
like other i386 GENERIC and INSTALL files.
 1.20 09-Mar-2005  xtraeme Now that GENERIC* kernels are removed, remove references.
 1.19 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.18 25-Feb-2005  simonb Add COMPAT_20 (and COMPAT_16 in some cases) to kernel config files
that didn't have those options but had other earlier compat options.
 1.17 18-Feb-2005  dsl Add FFS_NO_SNAPSHOT - commented out in XEN
 1.16 31-Jan-2005  hannken Add file system snapshots to kernel configs.

- Ffs internal snapshots get compiled in unconditionally.

- File system snapshot device fss(4) added to all kernel configs that
have a disk. Device is commented out on all non-GENERIC kernels.

Reviewed by: Jason Thorpe <thorpej@netbsd.org>
 1.15 17-Jan-2005  cube branches: 1.15.2;
Add tap(4) support to a random^Wcarefully chosen set of kernel configs.
All those kernels have a line for both tun and bridge, and if either is
commented out, tap is commented out also. With the exception of i386's
GENERIC_TINY.

XXX: we _need_ some way of making this more simple.
 1.14 10-Nov-2004  christos branches: 1.14.4; 1.14.6;
Add COMPAT_BSDPTY to the rest of the config files.
 1.13 23-Sep-2004  tls Changes from Christian Limpach to improve timekeeping on NetBSD/xen by
actually adjusting the time correctly (calling hardclock as needed, not
just blindly every time Xen schedules us) based on Xen's idea of the
time in the shared page.

Xen source repo change info:
ChangeSet
2004/09/22 13:47:22+01:00 cl349@freefall.cl.cam.ac.uk
Fix time.

netbsd-2.0-xen-sparse/sys/arch/xen/xen/clock.c
2004/09/22 13:47:21+01:00 cl349@freefall.cl.cam.ac.uk +28 -3
Don't call hardclock on spurious timer interrupt and call hardclock
for missed interrupts.

netbsd-2.0-xen-sparse/sys/arch/xen/conf/XEN
2004/09/22 13:47:21+01:00 cl349@freefall.cl.cam.ac.uk +0 -1
Don't need custom HZ value any longer.

: ----------------------------------------------------------------------
 1.12 04-Sep-2004  manu IPv4 PIM support, based on a submission from Pavlin Radoslavov posted on
tech-net@
 1.11 15-Jul-2004  atatat branches: 1.11.2;
Add "options SYSCTL_INCLUDE_DESCR" to a lot of configs, but commented
out in most of them.
 1.10 07-May-2004  cl Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.9 26-Apr-2004  cl Add mouse support.
 1.8 24-Apr-2004  cl Enable keyboard and vga display as console when running as domain-0.
 1.7 24-Apr-2004  cl Add keyboard support and wscons config options.
 1.6 24-Apr-2004  cl Add vga display support.
 1.5 24-Apr-2004  cl Allow the block device driver to impersonate wd/sd/cd devices. This allows
for most system seamless migration from a NetBSD/i386 setup to a NetBSD/xen
setup without changing /etc/fstab or creating additional device nodes.
 1.4 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.3 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.2 17-Apr-2004  cl add block device driver
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.3 23-Sep-2004  jmc Pullup rev 1.13 (requested by tls in ticket #871)

Changes to improve timekeeping on NetBSD/xen by actually adjusting the time
correctly (calling hardclock as needed, not just blindly every time Xen
schedules us) based on Xens idea of the time in the shared page.
 1.1.2.2 15-Jul-2004  he Pull up revision 1.11 (requested by atatat in ticket #663):
Add "options SYSCTL_INCLUDE_DESCR" to a lot of configurations,
but commented out in most of them.
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.10 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.11.2.10 01-Apr-2005  skrll Sync with HEAD.
 1.11.2.9 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.11.2.8 04-Feb-2005  skrll Sync with HEAD.
 1.11.2.7 24-Jan-2005  skrll Sync with HEAD.
 1.11.2.6 14-Nov-2004  skrll Sync with HEAD.
 1.11.2.5 24-Sep-2004  skrll Sync with HEAD.
 1.11.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.11.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.11.2.2 03-Aug-2004  skrll Sync with HEAD
 1.11.2.1 15-Jul-2004  skrll file XEN was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.14.6.1 29-Apr-2005  kent sync with -current
 1.14.4.4 09-Mar-2005  bouyer partial sync of options and pseudo-devices with i386/conf/
 1.14.4.3 19-Jan-2005  bouyer We can't attach {cd,wd,sd} at hypervisor, this conflicts with scsipi.
 1.14.4.2 17-Dec-2004  bouyer Merge changes between 2.0 and -current:
XEN: up to 1.14
files.xen: 1.12 - 1.14
 1.14.4.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.15.2.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.15.2.1 12-Feb-2005  yamt sync with head.
 1.4 11-Mar-2005  bouyer Move Xen kernel config files to arch/i386, so that they can be added to
the i386 release process. Make them include archi/i386/conf/GENERIC.local,
like other i386 GENERIC and INSTALL files.
 1.3 09-Mar-2005  xtraeme Now that GENERIC* kernels are removed, remove references.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 16-Feb-2005  bouyer branches: 1.1.2;
file XEN-INSTALL was initially added on branch bouyer-xen2.
 1.1.2.2 09-Mar-2005  bouyer partial sync of options and pseudo-devices with i386/conf/
 1.1.2.1 16-Feb-2005  bouyer A kernel config with root on md. Will be used to install NetBSD/xen on a
VBD on a existing xen setup. Also usefull for testing purposes, but for
now you have to mdsetimage a ramdisk by hand.
 1.4 11-Mar-2005  bouyer Move Xen kernel config files to arch/i386, so that they can be added to
the i386 release process. Make them include archi/i386/conf/GENERIC.local,
like other i386 GENERIC and INSTALL files.
 1.3 09-Mar-2005  xtraeme Now that GENERIC* kernels are removed, remove references.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 18-Jan-2005  bouyer branches: 1.1.2;
file XEN0 was initially added on branch bouyer-xen2.
 1.1.2.5 09-Mar-2005  bouyer partial sync of options and pseudo-devices with i386/conf/
 1.1.2.4 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.1.2.3 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.1.2.2 21-Jan-2005  bouyer Add siop, esiop and ahc.
 1.1.2.1 18-Jan-2005  bouyer A config file suitable for a domain0 guest OS.
 1.35 23-Feb-2024  andvar s/optionms/options/ in copy-pasted comment.
 1.34 23-Jun-2022  bouyer Don't override isadma defflag for Xen, now that isadma is built for Xen PV too.
While there, match amd64 mainbus and call isa_dmainit() before attaching acpi.

Fixes a panic at boot on some hardware with a floppy disk controller.
 1.33 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.32 09-Dec-2018  mrg branches: 1.32.10;
remove XXXKALSR that became MI.
 1.31 13-Jul-2018  maxv Remove the X86PMC code I had written, replaced by tprof. Many defines
become unused in specialreg.h, so remove them. We don't want to add
defines all the time, there are countless PMCs on many generations, and
it's better to just inline the event/unit values.
 1.30 08-Jan-2018  maxv branches: 1.30.2; 1.30.4;
Make Xen compile again.
 1.29 08-Oct-2017  maxv KASLR: add workarounds to compute the bootinfo VAs (use the direct map),
and don't use large pages yet. Both will be fixed later.
 1.28 12-Aug-2017  maxv Remove vm86.

Pass 3.
 1.27 29-Jul-2017  maxv Remove the remaining parts of compat_oldboot.
 1.26 12-Jul-2017  maxv include opt_pmc.h
 1.25 13-Dec-2016  kamil branches: 1.25.8;
Torn down KSTACK_CHECK_DR0, i386-only feature to detect stack overflow

This feature was intended to detect stack overflow with CPU Debug Registers
(x86). It was never ported to other ports, neither amd64 and should be
adapted for SMP...

Currently there might be better ways to detect stack overflows like page
mapping protection. Since the number of Debug Registers is restricted
(4 on x86), torn it down completely.

This interface introduced helper functions for Debug Registers, they will
be replaced with the new <x86/dbregs.h> interface.

KSTACK_CHECK_DR0 was disabled by default and won't affect ordinary users.

Sponsored by <The NetBSD Foundation>
 1.24 03-Oct-2012  dsl branches: 1.24.14; 1.24.18;
Remove all references to KVM86.
It was only ever used by APMBIOS - and then only if an option was selected.
Probably didn't work well at all!
 1.23 18-Nov-2011  jmcneill branches: 1.23.10;
remove Xbox support
 1.22 03-Oct-2009  jmcneill branches: 1.22.12;
add dummy opt_via_c7temp.h, spotted by Andreas Gustafsson.
 1.21 05-Aug-2009  jym Add Intel SpeedStep and AMD PowerNow! support in Xen dom0. MSR operations
are now compiled in by default.

Note that MSR support in Xen depends on its version. rdmsr() should always
succeed, but wrmsr() to certain registers can end in a NOOP. In that case,
the error will be logged (see xm dmesg).

Setting CPU frequency (SpeedStep) requires Xen 3.3 with the option
cpufreq="dom0-kernel" passed down to hypervisor during boot.

Compiled and tested for SpeedStep under i386 for XEN3_DOM0 and XEN3PAE_DOM0
by jym@. amd64 was tested by Joel Carnat.

See also http://mail-index.netbsd.org/port-xen/2009/08/02/msg005213.html .

Commit requested by bouyer@.
 1.20 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.19 19-Feb-2009  jmcneill vesabios is no more
 1.18 23-Jan-2008  bouyer branches: 1.18.10; 1.18.18; 1.18.20; 1.18.24; 1.18.28;
Merge the bouyer-xeni386 branch. This brings in PAE support to NetBSD xeni386
(domU only). PAE support is enabled by 'options PAE', see the new XEN3PAE_DOMU
and INSTALL_XEN3PAE_DOMU kernel config files.

See the comments in arch/i386/include/{pte.h,pmap.h} to see how it works.
In short, we still handle it as a 2-level MMU, with the second level page
directory being 4 pages in size. pmap switching is done by switching the
L2 pages in the L3 entries, instead of loading %cr3. This is almost required
by Xen, which handle the last L2 page (the one mapping 0xc0000000 - 0xffffffff)
in a very special way. But this approach should also work for native PAE
support if ever supported (in fact, the pmap should almost suport native
PAE, what's missing is bootstrap code in locore.S).
 1.17 16-Jan-2008  ad Remove options MATH_EMULATE.
 1.16 11-Jan-2008  bouyer 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.15 22-Nov-2007  bouyer branches: 1.15.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.14 05-Jan-2007  jmcneill branches: 1.14.6; 1.14.22; 1.14.24; 1.14.28; 1.14.30;
On the Xbox, prevent scanning past the first device on bus 1.
 1.13 01-Oct-2006  bouyer Map the ISA hole on Xen, which allows us to access the SMBios, which allows
us to find the IPMI address. Add IPMI support on Xen (commented out for now)
 1.12 28-Sep-2006  bouyer Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.11 09-Apr-2006  bouyer branches: 1.11.8; 1.11.10;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.10 15-Jan-2006  bouyer branches: 1.10.2; 1.10.4; 1.10.6; 1.10.8; 1.10.10;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.9 11-Dec-2005  christos branches: 1.9.2;
merge ktrace-lwp.
 1.8 28-Jun-2005  yamt branches: 1.8.2;
create opt_pcifixup.h to match with the recent i386 change.
 1.7 16-Jun-2005  bouyer Allow compiling a domain0 kernel with vga but without pckbc, and add
console support for USB keyboard. Problem pointed out by Karl Janmar on
port-xen.
 1.6 11-Mar-2005  bouyer branches: 1.6.2;
Properly define NISA and NPCI; cleanup use of NISA and NPCI.
 1.5 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.4 04-Feb-2005  jmc Add opt_scsipi_debug.h to list since compat_linux pulls in scsipi now for sg
driver
 1.3 24-Apr-2004  cl branches: 1.3.2; 1.3.8; 1.3.10;
Add keyboard support and wscons config options.
 1.2 24-Apr-2004  cl Add vga display support.
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.10.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.10.1 12-Feb-2005  yamt sync with head.
 1.3.8.1 29-Apr-2005  kent sync with -current
 1.3.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.3.2.5 04-Feb-2005  skrll Sync with HEAD.
 1.3.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.2.2 03-Aug-2004  skrll Sync with HEAD
 1.3.2.1 24-Apr-2004  skrll file files.compat was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.6.2.3 08-Jan-2007  ghen Pull up following revision(s) (requested by bouyer in ticket #1622):
sys/arch/xen/conf/files.xen: revision 1.49 via patch
sys/arch/xen/i386/autoconf.c: revision 1.19 via patch
sys/arch/xen/conf/files.compat: revision 1.13 via patch
sys/arch/xen/i386/mainbus.c: revision 1.10 via patch
sys/arch/xen/i386/bios32.c: revision 1.1 via patch
sys/arch/i386/conf/XEN2_DOM0: revision 1.9 via patch
sys/arch/xen/i386/locore.S: revision 1.19 via patch
Map the ISA hole on Xen, which allows us to access the SMBios, which allows
us to find the IPMI address. Add IPMI support on Xen (commented out for now)
 1.6.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.6.2.1 28-Jun-2005  tron Pull up revision 1.7 (requested by bouyer in ticket #482):
Allow compiling a domain0 kernel with vga but without pckbc, and add
console support for USB keyboard. Problem pointed out by Karl Janmar on
port-xen.
 1.8.2.6 04-Feb-2008  yamt sync with head.
 1.8.2.5 21-Jan-2008  yamt sync with head
 1.8.2.4 07-Dec-2007  yamt sync with head
 1.8.2.3 26-Feb-2007  yamt sync with head.
 1.8.2.2 30-Dec-2006  yamt sync with head.
 1.8.2.1 21-Jun-2006  yamt sync with head.
 1.9.2.1 01-Feb-2006  yamt sync with head.
 1.10.10.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.10.8.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.10.6.1 11-Apr-2006  yamt sync with head
 1.10.4.1 22-Apr-2006  simonb Sync with head.
 1.10.2.1 09-Sep-2006  rpaulo sync with head
 1.11.10.1 22-Oct-2006  yamt sync with head
 1.11.8.2 12-Jan-2007  ad Sync with head.
 1.11.8.1 18-Nov-2006  ad Sync with head.
 1.14.30.2 18-Feb-2008  mjf Sync with HEAD.
 1.14.30.1 08-Dec-2007  mjf Sync with HEAD.
 1.14.28.2 13-Nov-2007  bouyer catch up with changes in HEAD.
 1.14.28.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.14.24.2 23-Mar-2008  matt sync with HEAD
 1.14.24.1 09-Jan-2008  matt sync with HEAD
 1.14.22.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.14.6.1 03-Dec-2007  ad Sync with HEAD.
 1.15.6.3 19-Jan-2008  bouyer Sync with HEAD
 1.15.6.2 13-Jan-2008  bouyer Work in progress on xeni386 PAE support:
Make xeni386 build with a 64bit paddr_t. For this vaddr_t vs paddr_t vs
pointers usages had to be clarified.
If 'options PAE' is present in a Xen3 kernel, switch paddr_t, pd_entry_t
and pt_entry_t to 64bits, and add the PAE entry in the __xen_guest ELF section.
 1.15.6.1 07-Jan-2008  bouyer Move Xen support to i386/i386/machdep.c. two less files in xen/ :)
 1.18.28.1 21-Apr-2010  matt sync to netbsd-5
 1.18.24.2 01-Nov-2009  jym Sync with HEAD.
 1.18.24.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.18.20.1 05-Oct-2009  sborrill Pull up the following revisions(s) (requested by jmcneill in ticket #1061):
sys/arch/xen/conf/files.compat: revision 1.22
Extra file required in addition to original request.
 1.18.18.1 03-Mar-2009  skrll Sync with HEAD.
 1.18.10.3 11-Mar-2010  yamt sync with head
 1.18.10.2 19-Aug-2009  yamt sync with head.
 1.18.10.1 04-May-2009  yamt sync with head.
 1.22.12.2 30-Oct-2012  yamt sync with head
 1.22.12.1 17-Apr-2012  yamt sync with head
 1.23.10.2 03-Dec-2017  jdolecek update from HEAD
 1.23.10.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.24.18.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.24.14.2 28-Aug-2017  skrll Sync with HEAD
 1.24.14.1 05-Feb-2017  skrll Sync with HEAD
 1.25.8.2 22-Mar-2018  martin Pull up the following revisions, requested by maxv in ticket #652:

sys/arch/amd64/amd64/amd64_trap.S upto 1.39 (partial, patch)
sys/arch/amd64/amd64/db_machdep.c 1.6 (patch)
sys/arch/amd64/amd64/genassym.cf 1.65,1.66,1.67 (patch)
sys/arch/amd64/amd64/locore.S upto 1.159 (partial, patch)
sys/arch/amd64/amd64/machdep.c 1.299-1.302 (patch)
sys/arch/amd64/amd64/trap.c upto 1.113 (partial, patch)
sys/arch/amd64/amd64/amd64/vector.S upto 1.61 (partial, patch)
sys/arch/amd64/conf/GENERIC 1.477,1.478 (patch)
sys/arch/amd64/conf/kern.ldscript 1.26 (patch)
sys/arch/amd64/include/frameasm.h upto 1.37 (partial, patch)
sys/arch/amd64/include/param.h 1.25 (patch)
sys/arch/amd64/include/pmap.h 1.41,1.43,1.44 (patch)
sys/arch/x86/conf/files.x86 1.91,1.93 (patch)
sys/arch/x86/include/cpu.h 1.88,1.89 (patch)
sys/arch/x86/include/pmap.h 1.75 (patch)
sys/arch/x86/x86/cpu.c 1.144,1.146,1.148,1.149 (patch)
sys/arch/x86/x86/pmap.c upto 1.289 (partial, patch)
sys/arch/x86/x86/vm_machdep.c 1.31,1.32 (patch)
sys/arch/x86/x86/x86_machdep.c 1.104,1.106,1.108 (patch)
sys/arch/x86/x86/svs.c 1.1-1.14
sys/arch/xen/conf/files.compat 1.30 (patch)

Backport SVS. Not enabled yet.
 1.25.8.1 01-Aug-2017  snj Pull up following revision(s) (requested by maxv in ticket #164):
distrib/sets/lists/base/md.amd64: revision 1.269
distrib/sets/lists/debug/md.amd64: revision 1.97
sys/arch/amd64/conf/GENERIC: revision 1.460
sys/arch/amd64/conf/files.amd64: revision 1.89
sys/arch/i386/conf/GENERIC: revision 1.1157
sys/arch/i386/conf/files.i386: revision 1.379
sys/arch/i386/i386/i386_trap.S: revision 1.7-1.8
sys/arch/i386/include/frameasm.h: revision 1.16
sys/arch/x86/include/sysarch.h: revision 1.12
sys/arch/x86/x86/pmc.c: revision 1.8-1.10
sys/arch/x86/x86/sys_machdep.c: revision 1.36
sys/arch/xen/conf/files.compat: revision 1.26
sys/secmodel/suser/secmodel_suser.c: revision 1.43
sys/sys/kauth.h: revision 1.74
usr.bin/pmc/Makefile: revision 1.5
usr.bin/pmc/pmc.1: revision 1.12-1.13
usr.bin/pmc/pmc.c: revision 1.24-1.25
style
--
style
--
Disable interrupts for T_NMI (inline calltrap). Note that there's still a
way to evade the NMI mode here, if a segment register faults in
INTRFASTEXIT; but we don't care. I didn't test this change, but it seems
fine enough.
--
Make the PMC syscalls privileged.
--
Check argc, and add a message.
--
include opt_pmc.h
--
Build the pmc tool on amd64.
--
Properly handle overflows, and take them into account in userland.
--
Update.
--
Enable PMCs by default.
--
Sort sections. Fix macro usage.
 1.30.4.1 10-Jun-2019  christos Sync with HEAD
 1.30.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.30.2.1 28-Jul-2018  pgoyette Sync with HEAD
 1.32.10.1 25-Apr-2020  bouyer Include again xen/conf/files.compat for PV kernels, and make it build.
My build.sh -u used stale opt_*.h files ...
 1.189 17-Oct-2023  bouyer XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
 1.188 17-Oct-2023  bouyer Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.
Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen
when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.
x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console
xen/x86/consinit.c: support genfb as possible console
xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.
xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.
 1.187 31-Aug-2022  bouyer branches: 1.187.4;
Work in progress on dom0 PVH support: ioctl support for tools.
Basically, in PVH mode (where XENFEAT_auto_translated_physmap is enabled),
the hypervisor will not map foreing ressources in our virtual address
space for us. Instead, we have to pass it an address in our physical
address space (but not mapped to some RAM) where the ressource will show up
and then enter this PA in pour page table.

For this, introduce xenmem_* which manage the PA space. In PVH mode this
is just allocated from the iomem_ex extent.

With this, I can start a PV domU, and the guest's kernel boots (and
the console works). It hangs because the backend driver can't map the
frontend ressources (yet).

Note that, per https://xenbits.xen.org/docs/unstable/support-matrix.html,
dom0 PVH support is still considered experimental by Xen.
 1.186 26-May-2020  bouyer Add need-flags for kernfs.
Compile Xen kernfs support only if kernfs is compiled in the kernel.
Should fix MODULAR build.
 1.185 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.184 02-May-2020  bouyer This is called pvh_consinit not hvm_consinit
 1.183 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.182 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.181 21-Apr-2020  msaitoh Get TSC frequency from CPUID 0x15 and/or x16 for newer Intel processors.

- If the max CPUID leaf is >= 0x15, take TSC value from CPUID. Some processors
can take TSC/core crystal clock ratio but core crystal clock frequency
can't be taken. Intel SDM give us the values for some processors.
- It also required to change lapic_per_second to make LAPIC timer correctly.
- Add new file x86/x86/identcpu_subr.c to share common subroutines between
kernel and userland. Some code in x86/x86/identcpu.c and cpuctl/arch/i386.c
will be moved to this file in future.
- Add comment to clarify.
 1.180 03-Apr-2020  ad branches: 1.180.2;
CPU topology makes almost no sense for Xen, and populates it with B/S values
 1.179 07-Sep-2019  maxv Merge amd64func.S into cpufunc.S, and clean up.
 1.178 14-Feb-2019  cherry branches: 1.178.4;
Welcome XENPVHVM mode.

It is UP only, has xbd(4) and xennet(4) as PV drivers.

The console is com0 at isa and the native portion is very
rudimentary AT architecture, so is probably suboptimal to
run without PV support.
 1.177 14-Feb-2019  cherry Snag the final bits of PV only code to conditionally compile under
-DXENPV

This completes the bifurcation.

The next step is to add -DXENPVHVM code.
 1.176 27-Jan-2019  pgoyette Merge the [pgoyette-compat] branch
 1.175 25-Dec-2018  mlelstv Make ipmi driver available to other platforms.
Add ACPI attachment.
 1.174 25-Dec-2018  cherry Excise XEN specific code out of x86/x86/intr.c into xen/x86/xen_intr.c

While at it, separate the source function tracking so that the interrupt
paths are truly independant.

Use weak symbol exporting to provision for future PVHVM co-existence
of both files, but with independant paths. Introduce assembler code
such that in a unified scenario, native interrupts get first priority
in spllower(), followed by XEN event callbacks. IPL management and
semantics are unchanged - native handlers and xen callbacks are
expected to maintain their ipl related semantics.

In summary, after this commit, native and XEN now have completely
unrelated interrupt handling mechanisms, including
intr_establish_xname() and assembler stubs and intr handler
management.

Happy Christmas!
 1.173 24-Dec-2018  cherry Do not include files for MSI support on XEN dom0.

We do not support MSI as of now.
 1.172 22-Dec-2018  cherry This change modifies the mainbus(4) entry point for all x86 sub-archs
in the following way:

i) It provides a unified entry point in
x86/x86/mainbus.c:mainbus_attach()
ii) It carves out the preliminary bus attachment sequence that is
common to all sub-archs into
x86/x86/mainbus.c: x86_cpubus_attach()
iii) It consolidates the remaining pathways as internal callee
functions so that these may be called piecemeal if required. A
special usecase of this is XEN PVHVM which may need to call the
native configure path, the xen configure path, or both.
iv) It moves the driver private data structures from
i386/i386_mainbus.c to an x86/ level one. This allows for other
sub-arch's to do similar, if needed. (They do not at the moment).
v) For dom0 kernels, it enables 'acpi0 at mainbus?' and
'acpi0 at hypervisorbus'. This serves two purposes:
a) To demonstrate the possibility of dynamic configuration tree
traversal ordering changes.
b) To allow for the common acpi_check(self, "acpibus") call in
x86/mainbus.c to not barf when it is called from the dom0 attach
path. We allow for the acpi0 device to be a child of mainbus with
the changes to amd64/conf/XEN3_DOM0 and i386/conf/XEN3PAE_DOM0
without actually probing further in the code. This path will later
be pursued in a PVHVM boot codepath.

There should be no operative changes with this change. If there are,
please complain loudly.
 1.171 22-Dec-2018  cherry Move mainbus(4) driver files in various x86 sub-archs to name prefixed
versions. This allows us to further modularise them by unifying common
bus probe code in x86/x86/mainbus.c to be introduced next.

This commit has no functional changes. It is done for ease of
visibility of newer diffs in the queue.
 1.170 26-Jul-2018  maxv Merge the content of xen_debug.c into xen_machdep.c, there is only one
function.
 1.169 23-May-2018  maxv branches: 1.169.2;
Merge convert_xmm_s87.c into fpu.c. It contains only two functions, that
are used only in fpu.c.
 1.168 18-Mar-2018  christos Separate the compat code in its own file to facilitate module building.
 1.167 17-Mar-2018  christos dedup microcode handling.
 1.166 16-Mar-2018  maxv Remove ipkdb from i386. Also remove unused references in amd64.

I already talked about doing that six months ago on port-i386@. Back then
it was as general cleanup, but now, with SVS etc, we do actually have
good reasons for simplifying the entry points.

Ok kamil@. (christos@ was in the conversation too)
 1.165 22-Jan-2018  jdolecek branches: 1.165.2;
xen has separate list, update here also for x86/pmap_tlb.c -> x86/x86_tlb.c
 1.164 02-Dec-2017  maxv Drop COMPAT_13 on amd64, already not enabled. Reduces the number of
critical places.
 1.163 06-Nov-2017  cherry Summary: xen/x86/intr.c has been retired. Don't try to build it.
 1.162 04-Nov-2017  cherry Remove bitrotted xen specific versions of pci, pciide machdep related code.

Use the common x86/ code instead.
 1.161 04-Nov-2017  cherry Retire xen/x86/intr.c and use the new xen specific glue in x86/x86/intr.c

The purpose of this change is to expose the x86/include/intr.h API
to drivers. Specifically the following functions:

void *intr_establish_xname(...);
void *intr_establish(...);
void intr_disestablish(...);

while maintaining the old API from xen/include/evtchn.h, specifically
the following functions:

int event_set_handler(...);
int event_remove_handler(...);

This is so that if things break, we can keep using the old API until
everything stabilises. This is a stepping stone towards getting the
actual XEN event callback path rework code in place - which can be
done opaquely behind the intr.h API - NetBSD/XEN specific drivers that
have been ported to the intr.h API should then work without
significant further modifications.
 1.160 04-Nov-2017  cherry On XEN dom0, the function xen/x86/intr.c:xen_intr_map() is used to map
hardware interrupts to XEN callbacks called 'events'. This function
combines both the allocation and the binding.

This change is the first part of breaking up that combination into
xen_pirq_alloc() and the binding will happen as part of the
pic_addroute() callback of a new pseudo PIC_XEN

This code will be added later on.
 1.159 04-Nov-2017  cherry On XEN PV, the ISA 8259A PIC is only used on dom0
 1.158 04-Nov-2017  cherry In XEN PV, the idt vector table is not required to be altered at
runtime, since only entries for exceptions/traps are registered with
the hypervisor and interrupts are managed via a completely different
mechanism.

This change uses the idt_vec_reserve() mechanism nevertheless,
modifying it slightly to only do namespace management in XEN, while on
native it will continue to do idt entry init as before.

Rationale: Consistent API usage and potential future merging of
XEN/non-XEN code.

There are no functional changes in this commit.
 1.157 04-Nov-2017  cherry opt_intrdebug.h - this unbreaks the i386 DOMU build under some conditions
 1.156 15-Aug-2017  maxv Merge into x86/.
 1.155 15-Aug-2017  maxv Merge into x86/.
 1.154 12-Aug-2017  maxv Remove vm86.

Pass 3.
 1.153 09-Aug-2017  maxv Remove references to compat_ibcs2.
 1.152 01-Aug-2017  maxv Move arch/i386/i386/freebsd_* into compat/freebsd/. COMPAT_FREEBSD is
i386-specific.
 1.151 01-Aug-2017  maxv Don't include files.svr4 and files.svr4_32.
 1.150 29-Jul-2017  maxv Drop support for svr4 on i386. This feature is not maintained, not
reliable, and of a limited use case. Most svr4 applications got time to be
ported to linux, and we do have a functional, maintained linux emulation.

Reduces the number of entry points into the kernel, the number of
places that need special care (cpu context).

Note that compat_svr4 is still available on sparc.
 1.149 16-Jul-2017  cherry branches: 1.149.2;
Unify the xen and native x86/ interrupt setup functions and
spl traversal data structures.

This is towards PVHVM.
 1.148 22-Jun-2017  khorben Register support for SD card readers with Xen DOM0 kernels

Tested on a Lenovo ThinkPad T440s (amd64)
 1.147 23-May-2017  nonaka branches: 1.147.2;
x86: hypervisor detection from FreeBSD for x2APIC support.
 1.146 24-Mar-2017  maxv Don't compile PMCs on Xen.
 1.145 10-Mar-2017  maxv Move pmc.c into x86/, it can be shared with amd64.
 1.144 17-Feb-2017  kre Copy maxv's files.i386 change to files.xen ... this might fix the i386
xen kernel builds (I am doing a test build in parallel with this commit,
this shouldn't make things worse ... I will make further changes if
needed after my build finishes.)

Note: I am not currently in a position to test a Xen3 DomU kernel
(let alone Dom0) so I have no way of knowing whether a Xen kernel
with PMC included will actually work correctly or not.

If this results in a system that builds, I'd appreciate it if someone
who can test the Xen3_DOMU kernel could do so.
 1.143 15-Dec-2016  kamil branches: 1.143.2;
Add support for hardware assisted watchpoints/breakpoints API in ptrace(2)

Add new ptrace(2) calls:
- PT_COUNT_WATCHPOINTS - count the number of available hardware watchpoints
- PT_READ_WATCHPOINT - read struct ptrace_watchpoint from the kernel state
- PT_WRITE_WATCHPOINT - write new struct ptrace_watchpoint state, this
includes enabling and disabling watchpoints

The ptrace_watchpoint structure contains MI and MD parts:

typedef struct ptrace_watchpoint {
int pw_index; /* HW Watchpoint ID (count from 0) */
lwpid_t pw_lwpid; /* LWP described */
struct mdpw pw_md; /* MD fields */
} ptrace_watchpoint_t;

For example amd64 defines MD as follows:
struct mdpw {
void *md_address;
int md_condition;
int md_length;
};

These calls are protected with the __HAVE_PTRACE_WATCHPOINTS guard.

Tested on amd64, initial support added for i386 and XEN.

Sponsored by <The NetBSD Foundation>
 1.142 13-Dec-2016  kamil Torn down KSTACK_CHECK_DR0, i386-only feature to detect stack overflow

This feature was intended to detect stack overflow with CPU Debug Registers
(x86). It was never ported to other ports, neither amd64 and should be
adapted for SMP...

Currently there might be better ways to detect stack overflows like page
mapping protection. Since the number of Debug Registers is restricted
(4 on x86), torn it down completely.

This interface introduced helper functions for Debug Registers, they will
be replaced with the new <x86/dbregs.h> interface.

KSTACK_CHECK_DR0 was disabled by default and won't affect ordinary users.

Sponsored by <The NetBSD Foundation>
 1.141 20-May-2016  jnemeth branches: 1.141.2;
make CPU microcode loading dependent on both DOM0OPS AND CPU_UCODE
 1.140 27-Feb-2016  mlelstv no condition for cpu_rng here
 1.139 27-Feb-2016  mlelstv add missing cpu_rng.c to kernel
 1.138 11-Nov-2015  skrll Fix XEN build - does XEN really want/need pmap_pv_track?
 1.137 04-Jan-2015  uebayasi Include files.sljit as i386/amd64 do.
 1.136 15-Dec-2014  christos there is no ibcs2 in amd64 [yet?]
 1.135 15-Dec-2014  ozaki-r Add two new compat files for Xen on amd64
 1.134 11-Oct-2014  uebayasi branches: 1.134.2;
Mark machdep files.
 1.133 12-Feb-2014  dsl branches: 1.133.4; 1.133.8;
Change i386 to use x86/fpu.c instead of i386/isa/npx.c
This changes the trap10 and trap13 code to call directly into fpu.c,
removing all the code for T_ARITHTRAP, T_XMM and T_FPUNDA from i386/trap.c
Not all of the code thate appeared to handle fpu traps was ever called!
Most of the changes just replace the include of machine/npx.h with x86/fpu.h
(or remove it entirely).
 1.132 11-Feb-2014  dsl Move sys/arch/amd64/amd64/fpu.c and sys/arch/amd64/include/fpu.h
into sys/arch/x86 in preparation for using the same code for i386.
 1.131 07-Feb-2014  dsl Convert the amd64 build to use x86/cpu_extended_state.h so that the fpu
definitions match those of i386.
Mostly just structure and field renames, in addition:
1) process_xmm_to_s87() and process_s87_to_xmm() moved into
x86/convert_xmm_s87.c so they can be used by amd64's netbsd32 code.
2) The linux signal code simplified to use a structure copy for ths fxsave
data - it matches the hardware definition and won't change.
 1.130 26-Jan-2014  dsl Remove support for 'external' floating point units and the MS-DOS
compatible method of handling floating point exceptions.
Make kernel support for teh fpu non-optional (486SX should still work).
Only 386 cpus support external fpu, and i386 support was removed years ago.
This means that the npx code no longer uses port 0xf0 or interupt 13.
All the "npx at isa" lines go from the configs, arch/i386/isa/npx.c
is now mandatory for all i386 kernels.
I've renamed npxinit() to fpuinit() and npxinit_cpu() to fpuinit_cpu()
to match the very similar amd64 functions.
The fpu of the boot cpu is now initialised by a direct call from
cpu_configure(), this enables FP emulation for a 486SX.
(for amd64 the cr0 values are set in locore.S and similar).
This fixes a long-standing bug in linux_setregs() - which did not
save the fpu regsiters if they were active.
I've test booted a single cpu i386 kernel (using anita).
amd64 builds - none of teh changes should affect it.
The i386 XEN kernels build, but I'm not sure where they set cr0, and
it might have got lost!
 1.129 03-Oct-2012  dsl branches: 1.129.2;
Remove all references to KVM86.
It was only ever used by APMBIOS - and then only if an option was selected.
Probably didn't work well at all!
 1.128 29-Aug-2012  drochner branches: 1.128.2;
Extend the CPU microcode update framework to support Intel x86 CPUs.
Contrary to the AMD implementation, it doesn't use xcalls to distribute
the update to all CPUs but relies on cpuctl(8) to bind itself to the
right CPU -- to keep it simple and avoid possible problems with
hyperthreading.
Also, it doesn't parse the vendor supplied file to pick the right
part for the present CPU model but relies on userland to prepare
files with specific filenames. I'll commit a pkg for this in a minute
(pkgsrc/sysutils/intel-microcode).
The ioctl interface changed; compatibility is provided (should be
limited to COMPAT_NETBSD6 as soon as this is available).
 1.127 27-Jun-2012  jym Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.126 07-May-2012  jym Merge i386 and amd64 version of db_memrw.c.

Use this opportunity to skip calculating the VA of the page. Let the CPU
deal with the invalidation itself through invlpg + destination address to
avoid converting between canonical/non canonical forms.
 1.125 14-Jan-2012  pgoyette No need for CPU_UCODE here now that it is global
 1.124 13-Jan-2012  cegger Support CPU microcode loading via cpuctl(8).
Implemented and enabled via CPU_UCODE kernel config option
for x86 and Xen Dom0.
Tested on different AMD machines with different
CPU families.

ok wiz@ for the manpages
ok releng@
ok core@ via releng@
 1.123 22-Sep-2011  jym branches: 1.123.2; 1.123.6;
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.122 10-Aug-2011  cherry xen ipi infrastructure
 1.121 13-Jun-2011  pgoyette Include required file for xen acpi
 1.120 12-Jun-2011  jruoho Fix build failure for the odd child, as pointed out by pgoyette@.
 1.119 12-Jun-2011  rmind Welcome to 5.99.53! Merge rmind-uvmplock branch:

- Reorganize locking in UVM and provide extra serialisation for pmap(9).
New lock order: [vmpage-owner-lock] -> pmap-lock.

- Simplify locking in some pmap(9) modules by removing P->V locking.

- Use lock object on vmobjlock (and thus vnode_t::v_interlock) to share
the locks amongst UVM objects where necessary (tmpfs, layerfs, unionfs).

- Rewrite and optimise x86 TLB shootdown code, make it simpler and cleaner.
Add TLBSTATS option for x86 to collect statistics about TLB shootdowns.

- Unify /dev/mem et al in MI code and provide required locking (removes
kernel-lock on some ports). Also, avoid cache-aliasing issues.

Thanks to Andrew Doran and Joerg Sonnenberger, as their initial patches
formed the core changes of this branch.
 1.118 26-Apr-2011  joerg branches: 1.118.2;
Remove PECOFF/Win32 emulation.
 1.117 26-Apr-2011  joerg Remove Darwin, MACH and Mach-O support.
 1.116 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.115 11-Apr-2011  cegger build xen kernels again after db_trace merge
 1.114 03-Apr-2011  dyoung Clean up excessive #ifdef'age of NMI trap handling for amd64/i386/xen.
Handle NMI in all Xen kernels.
 1.113 24-Feb-2011  jruoho Move PowerNow! to the cpufeaturebus.
 1.112 24-Feb-2011  jruoho Add cpufeaturebus and est(4) for Xen.
 1.111 23-Feb-2011  jruoho Move ENHANCED_SPEEDSTEP, or henceforth est(4), to the cpufeaturebus.
 1.110 02-Dec-2010  bouyer branches: 1.110.2; 1.110.4;
Make maxpartitions 16 on !i386. Fixes hardwiring root on device autoconf
index > 0 on amd64. Problem reported and patch tested by Tobias Nygren.
 1.109 08-Jul-2010  rmind Unify i386 and amd64 procfs MD code into x86.
 1.108 06-Jul-2010  cherry The Xen balloon driver enables growing and shrinking
PV domains on the fly, by collaborating with UVM and the hypervisor
 1.107 10-May-2010  dyoung Provide pmap_enter_ma(), pmap_extract_ma(), pmap_kenter_ma() in all x86
kernels, and use them in the bus_space(9) implementation instead of ugly
Xen #ifdef-age. In a non-Xen kernel, the _ma() functions either call or
alias the equivalent _pa() functions.

Reviewed on port-xen@netbsd.org and port-i386@netbsd.org. Passes
rmind@'s and bouyer@'s inspection. Tested on i386 and on Xen DOMU /
DOM0.
 1.106 03-Nov-2009  dyoung branches: 1.106.2; 1.106.4;
Add a kernel configuration flag, SPLDEBUG, that activates a per-CPU log
of transitions to IPL_HIGH from lower IPLs. SPLDEBUG is only available
on i386 and Xen kernels, today.

'options SPLDEBUG' adds instrumentation to spllower() and splraise() as
well as routines to start/stop debugging and to record IPL transitions:
spldebug_start(), spldebug_stop(), spldebug_raise(), spldebug_lower().
 1.105 05-Oct-2009  rmind Remove X86_IPI_WRITE_MSR (and msr_ipifuncs.c), replace all uses in drivers
with xc_broadcast(). AMD K8 PowerNow driver tested by <jakllsch>, thanks!

Closes PR/37665.
 1.104 16-Aug-2009  manu Build COMPAT_DARWIN and COMPAT_MACH as a Xen domU too
 1.103 05-Aug-2009  jym Add Intel SpeedStep and AMD PowerNow! support in Xen dom0. MSR operations
are now compiled in by default.

Note that MSR support in Xen depends on its version. rdmsr() should always
succeed, but wrmsr() to certain registers can end in a NOOP. In that case,
the error will be logged (see xm dmesg).

Setting CPU frequency (SpeedStep) requires Xen 3.3 with the option
cpufreq="dom0-kernel" passed down to hypervisor during boot.

Compiled and tested for SpeedStep under i386 for XEN3_DOM0 and XEN3PAE_DOM0
by jym@. amd64 was tested by Joel Carnat.

See also http://mail-index.netbsd.org/port-xen/2009/08/02/msg005213.html .

Commit requested by bouyer@.
 1.102 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.101 24-Jul-2009  dyoung There's no such file as arch/i386/i386/core_machdep.c, so don't
list it here.
 1.100 16-Jun-2009  bouyer Split mc146818-related functions from clock.c into rtc.c.
Call rtc_set_ymdhms() from xen/xen/clock.c:xen_rtc_set() for xen3 dom0
kernels as the Xen3 hypervisor doesn't write the new date/time to the CMOS
by itself.
Now a XEN3_DOM0 kernel properly updates the CMOS time.
 1.99 30-Apr-2009  pgoyette Include cpu_topology.c for xen kernels, too.
 1.98 18-Apr-2009  cegger build mp.c for xen, too.
Fixes linking failures of Dom0 kernels. (Hi dyoung)
 1.97 16-Apr-2009  cegger make xen kernels build again after the merge of syscall.c
 1.96 31-Mar-2009  rmind Sync with recent i386/amd64 source merges - fix Xen builds.
 1.95 19-Feb-2009  jmcneill vesabios is no more
 1.94 14-Feb-2009  cube Don't set version deep in the source tree if it's older than the one in
conf/files.
 1.93 13-Feb-2009  bouyer Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.92 18-Jan-2009  bouyer branches: 1.92.2;
The Xen PCI_BUS_FIXUP/PCI_ADDR_FIXUP has rotted, catch up with x86 changes
in this area. Patch provided by FUKAUMI Naoki in PR#40356.
 1.91 20-Dec-2008  ad - Kill NOREDZONE.
- Make the redzone conditional on DIAGNOSTIC.
- Give amd64 an additional page for the uarea. 2 is not enough.
 1.90 20-Nov-2008  ad Put the COMPAT_NOMID stuff into its own file.
 1.89 19-Nov-2008  ad Make the emulations, exec formats, coredump, NFS, and the NFS server
into modules. By and large this commit:

- shuffles header files and ifdefs
- splits code out where necessary to be modular
- adds module glue for each of the components
- adds/replaces hooks for things that can be installed at runtime
 1.88 03-Aug-2008  joerg branches: 1.88.2; 1.88.4;
Move some MD declarations from x86/pci/files.pci to x86/conf/files.x86,
so that Xen can use the former.

Drop Xen's pcib.c in favor of the x86 code and thereby unbreak ichlpcib.
 1.87 11-May-2008  ad branches: 1.87.4;
Update xen for identcpu changes.
 1.86 10-May-2008  ad Make xen build after tsc changes.
 1.85 09-May-2008  joerg Make cpu_idle a macro calling a function pointer on x86.
Select the Xen idle routine for Xen, mwait if supported by the CPU and
it is not AMD and halt otherwise. As reported by Christoph Egger,
AMD Barcelona keeps the CPU in C0 state with MWAIT, contrary to HLT,
which uses C1 and therefore much less power.
 1.84 25-Apr-2008  ad branches: 1.84.2; 1.84.4;
Include null IPI functions if !MULTIPROCESSOR.
 1.83 27-Feb-2008  dogcow branches: 1.83.2;
fix longrun build borkage on i386
 1.82 20-Feb-2008  drochner branches: 1.82.2; 1.82.6;
Since files.wscons et al. are included by ~all ports anyway, include
them in the mi "files" file, and remove include statements from md files.
These shouldn't pull in additional kernel code when not in use, so it
shouldn't do any harm except a risk of namespace collisions which
should be easy to fix.
 1.81 31-Jan-2008  jmmv Let Xen kernels build with "options MODULAR" enabled by pulling in the
appropriate kobj_machdep.c file.
 1.80 25-Jan-2008  joerg Refactor in_cksum/in4_cksum/in6_cksum implementations:
- All three functions are included in the kernel by default.
They call a backend function cpu_in_cksum after possibly
computing the checksum of the pseudo header.
- cpu_in_cksum is the core to implement the one-complement sum.
The default implementation is moderate fast on most platforms
and provides a 32bit accumulator with 16bit addends for L32 platforms
and a 64bit accumulator with 32bit addends for L64 platforms.
It handles edge cases like very large mbuf chains (could happen with
native IPv6 in the future) and provides a good base for new native
implementations.
- Modify i386 and amd64 assembly to use the new interface.

This disables the MD implementations on !x86 until the conversion is
done. For Alpha, the portable version is faster.
 1.79 16-Jan-2008  ad Remove options MATH_EMULATE.
 1.78 12-Jan-2008  ad - Split crashdump code out into its own file.
- Remove NO_SPARSE_DUMP.
- Minor KNF, sprinkle static.
 1.77 11-Jan-2008  bouyer 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.76 01-Jan-2008  yamt try to detect processor resource sharing topologies. ie. package/core/smt IDs.
 1.75 03-Dec-2007  ad branches: 1.75.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.74 01-Dec-2007  bouyer Back out previous, majors.amd64 in files.amd64 is inside ifndef xen/endif
 1.73 30-Nov-2007  dsl Don't pull in majors.amd64 here, we get another copy from files.amd64
and config bleats like mad.
 1.72 28-Nov-2007  yamt - merge {i386,xen}/i386/trap.c.
- add a comment about a hack for xen2.
 1.71 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.70 22-Nov-2007  yamt remove lock_machdep.c
 1.69 15-Nov-2007  ad Remove support for 80386 level CPUs. PR port-i386/36163.
 1.68 26-Oct-2007  xtraeme branches: 1.68.2;
xen/i386/cpu.c uses i686_mtrr_init_first if MTRR is defined, but
mtrr_i686.c wasn't built...
 1.67 26-Oct-2007  xtraeme ichlpcib(4) lives in x86/pci these days...
 1.66 26-Oct-2007  xtraeme - Share pchb(4) between i386 and amd64; one copy is enough for both.
- Move some of the x86 PCI devices into x86/pci/files.pci.
- Add more x86 stuff into x86/conf/files.x86.

ok joerg.
 1.65 17-Oct-2007  garbled 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.64 26-Sep-2007  ad branches: 1.64.2;
x86 changes for pcc and LKMs.

- Replace most inline assembly with proper functions. As a side effect
this reduces the size of amd64 GENERIC by about 120kB, and i386 by a
smaller amount. Nearly all of the inlines did something slow, or something
that does not need to be fast.
- Make curcpu() and curlwp functions proper, unless __GNUC__ && _KERNEL.
In that case make them inlines. Makes curlwp LKM and preemption safe.
- Make bus_space and bus_dma more LKM friendly.
- Share a few more files between the ports.
- Other minor changes.
 1.63 07-Jul-2007  tsutsui branches: 1.63.8; 1.63.10; 1.63.12;
Remove leftover netns stuff.
 1.62 28-Jun-2007  alc - add ichlpcib to XEN2 config file
- few cosmetics

tested with XEN2 and XEN3 kernel
ok bouyer@
 1.61 17-May-2007  yamt merge yamt-idlelwp branch. asked by core@. some ports still needs work.

from doc/BRANCHES:

idle lwp, and some changes depending on it.

1. separate context switching and thread scheduling.
(cf. gmcgarry_ctxsw)
2. implement idle lwp.
3. clean up related MD/MI interfaces.
4. make scheduler(s) modular.
 1.60 12-May-2007  jld Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.59 16-Apr-2007  ad branches: 1.59.2;
Share the sysarch stuff between the x86 ports. PR kern/36046.
 1.58 10-Apr-2007  macallan include files.wsfb
 1.57 05-Mar-2007  drochner branches: 1.57.2; 1.57.4;
clean up how cpus and ioapics are attached at the mainbus:
Seperate "cpubus" and "ioapicbus" -- while they share a common "address
space" (the apic id), the kernel doesn't use this fact. There are different
data passed to cpus and apics, which caused some ugly polymorphism. This
also saves the special "submatch" functions needed to distingush cpus
and ioapics for autoconf. (And it makes that "apid" locators wired
in the kernel configuration are honored now; this allows one to dumb down
an mp box to singleprocessor by userconfig.)
Print "apid" locators in the buses "print" function "as everyone does",
so the per-port cpu drivers don't need to do it.
Being here, constify "struct cpu_functions" and g/c the unused MP_PICMODE
flag.
 1.56 18-Feb-2007  dsl Get config(1) to supply the default values for BEEP_ONHALT.
 1.55 09-Feb-2007  ad branches: 1.55.2;
Merge newlock2 to head.
 1.54 18-Dec-2006  gdt Use agp_machdep.c from the new location (x86, not i386).
 1.53 19-Oct-2006  drochner branches: 1.53.2; 1.53.4;
remove relicts of COMPAT_AOUT, fixes compilation problems with old
kernel config files reported by Patrick Welche on current-users
 1.52 17-Oct-2006  bouyer XEN_NO_HYPERCALLPAGE is redundant with XEN_COMPAT_030001; make
XEN_COMPAT_030001 disable the hypercall page too and remove
XEN_NO_HYPERCALLPAGE
 1.51 17-Oct-2006  bouyer Add ELF_PADDR_OFFSET and VIRT_ENTRY strings to __xen_guest ELF section,
so that our kernels works with newer xen-3 hypervisors; and correct the value
of VIRT_BASE for dom0.
Now that we can embed the values of KERNBASE and KERNTEXTOFF in the binary
for Xen, make the domU memory layout the same as dom0 for Xen3 (making
it the other way round doens't work; probably because of alignement
constraints in the hypervisor). The old domU layout is used if options
XEN_COMPAT_030001 is present in the kernel config file. Enable this the
domU kernel config files for now, in case someone wants to run a NetBSD
domU on an older Xen3 installation.
 1.50 15-Oct-2006  xtraeme Enable Bluetooth support and devices (except bt3c that requires pcmcia)
for Xen.
 1.49 01-Oct-2006  bouyer Map the ISA hole on Xen, which allows us to access the SMBios, which allows
us to find the IPMI address. Add IPMI support on Xen (commented out for now)
 1.48 28-Sep-2006  bouyer Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.47 24-Sep-2006  bouyer The Xen folks says everyone should use hypercall call page now, make it so.
Old hypercall call method still still available with
options XEN_NO_HYPERCALLPAGE
but this is disabled by default (xen-3.0.2-2 supports hypercall call page
just fine).
While there add a VIRT_BASE= string in __xen_guest section; from
Bastian Blank on port-xen@.
 1.46 11-Aug-2006  yamt branches: 1.46.2; 1.46.4;
"xm shutdown" support for xen3. ok'ed by Manuel Bouyer.
 1.45 12-Jul-2006  yamt branches: 1.45.2;
implement a simple NETTXF_csum_blank/NETRXF_csum_blank workaround
so that we can talk with linux guests at least.
just fill checksum field of received packets if the flag is set.
maybe should be revisited later.
 1.44 09-Jul-2006  christos include files.apm
 1.43 02-Jul-2006  bouyer block device backend driver for Xen3. Add
pseudo-device xbdback
in your config file to use.
 1.42 25-May-2006  bouyer branches: 1.42.2;
Always include xen_shm_machdep.c for Xen3, theorically a domU could also
provide backend services to other domains.
 1.41 23-May-2006  bouyer Add a network backend driver for Xen3. This will appear as a xvif pseudo
device in kernel config files.
 1.40 09-Apr-2006  bouyer branches: 1.40.2;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.39 04-Apr-2006  gdamore Convert existing ath users to new Makefile, except for amd64, which needs
the rules due to needing to conditionally postprocess the HAL object file.

Macppc needs a a non-ELF HAL (EABI) object, so take care of that by default
in the atheros include file.
 1.38 19-Mar-2006  bouyer Add a block device frontend driver for Xen-3. xbd at hypervisor now
attaches with xbd_hypervisor, adjust CFATTACH_DECL().
 1.37 06-Mar-2006  bouyer branches: 1.37.2; 1.37.4;
Fix build issues that didn't show up in my private tree.
 1.36 06-Mar-2006  bouyer Add a Xen Virtual Network device driver for Xenbus.
 1.35 06-Mar-2006  bouyer pull in the grant table and Xenbus interfaces, and attach xenbus at hypervisor.
 1.34 12-Feb-2006  tron branches: 1.34.2;
Share Intel hardware random number generator support between amd64 and
i386 port. This will benefit EM64T systems using Intel i9xx chipsets.
 1.33 15-Jan-2006  bouyer branches: 1.33.2; 1.33.4;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.32 30-Dec-2005  jmmv branches: 1.32.2;
Add a 'struct bootinfo' to represent the bootinfo structure used in the
kernel by x86 platforms (instead of a simple char *). This way, the code
in, e.g., lookup_bootinfo, is a bit easier to understand.

While here, move the lookup_bootinfo function used in x86 platforms (amd64,
i386 and xen) to a common file (x86/x86_machdep.c), as it was exactly the
same in all of them.
 1.31 21-Nov-2005  bouyer Sync with i386/conf/GENERIC:
- add a buch of PCI storage devices
- add firewire devices
- add some missing PCI network devices
- add serial and parallel PCI adapters
- add lpt0 at isa
- add com1 at isa
com0 not added for the benefit of serial console users (it will conflicts
with the Xen kernel).
XXX this means that setups with serial console on com1 will now break with the
default kernel.
Use userconf(4) (add -c to kernel command line) or change your setup to
com0 instead (most bios allows arbitrary mappings of com ports)
 1.30 07-Nov-2005  yamt branches: 1.30.2;
some assym cleanup.
- move copyin and friends from locore.S to their own file, copy.S.
share it between i386 and xen.
- defparam KERNBASE and kill KERNBASE_LOCORE hack.
- add more symbols to assym.h and use it where appropriate.
 1.29 21-Aug-2005  bouyer Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.28 20-Aug-2005  bouyer Deal with the machine address space being non-contigous in bus_dmamem_alloc():
- Define _BUS_AVAIL_END to 0xffffffff, as we don't have an easy way to
find the upper bound for our machine address space (and this can change
when we swap pages with the hypervisor).
- implement _xen_bus_dmamem_alloc_range(), which will request a contigous
set of pages to the hypervisor if the pages returned by uvm_pglistalloc()
don't fit the constraints.
We can't deal with the low/high constraints yet, because Xen doesn't offer a
way to get pages in a specific ranges of addresses.

Based on patches from Dave Thompson (in private mail), with heavy hacking
by me.
 1.27 19-Aug-2005  bouyer Make PCI devices usable for non-domain0 kernels. Based on patches sent by
Ceri Storey to port-xen, with some additionnal changes by me:
- include bus_dma.c, bus_space.c and pci_machdep.c if pci is defined
instead of dom0ops
- Make various initialisations, and probe/attach pci busses based on NPCI
instead of DOM0OPS
- in conf/files.xen, move xen-specific devices before non-xen specific devices
so that the xen-specific match function is called first, to avoid false
attachement from too liberal match function in non-xen code.
 1.26 15-Jul-2005  yamt include cardbus/pcmcia.

XXX 32bit cards don't work due to the way how xen handles
pci configuration space.
 1.25 25-Jun-2005  dyoung branches: 1.25.2;
Don't include files.ath any more, it's disappeared.
 1.24 22-Jun-2005  dyoung Resolve conflicts in importation of 18-May-2005 ath(4) / net80211(9)
from FreeBSD. Introduce compatibility shims (sys/dev/ic/ath_netbsd.[ch],
sys/net80211/ieee80211_netbsd.[ch]). Update drivers (an, atu, atw,
awi, ipw, iwi, rtw, wi) for the new net80211(9) API.
 1.23 19-Jun-2005  chs add ath stuff.
 1.22 16-May-2005  yamt arch/i386/i386/linux_trap.c -> arch/x86/x86/linux_trap.c
 1.21 18-Apr-2005  bouyer Add sysmon_power attribute to hypervisor, and an handler for CMSG_SHUTDOWN
messages.
Now a NetBSD guest will reboot or shutdown on 'xm shutdown' commands in
domain0.
 1.20 17-Apr-2005  bouyer Remove
xenkbc at hypervisor
vga at hypervisor
they have been obsoleted by PCI and ISA support, and don't compile any more.
 1.19 16-Apr-2005  yamt tweak x86 bus_dma code so that it can be used by xen port.

- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.18 11-Mar-2005  bouyer branches: 1.18.2;
Define xen devices in i386/conf/majors.i386. This makes sure there won't
be conflics with regular i386 kernels.
 1.17 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.16 04-Feb-2005  jmc vfr.c is still needed on the trunk or GENERIC doesn't compile...
 1.15 03-Feb-2005  bouyer vfr.c doesn't exists any more.
 1.14 14-Sep-2004  jdolecek branches: 1.14.4; 1.14.6; 1.14.8;
do not include "compat/aout/files.aout", COMPAT_AOUT is soon to be removed
 1.13 30-Aug-2004  drochner Phase out the use of a string as first "attach args" member to control
which bustype should be attached with a specific call to config_found()
(from a "mainbus" or a bus bridge).
Do it for isa/eisa/mca and pci/agp for now. These buses all attach to
an mi interface attribute "isabus", "eisabus" etc., and the autoconf
framework now allows to specify an interface attribute on config_found()
and config_search(), which limits the search of matching config data
to these which attach to that specific attribute.
So we basically have to call config_found_ia(..., "foobus", ...) where
such a bus is attached.
As a consequence, where a "mainbus" or alike also attaches other
devices (eg CPUs) which do not attach to a specific attribute yet,
we need at least pass an attribute name (different from "foobus") so
that the foo bus is not found at these places. This made some minor
changes necessary which are not obviously related to the mentioned buses.
 1.12 07-May-2004  cl branches: 1.12.2;
Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.11 25-Apr-2004  cl Add support for i386_iopl.
 1.10 24-Apr-2004  cl Add keyboard support and wscons config options.
 1.9 24-Apr-2004  cl Add vga display support.
 1.8 24-Apr-2004  cl Allow the block device driver to impersonate wd/sd/cd devices. This allows
for most system seamless migration from a NetBSD/i386 setup to a NetBSD/xen
setup without changing /etc/fstab or creating additional device nodes.
 1.7 24-Apr-2004  cl Make bus_space map machine addresses instead of physical addresses.
 1.6 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.5 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.4 17-Apr-2004  cl add block device driver
 1.3 25-Mar-2004  wiz branches: 1.3.2;
Backout previous -- this is not enough, config needs to be taught
how to reach partitions above h. Noted by Simon Burge, related to
PR 24905.
 1.2 25-Mar-2004  wiz Define maxpartitions to the same value as MAXPARTITIONS in disklabel.h.
Addresses PR 24905 by Simon Burge.
 1.1 11-Mar-2004  cl Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.3.2.1 22-May-2004  he Pull up revisions 1.4-1.12 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.12.2.8 11-Dec-2005  christos Sync with head.
 1.12.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.12.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.12.2.5 21-Sep-2004  skrll Fix the sync with head I botched.
 1.12.2.4 18-Sep-2004  skrll Sync with HEAD.
 1.12.2.3 03-Sep-2004  skrll Sync with HEAD
 1.12.2.2 03-Aug-2004  skrll Sync with HEAD
 1.12.2.1 07-May-2004  skrll file files.xen was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.14.8.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.14.6.1 29-Apr-2005  kent sync with -current
 1.14.4.10 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.14.4.9 16-Feb-2005  bouyer Glue xen_shm and the block and network backends to the system.
 1.14.4.8 12-Feb-2005  bouyer xen/machmem.c doesn't exists any more.
 1.14.4.7 04-Feb-2005  bouyer Commit to the right branch this time: vfr.c doesn't exists any more.
 1.14.4.6 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.14.4.5 20-Jan-2005  bouyer bus_dma(9) for xen. Derived from arch/x86/x86/bus_dma.c.
bounce buffers not supported yet, because xen doesn't have an interface
to request memory in a specific range (this means that DMA on ISA won't
be supported), but I've left the code commented out because xen will
likely provide an appropriate hypercall in the future.
 1.14.4.4 19-Jan-2005  bouyer We can't attach {cd,wd,sd} at hypervisor, this conflicts with scsipi.
 1.14.4.3 18-Jan-2005  bouyer snapshot of work in progress on physical devices support:
- support pci at hypervisor. Attach one PCI bus for each bus returned by
PHYSDEVOP_PCI_PROBE_ROOT_BUSES (it looks like xen hides ppb bridges from
the guest OS).
- implement pci_conf_read()/pci_conf_write() using the appropriate
PHYSDEVOP_PCI_* calls.
- call PHYSDEVOP_PCI_INITIALISE_DEVICE from pci_intr_map() to make xen
do interrupts routing if needed, and map the hardware interrupt to
a xen interrupt.
- add pci and associated devices to files.xen

This is enouth to have IDE controllers work in PIO mode.
TODO: bus_dma support (currently bus_dma won't translate pseudo-physical
addresses to machine addresses), ISA support, test memory-mapped
I/O registers.
 1.14.4.2 17-Dec-2004  bouyer Merge changes between 2.0 and -current:
XEN: up to 1.14
files.xen: 1.12 - 1.14
 1.14.4.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.18.2.12 08-Jan-2007  ghen Pull up following revision(s) (requested by bouyer in ticket #1622):
sys/arch/xen/conf/files.xen: revision 1.49 via patch
sys/arch/xen/i386/autoconf.c: revision 1.19 via patch
sys/arch/xen/conf/files.compat: revision 1.13 via patch
sys/arch/xen/i386/mainbus.c: revision 1.10 via patch
sys/arch/xen/i386/bios32.c: revision 1.1 via patch
sys/arch/i386/conf/XEN2_DOM0: revision 1.9 via patch
sys/arch/xen/i386/locore.S: revision 1.19 via patch
Map the ISA hole on Xen, which allows us to access the SMBios, which allows
us to find the IPMI address. Add IPMI support on Xen (commented out for now)
 1.18.2.11 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.18.2.10 05-May-2006  ghen Pull up following revision(s) (requested by bouyer in ticket #1311):
sys/arch/xen/conf/files.xen: revision 1.31 via patch
sys/arch/i386/conf/XEN0: revision 1.18 via patch
Sync with i386/conf/GENERIC:
- add a buch of PCI storage devices
- add firewire devices
- add some missing PCI network devices
- add serial and parallel PCI adapters
- add lpt0 at isa
- add com1 at isa
com0 not added for the benefit of serial console users (it will conflicts
with the Xen kernel).
XXX this means that setups with serial console on com1 will now break with the
default kernel.
Use userconf(4) (add -c to kernel command line) or change your setup to
com0 instead (most bios allows arbitrary mappings of com ports)
 1.18.2.9 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.18.2.8 29-Mar-2006  tron Apply patch (requested by bouyer in ticket #1231):
Add support for ath at pci for XEN domain0.
 1.18.2.7 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1083):
sys/arch/xen/xen/hypervisor.c: revision 1.16
sys/arch/xen/xen/if_xennet.c: revision 1.31
sys/arch/xen/conf/files.xen: revision 1.29
sys/arch/xen/xen/xbd.c: revision 1.22
Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.18.2.6 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #696):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.1
sys/arch/xen/include/bus_private.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.28
Deal with the machine address space being non-contigous in bus_dmamem_alloc():
- Define _BUS_AVAIL_END to 0xffffffff, as we don't have an easy way to
find the upper bound for our machine address space (and this can change
when we swap pages with the hypervisor).
- implement _xen_bus_dmamem_alloc_range(), which will request a contigous
set of pages to the hypervisor if the pages returned by uvm_pglistalloc()
don't fit the constraints.
We can't deal with the low/high constraints yet, because Xen doesn't offer a
way to get pages in a specific ranges of addresses.
Based on patches from Dave Thompson (in private mail), with heavy hacking
by me.
 1.18.2.5 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #694):
sys/arch/xen/xen/hypervisor.c: revision 1.15
sys/arch/xen/xen/evtchn.c: revision 1.16
sys/arch/xen/conf/files.xen: revision 1.27
sys/arch/xen/i386/machdep.c: revision 1.18
Make PCI devices usable for non-domain0 kernels. Based on patches sent by
Ceri Storey to port-xen, with some additionnal changes by me:
- include bus_dma.c, bus_space.c and pci_machdep.c if pci is defined
instead of dom0ops
- Make various initialisations, and probe/attach pci busses based on NPCI
instead of DOM0OPS
- in conf/files.xen, move xen-specific devices before non-xen specific devices
so that the xen-specific match function is called first, to avoid false
attachement from too liberal match function in non-xen code.
 1.18.2.4 18-Aug-2005  tron Pull up revision 1.26 (requested by yamt in ticket #675):
include cardbus/pcmcia.
XXX 32bit cards don't work due to the way how xen handles
pci configuration space.
 1.18.2.3 25-Apr-2005  tron Pull up revision 1.21 (requested by bouyer in ticket #190):
Add sysmon_power attribute to hypervisor, and an handler for CMSG_SHUTDOWN
messages.
Now a NetBSD guest will reboot or shutdown on 'xm shutdown' commands in
domain0.
 1.18.2.2 25-Apr-2005  tron Pull up revision 1.20 (requested by bouyer in ticket #186):
Remove
xenkbc at hypervisor
vga at hypervisor
they have been obsoleted by PCI and ISA support, and don't compile any more.
 1.18.2.1 21-Apr-2005  tron Pull up revision 1.19 (requested by yamt in ticket #175):
tweak x86 bus_dma code so that it can be used by xen port.
- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.25.2.9 27-Feb-2008  yamt sync with head.
 1.25.2.8 04-Feb-2008  yamt sync with head.
 1.25.2.7 21-Jan-2008  yamt sync with head
 1.25.2.6 07-Dec-2007  yamt sync with head
 1.25.2.5 27-Oct-2007  yamt sync with head.
 1.25.2.4 03-Sep-2007  yamt sync with head.
 1.25.2.3 26-Feb-2007  yamt sync with head.
 1.25.2.2 30-Dec-2006  yamt sync with head.
 1.25.2.1 21-Jun-2006  yamt sync with head.
 1.30.2.1 22-Nov-2005  yamt sync with head.
 1.32.2.2 18-Feb-2006  yamt sync with head.
 1.32.2.1 01-Feb-2006  yamt sync with head.
 1.33.4.2 01-Jun-2006  kardel Sync with head.
 1.33.4.1 22-Apr-2006  simonb Sync with head.
 1.33.2.1 09-Sep-2006  rpaulo sync with head
 1.34.2.6 11-Aug-2006  yamt sync with head
 1.34.2.5 26-Jun-2006  yamt sync with head.
 1.34.2.4 24-May-2006  yamt sync with head.
 1.34.2.3 11-Apr-2006  yamt sync with head
 1.34.2.2 01-Apr-2006  yamt sync with head.
 1.34.2.1 13-Mar-2006  yamt sync with head.
 1.37.4.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.37.4.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.37.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.40.2.1 19-Jun-2006  chap Sync with head.
 1.42.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.45.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.46.4.2 21-Dec-2006  yamt sync with head.
 1.46.4.1 22-Oct-2006  yamt sync with head
 1.46.2.3 28-Jan-2007  ad xen MD changes.
 1.46.2.2 12-Jan-2007  ad Sync with head.
 1.46.2.1 18-Nov-2006  ad Sync with head.
 1.53.4.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.53.2.1 10-Jun-2007  bouyer Pull up following revision(s) (requested by jld in ticket #711):
sys/arch/xen/xen/clock.c: revision 1.35 via patch
sys/arch/xen/include/types.h: revision 1.5 via patch
sys/arch/xen/i386/machdep.c: revision 1.38 via patch
sys/arch/xen/conf/files.xen: revision 1.60 via patch
sys/arch/xen/include/cpu.h: revision 1.19 via patch
Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.55.2.6 17-May-2007  yamt sync with head.
 1.55.2.5 07-May-2007  yamt sync with head.
 1.55.2.4 15-Apr-2007  yamt sync with head.
 1.55.2.3 26-Mar-2007  yamt adapt xen.
 1.55.2.2 12-Mar-2007  rmind Sync with HEAD.
 1.55.2.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.57.4.1 11-Jul-2007  mjf Sync with head.
 1.57.2.5 03-Dec-2007  ad Sync with HEAD.
 1.57.2.4 09-Oct-2007  ad Sync with head.
 1.57.2.3 15-Jul-2007  ad Sync with head.
 1.57.2.2 27-May-2007  ad Sync with head.
 1.57.2.1 10-Apr-2007  ad Sync with head.
 1.59.2.2 03-Oct-2007  garbled Sync with HEAD
 1.59.2.1 22-May-2007  matt Update to HEAD.
 1.63.12.1 06-Oct-2007  yamt sync with head.
 1.63.10.3 23-Mar-2008  matt sync with HEAD
 1.63.10.2 09-Jan-2008  matt sync with HEAD
 1.63.10.1 06-Nov-2007  matt sync with HEAD
 1.63.8.6 09-Dec-2007  jmcneill Sync with HEAD.
 1.63.8.5 03-Dec-2007  joerg Sync with HEAD.
 1.63.8.4 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.63.8.3 21-Nov-2007  joerg Sync with HEAD.
 1.63.8.2 28-Oct-2007  joerg Sync with HEAD.
 1.63.8.1 02-Oct-2007  joerg Sync with HEAD.
 1.64.2.7 22-Nov-2007  bouyer Sync with HEAD
 1.64.2.6 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 3: merge xenamd64/amd64/xpmap.c in
xen/x86/x86_xpmap.c
 1.64.2.5 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 1: empty xenamd64/conf
- move xenamd64/conf/std.xen to amd64/conf/
- define arch to amd64 amd64/conf/std.xen, and i386 in xen/conf/std.xen
(machine is xen)
- enclose content of amd64/conf/files.amd64 and i386/conf/files.i386 in
ifndef xen/endif, so it can be transparently inclued for xen kernels
- support MACHINE_ARCH=x86_64 in xen/conf/Makefile*
 1.64.2.4 18-Nov-2007  bouyer Sync with HEAD
 1.64.2.3 13-Nov-2007  bouyer Sync with HEAD
 1.64.2.2 21-Oct-2007  bouyer Factorise some Xen pmap code in x86_xpmap.c.
More xpmap_{ptom,mtop} -> xpmap_{ptom,mtop}_masked

The xenamd64 kernel is now good enough to complete a sysinst install from
xennet to xbd.
 1.64.2.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.68.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.68.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.68.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.75.6.8 19-Jan-2008  bouyer Sync with HEAD
 1.75.6.7 09-Jan-2008  bouyer Merge xen bits to i386/i386/gdt.c. Convert remaining uses of PTE_* macros to
pmap_pte_* macros/inlines.
Fix think-o in pmap.c for native i386.
 1.75.6.6 07-Jan-2008  bouyer Move Xen support to i386/i386/machdep.c. two less files in xen/ :)
 1.75.6.5 06-Jan-2008  bouyer Switch xen/i386 to the i386 npx.c. We have to keep npx_hv.c to attach npx
to hypervisor0.
 1.75.6.4 06-Jan-2008  bouyer Use the i386 bios32.c for xen/i386
 1.75.6.3 05-Jan-2008  bouyer Not used anymore since xen/i386 has been switched to xen_pmap_bootstrap()
 1.75.6.2 02-Jan-2008  bouyer Sync with HEAD
 1.75.6.1 11-Dec-2007  bouyer Switch i386 to x86/x86/pmap.c
 1.82.6.4 17-Jan-2009  mjf Sync with HEAD.
 1.82.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.82.6.2 02-Jun-2008  mjf Sync with HEAD.
 1.82.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.82.2.1 24-Mar-2008  keiichi sync with head.
 1.83.2.1 18-May-2008  yamt sync with head.
 1.84.4.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.84.4.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.84.2.6 11-Aug-2010  yamt sync with head.
 1.84.2.5 11-Mar-2010  yamt sync with head
 1.84.2.4 19-Aug-2009  yamt sync with head.
 1.84.2.3 20-Jun-2009  yamt sync with head
 1.84.2.2 04-May-2009  yamt sync with head.
 1.84.2.1 16-May-2008  yamt sync with head.
 1.87.4.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.87.4.1 19-Oct-2008  haad Sync with HEAD.
 1.88.4.7 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.88.4.6 07-Jan-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1503):
sys/arch/xen/conf/files.xen: revision 1.110
Make maxpartitions 16 on !i386. Fixes hardwiring root on device autoconf
index > 0 on amd64. Problem reported and patch tested by Tobias Nygren.
 1.88.4.5 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.88.4.4 19-Jun-2009  snj Pull up following revision(s) (requested by bouyer in ticket #816):
sys/arch/amd64/conf/files.amd64: revision 1.68
sys/arch/i386/conf/files.i386: revision 1.350
sys/arch/x86/include/rtc.h: revision 1.1
sys/arch/x86/isa/clock.c: revision 1.33
sys/arch/x86/isa/rtc.c: revision 1.1
sys/arch/xen/conf/files.xen: revision 1.100
sys/arch/xen/xen/clock.c: revision 1.50 via patch
Split mc146818-related functions from clock.c into rtc.c.
Call rtc_set_ymdhms() from xen/xen/clock.c:xen_rtc_set() for xen3 dom0
kernels as the Xen3 hypervisor doesn't write the new date/time to the CMOS
by itself.
Now a XEN3_DOM0 kernel properly updates the CMOS time.
 1.88.4.3 16-Jun-2009  snj Pull up following revision(s) (requested by rmind in ticket #782):
sys/arch/xen/conf/files.xen: revision 1.99 via patch
Include cpu_topology.c for xen kernels, too.
 1.88.4.2 16-Feb-2009  snj branches: 1.88.4.2.4;
Pull up following revision(s) (requested by ad in ticket #355):
sys/arch/i386/conf/GENERIC_TINY: revision 1.116
sys/arch/i386/conf/INSTALL_TINY: revision 1.122
sys/arch/i386/conf/files.i386: revision 1.341
sys/arch/i386/i386/vm_machdep.c: revision 1.142
sys/arch/i386/include/param.h: revision 1.68
sys/arch/xen/conf/files.xen: revision 1.91
- Kill NOREDZONE.
- Make the redzone conditional on DIAGNOSTIC.
- Give amd64 an additional page for the uarea. 2 is not enough.
 1.88.4.1 22-Jan-2009  snj Pull up following revision(s) (requested by bouyer in ticket #286):
sys/arch/xen/conf/files.xen: revision 1.92
sys/arch/xen/x86/mainbus.c: revision 1.7 via patch
sys/arch/xen/xen/hypervisor.c: revision 1.43
The Xen PCI_BUS_FIXUP/PCI_ADDR_FIXUP has rotted, catch up with x86 changes
in this area. Patch provided by FUKAUMI Naoki in PR#40356.
 1.88.4.2.4.1 21-Apr-2010  matt sync to netbsd-5
 1.88.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.88.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.88.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.92.2.8 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.92.2.7 02-May-2011  jym Sync with head.
 1.92.2.6 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.92.2.5 10-Jan-2011  jym Sync with HEAD
 1.92.2.4 24-Oct-2010  jym Sync with HEAD
 1.92.2.3 01-Nov-2009  jym Sync with HEAD.
 1.92.2.2 23-Jul-2009  jym Sync with HEAD.
 1.92.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.106.4.6 31-May-2011  rmind sync with head
 1.106.4.5 21-Apr-2011  rmind sync with head
 1.106.4.4 05-Mar-2011  rmind sync with head
 1.106.4.3 31-May-2010  rmind - Split off Xen versions of pmap_map_ptes/pmap_unmap_ptes into Xen pmap,
also move pmap_apte_flush() with pmap_unmap_apdp() there.
- Make Xen buildable.
 1.106.4.2 30-May-2010  rmind sync with head
 1.106.4.1 18-Mar-2010  rmind Unify /dev/{mem,kmem,zero,null} implementations in MI code. Based on patch
from Joerg Sonnenberger, proposed on tech-kern@, in February 2008.

Work and depression still in progress.
 1.106.2.3 30-Oct-2010  uebayasi Implement pmap_physload_device(9) to replace xmd(4) MD backend.
Implement pmap_mmap(9) and use it from mem(4) and xmd(4).
 1.106.2.2 27-Aug-2010  uebayasi Glue xmd(4).
 1.106.2.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.110.4.1 05-Mar-2011  bouyer Sync with HEAD
 1.110.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.118.2.3 27-Jun-2011  cherry Conditionally compile xen_ipi.c
 1.118.2.2 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.118.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.123.6.2 02-Jun-2012  mrg sync to latest -current.
 1.123.6.1 18-Feb-2012  mrg merge to -current.
 1.123.2.4 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.123.2.3 30-Oct-2012  yamt sync with head
 1.123.2.2 23-May-2012  yamt sync with head.
 1.123.2.1 17-Apr-2012  yamt sync with head
 1.128.2.3 03-Dec-2017  jdolecek update from HEAD
 1.128.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.128.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.129.2.1 18-May-2014  rmind sync with head
 1.133.8.1 18-Jan-2017  skrll Sync with netbsd-5
 1.133.4.3 08-Jul-2017  snj Pull up following revision(s) (requested by khorben in ticket #1445):
sys/arch/amd64/conf/XEN3_DOM0: revision 1.136
sys/arch/i386/conf/XEN3_DOM0: revision 1.113
sys/arch/xen/conf/files.xen: revision 1.148
Register support for SD card readers with Xen DOM0 kernels
Tested on a Lenovo ThinkPad T440s (amd64)
 1.133.4.2 18-Dec-2016  snj Pull up following revision(s) (requested by jnemeth in ticket #1326):
sys/arch/xen/conf/files.xen: revision 1.141
make CPU microcode loading dependent on both DOM0OPS AND CPU_UCODE
 1.133.4.1 25-Sep-2016  bouyer Pull up following revision(s) (requested by sborrill in ticket #1255):
sys/arch/xen/conf/files.xen: revision 1.137
Include files.sljit as i386/amd64 do.
 1.134.2.6 28-Aug-2017  skrll Sync with HEAD
 1.134.2.5 05-Feb-2017  skrll Sync with HEAD
 1.134.2.4 29-May-2016  skrll Sync with HEAD
 1.134.2.3 19-Mar-2016  skrll Sync with HEAD
 1.134.2.2 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.134.2.1 06-Apr-2015  skrll Sync with HEAD
 1.141.2.3 26-Apr-2017  pgoyette Sync with HEAD
 1.141.2.2 20-Mar-2017  pgoyette Sync with HEAD
 1.141.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.143.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.147.2.4 05-Aug-2020  martin Pull up the following revisions, requested by msaitoh in ticket #1593:

sys/arch/x86/conf/files.x86 1.108
sys/arch/x86/include/apicvar.h 1.7 via patch
sys/arch/x86/include/cpu.h 1.121
sys/arch/x86/x86/cpu.c 1.185 via patch
sys/arch/x86/x86/hyperv.c 1.7
sys/arch/x86/x86/tsc.c 1.41
sys/arch/xen/conf/files.xen 1.181

Get TSC frequency from CPUID 0x15 and/or x16 if it's available.
This change fixes a problem that newer Intel processors' timer
counts very slowly.
 1.147.2.3 23-Sep-2019  martin Backout previous try to fix ticket #1380, this causes more build fallout,
will be fixed differently.
 1.147.2.2 23-Sep-2019  martin Cherry pick a small part of rev1.178 to get efi UUIDs for ticket #1380.
 1.147.2.1 30-Jun-2017  snj Pull up following revision(s) (requested by khorben in ticket #62):
sys/arch/amd64/conf/XEN3_DOM0: revision 1.136
sys/arch/i386/conf/XEN3_DOM0: revision 1.113
sys/arch/xen/conf/files.xen: revision 1.148
Register support for SD card readers with Xen DOM0 kernels
Tested on a Lenovo ThinkPad T440s (amd64)
 1.149.2.2 16-Jul-2017  cherry 2302716
 1.149.2.1 16-Jul-2017  cherry file files.xen was added on branch perseant-stdc-iso10646 on 2017-07-16 14:02:49 +0000
 1.165.2.7 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.165.2.6 14-Sep-2018  pgoyette OMG it builds!

Finish rearranging the amd64 MD compat_netbsd32 code.

XXX Other architectures will be dealt with later.
 1.165.2.5 28-Jul-2018  pgoyette Sync with HEAD
 1.165.2.4 25-Jun-2018  pgoyette Sync with HEAD
 1.165.2.3 22-Mar-2018  pgoyette Synch with HEAD, resolve conflicts
 1.165.2.2 18-Mar-2018  pgoyette Import more christos@ changes from -current
 1.165.2.1 17-Mar-2018  pgoyette Import christos's changes for the compat_60 cpu_ucode stuff
 1.169.2.4 21-Apr-2020  martin Sync with HEAD
 1.169.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.169.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.169.2.1 10-Jun-2019  christos Sync with HEAD
 1.178.4.2 15-May-2023  martin Apply patch, requested by manu in ticket #1632:

sys/arch/xen/conf/files.xen (apply patch)

Build fix to define the SELFRELOC option for XEN kernels.
This is not needed in newer branches as those include amd64/conf/files.amd64
from x86/conf/files.x86 (and get the definition via that path)
 1.178.4.1 15-Jul-2020  martin Pull up the following, requested by msaitoh in ticket #1015

sys/arch/x86/conf/files.x86 1.108 (via patch)
sys/arch/x86/include/apicvar.h 1.7 (via patch)
sys/arch/x86/include/cpu.h 1.121 (via patch)
sys/arch/x86/x86/cpu.c 1.185 (via patch)
sys/arch/x86/x86/hyperv.c 1.7 (via patch)
sys/arch/x86/x86/tsc.c 1.41 (via patch)
sys/arch/xen/conf/files.xen 1.181 (via patch)

Get TSC frequency from CPUID 0x15 and/or x16 if it's available.
This change fixes a problem that newer Intel processors' timer
counts very slowly.
 1.180.2.9 25-Apr-2020  bouyer restore part accidentaly deleted
 1.180.2.8 25-Apr-2020  bouyer sync with bouyer-xenpvh-base2 (HEAD)
 1.180.2.7 25-Apr-2020  bouyer Include again xen/conf/files.compat for PV kernels, and make it build.
My build.sh -u used stale opt_*.h files ...
 1.180.2.6 20-Apr-2020  bouyer We need xenbus_bus_dma_tag for PVHVM too, but without phys->machine translation
 1.180.2.5 19-Apr-2020  bouyer Move xen_ipi.c to XENPV only.
Make sure we don't need to send events to remote CPUs (outside of IPIs)
 1.180.2.4 16-Apr-2020  bouyer Rename xen/xen/clock.c to xen_clock.c, so that it can be compiled
with x86/isa/clock.c
call events_default_setup() from hypervisor_match to that event
arrays are properly initialised.
Use xen_delay() and xen's timecounter for PVHVM.
 1.180.2.3 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.180.2.2 14-Apr-2020  bouyer Always patch spllower with cx8_spllower; it works fine for Xen now
Include x86/x86/patch.c is !xenpv
While there, defopt XENPV
 1.180.2.1 11-Apr-2020  bouyer Move softint and preemtion-related functions out of x86/x86/intr.c to
its own file, x86/x86/x86_softintr.c
Add x86/x86/x86_softintr.c for native and XenPV
Make sure XenPV also check ci_ioending, which is used for softints.
Switch XenPV to fast softints and allow kernel preemption.
kpreempt_disable() before calling pmap_changeprot_local()
run xen_wallclock_time() and xen_global_systime_ns() at splshed() to
avoid being interrupted.

XXX amd64 lock stubs are racy for XPENDING
 1.187.4.1 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #428):

sys/arch/xen/xen/xen_machdep.c: revision 1.28
sys/arch/x86/pci/pci_machdep.c: revision 1.97
sys/arch/xen/xen/genfb_xen.c: revision 1.1
sys/arch/xen/xen/genfb_xen.c: revision 1.2
sys/arch/xen/include/hypervisor.h: revision 1.59
sys/arch/i386/conf/XEN3PAE_DOM0: revision 1.41 (patch)
sys/arch/x86/x86/genfb_machdep.c: revision 1.22
sys/arch/xen/x86/consinit.c: revision 1.18
sys/arch/xen/x86/autoconf.c: revision 1.26
sys/external/mit/xen-include-public/dist/xen/include/public/platform.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.188
sys/arch/x86/x86/consinit.c: revision 1.37
sys/arch/xen/conf/files.xen: revision 1.189
sys/arch/x86/x86/consinit.c: revision 1.38
sys/external/mit/xen-include-public/dist/xen/include/public/xen.h: revision 1.2
sys/arch/x86/include/genfb_machdep.h: revision 1.7
sys/arch/xen/x86/pvh_consinit.c: revision 1.5
sys/arch/xen/x86/pvh_consinit.c: revision 1.6
sys/arch/amd64/conf/XEN3_DOM0: revision 1.201

Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.

Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.

Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen

when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.

x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console

xen/x86/consinit.c: support genfb as possible console

xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.

xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.

Make sure to always fall back to xen_early_console, even for dom0

Enable genfb in DOM0 kernels

Add ext_lfb_base to dom0_vga_console_info, from recent Xen. We know if it's
present or not by checking dom0.info_size

Add XENPF_get_dom0_console, which gets a dom0_vga_console_info stucture
from the hypervisor. To be used by PVH dom0 kernels.

XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.

Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.3 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.2 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.1 16-Apr-2020  bouyer branches: 1.1.2;
file files.xen.pv was initially added on branch bouyer-xenpvh.
 1.1.2.5 25-Apr-2020  bouyer Include again xen/conf/files.compat for PV kernels, and make it build.
My build.sh -u used stale opt_*.h files ...
 1.1.2.4 20-Apr-2020  bouyer We need xenbus_bus_dma_tag for PVHVM too, but without phys->machine translation
 1.1.2.3 19-Apr-2020  bouyer Move xen_ipi.c to XENPV only.
Make sure we don't need to send events to remote CPUs (outside of IPIs)
 1.1.2.2 16-Apr-2020  bouyer Rename xen/xen/clock.c to xen_clock.c, so that it can be compiled
with x86/isa/clock.c
call events_default_setup() from hypervisor_match to that event
arrays are properly initialised.
Use xen_delay() and xen's timecounter for PVHVM.
 1.1.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.3 28-May-2006  bouyer Remove unused file. Pointed out by Pavel Cahyna.
Probably unused since kernel config files were moved to i386/conf.
 1.2 11-Dec-2005  christos branches: 1.2.4; 1.2.6; 1.2.8; 1.2.14;
merge ktrace-lwp.
 1.1 11-Mar-2004  cl branches: 1.1.4; 1.1.18; 1.1.20;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.20.1 21-Jun-2006  yamt sync with head.
 1.1.18.1 05-Aug-2006  ghen Pull up following revision(s) (requested by bouyer in ticket #1453):
sys/arch/xen/conf/kern.ldscript: file removal
Remove unused file. Pointed out by Pavel Cahyna.
Probably unused since kernel config files were moved to i386/conf.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file kern.ldscript was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.2.14.1 19-Jun-2006  chap Sync with head.
 1.2.8.1 26-Jun-2006  yamt sync with head.
 1.2.6.1 01-Jun-2006  kardel Sync with head.
 1.2.4.1 09-Sep-2006  rpaulo sync with head
 1.2 11-Mar-2005  bouyer Define xen devices in i386/conf/majors.i386. This makes sure there won't
be conflics with regular i386 kernels.
 1.1 11-Mar-2004  cl branches: 1.1.4; 1.1.12; 1.1.14;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.14.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.12.1 29-Apr-2005  kent sync with -current
 1.1.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file majors.i386 was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.5 11-Mar-2005  bouyer Define xen devices in i386/conf/majors.i386. This makes sure there won't
be conflics with regular i386 kernels.
 1.4 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 24-Apr-2004  cl branches: 1.3.2; 1.3.6; 1.3.8; 1.3.10;
Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.2 17-Apr-2004  cl add block device driver
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.8.1 29-Apr-2005  kent sync with -current
 1.3.6.1 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.3.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.3.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.2.2 03-Aug-2004  skrll Sync with HEAD
 1.3.2.1 24-Apr-2004  skrll file majors.xen was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.11 09-Apr-2020  jdolecek move xen/conf/std.xen to i386/conf/std.xen, it's i386-specific

split off __XEN_INTERFACE_VERSION__ to new xen/conf/std.xenversion
and use from both i386/conf/std.xen and amd64/conf/stf.xen, so that
there is single place for the definition
 1.10 04-Feb-2019  cherry branches: 1.10.10;
Bump up XEN source API compatibility to 0x00030208 from 0x00030201,

but maintain backwards source API compilation compatibility.

ie; sources with config(5)
options __XEN_INTERFACE_VERSION__=0x00030201 # Xen 3.1 interface

should compile and run without problems.

Not that API version 0x00030201 is the lowest version we support now.
 1.9 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.8 23-Sep-2016  joerg branches: 1.8.16;
Given Xen/i386 the same process and file limit as native i386.
 1.7 30-Jun-2011  wiz branches: 1.7.12; 1.7.28; 1.7.30; 1.7.32; 1.7.34; 1.7.36;
dependant -> dependent
 1.6 12-Dec-2008  alc branches: 1.6.2; 1.6.12;
include standard HAL option.

Fix build breaks reported by Paul Goyette on current-users@
 1.5 25-Jan-2008  joerg branches: 1.5.6; 1.5.10; 1.5.16; 1.5.18; 1.5.20; 1.5.26;
Refactor in_cksum/in4_cksum/in6_cksum implementations:
- All three functions are included in the kernel by default.
They call a backend function cpu_in_cksum after possibly
computing the checksum of the pseudo header.
- cpu_in_cksum is the core to implement the one-complement sum.
The default implementation is moderate fast on most platforms
and provides a 32bit accumulator with 16bit addends for L32 platforms
and a 64bit accumulator with 32bit addends for L64 platforms.
It handles edge cases like very large mbuf chains (could happen with
native IPv6 in the future) and provides a good base for new native
implementations.
- Modify i386 and amd64 assembly to use the new interface.

This disables the MD implementations on !x86 until the conversion is
done. For Alpha, the portable version is faster.
 1.4 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.3 11-Dec-2005  christos branches: 1.3.30; 1.3.48; 1.3.50; 1.3.54; 1.3.56;
merge ktrace-lwp.
 1.2 17-Sep-2005  yamt include "conf/std".
 1.1 11-Mar-2004  cl branches: 1.1.4; 1.1.20;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.20.3 04-Feb-2008  yamt sync with head.
 1.1.20.2 07-Dec-2007  yamt sync with head
 1.1.20.1 21-Jun-2006  yamt sync with head.
 1.1.4.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file std.xen was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.3.56.2 18-Feb-2008  mjf Sync with HEAD.
 1.3.56.1 08-Dec-2007  mjf Sync with HEAD.
 1.3.54.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 1: empty xenamd64/conf
- move xenamd64/conf/std.xen to amd64/conf/
- define arch to amd64 amd64/conf/std.xen, and i386 in xen/conf/std.xen
(machine is xen)
- enclose content of amd64/conf/files.amd64 and i386/conf/files.i386 in
ifndef xen/endif, so it can be transparently inclued for xen kernels
- support MACHINE_ARCH=x86_64 in xen/conf/Makefile*
 1.3.50.2 23-Mar-2008  matt sync with HEAD
 1.3.50.1 09-Jan-2008  matt sync with HEAD
 1.3.48.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.3.30.1 03-Dec-2007  ad Sync with HEAD.
 1.5.26.1 21-Apr-2010  matt sync to netbsd-5
 1.5.20.1 07-Aug-2009  snj Apply patch (requested by jmcneill in ticket 775):
Update to the open source atheros HAL.
 1.5.18.1 19-Jan-2009  skrll Sync with HEAD.
 1.5.16.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.5.10.1 04-May-2009  yamt sync with head.
 1.5.6.1 17-Jan-2009  mjf Sync with HEAD.
 1.6.12.1 31-Jul-2011  cherry grow MP support for i386. boots to single user
 1.6.2.1 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.7.36.1 18-Jan-2017  skrll Sync with netbsd-5
 1.7.34.1 04-Nov-2016  pgoyette Sync with HEAD
 1.7.32.1 01-Nov-2016  snj Pull up following revision(s) (requested by joerg in ticket #1258):
sys/arch/amd64/conf/std.xen: revision 1.8
sys/arch/xen/conf/std.xen: revision 1.8
Use the same process and file limits as normal AND64 for Xen kernels.
Given Xen/i386 the same process and file limit as native i386.
 1.7.30.1 05-Oct-2016  skrll Sync with HEAD
 1.7.28.1 01-Nov-2016  snj Pull up following revision(s) (requested by joerg in ticket #1258):
sys/arch/amd64/conf/std.xen: revision 1.8
sys/arch/xen/conf/std.xen: revision 1.8
Use the same process and file limits as normal AND64 for Xen kernels.
Given Xen/i386 the same process and file limit as native i386.
 1.7.12.1 03-Dec-2017  jdolecek update from HEAD
 1.8.16.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.8.16.1 10-Jun-2019  christos Sync with HEAD
 1.10.10.2 20-Apr-2020  bouyer Sync with HEAD
 1.10.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.3 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.2 09-Apr-2020  jdolecek branches: 1.2.2; 1.2.4;
update to __XEN_INTERFACE_VERSION__ 0x0003020a aka Xen 3.2.10

this brings grant memory v2 support:
- status separated from flags - revoking access needs just memory barrier,
no need for expensive cmpxchg16 any more
- sub-page hypervisor copy-only grants, to be used by xennet(4)
- 64-bit frame, i.e. support for DomU RAM >16TB

the grant table is now always allocated on boot to maximum size, it's now
never grown in runtime; switch back to regular kmem_alloc()/kmem_free()

code now requires v2 support, no compatibility for grant version 1 retained -
Xen v2 support predates all currently supported Xen versions

also interface for baloon changed slightly, code updated
 1.1 09-Apr-2020  jdolecek move xen/conf/std.xen to i386/conf/std.xen, it's i386-specific

split off __XEN_INTERFACE_VERSION__ to new xen/conf/std.xenversion
and use from both i386/conf/std.xen and amd64/conf/stf.xen, so that
there is single place for the definition
 1.2.4.2 20-Apr-2020  bouyer Sync with HEAD
 1.2.4.1 09-Apr-2020  bouyer file std.xenversion was added on branch bouyer-xenpvh on 2020-04-20 11:29:00 +0000
 1.2.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.2.2.1 09-Apr-2020  martin file std.xenversion was added on branch phil-wifi on 2020-04-13 08:04:12 +0000
 1.7 31-Aug-2020  bouyer Add back <xen/xenio3.h>, it's needed by xentools413
 1.6 24-Jul-2020  jdolecek <xen/xenio.h> is in fact used by the newly imported xentools 4.13,
reinstantiate it

header is for use by xentools only, which define domid_t themselves,
so it should not be a problem the header doesn't define it per PR port-xen/52874
 1.5 17-Jul-2020  jdolecek don't install xen/xenio.h and xen/xenio3.h anymore, xentools don't use it

PR port-xen/52874
 1.4 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.3 17-Feb-2008  bouyer branches: 1.3.108;
Install xenio.h and xenio3.h in /usr/include/xen, for the benefit
of xentools3.

XXX ignore those in Makefile.ioctl-c, they don't compile properly outside
of the Xen context and the ioctls from xenio.h conflicts with
soundcard.h
 1.2 11-Dec-2005  christos branches: 1.2.24; 1.2.40; 1.2.50; 1.2.56;
merge ktrace-lwp.
 1.1 12-May-2004  cl branches: 1.1.2; 1.1.4; 1.1.18;
``build.sh -m xen-i386 release'' now builds a release for NetBSD/xen
for i386. The resulting release consists of:
- NetBSD/xen for i386 kernel, loader and docuemntation
- NetBSD/i386 userland sets
 1.1.18.1 27-Feb-2008  yamt sync with head.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 12-May-2004  skrll file Makefile was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 12-May-2004  he file Makefile was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.2.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.2.50.1 23-Mar-2008  matt sync with HEAD
 1.2.40.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.2.24.1 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1087):
src/distrib/sets/lists/comp/md.i386 1.104
src/sys/arch/i386/Makefile 1.33
src/sys/arch/xen/include/Makefile 1.3
src/usr.bin/kdump/Makefile.ioctl-c 1.20

Install xenio.h and xenio3.h in /usr/include/xen, for the benefit
of xentools3.

XXX ignore those in Makefile.ioctl-c, they don't compile properly outside
of the Xen context and the ioctls from xenio.h conflicts with
soundcard.h
 1.3.108.1 25-Apr-2020  bouyer Also install xen/intrdefs.h
 1.2 11-Jan-2008  bouyer 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.1 09-Apr-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.6; 1.1.10; 1.1.14; 1.1.20; 1.1.50; 1.1.56; 1.1.64;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.1.64.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.1.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.1.50.1 23-Mar-2008  matt sync with HEAD
 1.1.20.2 09-Sep-2006  rpaulo sync with head
 1.1.20.1 09-Apr-2006  rpaulo file acpi_func.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.1.14.3 21-Jan-2008  yamt sync with head
 1.1.14.2 21-Jun-2006  yamt sync with head.
 1.1.14.1 09-Apr-2006  yamt file acpi_func.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:15 +0000
 1.1.10.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.1.10.1 09-Apr-2006  tron file acpi_func.h was added on branch peter-altq on 2006-05-24 15:48:25 +0000
 1.1.6.2 22-Apr-2006  simonb Sync with head.
 1.1.6.1 09-Apr-2006  simonb file acpi_func.h was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.1.4.2 19-Apr-2006  elad sync with head - hopefully this will work
 1.1.4.1 09-Apr-2006  elad file acpi_func.h was added on branch elad-kernelauth on 2006-04-19 02:34:03 +0000
 1.1.2.2 11-Apr-2006  yamt sync with head
 1.1.2.1 09-Apr-2006  yamt file acpi_func.h was added on branch yamt-pdpolicy on 2006-04-11 11:53:48 +0000
 1.2 11-Jan-2008  bouyer 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.1 09-Apr-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.6; 1.1.10; 1.1.14; 1.1.20; 1.1.50; 1.1.56; 1.1.64;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.1.64.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.1.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.1.50.1 23-Mar-2008  matt sync with HEAD
 1.1.20.2 09-Sep-2006  rpaulo sync with head
 1.1.20.1 09-Apr-2006  rpaulo file acpi_machdep.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.1.14.3 21-Jan-2008  yamt sync with head
 1.1.14.2 21-Jun-2006  yamt sync with head.
 1.1.14.1 09-Apr-2006  yamt file acpi_machdep.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:15 +0000
 1.1.10.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.1.10.1 09-Apr-2006  tron file acpi_machdep.h was added on branch peter-altq on 2006-05-24 15:48:25 +0000
 1.1.6.2 22-Apr-2006  simonb Sync with head.
 1.1.6.1 09-Apr-2006  simonb file acpi_machdep.h was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.1.4.2 19-Apr-2006  elad sync with head - hopefully this will work
 1.1.4.1 09-Apr-2006  elad file acpi_machdep.h was added on branch elad-kernelauth on 2006-04-19 02:34:03 +0000
 1.1.2.2 11-Apr-2006  yamt sync with head
 1.1.2.1 09-Apr-2006  yamt file acpi_machdep.h was added on branch yamt-pdpolicy on 2006-04-11 11:53:48 +0000
 1.1 06-Jul-2010  cherry branches: 1.1.2; 1.1.4; 1.1.6; 1.1.12;
The Xen balloon driver enables growing and shrinking
PV domains on the fly, by collaborating with UVM and the hypervisor
 1.1.12.2 05-Mar-2011  rmind sync with head
 1.1.12.1 06-Jul-2010  rmind file balloon.h was added on branch rmind-uvmplock on 2011-03-05 20:52:33 +0000
 1.1.6.2 24-Oct-2010  jym Sync with HEAD
 1.1.6.1 06-Jul-2010  jym file balloon.h was added on branch jym-xensuspend on 2010-10-24 22:48:21 +0000
 1.1.4.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.1.4.1 06-Jul-2010  uebayasi file balloon.h was added on branch uebayasi-xip on 2010-08-17 06:45:35 +0000
 1.1.2.2 11-Aug-2010  yamt sync with head.
 1.1.2.1 06-Jul-2010  yamt file balloon.h was added on branch yamt-nfs-mp on 2010-08-11 22:52:59 +0000
 1.4 11-Jan-2008  bouyer 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.3 11-Dec-2005  christos branches: 1.3.50; 1.3.56; 1.3.64;
merge ktrace-lwp.
 1.2 09-Mar-2005  bouyer branches: 1.2.4; 1.2.6; 1.2.8;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 18-Jan-2005  bouyer branches: 1.1.2; 1.1.4;
file bus.h was initially added on branch bouyer-xen2.
 1.1.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.2.1 18-Jan-2005  bouyer Add a suitable <machine/bus.h>. Just #include <x86/bus.h> for now.
 1.2.8.1 21-Jan-2008  yamt sync with head
 1.2.6.2 29-Apr-2005  kent sync with -current
 1.2.6.1 09-Mar-2005  kent file bus.h was added on branch kent-audio2 on 2005-04-29 11:28:29 +0000
 1.2.4.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.1 09-Mar-2005  skrll file bus.h was added on branch ktrace-lwp on 2005-04-01 14:29:10 +0000
 1.3.64.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.3.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.3.50.1 23-Mar-2008  matt sync with HEAD
 1.10 14-Feb-2019  cherry Snag the final bits of PV only code to conditionally compile under
-DXENPV

This completes the bifurcation.

The next step is to add -DXENPVHVM code.
 1.9 23-Jan-2008  bouyer branches: 1.9.98;
Merge the bouyer-xeni386 branch. This brings in PAE support to NetBSD xeni386
(domU only). PAE support is enabled by 'options PAE', see the new XEN3PAE_DOMU
and INSTALL_XEN3PAE_DOMU kernel config files.

See the comments in arch/i386/include/{pte.h,pmap.h} to see how it works.
In short, we still handle it as a 2-level MMU, with the second level page
directory being 4 pages in size. pmap switching is done by switching the
L2 pages in the L3 entries, instead of loading %cr3. This is almost required
by Xen, which handle the last L2 page (the one mapping 0xc0000000 - 0xffffffff)
in a very special way. But this approach should also work for native PAE
support if ever supported (in fact, the pmap should almost suport native
PAE, what's missing is bootstrap code in locore.S).
 1.8 22-Nov-2007  bouyer branches: 1.8.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.7 28-Aug-2006  bouyer branches: 1.7.12; 1.7.30; 1.7.32; 1.7.36; 1.7.38;
Some bus_dma(9) fixes for Xen:
- Attempt to gracefully recover from a failed decrease_reservation or
increase_reservation, by avoiding physical memory loss.
- always store a machine address in ds_addr; this avoids some mistakes
where machine address would in some case be freed at physical address, or
mapped as physical address.
 1.6 16-Feb-2006  perry branches: 1.6.2; 1.6.12;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.5 24-Dec-2005  perry branches: 1.5.2; 1.5.4; 1.5.6;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.4 11-Dec-2005  christos merge ktrace-lwp.
 1.3 22-Aug-2005  bouyer branches: 1.3.6;
Rename _PRIVATE_BUS_DMAMEM_ALLOC_RANGE to _BUS_DMAMEM_ALLOC_RANGE for
consistency with other macros defined in bus_private.h. Pointed out by
YAMAMOTO Takashi.
 1.2 20-Aug-2005  bouyer Deal with the machine address space being non-contigous in bus_dmamem_alloc():
- Define _BUS_AVAIL_END to 0xffffffff, as we don't have an easy way to
find the upper bound for our machine address space (and this can change
when we swap pages with the hypervisor).
- implement _xen_bus_dmamem_alloc_range(), which will request a contigous
set of pages to the hypervisor if the pages returned by uvm_pglistalloc()
don't fit the constraints.
We can't deal with the low/high constraints yet, because Xen doesn't offer a
way to get pages in a specific ranges of addresses.

Based on patches from Dave Thompson (in private mail), with heavy hacking
by me.
 1.1 16-Apr-2005  yamt branches: 1.1.2; 1.1.4; 1.1.6;
add files which i forgot to add with arch/x86/x86/bus_dma.c rev.1.21.
 1.1.6.4 04-Feb-2008  yamt sync with head.
 1.1.6.3 07-Dec-2007  yamt sync with head
 1.1.6.2 30-Dec-2006  yamt sync with head.
 1.1.6.1 21-Jun-2006  yamt sync with head.
 1.1.4.2 29-Apr-2005  kent sync with -current
 1.1.4.1 16-Apr-2005  kent file bus_private.h was added on branch kent-audio2 on 2005-04-29 11:28:29 +0000
 1.1.2.5 16-Sep-2006  ghen Pull up following revision(s) (requested by bouyer in ticket #1510):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.7
sys/arch/xen/x86/xen_bus_dma.c: revision 1.8
sys/arch/x86/include/bus_private.h: revision 1.6
sys/arch/x86/x86/bus_dma.c: revision 1.30
sys/arch/xen/include/bus_private.h: revision 1.7
Some bus_dma(9) fixes for Xen:
- Attempt to gracefully recover from a failed decrease_reservation or
increase_reservation, by avoiding physical memory loss.
- always store a machine address in ds_addr; this avoids some mistakes
where machine address would in some case be freed at physical address, or
mapped as physical address.
Wrap some printfs in #ifdef DEBUG, as we should not leak memory any more when
bus_dma memory allocation fails.
 1.1.2.4 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #697):
sys/arch/x86/x86/bus_dma.c: revision 1.23
sys/arch/x86/include/bus_private.h: revision 1.3
sys/arch/xen/include/bus_private.h: revision 1.3
Rename _PRIVATE_BUS_DMAMEM_ALLOC_RANGE to _BUS_DMAMEM_ALLOC_RANGE for
consistency with other macros defined in bus_private.h. Pointed out by
YAMAMOTO Takashi.
 1.1.2.3 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #696):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.1
sys/arch/xen/include/bus_private.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.28
Deal with the machine address space being non-contigous in bus_dmamem_alloc():
- Define _BUS_AVAIL_END to 0xffffffff, as we don't have an easy way to
find the upper bound for our machine address space (and this can change
when we swap pages with the hypervisor).
- implement _xen_bus_dmamem_alloc_range(), which will request a contigous
set of pages to the hypervisor if the pages returned by uvm_pglistalloc()
don't fit the constraints.
We can't deal with the low/high constraints yet, because Xen doesn't offer a
way to get pages in a specific ranges of addresses.
Based on patches from Dave Thompson (in private mail), with heavy hacking
by me.
 1.1.2.2 21-Apr-2005  tron Pull up revision 1.1 (requested by yamt in ticket #175):
add files which i forgot to add with arch/x86/x86/bus_dma.c rev.1.21.
 1.1.2.1 16-Apr-2005  tron file bus_private.h was added on branch netbsd-3 on 2005-04-21 18:43:01 +0000
 1.3.6.2 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.6.1 22-Aug-2005  skrll file bus_private.h was added on branch ktrace-lwp on 2005-11-10 14:00:34 +0000
 1.5.6.1 22-Apr-2006  simonb Sync with head.
 1.5.4.1 09-Sep-2006  rpaulo sync with head
 1.5.2.1 18-Feb-2006  yamt sync with head.
 1.6.12.1 14-Sep-2006  riz Pull up following revision(s) (requested by bouyer in ticket #150):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.7
sys/arch/xen/x86/xen_bus_dma.c: revision 1.8
sys/arch/x86/include/bus_private.h: revision 1.6
sys/arch/x86/x86/bus_dma.c: revision 1.30
sys/arch/xen/include/bus_private.h: revision 1.7
Some bus_dma(9) fixes for Xen:
- Attempt to gracefully recover from a failed decrease_reservation or
increase_reservation, by avoiding physical memory loss.
- always store a machine address in ds_addr; this avoids some mistakes
where machine address would in some case be freed at physical address, or
mapped as physical address.
Wrap some printfs in #ifdef DEBUG, as we should not leak memory any more when
bus_dma memory allocation fails.
 1.6.2.1 03-Sep-2006  yamt sync with head.
 1.7.38.2 18-Feb-2008  mjf Sync with HEAD.
 1.7.38.1 08-Dec-2007  mjf Sync with HEAD.
 1.7.36.1 21-Nov-2007  bouyer Use an appropriate _BUS_AVAIL_END for _LP64
 1.7.32.2 23-Mar-2008  matt sync with HEAD
 1.7.32.1 09-Jan-2008  matt sync with HEAD
 1.7.30.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.7.12.1 03-Dec-2007  ad Sync with HEAD.
 1.8.6.1 13-Jan-2008  bouyer Work in progress on xeni386 PAE support:
Make xeni386 build with a 64bit paddr_t. For this vaddr_t vs paddr_t vs
pointers usages had to be clarified.
If 'options PAE' is present in a Xen3 kernel, switch paddr_t, pd_entry_t
and pt_entry_t to 64bits, and add the PAE entry in the __xen_guest ELF section.
 1.9.98.1 10-Jun-2019  christos Sync with HEAD
 1.29 11-Jan-2008  bouyer 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.28 05-Jan-2008  yamt g/c ci_idle_pcb_paddr
 1.27 04-Jan-2008  yamt i386:
- make tss per-cpu. this considerably speeds up context switch for,
at least, pentium4, where ltr instruction seems very slow.
i386, xen:
- kill cpu_maxproc.
kvm86:
- adapt to per-cpu tss.
- cleanup and simplify.
- move kvm86_mp_lock to more meaningful place.
- disable preemption during a call.
 1.26 01-Jan-2008  yamt try to detect processor resource sharing topologies. ie. package/core/smt IDs.
 1.25 25-Dec-2007  perry Convert many of the uses of __attribute__ to equivalent
__packed, __unused and __dead macros from cdefs.h
 1.24 22-Nov-2007  bouyer branches: 1.24.2; 1.24.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.23 26-Oct-2007  joerg branches: 1.23.2;
Match delay/DELAY on x86 with delay(9). It takes an unsigned int as
argument. Use this and replace the inline assembly (mul + div using the
64bit intermediate result) with normal 32bit multiplication and
division. The compiler can turn the division into a multiplication and
shift, making it even cheaper then the original assembly. For extreme
long delays, just use 64bit arithmetic.
 1.22 17-Oct-2007  garbled 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.21 25-Sep-2007  ad branches: 1.21.2;
ci_astpending is no more.
 1.20 17-May-2007  yamt branches: 1.20.8; 1.20.10; 1.20.12;
merge yamt-idlelwp branch. asked by core@. some ports still needs work.

from doc/BRANCHES:

idle lwp, and some changes depending on it.

1. separate context switching and thread scheduling.
(cf. gmcgarry_ctxsw)
2. implement idle lwp.
3. clean up related MD/MI interfaces.
4. make scheduler(s) modular.
 1.19 12-May-2007  jld Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.18 12-Mar-2007  ad branches: 1.18.2; 1.18.8;
Include sys/simplelock.h, not sys/lock.h.
 1.17 05-Mar-2007  drochner branches: 1.17.2;
clean up how cpus and ioapics are attached at the mainbus:
Seperate "cpubus" and "ioapicbus" -- while they share a common "address
space" (the apic id), the kernel doesn't use this fact. There are different
data passed to cpus and apics, which caused some ugly polymorphism. This
also saves the special "submatch" functions needed to distingush cpus
and ioapics for autoconf. (And it makes that "apid" locators wired
in the kernel configuration are honored now; this allows one to dumb down
an mp box to singleprocessor by userconfig.)
Print "apid" locators in the buses "print" function "as everyone does",
so the per-port cpu drivers don't need to do it.
Being here, constify "struct cpu_functions" and g/c the unused MP_PICMODE
flag.
 1.16 04-Mar-2007  christos Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.15 16-Feb-2007  ad branches: 1.15.2;
Remove spllowersoftclock() and CLKF_BASEPRI(), and always dispatch callouts
via a soft interrupt. In the near future, softclock will be run from process
context.
 1.14 09-Feb-2007  ad Merge newlock2 to head.
 1.13 08-Dec-2006  yamt - pass intrframe by-pointer, not by-value.
- make i386 and xen use per-cpu interrupt stack.

xen part is reviewed by Manuel Bouyer.
 1.12 16-Feb-2006  perry branches: 1.12.14; 1.12.16; 1.12.18; 1.12.20;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.11 24-Dec-2005  perry branches: 1.11.2; 1.11.4; 1.11.6;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.10 11-Dec-2005  christos merge ktrace-lwp.
 1.9 11-Aug-2005  cube Change all archs that did:

#define clockframe somethingelse

to:

struct clockframe {
struct somethingelse cf_se;
};

and change access macros accordingly.

That means that, at least for that very issue, things will not go
ka-boomy if you don't have the actual definition of struct clockframe
before including systm.h.
 1.8 16-Apr-2005  bouyer branches: 1.8.2;
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.7 22-Sep-2004  yamt branches: 1.7.6; 1.7.12;
move some per-cpu data definitions to MI place so that they can be modified
without touching all ports. discussed on tech-kern@.
 1.6 07-May-2004  cl branches: 1.6.2;
fix lazy fpu state saving
 1.5 26-Apr-2004  cl Move struct xen_netinfo into include/xen.h so that xen12load compiles again.
 1.4 26-Apr-2004  cl Rework the physical<->machine memory mapping: offset physical addresses
by 0x100000 (above the I/O Memory "hole") leaving all physical addresses
below unused, don't perform phys<->mach mapping for addresses below 0x100000
or beyond the real hardware's physical memory.

-> /dev/mem works now as expected and X works in domain0.
 1.3 25-Apr-2004  cl Move some of the context switching code into a C function.
 1.2 24-Apr-2004  cl Make kernel command line parsing support additional keywords without
having to change existing code which calls the parser.
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.6 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.6.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.6.2.5 24-Sep-2004  skrll Sync with HEAD.
 1.6.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.6.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.6.2.2 03-Aug-2004  skrll Sync with HEAD
 1.6.2.1 07-May-2004  skrll file cpu.h was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.7.12.1 28-Apr-2005  tron Pull up revision 1.8 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.7.6.1 29-Apr-2005  kent sync with -current
 1.8.2.7 21-Jan-2008  yamt sync with head
 1.8.2.6 07-Dec-2007  yamt sync with head
 1.8.2.5 27-Oct-2007  yamt sync with head.
 1.8.2.4 03-Sep-2007  yamt sync with head.
 1.8.2.3 26-Feb-2007  yamt sync with head.
 1.8.2.2 30-Dec-2006  yamt sync with head.
 1.8.2.1 21-Jun-2006  yamt sync with head.
 1.11.6.1 22-Apr-2006  simonb Sync with head.
 1.11.4.1 09-Sep-2006  rpaulo sync with head
 1.11.2.1 18-Feb-2006  yamt sync with head.
 1.12.20.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.12.18.1 10-Jun-2007  bouyer Pull up following revision(s) (requested by jld in ticket #711):
sys/arch/xen/xen/clock.c: revision 1.35 via patch
sys/arch/xen/include/types.h: revision 1.5 via patch
sys/arch/xen/i386/machdep.c: revision 1.38 via patch
sys/arch/xen/conf/files.xen: revision 1.60 via patch
sys/arch/xen/include/cpu.h: revision 1.19 via patch
Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.12.16.1 10-Dec-2006  yamt sync with head.
 1.12.14.4 28-Jan-2007  ad xen MD changes.
 1.12.14.3 12-Jan-2007  ad Sync with head.
 1.12.14.2 11-Jan-2007  ad Checkpoint work in progress.
 1.12.14.1 29-Dec-2006  ad Checkpoint work in progress.
 1.15.2.4 17-May-2007  yamt sync with head.
 1.15.2.3 26-Mar-2007  yamt adapt xen.
 1.15.2.2 17-Mar-2007  rmind Backport lock.h split into the simplelock.h and other #include changes
from HEAD. This fixes the problems with circular includes.
 1.15.2.1 12-Mar-2007  rmind Sync with HEAD.
 1.17.2.4 03-Dec-2007  ad Sync with HEAD.
 1.17.2.3 09-Oct-2007  ad Sync with head.
 1.17.2.2 27-May-2007  ad Sync with head.
 1.17.2.1 13-Mar-2007  ad Sync with head.
 1.18.8.2 03-Oct-2007  garbled Sync with HEAD
 1.18.8.1 22-May-2007  matt Update to HEAD.
 1.18.2.1 11-Jul-2007  mjf Sync with head.
 1.20.12.1 06-Oct-2007  yamt sync with head.
 1.20.10.3 23-Mar-2008  matt sync with HEAD
 1.20.10.2 09-Jan-2008  matt sync with HEAD
 1.20.10.1 06-Nov-2007  matt sync with HEAD
 1.20.8.3 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.20.8.2 28-Oct-2007  joerg Sync with HEAD.
 1.20.8.1 02-Oct-2007  joerg Sync with HEAD.
 1.21.2.4 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.21.2.3 13-Nov-2007  bouyer Sync with HEAD
 1.21.2.2 26-Oct-2007  bouyer Make amd64, i386 and xen kernels build and work again.
 1.21.2.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.23.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.23.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.24.6.4 06-Jan-2008  bouyer Merge needed changes to genassym.cf and locore.S for xeni386 back to
arch/i386. Switch xeni386 to use the arch/i386 cpu.h.
 1.24.6.3 05-Jan-2008  bouyer Not used anymore since xen/i386 has been switched to xen_pmap_bootstrap()
 1.24.6.2 02-Jan-2008  bouyer Sync with HEAD
 1.24.6.1 11-Dec-2007  bouyer Switch i386 to x86/x86/pmap.c
 1.24.2.1 26-Dec-2007  ad Sync with head.
 1.20 11-Jan-2008  bouyer 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.19 22-Nov-2007  bouyer branches: 1.19.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.18 17-Oct-2007  garbled branches: 1.18.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.17 26-Sep-2007  ad branches: 1.17.2;
x86 changes for pcc and LKMs.

- Replace most inline assembly with proper functions. As a side effect
this reduces the size of amd64 GENERIC by about 120kB, and i386 by a
smaller amount. Nearly all of the inlines did something slow, or something
that does not need to be fast.
- Make curcpu() and curlwp functions proper, unless __GNUC__ && _KERNEL.
In that case make them inlines. Makes curlwp LKM and preemption safe.
- Make bus_space and bus_dma more LKM friendly.
- Share a few more files between the ports.
- Other minor changes.
 1.16 23-Sep-2007  bouyer branches: 1.16.2;
Ajust for Xen 3.1.0 public headers. From Christoph Egger in private mail.
 1.15 04-Mar-2007  christos branches: 1.15.2; 1.15.10; 1.15.18; 1.15.20;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.14 14-Jan-2007  ad branches: 1.14.2;
Update x86_pause() as per i386.
 1.13 15-Oct-2006  bouyer Define x86_mfence(). Use it where appropriate. Seems to fix a hang in
the network interface front-end/back-end for me ...
 1.12 19-Aug-2006  dsl branches: 1.12.2; 1.12.4;
Fix build of machdep.c with -Os and -O3
 1.11 06-Mar-2006  bouyer branches: 1.11.10;
Add x86_sfence() for symetry, which is just a __insn_barrier() here.
 1.10 28-Dec-2005  perry branches: 1.10.4; 1.10.6; 1.10.8;
inline -> __inline
 1.9 24-Dec-2005  perry Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.8 11-Dec-2005  christos merge ktrace-lwp.
 1.7 22-Apr-2005  yamt branches: 1.7.2;
don't use block scope static function decl. found by gcc4.
 1.6 26-Mar-2005  bouyer Don't assume we're on a uniprocessor system (the hypervisor may be running SMP)
and add "lock;" barrier to avoid instruction reordering in event handlings.
This fix the last issue with event loss I was getting on SMP systems.
 1.5 09-Mar-2005  bouyer branches: 1.5.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.4 10-Dec-2004  christos branches: 1.4.2; 1.4.4; 1.4.6;
Move the disable_intr and enable_intr functions from xenfunc.h to cpufunc.h,
because the x86/intr.h needs them and does not include xenfunc.h. Including
xenfunc.h in cpufunc.h is a clear lose because xenfunc.h needs a boatload
of include functions in order to compile.
 1.3 07-May-2004  cl branches: 1.3.2;
replace rdr6/ldr6 with the corresponding hypervisor traps
 1.2 10-Apr-2004  cl Make rcr2() always return 0. We only get cr2's value for page faults and
trap() knows where to find it.
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.3.2.5 18-Dec-2004  skrll Sync with HEAD.
 1.3.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.2.2 03-Aug-2004  skrll Sync with HEAD
 1.3.2.1 07-May-2004  skrll file cpufunc.h was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.4.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.4.4.1 29-Apr-2005  kent sync with -current
 1.4.2.2 18-Jan-2005  bouyer The wbinvd instruction can't be used with xen, it case a kernel trap.
Implement wbinvd() with MMUEXT_FLUSH_CACHE.
Make shared_info_t volatile.
 1.4.2.1 17-Dec-2004  bouyer arch/x86/include/intr.h:spllower() now use read_psl/write_psl to reenable
interrupts if they were enabled at spllower() call.
Change read_psl()/write_psl() to DTRT with
HYPERVISOR_shared_info->vcpu_data[0].evtchn_upcall_mask.
XXX maybe spllower() should use __save_flags/__restore_flags instead.
 1.5.2.4 22-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1652):
sys/arch/xen/include/cpufunc.h: revision 1.13
sys/arch/xen/include/xen3-public/io/ring.h: revision 1.6
Define x86_mfence(). Use it where appropriate. Seems to fix a hang in
the network interface front-end/back-end for me ...
 1.5.2.3 07-Apr-2006  tron branches: 1.5.2.3.2;
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.2 01-May-2005  tron Pull up revision 1.7 (requested by yamt in ticket #233):
don't use block scope static function decl. found by gcc4.
 1.5.2.1 30-Mar-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #71):
Don't assume we're on a uniprocessor system (the hypervisor may be running SMP)
and add "lock;" barrier to avoid instruction reordering in event handlings.
This fix the last issue with event loss I was getting on SMP systems.
 1.5.2.3.2.1 22-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1652):
sys/arch/xen/include/cpufunc.h: revision 1.13
sys/arch/xen/include/xen3-public/io/ring.h: revision 1.6
Define x86_mfence(). Use it where appropriate. Seems to fix a hang in
the network interface front-end/back-end for me ...
 1.7.2.7 21-Jan-2008  yamt sync with head
 1.7.2.6 07-Dec-2007  yamt sync with head
 1.7.2.5 27-Oct-2007  yamt sync with head.
 1.7.2.4 03-Sep-2007  yamt sync with head.
 1.7.2.3 26-Feb-2007  yamt sync with head.
 1.7.2.2 30-Dec-2006  yamt sync with head.
 1.7.2.1 21-Jun-2006  yamt sync with head.
 1.10.8.2 03-Sep-2006  yamt sync with head.
 1.10.8.1 13-Mar-2006  yamt sync with head.
 1.10.6.1 22-Apr-2006  simonb Sync with head.
 1.10.4.1 09-Sep-2006  rpaulo sync with head
 1.11.10.1 27-Aug-2006  riz Pull up following revision(s) (requested by dsl in ticket #69):
sys/arch/xen/include/cpufunc.h: revision 1.12
Fix build of machdep.c with -Os and -O3
 1.12.4.1 22-Oct-2006  yamt sync with head
 1.12.2.2 01-Feb-2007  ad Sync with head.
 1.12.2.1 18-Nov-2006  ad Sync with head.
 1.14.2.1 12-Mar-2007  rmind Sync with HEAD.
 1.15.20.3 23-Mar-2008  matt sync with HEAD
 1.15.20.2 09-Jan-2008  matt sync with HEAD
 1.15.20.1 06-Nov-2007  matt sync with HEAD
 1.15.18.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.15.18.1 02-Oct-2007  joerg Sync with HEAD.
 1.15.10.1 03-Oct-2007  garbled Sync with HEAD
 1.15.2.2 03-Dec-2007  ad Sync with HEAD.
 1.15.2.1 09-Oct-2007  ad Sync with head.
 1.16.2.1 06-Oct-2007  yamt sync with head.
 1.17.2.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.18.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.18.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.19.6.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.6 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.5 17-Oct-2007  garbled branches: 1.5.20; 1.5.34;
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.4 23-Sep-2007  bouyer Add NetBSD RCS Id.
 1.3 20-Apr-2005  bouyer branches: 1.3.2; 1.3.38; 1.3.46; 1.3.56; 1.3.58;
Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.

Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 13-Dec-2004  bouyer branches: 1.1.2; 1.1.4; 1.1.6;
file ctrl_if.h was initially added on branch bouyer-xen2.
 1.1.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.4.1 29-Apr-2005  kent sync with -current
 1.1.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.2.4.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.4.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.1 09-Mar-2005  skrll file ctrl_if.h was added on branch ktrace-lwp on 2005-04-01 14:29:10 +0000
 1.2.2.1 28-Apr-2005  tron Pull up revision 1.3 (requested by bouyer in ticket #193):
Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.
Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.3.58.1 06-Nov-2007  matt sync with HEAD
 1.3.56.1 02-Oct-2007  joerg Sync with HEAD.
 1.3.46.1 03-Oct-2007  garbled Sync with HEAD
 1.3.38.1 09-Oct-2007  ad Sync with head.
 1.3.2.1 27-Oct-2007  yamt sync with head.
 1.5.34.1 01-Nov-2009  jym Sync with HEAD.
 1.5.20.1 19-Aug-2009  yamt sync with head.
 1.4 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 24-Apr-2004  cl branches: 1.3.2; 1.3.6; 1.3.8; 1.3.10;
Let event_set_handler set the void * argument to event handlers.
 1.2 17-Apr-2004  cl - fix event dispatching for event 0
- use struct trapframe instead of struct pt_regs
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.8.1 29-Apr-2005  kent sync with -current
 1.3.6.1 18-Jan-2005  bouyer Remove unused files.
 1.3.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.3.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.2.2 03-Aug-2004  skrll Sync with HEAD
 1.3.2.1 24-Apr-2004  skrll file events.h was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.32 07-May-2020  bouyer Change event_set_handler() to take the target CPU parameter. If ci is NULL,
event_set_handler() will choose the CPU and bind the event.
If ci is not NULL the caller is responsible for binding the event.
Use a IPI xcall to register the handlers if needed.
pull in a hack from x86 to force pirq handlers to be mpsafe if registered at
a level != IPL_VM. This is for the com at isa interrupt handler, which
registers at IPL_HIGH and has to way to tell it's mpsafe (taking
KERNEL_LOCK at IPL_HIGH causes deadlocks on MP systems).
 1.31 04-May-2020  jdolecek add support for using MSI for XenPV Dom0

use PHYSDEVOP_map_pirq to get the pirq/gsi for MSI/MSI-X, switch also INTx
to use it instead of PHYSDEVOP_alloc_irq_vector

MSI confirmed working with single-vector MSI for wm(4), ahcisata(4), bge(4)

XXX added some provision for MSI-X, but it doesn't actually work (no interrupts
delivered), needs some further investigation; disable MSI-X for XENPV
via flag in x86/pci/pci_machdep.c
 1.30 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.29 13-Apr-2020  bouyer By default, events are bound to CPU 0 (exept for IPIs and VTIMERs which
are bound to a different CPU at creation time).
Recent MI changes caused the scheduler to choose a different CPU when
probing and attaching xennet devices (I guess it's the xenbus thread which
runs on a different CPU). This cause the callback to be called on a different
CPU than the one expected by the kernel, and the event is ignored.
It is handled when the clock causes the callback to be called on the right
CPU, which is why xennet still run, but slowly.

Change event_set_handler() to do a EVTCHNOP_bind_vcpu if requested to,
and make sure we don't do it for IPIs and VIRQs (for theses, the op fails).
 1.28 06-Apr-2020  jdolecek branches: 1.28.2;
add known_mpsafe parameter also to pirq_establish(), and pass the parameter
to underlying event_set_handler()
 1.27 06-Apr-2020  jdolecek remove restriction on interrupt level for MP-safe interrupt handlers
 1.26 24-Jun-2018  jdolecek branches: 1.26.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.25 04-Nov-2017  cherry branches: 1.25.2;
Retire xen/x86/intr.c and use the new xen specific glue in x86/x86/intr.c

The purpose of this change is to expose the x86/include/intr.h API
to drivers. Specifically the following functions:

void *intr_establish_xname(...);
void *intr_establish(...);
void intr_disestablish(...);

while maintaining the old API from xen/include/evtchn.h, specifically
the following functions:

int event_set_handler(...);
int event_remove_handler(...);

This is so that if things break, we can keep using the old API until
everything stabilises. This is a stepping stone towards getting the
actual XEN event callback path rework code in place - which can be
done opaquely behind the intr.h API - NetBSD/XEN specific drivers that
have been ported to the intr.h API should then work without
significant further modifications.
 1.24 16-Jul-2017  cherry branches: 1.24.2;
Add a glue function to inspect event channel<->legacy IRQ mappings.
This is towards getting xen to use more x86/ "native" code.
 1.23 14-Mar-2015  bouyer Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.22 12-Jan-2013  bouyer branches: 1.22.12; 1.22.14;
Back out this commit:
http://mail-index.netbsd.org/source-changes/2012/12/28/msg039950.html
which cause a panic when running tests on amd64, as shown on:
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/
(i386 hangs for unrelated reasons).
 1.21 28-Dec-2012  cherry Simplify the xen event handler callback by:
- moving the interrupt handler callback traversal into a separate
function.
- using evt_iterate_bits() to scan through the pending bitfield
- removing cross-cpu pending actions - events recieved on the wrong
vcpu are re-routed via hypervisor_send_event().
- simplifying nested while() loops by encapsulating them in
equivalent functions.

Many thanks for multiple reviews by bouyer@ and jym@
 1.20 20-Sep-2011  jym branches: 1.20.8; 1.20.12;
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.19 11-Aug-2011  cherry Make event/interrupt handling MP aware
 1.18 23-Oct-2009  snj branches: 1.18.10;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.17 24-Oct-2008  jym branches: 1.17.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.16 01-Jul-2008  bouyer branches: 1.16.2;
Raise ci_idepth (and switch to interrupt stack on i386) becore calling
xenevt_event().
 1.15 19-Feb-2008  bouyer branches: 1.15.6; 1.15.10; 1.15.12; 1.15.14;
Fix xenevt to not call softint_schedule() above IPL_HIGH:
Register a ipl callback for IPL_HIGH.
if the current ipl level is too high, just record the event in a bitmap,
and record IPL_HIGH as pending. The callback will process the pending events.
 1.14 12-Dec-2007  bouyer cleanup the debug event handler to not use the IPL system at all. Fix
debug event storm on XEN2.
 1.13 08-Dec-2006  yamt branches: 1.13.24; 1.13.32; 1.13.34; 1.13.36;
- pass intrframe by-pointer, not by-value.
- make i386 and xen use per-cpu interrupt stack.

xen part is reviewed by Manuel Bouyer.
 1.12 28-Sep-2006  bouyer Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.11 11-Dec-2005  christos branches: 1.11.20; 1.11.22;
merge ktrace-lwp.
 1.10 10-Sep-2005  bouyer The line number in the PCI interrupt register can be larger than 31 on
some hardware, depending on how Xen set things up. So bump NR_PIRQ from 32
to 256.
 1.9 28-Apr-2005  yamt branches: 1.9.2;
rename do_event to evtchan_do_event.
the former is too generic name and it actually hides a bug in xennetback.
 1.8 20-Apr-2005  bouyer Event handling optimisations:
- sort the ih_evt_handler list by IPL, higher first. Otherwise some handlers
would have been delayed, event if they could run at the current IPL.
- As ih_evt_handler is sorted, remove IPLs that have been processed for
an event when calling hypervisor_set_ipending()
- In hypervisor_set_ipending(), enter the event in ipl_evt_mask only
for the lowest IPL. As deffered IPLs are processed high to low,
this ensure that hypervisor_enable_event() will be called only when all
callbacks have been called for an event. We don't need the evtch_maskcount[]
counters any more.

Thanks to YAMAMOTO Takashi for ideas and feedback.
 1.7 18-Apr-2005  yamt don't unmask an event channel until all interrupts for it are served.
fix problems with shared (physical) interrupts.
 1.6 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.5 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4 11-Apr-2005  yamt fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.3 11-Apr-2005  yamt don't stamp soft interrupts.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 13-Dec-2004  bouyer branches: 1.1.2; 1.1.4; 1.1.6;
file evtchn.h was initially added on branch bouyer-xen2.
 1.1.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.4.1 29-Apr-2005  kent sync with -current
 1.1.2.4 08-Mar-2005  bouyer Add infrastructure to unregsiter event callbacks.
 1.1.2.3 21-Jan-2005  bouyer - Add a centralised handler for physical interrupts, which will call the
real handler. This is not necessery but is usefull for debug.
- make irq_needs_unmask_notify[] per-IRQ, instead of per physical IRQ
- use a static physdev_op_t for PHYSDEVOP_IRQ_UNMASK_NOTIFY (it never changes,
so no need to allocate a new one each time)
- call pirq_notify() from hypervisor_enable_irq().
 1.1.2.2 18-Jan-2005  bouyer Implement physical IRQ mapping.
 1.1.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.2.4.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.4.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.1 09-Mar-2005  skrll file evtchn.h was added on branch ktrace-lwp on 2005-04-01 14:29:10 +0000
 1.2.2.8 14-Sep-2005  tron Pull up following revision(s) (requested by bouyer in ticket #777):
sys/arch/xen/include/evtchn.h: revision 1.10
The line number in the PCI interrupt register can be larger than 31 on
some hardware, depending on how Xen set things up. So bump NR_PIRQ from 32
to 256.
 1.2.2.7 01-May-2005  tron Pull up revision 1.9 (requested by yamt in ticket #238):
rename do_event to evtchan_do_event.
the former is too generic name and it actually hides a bug in xennetback.
 1.2.2.6 28-Apr-2005  tron Pull up revision 1.8 (requested by bouyer in ticket #192):
Event handling optimisations:
- sort the ih_evt_handler list by IPL, higher first. Otherwise some handlers
would have been delayed, event if they could run at the current IPL.
- As ih_evt_handler is sorted, remove IPLs that have been processed for
an event when calling hypervisor_set_ipending()
- In hypervisor_set_ipending(), enter the event in ipl_evt_mask only
for the lowest IPL. As deffered IPLs are processed high to low,
this ensure that hypervisor_enable_event() will be called only when all
callbacks have been called for an event. We don't need the evtch_maskcount[]
counters any more.
Thanks to YAMAMOTO Takashi for ideas and feedback.
 1.2.2.5 28-Apr-2005  tron Pull up revision 1.7 (requested by bouyer in ticket #192):
don't unmask an event channel until all interrupts for it are served.
fix problems with shared (physical) interrupts.
 1.2.2.4 28-Apr-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.2.2.3 28-Apr-2005  tron Pull up revision 1.5 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2.2.2 13-Apr-2005  tron Pull up revision 1.4 (requested by yamt in ticket #146):
fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.2.2.1 13-Apr-2005  tron Pull up revision 1.3 (requested by yamt in ticket #143):
don't stamp soft interrupts.
 1.9.2.4 27-Feb-2008  yamt sync with head.
 1.9.2.3 21-Jan-2008  yamt sync with head
 1.9.2.2 30-Dec-2006  yamt sync with head.
 1.9.2.1 21-Jun-2006  yamt sync with head.
 1.11.22.2 10-Dec-2006  yamt sync with head.
 1.11.22.1 22-Oct-2006  yamt sync with head
 1.11.20.2 12-Jan-2007  ad Sync with head.
 1.11.20.1 18-Nov-2006  ad Sync with head.
 1.13.36.1 13-Dec-2007  bouyer cleanup the way debug event is handled: make it bypass the IPL system
completely, it's called by shortcuts in the normal path because we want it to
be always called, even if the IPL is high.
Fix debug even recursion on XEN2
 1.13.34.1 13-Dec-2007  yamt sync with head.
 1.13.32.1 26-Dec-2007  ad Sync with head.
 1.13.24.2 23-Mar-2008  matt sync with HEAD
 1.13.24.1 09-Jan-2008  matt sync with HEAD
 1.15.14.1 03-Jul-2008  simonb Sync with head.
 1.15.12.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.15.10.2 11-Mar-2010  yamt sync with head
 1.15.10.1 04-May-2009  yamt sync with head.
 1.15.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.15.6.1 02-Jul-2008  mjf Sync with HEAD.
 1.16.2.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.17.8.4 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.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.17.8.2 01-Nov-2009  jym Sync with HEAD.
 1.17.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.18.10.3 17-Aug-2011  cherry Pullup relevant changes from -current
 1.18.10.2 04-Aug-2011  cherry first cut at per-cpu event handling
 1.18.10.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.20.12.1 03-Dec-2017  jdolecek update from HEAD
 1.20.8.1 14-Apr-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1278):
sys/arch/xen/include/evtchn.h: revision 1.23
sys/arch/xen/xen/evtchn.c: revision 1.71
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.17
Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.22.14.2 28-Aug-2017  skrll Sync with HEAD
 1.22.14.1 06-Apr-2015  skrll Sync with HEAD
 1.22.12.1 18-Mar-2015  snj Pull up following revision(s) (requested by bouyer in ticket #618):
sys/arch/xen/include/evtchn.h: revision 1.23
sys/arch/xen/xen/evtchn.c: revision 1.71
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.17
Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.24.2.2 16-Jul-2017  cherry 2752365
 1.24.2.1 16-Jul-2017  cherry file evtchn.h was added on branch perseant-stdc-iso10646 on 2017-07-16 05:03:37 +0000
 1.25.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.26.2.3 21-Apr-2020  martin Sync with HEAD
 1.26.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.26.2.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.28.2.3 20-Apr-2020  bouyer Misc fixes after merge
 1.28.2.2 20-Apr-2020  bouyer Sync with HEAD
 1.28.2.1 19-Apr-2020  bouyer Add a struct pic * member to struct intrhand.
This will be used for interrupt_get_count()
For Xen remplace pic_type with a pointer to the pic, and add a pointer
to intrhand, in struct pintrhand
Make event_set_handler return the pointer to struct intrhand.
Don't allocate a fake intrhand in xen_intr_establish_xname(), use the
one returned by event_set_handler().
 1.6 11-Jan-2008  bouyer 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.5 21-Nov-2006  yamt branches: 1.5.28; 1.5.34; 1.5.42;
include the i386 frameasm.h rather than inlining it.
 1.4 15-Jan-2006  bouyer branches: 1.4.18; 1.4.20;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.3 11-Dec-2005  christos branches: 1.3.2;
merge ktrace-lwp.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 11-Mar-2004  cl branches: 1.1.4; 1.1.8; 1.1.12; 1.1.14;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.14.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.12.1 29-Apr-2005  kent sync with -current
 1.1.8.2 21-Jan-2005  bouyer Redefine __HYPERVISOR_physdev_op in frameasm.h, so that we can use the
symbolic name in assembly.
 1.1.8.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.1.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file frameasm.h was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.2.4.3 21-Jan-2008  yamt sync with head
 1.2.4.2 30-Dec-2006  yamt sync with head.
 1.2.4.1 21-Jun-2006  yamt sync with head.
 1.2.2.1 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.3.2.1 01-Feb-2006  yamt sync with head.
 1.4.20.1 10-Dec-2006  yamt sync with head.
 1.4.18.1 12-Jan-2007  ad Sync with head.
 1.5.42.1 09-Jan-2008  bouyer Move Xen bits to i386/include/frameasm.h and add CLI/STI macros for
i386. Make native i386 kernels build again.
 1.5.34.1 18-Feb-2008  mjf Sync with HEAD.
 1.5.28.1 23-Mar-2008  matt sync with HEAD
 1.11 05-Apr-2020  jdolecek remove xengnt_grant_transfer() and xengnt_revoke_transfer(), nothing
uses it any more
 1.10 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.9 07-Dec-2011  cegger branches: 1.9.48;
switch from xen3-public to xen-public.
 1.8 20-Sep-2011  jym branches: 1.8.2; 1.8.6;
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.7 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.6 30-Oct-2008  cegger branches: 1.6.6;
make this header compile standalone
 1.5 24-Oct-2008  jym branches: 1.5.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.4 22-Nov-2007  bouyer branches: 1.4.14; 1.4.18; 1.4.24;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.3 17-Oct-2007  garbled branches: 1.3.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.2 05-Sep-2007  bouyer branches: 1.2.4;
Switch to dynamically-sized grant tables, available since Xen-3.1.0.
This removes a hard limit that would prevent a guest from running with more
than 7 virtual network interface.
If running on a hypervisor that doesn't support GNTTABOP_query_size, fall back
to a 4-pages grant table, so this change is backward-compatible.
 1.1 06-Mar-2006  bouyer branches: 1.1.4; 1.1.8; 1.1.10; 1.1.16; 1.1.22; 1.1.26; 1.1.32; 1.1.40; 1.1.42; 1.1.46; 1.1.50; 1.1.52;
Implement granttables operations.
 1.1.52.2 09-Jan-2008  matt sync with HEAD
 1.1.52.1 06-Nov-2007  matt sync with HEAD
 1.1.50.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.1.50.1 02-Oct-2007  joerg Sync with HEAD.
 1.1.46.1 10-Sep-2007  skrll Sync with HEAD.
 1.1.42.1 23-Sep-2007  wrstuden Sync with somewhat-recent netbsd-4.
 1.1.40.1 03-Oct-2007  garbled Sync with HEAD
 1.1.32.2 03-Dec-2007  ad Sync with HEAD.
 1.1.32.1 09-Oct-2007  ad Sync with head.
 1.1.26.1 11-Sep-2007  xtraeme Pull up following revision(s) (requested by bouyer in ticket #875):
sys/arch/xen/include/granttables.h: revision 1.2
sys/arch/xen/include/xen3-public/grant_table.h: revision 1.4
sys/arch/xen/xen/xengnt.c: revision 1.3
Switch to dynamically-sized grant tables, available since Xen-3.1.0.
This removes a hard limit that would prevent a guest from running with more
than 7 virtual network interface.
If running on a hypervisor that doesn't support GNTTABOP_query_size, fall back
to a 4-pages grant table, so this change is backward-compatible.
 1.1.22.2 09-Sep-2006  rpaulo sync with head
 1.1.22.1 06-Mar-2006  rpaulo file granttables.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.1.16.4 07-Dec-2007  yamt sync with head
 1.1.16.3 27-Oct-2007  yamt sync with head.
 1.1.16.2 21-Jun-2006  yamt sync with head.
 1.1.16.1 06-Mar-2006  yamt file granttables.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:15 +0000
 1.1.10.2 22-Apr-2006  simonb Sync with head.
 1.1.10.1 06-Mar-2006  simonb file granttables.h was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.1.8.3 15-Oct-2007  riz Pull up following revision(s) (requested by bouyer in ticket #1836):
sys/arch/xen/include/granttables.h: revision 1.2 via patch
sys/arch/xen/include/xen3-public/grant_table.h: revision 1.4 via patch
sys/arch/xen/xen/xengnt.c: revision 1.3 via patch
Switch to dynamically-sized grant tables, available since Xen-3.1.0.
This removes a hard limit that would prevent a guest from running with more
than 7 virtual network interface.
If running on a hypervisor that doesn't support GNTTABOP_query_size, fall back
to a 4-pages grant table, so this change is backward-compatible.
 1.1.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.1.8.1 06-Mar-2006  tron file granttables.h was added on branch netbsd-3 on 2006-04-07 12:51:25 +0000
 1.1.4.2 13-Mar-2006  yamt sync with head.
 1.1.4.1 06-Mar-2006  yamt file granttables.h was added on branch yamt-pdpolicy on 2006-03-13 09:07:03 +0000
 1.2.4.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.3.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.4.24.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.4.18.2 11-Mar-2010  yamt sync with head
 1.4.18.1 04-May-2009  yamt sync with head.
 1.4.14.1 17-Jan-2009  mjf Sync with HEAD.
 1.5.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.6.6.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.6.6.2 01-Nov-2009  jym Sync with HEAD.
 1.6.6.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.8.6.1 18-Feb-2012  mrg merge to -current.
 1.8.2.1 17-Apr-2012  yamt sync with head
 1.9.48.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.9.48.1 10-Jun-2019  christos Sync with HEAD
 1.3 11-Jan-2008  bouyer 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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.8; 1.2.12; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file hypercalls.h was initially added on branch bouyer-xenamd64.
 1.1.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file hypercalls.h was added on branch matt-armv6 on 2008-01-09 01:50:06 +0000
 1.2.12.2 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.12.1 05-Jan-2008  bouyer Fix HYPERVISOR_update_va_mapping() for XEN2 (which was unused until now).
Make XEN2 kernels boot.
 1.2.8.3 21-Jan-2008  yamt sync with head
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file hypercalls.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:09 +0000
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file hypercalls.h was added on branch vmlocking on 2007-12-03 19:04:36 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file hypercalls.h was added on branch jmcneill-pm on 2007-11-27 19:36:06 +0000
 1.60 16-Jul-2024  riastradh xen: Don't hotpatch away LOCK prefix in xen_mb, even on UP boots.

Both xen_mb and membar_sync are designed to provide store-before-load
ordering, but xen_mb has to provide it in synchronizing guest with
hypervisor, while membar_sync only has to provide it in synchronizing
one (guest) CPU with another (guest) CPU.

It is safe to hotpatch away the LOCK prefix in membar_sync on a
uniprocessor boot because membar_sync is only designed to coordinate
between normal memory on multiple CPUs, and is never necessary when
there's only one CPU involved.

But xen_mb is used to coordinate between the guest and the `device'
implemented by a hypervisor, which might be running on another
_physical_ CPU even if the NetBSD guest only sees one `CPU', i.e.,
one _virtual_ CPU. So even on `uniprocessor' boots, xen_mb must
still issue an instruction with store-before-load ordering on
multiprocessor systems, such as a LOCK ADD (or MFENCE, but MFENCE is
costlier for no benefit here).

No need to change xen_wmb (release ordering, load/store-before-store)
or xen_rmb (acquire ordering, load-before-load/store) because every
x86 store is a store-release and every x86 load is a load-acquire,
even on multiprocessor systems, so there's no hotpatching involved
anyway.

PR kern/57199
 1.59 16-Oct-2023  bouyer branches: 1.59.6;
Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.
 1.58 16-Oct-2023  bouyer Xen's start_info_t is larger than 512 bytes these days, so bump the copy
size to a whole PAGE_SIZE, and CTASSERT() that start_info_t is smaller.
Luckily we didn't use yet the parts skipped
 1.57 25-Feb-2023  riastradh xen/hypervisor.h: Nix trailing whitespace.

No functional change intended.
 1.56 25-Feb-2023  riastradh 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
 1.55 07-Sep-2022  knakahara branches: 1.55.4;
NetBSD/x86: Raise the number of interrupt sources per CPU from 32 to 56.

There has been no objection for three years.
https://mail-index.netbsd.org/port-amd64/2019/09/22/msg003012.html
Implemented by nonaka@n.o, updated by me.
 1.54 25-May-2022  bouyer PVH and HVM guests can easily have more than XEN_LEGACY_MAX_VCPUS (32) cpus.
Support up to HVM_MAX_VCPUS (256). This requires resizing a few arrays in
evtchn.c, and using
VCPUOP_register_vcpu_info for vcpuid >= XEN_LEGACY_MAX_VCPUS
Tested with 96 vCPUs.
 1.53 19-May-2022  bouyer Restore de EOI mechanism for pirq, using the newer hypervisor interface.
It is needed.
Hopefully fixes kern/56291, kern/56793, kern/55667
 1.52 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.51 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.50 21-Apr-2020  jdolecek convert to newer HYPERVISOR_physdev_op() interface, now command and the
arg are separate arguments - this is needed for newer physdev_op commands

remove code for PHYSDEVOP_IRQ_UNMASK_NOTIFY, it is obsolete since
interface version 0x00030202 and is unsupported by newer versions of Xen

confirmed working on amd64 Dom0, i386 compile-tested only
 1.49 04-Feb-2019  cherry branches: 1.49.10;
Bump up XEN source API compatibility to 0x00030208 from 0x00030201,

but maintain backwards source API compilation compatibility.

ie; sources with config(5)
options __XEN_INTERFACE_VERSION__=0x00030201 # Xen 3.1 interface

should compile and run without problems.

Not that API version 0x00030201 is the lowest version we support now.
 1.48 02-Feb-2019  cherry Remove mb(), rmb() and wmb() from the kernel namespace.

These are introduced by external/bsd/common/include/asm/barrier.h

The purpose of barrier.h is to bridge the use of linux API calls
within code which uses them, such as drm code. The XEN api implicitly
uses these calls which are linuxisms within io/ring.h

This diff undos the damage.

The correct fix is to modify io/ring.h to not assume that all OSs that
XEN runs on has these functions, and to appropriately conditionally via
#ifdef __NetBSD__/#endif use the appropriate NetBSD functions. These
changes then need to be pushed upstream.
 1.47 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.46 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.45 23-Sep-2018  cherry Encapsulate pre-processing of registered (pirq,evtchn) pair in
preparation for API reorg.
 1.44 14-Jun-2014  pgoyette branches: 1.44.26; 1.44.28;
Define a macro to check hypervisor version. OK cherry@
 1.43 13-Jan-2013  bouyer branches: 1.43.10;
Re-apply
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039125.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039126.html
they're not involved in i386 domU hang shown by ATF.
 1.42 12-Jan-2013  bouyer Revert these commits from november 2012:
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039125.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039126.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039142.html

they cause a i386PAE domU to hang while running ATF tests, as shown in
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/

(we should pay more attention to test results, myself first).
 1.41 12-Jan-2013  bouyer Back out this commit:
http://mail-index.netbsd.org/source-changes/2012/12/28/msg039950.html
which cause a panic when running tests on amd64, as shown on:
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/
(i386 hangs for unrelated reasons).
 1.40 28-Dec-2012  cherry Simplify the xen event handler callback by:
- moving the interrupt handler callback traversal into a separate
function.
- using evt_iterate_bits() to scan through the pending bitfield
- removing cross-cpu pending actions - events recieved on the wrong
vcpu are re-routed via hypervisor_send_event().
- simplifying nested while() loops by encapsulating them in
equivalent functions.

Many thanks for multiple reviews by bouyer@ and jym@
 1.39 25-Nov-2012  cherry Make hypervisor_set_ipending() and its consumers cpu unaware. This syncs syntax with semantics
 1.38 17-Feb-2012  bouyer branches: 1.38.2;
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
 1.37 17-Feb-2012  bouyer Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.

2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.

To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.

to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.

While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
 1.36 07-Dec-2011  cegger branches: 1.36.2;
switch from xen3-public to xen-public.
 1.35 19-Nov-2011  cherry branches: 1.35.4;
[merging from cherry-xenmp] bring in bouyer@'s changes via:
http://mail-index.netbsd.org/source-changes/2011/10/22/msg028271.html
From the Log:
Log Message:
Various interrupt fixes, mainly:
keep a per-cpu mask of enabled events, and use it to get pending events.
A cpu-specific event (all of them at this time) should not be ever masked
by another CPU, because it may prevent the target CPU from seeing it
(the clock events all fires at once for example).
 1.34 06-Nov-2011  cherry [merging from cherry-xenmp] Make the xen MMU op queue locking api private. Implement per-cpu queues.
 1.33 20-Sep-2011  jym branches: 1.33.2;
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.32 10-Aug-2011  cherry refactor the bitstring/mask operations to be behind an API. Make pending interrupt marking cpu aware.
 1.31 19-Oct-2009  bouyer branches: 1.31.10;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.30 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.29 13-Nov-2008  cegger branches: 1.29.4;
Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.28 13-Nov-2008  cegger prepare move to new interface
 1.27 16-Sep-2008  bouyer branches: 1.27.2;
Implement the arch-dependent p2m frame lists list. This adds support for
'xm dump-core' for NetBSD domUs.
From Jean-Yves Migeon (jean-yves dot migeon at espci dot fr)
 1.26 05-Sep-2008  tron Compile NetBSD/amd64 kernels with "-Wextra". Patches contributed by
Juan RP in PR port-amd64/39266.
 1.25 14-Apr-2008  cegger branches: 1.25.4; 1.25.6; 1.25.10;
- use POSIX integer types
- ansify functions
 1.24 22-Nov-2007  bouyer branches: 1.24.14;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.23 17-Oct-2006  bouyer branches: 1.23.8; 1.23.26; 1.23.28; 1.23.32; 1.23.34;
XEN_NO_HYPERCALLPAGE is redundant with XEN_COMPAT_030001; make
XEN_COMPAT_030001 disable the hypercall page too and remove
XEN_NO_HYPERCALLPAGE
 1.22 24-Sep-2006  bouyer The Xen folks says everyone should use hypercall call page now, make it so.
Old hypercall call method still still available with
options XEN_NO_HYPERCALLPAGE
but this is disabled by default (xen-3.0.2-2 supports hypercall call page
just fine).
While there add a VIRT_BASE= string in __xen_guest section; from
Bastian Blank on port-xen@.
 1.21 24-Sep-2006  bouyer Factor out some hypercall code in macros, so that it's easier to change
the way hypercalls are called.
 1.20 06-Mar-2006  bouyer branches: 1.20.12; 1.20.14;
Correct arguments for __HYPERVISOR_set_timer_op
 1.19 16-Feb-2006  perry branches: 1.19.2;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.18 15-Jan-2006  bouyer branches: 1.18.2; 1.18.4;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.17 24-Dec-2005  perry branches: 1.17.2;
__asm__ -> __asm
__const__ -> const
__inline__ -> inline
__volatile__ -> volatile
 1.16 11-Dec-2005  christos merge ktrace-lwp.
 1.15 20-Sep-2005  bouyer inline 2 trivial functions that are called often (according to profiling
data).
 1.14 11-May-2005  yamt branches: 1.14.2;
add no_instrument_function attribute to hypervisor_force_callback
because it's used from mcount. kernel profiling now works.
 1.13 20-Apr-2005  bouyer Event handling optimisations:
- sort the ih_evt_handler list by IPL, higher first. Otherwise some handlers
would have been delayed, event if they could run at the current IPL.
- As ih_evt_handler is sorted, remove IPLs that have been processed for
an event when calling hypervisor_set_ipending()
- In hypervisor_set_ipending(), enter the event in ipl_evt_mask only
for the lowest IPL. As deffered IPLs are processed high to low,
this ensure that hypervisor_enable_event() will be called only when all
callbacks have been called for an event. We don't need the evtch_maskcount[]
counters any more.

Thanks to YAMAMOTO Takashi for ideas and feedback.
 1.12 16-Apr-2005  bouyer Remove functions that just call another one.
Inline a function used at only one place.
 1.11 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.10 09-Mar-2005  bouyer branches: 1.10.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.9 10-Dec-2004  christos branches: 1.9.2; 1.9.4; 1.9.6;
Just when I thought it was all working... Unfortunately cpu.h defines
clockframe -> intrframe, but that is included too late, because this
file includes systm.h and it is in the path of including systm.h. Fix
it by not including <systm.h>; it was only needed for the panic() calls
which I have disabled, since they look more like debugging calls to me.
Also add forward struct declaration for trapframe.
 1.8 14-Jun-2004  cl branches: 1.8.2;
cleanup low-level bit mangling code
 1.7 26-Apr-2004  cl Rework the physical<->machine memory mapping: offset physical addresses
by 0x100000 (above the I/O Memory "hole") leaving all physical addresses
below unused, don't perform phys<->mach mapping for addresses below 0x100000
or beyond the real hardware's physical memory.

-> /dev/mem works now as expected and X works in domain0.
 1.6 25-Apr-2004  cl Add memory barriers.
 1.5 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.4 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.3 17-Apr-2004  cl - fix event dispatching for event 0
- use struct trapframe instead of struct pt_regs
 1.2 10-Apr-2004  cl - fix signatures for HYPERVISOR_stop, HYPERVISOR_dom0_op and
HYPERVISOR_block_io_op
- add error checking for HYPERVISOR_mmu_update and
HYPERVISOR_update_va_mapping
- add missing memory barriers
- initialize dom0_op->interface_version
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.2 17-Jun-2004  tron Pull up revision 1.8 (requested by cl in ticket #498):
cleanup low-level bit mangling code
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.7 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.8.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.8.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.8.2.5 18-Dec-2004  skrll Sync with HEAD.
 1.8.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.8.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.8.2.2 03-Aug-2004  skrll Sync with HEAD
 1.8.2.1 14-Jun-2004  skrll file hypervisor.h was added on branch ktrace-lwp on 2004-08-03 10:43:10 +0000
 1.9.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.9.4.1 29-Apr-2005  kent sync with -current
 1.9.2.3 18-Jan-2005  bouyer snapshot of work in progress on physical devices support:
- support pci at hypervisor. Attach one PCI bus for each bus returned by
PHYSDEVOP_PCI_PROBE_ROOT_BUSES (it looks like xen hides ppb bridges from
the guest OS).
- implement pci_conf_read()/pci_conf_write() using the appropriate
PHYSDEVOP_PCI_* calls.
- call PHYSDEVOP_PCI_INITIALISE_DEVICE from pci_intr_map() to make xen
do interrupts routing if needed, and map the hardware interrupt to
a xen interrupt.
- add pci and associated devices to files.xen

This is enouth to have IDE controllers work in PIO mode.
TODO: bus_dma support (currently bus_dma won't translate pseudo-physical
addresses to machine addresses), ISA support, test memory-mapped
I/O registers.
 1.9.2.2 17-Dec-2004  bouyer Merge in changes between netbsd-2-0-RELEASE and 1.9.
 1.9.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.10.2.6 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.10.2.5 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1085):
sys/arch/xen/xen/hypervisor.c: revision 1.17
sys/arch/xen/i386/hypervisor_machdep.c: revision 1.13
sys/arch/xen/include/hypervisor.h: revision 1.15
inline 2 trivial functions that are called often (according to profiling
data).
 1.10.2.4 22-May-2005  snj Pull up revision 1.14 (requested by yamt in ticket #315):
add no_instrument_function attribute to hypervisor_force_callback
because it's used from mcount. kernel profiling now works.
 1.10.2.3 28-Apr-2005  tron Pull up revision 1.13 (requested by bouyer in ticket #192):
Event handling optimisations:
- sort the ih_evt_handler list by IPL, higher first. Otherwise some handlers
would have been delayed, event if they could run at the current IPL.
- As ih_evt_handler is sorted, remove IPLs that have been processed for
an event when calling hypervisor_set_ipending()
- In hypervisor_set_ipending(), enter the event in ipl_evt_mask only
for the lowest IPL. As deffered IPLs are processed high to low,
this ensure that hypervisor_enable_event() will be called only when all
callbacks have been called for an event. We don't need the evtch_maskcount[]
counters any more.
Thanks to YAMAMOTO Takashi for ideas and feedback.
 1.10.2.2 28-Apr-2005  tron Pull up revision 1.12 (requested by bouyer in ticket #192):
Remove functions that just call another one.
Inline a function used at only one place.
 1.10.2.1 28-Apr-2005  tron Pull up revision 1.11 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.14.2.3 07-Dec-2007  yamt sync with head
 1.14.2.2 30-Dec-2006  yamt sync with head.
 1.14.2.1 21-Jun-2006  yamt sync with head.
 1.17.2.2 18-Feb-2006  yamt sync with head.
 1.17.2.1 01-Feb-2006  yamt sync with head.
 1.18.4.1 22-Apr-2006  simonb Sync with head.
 1.18.2.1 09-Sep-2006  rpaulo sync with head
 1.19.2.1 13-Mar-2006  yamt sync with head.
 1.20.14.1 22-Oct-2006  yamt sync with head
 1.20.12.1 18-Nov-2006  ad Sync with head.
 1.23.34.1 08-Dec-2007  mjf Sync with HEAD.
 1.23.32.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.23.28.1 09-Jan-2008  matt sync with HEAD
 1.23.26.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.23.8.1 03-Dec-2007  ad Sync with HEAD.
 1.24.14.3 17-Jan-2009  mjf Sync with HEAD.
 1.24.14.2 28-Sep-2008  mjf Sync with HEAD.
 1.24.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.25.10.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.25.10.1 19-Oct-2008  haad Sync with HEAD.
 1.25.6.2 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.25.6.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.25.4.3 11-Mar-2010  yamt sync with head
 1.25.4.2 19-Aug-2009  yamt sync with head.
 1.25.4.1 04-May-2009  yamt sync with head.
 1.27.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.29.4.4 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.29.4.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.29.4.2 01-Nov-2009  jym Sync with HEAD.
 1.29.4.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.31.10.3 18-Sep-2011  cherry Use an IPI to re-route events to the cpu where the handler has been registered
 1.31.10.2 20-Aug-2011  cherry PAE MP support (preliminary), amd64 per-cpu L4 model redesigned, i386 pmap_pa_start/end fixup
 1.31.10.1 04-Aug-2011  cherry first cut at per-cpu event handling
 1.33.2.3 16-Jan-2013  yamt sync with (a bit old) head
 1.33.2.2 17-Apr-2012  yamt sync with head
 1.33.2.1 10-Nov-2011  yamt sync with head
 1.35.4.1 18-Feb-2012  mrg merge to -current.
 1.36.2.2 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #29):
sys/arch/xen/x86/x86_xpmap.c: revision 1.39
sys/arch/xen/include/hypervisor.h: revision 1.37
sys/arch/xen/include/intr.h: revision 1.34
sys/arch/xen/x86/xen_ipi.c: revision 1.10
sys/arch/x86/x86/cpu.c: revision 1.97
sys/arch/x86/include/cpu.h: revision 1.48
sys/uvm/uvm_map.c: revision 1.315
sys/arch/x86/x86/pmap.c: revision 1.165
sys/arch/xen/x86/cpu.c: revision 1.81
sys/arch/x86/x86/pmap.c: revision 1.167
sys/arch/xen/x86/cpu.c: revision 1.82
sys/arch/x86/x86/pmap.c: revision 1.168
sys/arch/xen/x86/xen_pmap.c: revision 1.17
sys/uvm/uvm_km.c: revision 1.122
sys/uvm/uvm_kmguard.c: revision 1.10
sys/arch/x86/include/pmap.h: revision 1.50
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.
2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.
To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.
to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.
While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
When using uvm_km_pgremove_intrsafe() make sure mappings are removed
before returning the pages to the free pool. Otherwise, under Xen,
a page which still has a writable mapping could be allocated for
a PDP by another CPU and the hypervisor would refuse it (this is
PR port-xen/45975).
For this, move the pmap_kremove() calls inside uvm_km_pgremove_intrsafe(),
and do pmap_kremove()/uvm_pagefree() in batch of (at most) 16 entries
(as suggested by Chuck Silvers on tech-kern@, see also
http://mail-index.netbsd.org/tech-kern/2012/02/17/msg012727.html and
followups).
Avoid early use of xen_kpm_sync(); locks are not available at this time.
Don't call cpu_init() twice.
Makes LOCKDEBUG kernels boot again
Revert pmap_pte_flush() -> xpq_flush_queue() in previous.
 1.36.2.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #28):
sys/arch/xen/include/hypervisor.h: revision 1.38
sys/arch/xen/xen/hypervisor.c: revision 1.61
sys/arch/xen/xen/clock.c: revision 1.62
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
 1.38.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.38.2.1 25-Feb-2013  tls resync with head
 1.43.10.1 10-Aug-2014  tls Rebase.
 1.44.28.1 10-Jun-2019  christos Sync with HEAD
 1.44.26.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.44.26.1 30-Sep-2018  pgoyette Ssync with HEAD
 1.49.10.4 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.49.10.3 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.49.10.2 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.49.10.1 12-Apr-2020  bouyer Get rid of xen-specific ci_x* interrupt handling:
- use the general SIR mechanism, reserving 3 more slots for IPL_VM, IPL_SCHED
and IPL_HIGH
- remove specific handling from C sources, or change to ipending
- convert IPL number to SIR number in various places
- Remove XUNMASK/XPENDING in assembly or change to IUNMASK/IPENDING
- remove Xen-specific ci_xsources, ci_xmask, ci_xunmask, ci_xpending from
struct cpu_info
- for now remove a KASSERT that there are no pending interrupts in
idle_block(). We can get there with some software interrupts pending
in autoconf XXX needs to be looked at.
 1.55.4.4 20-Jul-2024  martin Pull up following revision(s) (requested by riastradh in ticket #764):

common/lib/libc/arch/i386/atomic/atomic.S: revision 1.37
sys/arch/xen/include/xenring.h: revision 1.8
sys/arch/i386/i386/cpufunc.S: revision 1.52
sys/arch/amd64/amd64/cpufunc.S: revision 1.68
sys/arch/xen/include/hypervisor.h: revision 1.60
common/lib/libc/arch/x86_64/atomic/atomic.S: revision 1.30

xen: Don't hotpatch away LOCK prefix in xen_mb, even on UP boots.

Both xen_mb and membar_sync are designed to provide store-before-load
ordering, but xen_mb has to provide it in synchronizing guest with
hypervisor, while membar_sync only has to provide it in synchronizing
one (guest) CPU with another (guest) CPU.

It is safe to hotpatch away the LOCK prefix in membar_sync on a
uniprocessor boot because membar_sync is only designed to coordinate
between normal memory on multiple CPUs, and is never necessary when
there's only one CPU involved.

But xen_mb is used to coordinate between the guest and the `device'
implemented by a hypervisor, which might be running on another
_physical_ CPU even if the NetBSD guest only sees one `CPU', i.e.,
one _virtual_ CPU. So even on `uniprocessor' boots, xen_mb must
still issue an instruction with store-before-load ordering on
multiprocessor systems, such as a LOCK ADD (or MFENCE, but MFENCE is
costlier for no benefit here).

No need to change xen_wmb (release ordering, load/store-before-store)
or xen_rmb (acquire ordering, load-before-load/store) because every
x86 store is a store-release and every x86 load is a load-acquire,
even on multiprocessor systems, so there's no hotpatching involved
anyway.

PR kern/57199
 1.55.4.3 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #428):

sys/arch/xen/xen/xen_machdep.c: revision 1.28
sys/arch/x86/pci/pci_machdep.c: revision 1.97
sys/arch/xen/xen/genfb_xen.c: revision 1.1
sys/arch/xen/xen/genfb_xen.c: revision 1.2
sys/arch/xen/include/hypervisor.h: revision 1.59
sys/arch/i386/conf/XEN3PAE_DOM0: revision 1.41 (patch)
sys/arch/x86/x86/genfb_machdep.c: revision 1.22
sys/arch/xen/x86/consinit.c: revision 1.18
sys/arch/xen/x86/autoconf.c: revision 1.26
sys/external/mit/xen-include-public/dist/xen/include/public/platform.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.188
sys/arch/x86/x86/consinit.c: revision 1.37
sys/arch/xen/conf/files.xen: revision 1.189
sys/arch/x86/x86/consinit.c: revision 1.38
sys/external/mit/xen-include-public/dist/xen/include/public/xen.h: revision 1.2
sys/arch/x86/include/genfb_machdep.h: revision 1.7
sys/arch/xen/x86/pvh_consinit.c: revision 1.5
sys/arch/xen/x86/pvh_consinit.c: revision 1.6
sys/arch/amd64/conf/XEN3_DOM0: revision 1.201

Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.

Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.

Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen

when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.

x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console

xen/x86/consinit.c: support genfb as possible console

xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.

xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.

Make sure to always fall back to xen_early_console, even for dom0

Enable genfb in DOM0 kernels

Add ext_lfb_base to dom0_vga_console_info, from recent Xen. We know if it's
present or not by checking dom0.info_size

Add XENPF_get_dom0_console, which gets a dom0_vga_console_info stucture
from the hypervisor. To be used by PVH dom0 kernels.

XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.

Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.55.4.2 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #427):

sys/arch/xen/include/hypervisor.h: revision 1.58
sys/arch/i386/i386/locore.S: revision 1.197
sys/arch/amd64/amd64/locore.S: revision 1.224

Xen's start_info_t is larger than 512 bytes these days, so bump the copy
size to a whole PAGE_SIZE, and CTASSERT() that start_info_t is smaller.

Luckily we didn't use yet the parts skipped
 1.55.4.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.59.6.1 02-Aug-2025  perseant Sync with HEAD
 1.2 11-Jan-2008  bouyer 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.1 28-Sep-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.8; 1.1.34; 1.1.40; 1.1.48;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.1.48.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.1.40.1 18-Feb-2008  mjf Sync with HEAD.
 1.1.34.1 23-Mar-2008  matt sync with HEAD
 1.1.8.3 21-Jan-2008  yamt sync with head
 1.1.8.2 30-Dec-2006  yamt sync with head.
 1.1.8.1 28-Sep-2006  yamt file i82093reg.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.4.2 18-Nov-2006  ad Sync with head.
 1.1.4.1 28-Sep-2006  ad file i82093reg.h was added on branch newlock2 on 2006-11-18 21:29:39 +0000
 1.1.2.2 22-Oct-2006  yamt sync with head
 1.1.2.1 28-Sep-2006  yamt file i82093reg.h was added on branch yamt-splraiseipl on 2006-10-22 06:05:20 +0000
 1.7 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.6 21-Apr-2020  jdolecek convert to newer HYPERVISOR_physdev_op() interface, now command and the
arg are separate arguments - this is needed for newer physdev_op commands

remove code for PHYSDEVOP_IRQ_UNMASK_NOTIFY, it is obsolete since
interface version 0x00030202 and is unsupported by newer versions of Xen

confirmed working on amd64 Dom0, i386 compile-tested only
 1.5 04-Nov-2017  cherry branches: 1.5.14;
protect header against recursive include
 1.4 22-Mar-2010  cegger branches: 1.4.18;
Ignore ioapic write failures.
They happen when writing to ioapic pins Xen already disabled.
This makes netbsd dom0 booting.
 1.3 29-Jul-2009  cegger branches: 1.3.2; 1.3.4;
remove Xen2 support.
ok bouyer@
 1.2 14-Apr-2008  cegger branches: 1.2.4; 1.2.18;
- use POSIX integer types
- ansify functions
 1.1 28-Sep-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.8; 1.1.58;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.1.58.1 02-Jun-2008  mjf Sync with HEAD.
 1.1.8.2 30-Dec-2006  yamt sync with head.
 1.1.8.1 28-Sep-2006  yamt file i82093var.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.4.2 18-Nov-2006  ad Sync with head.
 1.1.4.1 28-Sep-2006  ad file i82093var.h was added on branch newlock2 on 2006-11-18 21:29:39 +0000
 1.1.2.2 22-Oct-2006  yamt sync with head
 1.1.2.1 28-Sep-2006  yamt file i82093var.h was added on branch yamt-splraiseipl on 2006-10-22 06:05:20 +0000
 1.2.18.2 24-Oct-2010  jym Sync with HEAD
 1.2.18.1 01-Nov-2009  jym Sync with HEAD.
 1.2.4.2 11-Aug-2010  yamt sync with head.
 1.2.4.1 19-Aug-2009  yamt sync with head.
 1.3.4.1 30-May-2010  rmind sync with head
 1.3.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.4.18.1 03-Dec-2017  jdolecek update from HEAD
 1.5.14.1 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.2 11-Jan-2008  bouyer 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.1 28-Sep-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.8; 1.1.34; 1.1.40; 1.1.48;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.1.48.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.1.40.1 18-Feb-2008  mjf Sync with HEAD.
 1.1.34.1 23-Mar-2008  matt sync with HEAD
 1.1.8.3 21-Jan-2008  yamt sync with head
 1.1.8.2 30-Dec-2006  yamt sync with head.
 1.1.8.1 28-Sep-2006  yamt file i82489reg.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.4.2 18-Nov-2006  ad Sync with head.
 1.1.4.1 28-Sep-2006  ad file i82489reg.h was added on branch newlock2 on 2006-11-18 21:29:39 +0000
 1.1.2.2 22-Oct-2006  yamt sync with head
 1.1.2.1 28-Sep-2006  yamt file i82489reg.h was added on branch yamt-splraiseipl on 2006-10-22 06:05:20 +0000
 1.3 16-Sep-2021  andvar fix various typos, mainly in comments.
 1.2 14-Nov-2010  bouyer Explain why we hardwire lapic_cpu_number() to 0 on Xen.
 1.1 28-Sep-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.8; 1.1.76; 1.1.84;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.1.84.1 05-Mar-2011  rmind sync with head
 1.1.76.1 10-Jan-2011  jym Sync with HEAD
 1.1.8.2 30-Dec-2006  yamt sync with head.
 1.1.8.1 28-Sep-2006  yamt file i82489var.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.4.2 18-Nov-2006  ad Sync with head.
 1.1.4.1 28-Sep-2006  ad file i82489var.h was added on branch newlock2 on 2006-11-18 21:29:39 +0000
 1.1.2.2 22-Oct-2006  yamt sync with head
 1.1.2.1 28-Sep-2006  yamt file i82489var.h was added on branch yamt-splraiseipl on 2006-10-22 06:05:20 +0000
 1.14 23-Oct-2009  snj Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.13 16-Apr-2008  cegger branches: 1.13.4; 1.13.18;
device_t / softc split
reviewed, tested and approved by bouyer
 1.12 22-Nov-2007  bouyer branches: 1.12.14;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.11 08-Jan-2006  bouyer branches: 1.11.28; 1.11.46; 1.11.48; 1.11.52; 1.11.54;
Move if_xennet private functions and structures from if_xennetvar.h
to if_xennet.c
 1.10 11-Dec-2005  christos branches: 1.10.2;
merge ktrace-lwp.
 1.9 02-Oct-2005  bouyer Have xennet_start() schedule a software interrupt to defer processing of the
send queue. This give upper layer an opportunity to queue up all available
packets before starting to process them. This reduce the number of interrupt
generated on the backend, and the time spent doing hypercalls in a significant
way.
 1.8 11-Sep-2005  bouyer Use MCLGET() instead of local list of buffers. Garbage-collect
struct xennet_txbuf usage.
 1.7 06-Jun-2005  yamt branches: 1.7.2;
fix a typo (NETIF_TX_RING_SIZE <-> NETIF_RX_RING_SIZE)
 1.6 17-Apr-2005  bouyer Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.5 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4 09-Mar-2005  bouyer branches: 1.4.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 15-Sep-2004  tls branches: 1.3.4; 1.3.6; 1.3.8;
Actually mix samples into the kernel RNG pool. There's a buglet here: all
disk samples are reported as belonging to the first disk.
 1.2 24-Apr-2004  cl branches: 1.2.2;
Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.2 16-Sep-2004  jmc Pullup rev 1.3 (requested by tls in ticket #846)

Actually mix samples into the kernel RNG pool.
 1.1.2.1 22-May-2004  he Pull up revision 1.2 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.2.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.2.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.2.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.2.2.2 03-Aug-2004  skrll Sync with HEAD
 1.2.2.1 24-Apr-2004  skrll file if_xennetvar.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.3.8.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.6.1 29-Apr-2005  kent sync with -current
 1.3.4.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.4.2.6 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.5 06-Apr-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1239):
sys/arch/xen/include/if_xennetvar.h: revision 1.9
sys/arch/xen/xen/if_xennet.c: revision 1.37
Have xennet_start() schedule a software interrupt to defer processing of the
send queue. This give upper layer an opportunity to queue up all available
packets before starting to process them. This reduce the number of interrupt
generated on the backend, and the time spent doing hypercalls in a significant
way.
 1.4.2.4 22-Jan-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1105):
sys/arch/xen/include/if_xennetvar.h: revision 1.8
Use MCLGET() instead of local list of buffers. Garbage-collect
struct xennet_txbuf usage.
 1.4.2.3 18-Jun-2005  tron Pull up revision 1.7 (requested by yamt in ticket #471):
fix a typo (NETIF_TX_RING_SIZE <-> NETIF_RX_RING_SIZE)
 1.4.2.2 28-Apr-2005  tron Pull up revision 1.5 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4.2.1 25-Apr-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #187):
Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.7.2.2 07-Dec-2007  yamt sync with head
 1.7.2.1 21-Jun-2006  yamt sync with head.
 1.10.2.1 15-Jan-2006  yamt sync with head.
 1.11.54.1 08-Dec-2007  mjf Sync with HEAD.
 1.11.52.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.11.48.1 09-Jan-2008  matt sync with HEAD
 1.11.46.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.11.28.1 03-Dec-2007  ad Sync with HEAD.
 1.12.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.13.18.1 01-Nov-2009  jym Sync with HEAD.
 1.13.4.1 11-Mar-2010  yamt sync with head
 1.61 18-Aug-2025  andvar Fix various typos, mainly in comments:
s/invaid/invalid/
s/instad/instead/
s/wich/with/
s/tranform/transform/
s/tranmist/transmit/
s/tranceiver/transceiver/
s/Tranparent/Transparent/
s/tranlated/translated/
s/tranfer/transfer/
s/tranmissions/transmissions/
s/condtions/conditions/
s/Recient/Recent/
 1.60 07-Sep-2022  knakahara NetBSD/x86: Raise the number of interrupt sources per CPU from 32 to 56.

There has been no objection for three years.
https://mail-index.netbsd.org/port-amd64/2019/09/22/msg003012.html
Implemented by nonaka@n.o, updated by me.
 1.59 23-May-2022  bouyer Work in progress on MSI/MSI-X on Xen (MSI works on my hardware, more work
needed for MSI-X):
- Xen silently rejects 32 bits writes to MSI configuration registers
(especially when setting PCI_MSI_CTL_MSI_ENABLE/PCI_MSIX_CTL_ENABLE),
it expects 16 bits writes. So introduce a pci_conf_write16(),
only available on XENPV (and working only for mode 1 without
PCI_OVERRIDE_CONF_WRITE) and use it to enable MSI or MSI-X on XENPV.
- for multi-MSI vectors, Xen allocates all of them in a single hypercall,
so it's not convenient to do it at intr_establish() time.
So do it at alloc() time and register the pirqs in the msipic structure.
xen_pic_to_gsi() now just returns the values cached in the msipic.
As a bonus, if the PHYSDEVOP_map_pirq hypercall fails we can fail
the alloc() and we don't need the xen_pci_msi*_probe() hacks.

options NO_PCI_MSI_MSIX still on by default for XEN3_DOM0.
 1.58 19-Jul-2020  jdolecek for Xen MSI, fallback to INTx when PHYSDEVOP_map_pirq fails for the device

apparently Xen requires VT-d to be enabled in BIOS for PHYSDEVOP_map_pirq
to work, this change makes it work on systems with VT-d disabled or missing

adresses the panic part of PR port-xen/55285 by Patrick Welche
 1.57 15-May-2020  jdolecek use short for irq2port[] to save memory (4KB), it only needs to store
numbers <= NR_EVENT_CHANNELS (2048)
 1.56 14-May-2020  jdolecek xen_vec_alloc() is no more
 1.55 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.54 21-Apr-2020  jdolecek adjust so that this at least compiles and links with __HAVE_PCI_MSI_MSIX
 1.53 23-Dec-2019  thorpej branches: 1.53.6;
Provide XEN stubs for intr_mask() / intr_unmask().
 1.52 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.51 25-Dec-2018  cherry Excise XEN specific code out of x86/x86/intr.c into xen/x86/xen_intr.c

While at it, separate the source function tracking so that the interrupt
paths are truly independant.

Use weak symbol exporting to provision for future PVHVM co-existence
of both files, but with independant paths. Introduce assembler code
such that in a unified scenario, native interrupts get first priority
in spllower(), followed by XEN event callbacks. IPL management and
semantics are unchanged - native handlers and xen callbacks are
expected to maintain their ipl related semantics.

In summary, after this commit, native and XEN now have completely
unrelated interrupt handling mechanisms, including
intr_establish_xname() and assembler stubs and intr handler
management.

Happy Christmas!
 1.50 24-Dec-2018  cherry 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.49 10-Oct-2018  cherry Do not export the 'irq<->vector' abstraction outside of pintr.c
anymore. We now think of them as a unified thing called 'gsi',
which is generated by mpacpi/mpbios
 1.48 07-Oct-2018  cherry Switch over to a "GSI" concept for guest irqs.

On XEN there is a namespace called GSI which includes:

i) legacy_irq (0 - 16)
ii) "gsi" (16-nr_irqs_gsi)
iii) msi

We try to mirror this in guest space, but are mindful that legacy_irq
is 1:1 bound to actual hardware legacy_irq. Apart from this, XEN doesn't
really care what number scheme we use, as long as it doesn't encroach
on the MSI space, which is TBD for us.

Thus we trust the mpbios.c/mpacpi.c code to correctly map the pic,pin
tuples into the correct global gsi space, which we then register with
xen. As we now do, we allow for duplicate gsi registrations, in case
any hardware shares the same (pic,pin);

This enables us to now use the (pic,pin) tuple as the canonical reference
for device interrupt addresses, and leave any global mappings to specific
code. Thus xen_pic_to_gsi().

Note that this requires separate support for MSI, which I will get around to
once things stabilise - however the API change facilitates this nicely.

I note that the msi addroute() function does not use the "pin" parameter.
This can be made use of, to encode the gsi number, for XEN. This is however
TBD.

We further tweak the xen_vec_alloc() code to be uniform for the NIOAPICS
and other cases, and ensure that i8259.c DTRT wrt to route().

This will allow us to use pic->pic_addroute() without needing to worry about
pic specific issues.

The next step is to consolidate the pic_addroute() XEN related #ifdefs into
a -DXEN specific file, so that we don't clutter x86/ code with #ifdef XENs.

This change has functional implications, and there is likely breakage coming
especially on bespoke platforms that I haven't been able to test yet.

I am especially interested in bug reports from platforms with legacy (esp. i386)
and with multiple ioapics.
 1.47 06-Oct-2018  cherry Change the name of xen_pirq_alloc() to xen_vec_alloc() to reflect
its actual job.

The idea is that we will strip this down until it is as close to
idt_vec_alloc() as possible.
 1.46 24-Jun-2018  jdolecek branches: 1.46.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.45 13-Dec-2017  bouyer branches: 1.45.2;
Fixes for physical interrupts on Xen:
- do not cast int * to intr_handle_t *, they're not the same size
- legacy_irq is not always -1 for ioapic interrupts, test pic_type instead
- change irq2port[] to hold (port + 1) so that 0 is an invalid value
- add KASSERTs to make sure vect, port or irq values extracted from arrays are
valid (or that they are invalid before write)
- for the !ioapic case, we still need to do PHYSDEVOP_ASSIGN_VECTOR and
bind_pirq_to_evtch().

now XEN3_DOM0 boots again
 1.44 04-Nov-2017  cherry Retire xen/x86/intr.c and use the new xen specific glue in x86/x86/intr.c

The purpose of this change is to expose the x86/include/intr.h API
to drivers. Specifically the following functions:

void *intr_establish_xname(...);
void *intr_establish(...);
void intr_disestablish(...);

while maintaining the old API from xen/include/evtchn.h, specifically
the following functions:

int event_set_handler(...);
int event_remove_handler(...);

This is so that if things break, we can keep using the old API until
everything stabilises. This is a stepping stone towards getting the
actual XEN event callback path rework code in place - which can be
done opaquely behind the intr.h API - NetBSD/XEN specific drivers that
have been ported to the intr.h API should then work without
significant further modifications.
 1.43 04-Nov-2017  cherry On XEN dom0, the function xen/x86/intr.c:xen_intr_map() is used to map
hardware interrupts to XEN callbacks called 'events'. This function
combines both the allocation and the binding.

This change is the first part of breaking up that combination into
xen_pirq_alloc() and the binding will happen as part of the
pic_addroute() callback of a new pseudo PIC_XEN

This code will be added later on.
 1.42 16-Jul-2017  cherry branches: 1.42.2;
Unify the xen and native x86/ interrupt setup functions and
spl traversal data structures.

This is towards PVHVM.
 1.41 16-Jul-2017  cherry Remove the xen specific interrupt type for the x86 intr_handle_t
For this to work, we use the evtchn.c:get_pirq_to_evtchn() glue
function to make things easier.
 1.40 23-May-2017  nonaka x86: Add preliminary x2APIC support.

x2APIC is used only when x2APIC is enabled in BIOS/UEFI.
LAPIC ID is not supported above 256.
 1.39 17-Oct-2016  jdolecek provide stub intr xname establish for xen
 1.38 16-Oct-2016  kre This should return the amd64 build to a working state (and hopefully
i386 as well) - but this is a hideous hack, and should be reverted
as soon as a better (which means any) alternative is available.
 1.37 07-Jul-2016  msaitoh branches: 1.37.2;
KNF. Remove extra spaces. No functional change.
 1.36 27-Apr-2015  knakahara add x86 MD MSI/MSI-X support code.
 1.35 27-Dec-2012  cherry branches: 1.35.14;
Remove unused header evtchn.h from intr.h
 1.34 17-Feb-2012  bouyer branches: 1.34.2;
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.

2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.

To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.

to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.

While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
 1.33 11-Aug-2011  cherry branches: 1.33.2; 1.33.6; 1.33.8;
Make event/interrupt handling MP aware
 1.32 10-Aug-2011  cherry xen ipi infrastructure
 1.31 29-Jul-2009  cegger branches: 1.31.10;
remove Xen2 support.
ok bouyer@
 1.30 27-Mar-2009  dyoung Explicitly #include <sys/device.h> and <sys/evcnt.h>. These files only
got the definitions they needed by chance, before.
 1.29 30-May-2008  ad branches: 1.29.6; 1.29.12;
Add a 'known_mpsafe' argument to intr_establish().
 1.28 24-May-2008  bouyer G/C dead code: remove now-unused softintr-related code.
 1.27 28-Apr-2008  martin branches: 1.27.2;
Remove clause 3 and 4 from TNF licenses
 1.26 25-Apr-2008  ad branches: 1.26.2;
Include null IPI functions if !MULTIPROCESSOR.
 1.25 19-Apr-2008  cegger Use interrupt biglock wrapper as in x86/x86/intr.c
This change is based on http://mail-index.netbsd.org/port-amd64/2004/02/22/0000.html
OK bouyer
 1.24 14-Apr-2008  cegger branches: 1.24.2;
- use POSIX integer types
- ansify functions
 1.23 19-Feb-2008  bouyer branches: 1.23.6;
The event bitmasks provided by the hypervisor are unsigned long (so 64bits
on amd64). Make sure to use the right type to store and manipulate them.
This fixes amd64, where basically any event channel > 31 was not working
(and you get there after starting/stopping a domU a few times). Things
would occasionally unwedge though the spllower() callbacks.
 1.22 11-Jan-2008  bouyer 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.21 26-Dec-2007  yamt - share idt entry allocation code among x86.
- introduce a function to reserve an idt entry and use it instead of
manipulating idt_allocmap directly.
- rename idt to xen_idt for amd64 xen. add missing #ifdef XEN.
 1.20 03-Dec-2007  ad branches: 1.20.2; 1.20.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.19 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.18 17-Oct-2007  garbled branches: 1.18.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.17 26-Sep-2007  ad branches: 1.17.2;
x86 changes for pcc and LKMs.

- Replace most inline assembly with proper functions. As a side effect
this reduces the size of amd64 GENERIC by about 120kB, and i386 by a
smaller amount. Nearly all of the inlines did something slow, or something
that does not need to be fast.
- Make curcpu() and curlwp functions proper, unless __GNUC__ && _KERNEL.
In that case make them inlines. Makes curlwp LKM and preemption safe.
- Make bus_space and bus_dma more LKM friendly.
- Share a few more files between the ports.
- Other minor changes.
 1.16 17-May-2007  yamt branches: 1.16.8; 1.16.10; 1.16.12;
merge yamt-idlelwp branch. asked by core@. some ports still needs work.

from doc/BRANCHES:

idle lwp, and some changes depending on it.

1. separate context switching and thread scheduling.
(cf. gmcgarry_ctxsw)
2. implement idle lwp.
3. clean up related MD/MI interfaces.
4. make scheduler(s) modular.
 1.15 16-Feb-2007  ad branches: 1.15.2; 1.15.6; 1.15.8; 1.15.14;
Remove spllowersoftclock() and CLKF_BASEPRI(), and always dispatch callouts
via a soft interrupt. In the near future, softclock will be run from process
context.
 1.14 26-Dec-2006  ad Define ipl_t as uint8_t so that it can be packed into a word with a lock
byte. Ok yamt@.
 1.13 21-Dec-2006  yamt merge yamt-splraiseipl branch.

- finish implementing splraiseipl (and makeiplcookie).
http://mail-index.NetBSD.org/tech-kern/2006/07/01/0000.html
- complete workqueue(9) and fix its ipl problem, which is reported
to cause audio skipping.
- fix netbt (at least compilation problems) for some ports.
- fix PR/33218.
 1.12 28-Sep-2006  bouyer Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.11 15-May-2006  dogcow branches: 1.11.8; 1.11.10;
For whatever reason, GCC4 really really wants extern struct decls to be
after the struct is defined.
 1.10 09-Apr-2006  bouyer Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.9 16-Feb-2006  perry branches: 1.9.2; 1.9.4; 1.9.6;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.8 24-Dec-2005  perry branches: 1.8.2; 1.8.4; 1.8.6;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.7 11-Dec-2005  christos merge ktrace-lwp.
 1.6 03-Nov-2005  yamt - use sys/spl.h.
- add some IPL_ definitions.
 1.5 29-Oct-2005  yamt add splraiseipl().
 1.4 28-Oct-2005  yamt remove duplicated spllpt().
 1.3 16-Apr-2005  bouyer branches: 1.3.2; 1.3.4;
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2 11-Apr-2005  yamt fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.1 11-Mar-2004  cl branches: 1.1.4; 1.1.12; 1.1.18;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.18.2 28-Apr-2005  tron Pull up revision 1.3 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.1.18.1 13-Apr-2005  tron Pull up revision 1.2 (requested by yamt in ticket #146):
fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.1.12.1 29-Apr-2005  kent sync with -current
 1.1.4.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file intr.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.3.4.1 02-Nov-2005  yamt sync with head.
 1.3.2.8 27-Feb-2008  yamt sync with head.
 1.3.2.7 21-Jan-2008  yamt sync with head
 1.3.2.6 07-Dec-2007  yamt sync with head
 1.3.2.5 27-Oct-2007  yamt sync with head.
 1.3.2.4 03-Sep-2007  yamt sync with head.
 1.3.2.3 26-Feb-2007  yamt sync with head.
 1.3.2.2 30-Dec-2006  yamt sync with head.
 1.3.2.1 21-Jun-2006  yamt sync with head.
 1.8.6.2 01-Jun-2006  kardel Sync with head.
 1.8.6.1 22-Apr-2006  simonb Sync with head.
 1.8.4.1 09-Sep-2006  rpaulo sync with head
 1.8.2.1 18-Feb-2006  yamt sync with head.
 1.9.6.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.9.4.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.9.2.2 24-May-2006  yamt sync with head.
 1.9.2.1 11-Apr-2006  yamt sync with head
 1.11.10.3 22-Oct-2006  yamt sync with head
 1.11.10.2 21-Sep-2006  yamt rename splraiseipl argument to match with the rest of ports.
 1.11.10.1 18-Sep-2006  yamt implement new api for xen.
 1.11.8.2 12-Jan-2007  ad Sync with head.
 1.11.8.1 18-Nov-2006  ad Sync with head.
 1.15.14.2 03-Oct-2007  garbled Sync with HEAD
 1.15.14.1 22-May-2007  matt Update to HEAD.
 1.15.8.1 11-Jul-2007  mjf Sync with head.
 1.15.6.3 03-Dec-2007  ad Sync with HEAD.
 1.15.6.2 09-Oct-2007  ad Sync with head.
 1.15.6.1 27-May-2007  ad Sync with head.
 1.15.2.1 26-Mar-2007  yamt adapt xen.
 1.16.12.1 06-Oct-2007  yamt sync with head.
 1.16.10.3 23-Mar-2008  matt sync with HEAD
 1.16.10.2 09-Jan-2008  matt sync with HEAD
 1.16.10.1 06-Nov-2007  matt sync with HEAD
 1.16.8.3 09-Dec-2007  jmcneill Sync with HEAD.
 1.16.8.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.16.8.1 02-Oct-2007  joerg Sync with HEAD.
 1.17.2.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.18.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.18.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.20.6.3 06-Jan-2008  bouyer Remove unused splsoftxenevt()
 1.20.6.2 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.20.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.20.2.1 26-Dec-2007  ad Sync with head.
 1.23.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.24.2.2 04-Jun-2008  yamt sync with head
 1.24.2.1 18-May-2008  yamt sync with head.
 1.26.2.3 19-Aug-2009  yamt sync with head.
 1.26.2.2 04-May-2009  yamt sync with head.
 1.26.2.1 16-May-2008  yamt sync with head.
 1.27.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.29.12.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.29.12.2 01-Nov-2009  jym Sync with HEAD.
 1.29.12.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.29.6.1 28-Apr-2009  skrll Sync with HEAD.
 1.31.10.4 17-Aug-2011  cherry Pullup relevant changes from -current
 1.31.10.3 04-Aug-2011  cherry first cut at per-cpu event handling
 1.31.10.2 26-Jun-2011  cherry Unbreak uniprocessor build
 1.31.10.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.33.8.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #29):
sys/arch/xen/x86/x86_xpmap.c: revision 1.39
sys/arch/xen/include/hypervisor.h: revision 1.37
sys/arch/xen/include/intr.h: revision 1.34
sys/arch/xen/x86/xen_ipi.c: revision 1.10
sys/arch/x86/x86/cpu.c: revision 1.97
sys/arch/x86/include/cpu.h: revision 1.48
sys/uvm/uvm_map.c: revision 1.315
sys/arch/x86/x86/pmap.c: revision 1.165
sys/arch/xen/x86/cpu.c: revision 1.81
sys/arch/x86/x86/pmap.c: revision 1.167
sys/arch/xen/x86/cpu.c: revision 1.82
sys/arch/x86/x86/pmap.c: revision 1.168
sys/arch/xen/x86/xen_pmap.c: revision 1.17
sys/uvm/uvm_km.c: revision 1.122
sys/uvm/uvm_kmguard.c: revision 1.10
sys/arch/x86/include/pmap.h: revision 1.50
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.
2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.
To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.
to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.
While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
When using uvm_km_pgremove_intrsafe() make sure mappings are removed
before returning the pages to the free pool. Otherwise, under Xen,
a page which still has a writable mapping could be allocated for
a PDP by another CPU and the hypervisor would refuse it (this is
PR port-xen/45975).
For this, move the pmap_kremove() calls inside uvm_km_pgremove_intrsafe(),
and do pmap_kremove()/uvm_pagefree() in batch of (at most) 16 entries
(as suggested by Chuck Silvers on tech-kern@, see also
http://mail-index.netbsd.org/tech-kern/2012/02/17/msg012727.html and
followups).
Avoid early use of xen_kpm_sync(); locks are not available at this time.
Don't call cpu_init() twice.
Makes LOCKDEBUG kernels boot again
Revert pmap_pte_flush() -> xpq_flush_queue() in previous.
 1.33.6.1 18-Feb-2012  mrg merge to -current.
 1.33.2.2 23-Jan-2013  yamt sync with head
 1.33.2.1 17-Apr-2012  yamt sync with head
 1.34.2.2 03-Dec-2017  jdolecek update from HEAD
 1.34.2.1 25-Feb-2013  tls resync with head
 1.35.14.4 28-Aug-2017  skrll Sync with HEAD
 1.35.14.3 05-Dec-2016  skrll Sync with HEAD
 1.35.14.2 09-Jul-2016  skrll Sync with HEAD
 1.35.14.1 06-Jun-2015  skrll Sync with HEAD
 1.37.2.1 04-Nov-2016  pgoyette Sync with HEAD
 1.42.2.2 16-Jul-2017  cherry 2302677
 1.42.2.1 16-Jul-2017  cherry file intr.h was added on branch perseant-stdc-iso10646 on 2017-07-16 14:02:49 +0000
 1.45.2.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.45.2.2 20-Oct-2018  pgoyette Sync with head
 1.45.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.46.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.46.2.1 10-Jun-2019  christos Sync with HEAD
 1.53.6.4 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.53.6.3 19-Apr-2020  bouyer Add per-PIC callbacks for interrupt_get_devname(), interrupt_get_assigned()
and interrupt_get_count(). Implement Xen-specific callbacks for
PIC_XEN and use the x86 one for others.
In event_set_handler(), call intr_allocate_io_intrsource() so that
events appears in interrupt list (intrctl list).
 1.53.6.2 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.53.6.1 12-Apr-2020  bouyer Get rid of xen-specific ci_x* interrupt handling:
- use the general SIR mechanism, reserving 3 more slots for IPL_VM, IPL_SCHED
and IPL_HIGH
- remove specific handling from C sources, or change to ipending
- convert IPL number to SIR number in various places
- Remove XUNMASK/XPENDING in assembly or change to IUNMASK/IPENDING
- remove Xen-specific ci_xsources, ci_xmask, ci_xunmask, ci_xpending from
struct cpu_info
- for now remove a KASSERT that there are no pending interrupts in
idle_block(). We can get there with some software interrupts pending
in autoconf XXX needs to be looked at.
 1.17 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.16 21-Apr-2020  ad XEN_IPI_KICK was replaced by XEN_IPI_AST.
 1.15 03-Apr-2020  ad branches: 1.15.2;
Attach xen IPI event counters.
 1.14 23-Nov-2019  ad cpu_need_resched():

- Remove all code that should be MI, leaving the bare minimum under arch/.
- Make the required actions very explicit.
- Pass in LWP pointer for convenience.
- When a trap is required on another CPU, have the IPI set it locally.
- Expunge cpu_did_resched().
 1.13 10-Sep-2018  cherry Make the use of 'irqs' in the range 0 < irq < 255 by xen
as a handle for internal use explicit.

This allows us to pass up the handle as "legacy" irq while
establishing interrupt handlers for xen.

No functional change.
 1.12 19-May-2014  rmind branches: 1.12.26; 1.12.28;
Implement MI IPI interface with cross-call support.
 1.11 07-Nov-2011  cherry branches: 1.11.10; 1.11.24;
Add an ipi callback to force hypervisor callback. this is useful to "re-route" interrupts to a given vcpu
 1.10 10-Aug-2011  cherry branches: 1.10.2;
Add Xen specific ipi bitmasks
 1.9 11-Jan-2008  bouyer branches: 1.9.18; 1.9.34;
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.8 03-Dec-2007  ad branches: 1.8.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.7 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.6 28-Sep-2006  bouyer branches: 1.6.8; 1.6.26; 1.6.28; 1.6.32; 1.6.34;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.5 09-Apr-2006  bouyer branches: 1.5.8; 1.5.10;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.4 11-Dec-2005  christos branches: 1.4.4; 1.4.6; 1.4.8; 1.4.10; 1.4.12;
merge ktrace-lwp.
 1.3 03-Nov-2005  yamt branches: 1.3.2;
- use sys/spl.h.
- add some IPL_ definitions.
 1.2 20-Apr-2005  bouyer branches: 1.2.2; 1.2.4; 1.2.6;
Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.

Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.1 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2.6.4 21-Jan-2008  yamt sync with head
 1.2.6.3 07-Dec-2007  yamt sync with head
 1.2.6.2 30-Dec-2006  yamt sync with head.
 1.2.6.1 21-Jun-2006  yamt sync with head.
 1.2.4.2 29-Apr-2005  kent sync with -current
 1.2.4.1 20-Apr-2005  kent file intrdefs.h was added on branch kent-audio2 on 2005-04-29 11:28:29 +0000
 1.2.2.3 28-Apr-2005  tron Pull up revision 1.2 (requested by bouyer in ticket #193):
Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.
Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.2.2.2 28-Apr-2005  tron Pull up revision 1.1 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2.2.1 20-Apr-2005  tron file intrdefs.h was added on branch netbsd-3 on 2005-04-28 10:20:11 +0000
 1.3.2.2 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.2.1 03-Nov-2005  skrll file intrdefs.h was added on branch ktrace-lwp on 2005-11-10 14:00:34 +0000
 1.4.12.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.4.10.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.4.8.1 11-Apr-2006  yamt sync with head
 1.4.6.1 22-Apr-2006  simonb Sync with head.
 1.4.4.1 09-Sep-2006  rpaulo sync with head
 1.5.10.1 22-Oct-2006  yamt sync with head
 1.5.8.1 18-Nov-2006  ad Sync with head.
 1.6.34.2 18-Feb-2008  mjf Sync with HEAD.
 1.6.34.1 08-Dec-2007  mjf Sync with HEAD.
 1.6.32.2 25-Oct-2007  bouyer Finish sync with HEAD. Especially use the new x86 pmap for xenamd64.
For this:
- rename pmap_pte_set() to pmap_pte_testset()
- make pmap_pte_set() a function or macro for non-atomic PTE write
- define and use pmap_pa2pte()/pmap_pte2pa() to read/write PTE entries
- define pmap_pte_flush() which is a nop in x86 case, and flush the
MMUops queue in the Xen case
 1.6.32.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.28.2 23-Mar-2008  matt sync with HEAD
 1.6.28.1 09-Jan-2008  matt sync with HEAD
 1.6.26.2 09-Dec-2007  jmcneill Sync with HEAD.
 1.6.26.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.6.8.1 03-Dec-2007  ad Sync with HEAD.
 1.8.6.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.9.34.3 18-Sep-2011  cherry Use an IPI to re-route events to the cpu where the handler has been registered
 1.9.34.2 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.9.34.1 11-Jan-2008  cherry file intrdefs.h was added on branch cherry-xenmp on 2011-06-03 13:27:40 +0000
 1.9.18.1 27-Aug-2011  jym Add/remove files, like in HEAD.
 1.10.2.1 10-Nov-2011  yamt sync with head
 1.11.24.1 10-Aug-2014  tls Rebase.
 1.11.10.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.12.28.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.12.28.1 10-Jun-2019  christos Sync with HEAD
 1.12.26.1 30-Sep-2018  pgoyette Ssync with HEAD
 1.15.2.2 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.15.2.1 11-Apr-2020  bouyer Move softint and preemtion-related functions out of x86/x86/intr.c to
its own file, x86/x86/x86_softintr.c
Add x86/x86/x86_softintr.c for native and XenPV
Make sure XenPV also check ci_ioending, which is used for softints.
Switch XenPV to fast softints and allow kernel preemption.
kpreempt_disable() before calling pmap_changeprot_local()
run xen_wallclock_time() and xen_global_systime_ns() at splshed() to
avoid being interrupted.

XXX amd64 lock stubs are racy for XPENDING
 1.4 11-Jan-2008  bouyer 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.3 11-Dec-2005  christos branches: 1.3.50; 1.3.56; 1.3.64;
merge ktrace-lwp.
 1.2 09-Mar-2005  bouyer branches: 1.2.4; 1.2.6; 1.2.8; 1.2.10;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 08-Mar-2005  bouyer branches: 1.1.2;
file isa_machdep.h was initially added on branch bouyer-xen2.
 1.1.2.1 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.2.10.1 21-Jan-2008  yamt sync with head
 1.2.8.2 29-Apr-2005  kent sync with -current
 1.2.8.1 09-Mar-2005  kent file isa_machdep.h was added on branch kent-audio2 on 2005-04-29 11:28:29 +0000
 1.2.6.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.6.1 09-Mar-2005  skrll file isa_machdep.h was added on branch ktrace-lwp on 2005-04-01 14:29:10 +0000
 1.2.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.4.1 09-Mar-2005  yamt file isa_machdep.h was added on branch yamt-km on 2005-03-19 08:33:26 +0000
 1.3.64.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.3.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.3.50.1 23-Mar-2008  matt sync with HEAD
 1.3 23-Oct-2009  snj Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.2 11-Dec-2005  christos branches: 1.2.78; 1.2.92;
merge ktrace-lwp.
 1.1 07-May-2004  cl branches: 1.1.2; 1.1.4;
Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 07-May-2004  skrll file kernfs_machdep.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 07-May-2004  he file kernfs_machdep.h was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.2.92.1 01-Nov-2009  jym Sync with HEAD.
 1.2.78.1 11-Mar-2010  yamt sync with head
 1.2 23-May-2017  nonaka x86: Add preliminary x2APIC support.

x2APIC is used only when x2APIC is enabled in BIOS/UEFI.
LAPIC ID is not supported above 256.
 1.1 28-Sep-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.8; 1.1.106; 1.1.126;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.1.126.1 28-Aug-2017  skrll Sync with HEAD
 1.1.106.1 03-Dec-2017  jdolecek update from HEAD
 1.1.8.2 30-Dec-2006  yamt sync with head.
 1.1.8.1 28-Sep-2006  yamt file mpacpi.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.4.2 18-Nov-2006  ad Sync with head.
 1.1.4.1 28-Sep-2006  ad file mpacpi.h was added on branch newlock2 on 2006-11-18 21:29:39 +0000
 1.1.2.2 22-Oct-2006  yamt sync with head
 1.1.2.1 28-Sep-2006  yamt file mpacpi.h was added on branch yamt-splraiseipl on 2006-10-22 06:05:20 +0000
 1.2 11-Jan-2008  bouyer 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.1 28-Sep-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.8; 1.1.34; 1.1.40; 1.1.48;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.1.48.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.1.40.1 18-Feb-2008  mjf Sync with HEAD.
 1.1.34.1 23-Mar-2008  matt sync with HEAD
 1.1.8.3 21-Jan-2008  yamt sync with head
 1.1.8.2 30-Dec-2006  yamt sync with head.
 1.1.8.1 28-Sep-2006  yamt file mpbiosvar.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.4.2 18-Nov-2006  ad Sync with head.
 1.1.4.1 28-Sep-2006  ad file mpbiosvar.h was added on branch newlock2 on 2006-11-18 21:29:39 +0000
 1.1.2.2 22-Oct-2006  yamt sync with head
 1.1.2.1 28-Sep-2006  yamt file mpbiosvar.h was added on branch yamt-splraiseipl on 2006-10-22 06:05:20 +0000
 1.3 11-Jan-2008  bouyer 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.2 09-Feb-2007  ad branches: 1.2.4; 1.2.26; 1.2.32; 1.2.38;
Merge newlock2 to head.
 1.1 22-Dec-2006  ad branches: 1.1.2;
file mutex.h was initially added on branch newlock2.
 1.1.2.1 22-Dec-2006  ad Pull in x86 headers.
 1.2.38.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.2.32.1 18-Feb-2008  mjf Sync with HEAD.
 1.2.26.1 23-Mar-2008  matt sync with HEAD
 1.2.4.3 21-Jan-2008  yamt sync with head
 1.2.4.2 26-Feb-2007  yamt sync with head.
 1.2.4.1 09-Feb-2007  yamt file mutex.h was added on branch yamt-lazymbuf on 2007-02-26 09:08:55 +0000
 1.24 23-Jun-2024  riastradh pci: Pass cookie through pci_find_device, pci_enumerate_bus, take 2.

New functions pci_find_device1 and pci_enumerate_bus1 have the cookie
argument. Existing symbols pci_find_device and pci_enumerate_bus are
now wrappers for the cookieless version.

This will allow pci_find_device callers to pass a cookie through to
the match function so they can keep state or pass in extra parameters
like b/d/f numbers, which will allow us to nix some horrible kludges
in the Linux PCI API emulation for drm (and, perhaps, Intel wifi).

This change drops the symbol pci_probe_device, in favour of a new
pci_probe_device1 with the cookie argument. But I don't think that
requires a revbump because it's only called by MD pci_enumerate_bus1
implementations, which don't live in modules anyway.

Take 2: Make sure to handle NULL match function.
 1.23 20-May-2024  riastradh Revert "pci: Pass cookie through pci_find_device, pci_enumerate_bus."

Evidently something is wrong with this, to be diagnosed and redone
once the builds and tests are in better shape.
 1.22 20-May-2024  riastradh pci: Pass cookie through pci_find_device, pci_enumerate_bus.

New functions pci_find_device1 and pci_enumerate_bus1 have the cookie
argument. Existing symbols pci_find_device and pci_enumerate_bus are
now wrappers for the cookieless version.

This drops the symbol pci_probe_device, in favour of a new
pci_probe_device1 with the cookie argument. But I don't think that
requires a revbump because it's only called by MD pci_enumerate_bus1
implementations, which don't live in modules anyway.
 1.21 23-May-2022  bouyer branches: 1.21.4;
Work in progress on MSI/MSI-X on Xen (MSI works on my hardware, more work
needed for MSI-X):
- Xen silently rejects 32 bits writes to MSI configuration registers
(especially when setting PCI_MSI_CTL_MSI_ENABLE/PCI_MSIX_CTL_ENABLE),
it expects 16 bits writes. So introduce a pci_conf_write16(),
only available on XENPV (and working only for mode 1 without
PCI_OVERRIDE_CONF_WRITE) and use it to enable MSI or MSI-X on XENPV.
- for multi-MSI vectors, Xen allocates all of them in a single hypercall,
so it's not convenient to do it at intr_establish() time.
So do it at alloc() time and register the pirqs in the msipic structure.
xen_pic_to_gsi() now just returns the values cached in the msipic.
As a bonus, if the PHYSDEVOP_map_pirq hypercall fails we can fail
the alloc() and we don't need the xen_pci_msi*_probe() hacks.

options NO_PCI_MSI_MSIX still on by default for XEN3_DOM0.
 1.20 12-Feb-2019  cherry conditionally include XENPV specific code.

This explicitly excludes PV only functionality that would be wrong to
attempt to use in other modes, for eg: p2m table management.
 1.19 16-Jul-2017  cherry branches: 1.19.2; 1.19.6;
Remove the xen specific interrupt type for the x86 intr_handle_t
For this to work, we use the evtchn.c:get_pirq_to_evtchn() glue
function to make things easier.
 1.18 04-Apr-2011  dyoung branches: 1.18.14; 1.18.32;
Neither pci_dma64_available(), pci_probe_device(), pci_mapreg_map(9),
pci_find_rom(), pci_intr_map(9), pci_enumerate_bus(), nor the match
predicate passed to pciide_compat_intr_establish() should ever modify
their pci_attach_args argument, so make their pci_attach_args arguments
const and deal with the fallout throughout the kernel.

For the most part, these changes add a 'const' where there was no
'const' before, however, some drivers and MD code used to modify
pci_attach_args. Now those drivers either copy their pci_attach_args
and modify the copy, or refrain from modifying pci_attach_args:

Xen: according to Manuel Bouyer, writing to pci_attach_args in
pci_intr_map() was a leftover from Xen 2. Probably a bug. I
stopped writing it. I have not tested this change.

siside(4): sis_hostbr_match() needlessly wrote to pci_attach_args.
Probably a bug. I use a temporary variable. I have not tested this
change.

slide(4): sl82c105_chip_map() overwrote the caller's pci_attach_args.
Probably a bug. Use a local pci_attach_args. I have not tested
this change.

viaide(4): via_sata_chip_map() and via_sata_chip_map_new() overwrote the
caller's pci_attach_args. Probably a bug. Make a local copy of the
caller's pci_attach_args and modify the copy. I have not tested
this change.

While I'm here, make pci_mapreg_submap() static.

With these changes in place, I have tested the compilation of these
kernels:

alpha GENERIC
amd64 GENERIC XEN3_DOM0
arc GENERIC
atari HADES MILAN-PCIIDE
bebox GENERIC
cats GENERIC
cobalt GENERIC
evbarm-eb NSLU2
evbarm-el ADI_BRH ARMADILLO9 CP3100 GEMINI GEMINI_MASTER GEMINI_SLAVE GUMSTIX
HDL_G IMX31LITE INTEGRATOR IQ31244 IQ80310 IQ80321 IXDP425 IXM1200
KUROBOX_PRO LUBBOCK MARVELL_NAS NAPPI SHEEVAPLUG SMDK2800 TEAMASA_NPWR
TEAMASA_NPWR_FC TS7200 TWINTAIL ZAO425
evbmips-el AP30 DBAU1500 DBAU1550 MALTA MERAKI MTX-1 OMSAL400 RB153 WGT624V3
evbmips64-el XLSATX
evbppc EV64260 MPC8536DS MPC8548CDS OPENBLOCKS200 OPENBLOCKS266
OPENBLOCKS266_OPT P2020RDB PMPPC RB800 WALNUT
hp700 GENERIC
i386 ALL XEN3_DOM0 XEN3_DOMU
ibmnws GENERIC
macppc GENERIC
mvmeppc GENERIC
netwinder GENERIC
ofppc GENERIC
prep GENERIC
sandpoint GENERIC
sgimips GENERIC32_IP2x
sparc GENERIC_SUN4U KRUPS
sparc64 GENERIC

As of Sun Apr 3 15:26:26 CDT 2011, I could not compile these kernels
with or without my patches in place:

### evbmips-el GDIUM

nbmake: nbmake: don't know how to make /home/dyoung/pristine-nbsd/src/sys/arch/mips/mips/softintr.c. Stop

### evbarm-el MPCSA_GENERIC
src/sys/arch/evbarm/conf/MPCSA_GENERIC:318: ds1672rtc*: unknown device `ds1672rtc'

### ia64 GENERIC

/tmp/genassym.28085/assym.c: In function 'f111':
/tmp/genassym.28085/assym.c:67: error: invalid application of 'sizeof' to incomplete type 'struct pcb'
/tmp/genassym.28085/assym.c:76: error: dereferencing pointer to incomplete type

### sgimips GENERIC32_IP3x

crmfb.o: In function `crmfb_attach':
crmfb.c:(.text+0x2304): undefined reference to `ddc_read_edid'
crmfb.c:(.text+0x2304): relocation truncated to fit: R_MIPS_26 against `ddc_read_edid'
crmfb.c:(.text+0x234c): undefined reference to `edid_parse'
crmfb.c:(.text+0x234c): relocation truncated to fit: R_MIPS_26 against `edid_parse'
crmfb.c:(.text+0x2354): undefined reference to `edid_print'
crmfb.c:(.text+0x2354): relocation truncated to fit: R_MIPS_26 against `edid_print'
 1.17 14-Mar-2010  dyoung branches: 1.17.2; 1.17.4;
Add a new member, pc_super, to x86's pci_chipset_tag: pc.pc_super points
to the tag that pc inherits its behavior from. Add code to deal with
pc.pc_super.

Pull identical declarations out of xen/include/pci_machdep.h and
x86/include/pci_machdep.h into x86/include/pci_machdep_common.h.
 1.16 25-Feb-2010  dyoung Fix Xen.

XXX Almost everything in this header file, x86, amd64, and i386 should
XXX share.
 1.15 16-Feb-2010  dyoung Declare pci_mode_set().
 1.14 15-Feb-2010  dyoung Get pci_mode out of the global namespace.
 1.13 19-Oct-2009  bouyer branches: 1.13.2;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.12 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.11 13-Feb-2009  bouyer Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.10 30-May-2008  cegger branches: 1.10.6; 1.10.8; 1.10.12; 1.10.16;
build fix: add missing prototype
 1.9 16-Apr-2008  cegger branches: 1.9.2; 1.9.4; 1.9.6;
device_t / softc split
reviewed, tested and approved by bouyer
 1.8 14-Apr-2008  cegger - use POSIX integer types
- ansify functions
 1.7 28-Sep-2006  bouyer branches: 1.7.52;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.6 09-Apr-2006  bouyer branches: 1.6.8; 1.6.10;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.5 11-Dec-2005  christos branches: 1.5.4; 1.5.6; 1.5.8; 1.5.10; 1.5.12;
merge ktrace-lwp.
 1.4 19-Aug-2005  bouyer Provide a PCI_MACHDEP_ENUMERATE_BUS to the MI pci system. We can't use
the generic pci_enumerate_bus() in Xen because the hypervisor may
hide the function 0, but give access to other functions of the same device
and pci_enumerate_bus() will stop if function 0 isn't available.
 1.3 16-Apr-2005  bouyer branches: 1.3.2; 1.3.4;
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 18-Jan-2005  bouyer branches: 1.1.2; 1.1.4;
file pci_machdep.h was initially added on branch bouyer-xen2.
 1.1.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.2.1 18-Jan-2005  bouyer snapshot of work in progress on physical devices support:
- support pci at hypervisor. Attach one PCI bus for each bus returned by
PHYSDEVOP_PCI_PROBE_ROOT_BUSES (it looks like xen hides ppb bridges from
the guest OS).
- implement pci_conf_read()/pci_conf_write() using the appropriate
PHYSDEVOP_PCI_* calls.
- call PHYSDEVOP_PCI_INITIALISE_DEVICE from pci_intr_map() to make xen
do interrupts routing if needed, and map the hardware interrupt to
a xen interrupt.
- add pci and associated devices to files.xen

This is enouth to have IDE controllers work in PIO mode.
TODO: bus_dma support (currently bus_dma won't translate pseudo-physical
addresses to machine addresses), ISA support, test memory-mapped
I/O registers.
 1.2.4.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.4.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.1 09-Mar-2005  skrll file pci_machdep.h was added on branch ktrace-lwp on 2005-04-01 14:29:10 +0000
 1.2.2.2 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #694):
sys/arch/xen/xen/pci_machdep.c: revision 1.5
sys/arch/xen/include/pci_machdep.h: revision 1.4
Provide a PCI_MACHDEP_ENUMERATE_BUS to the MI pci system. We can't use
the generic pci_enumerate_bus() in Xen because the hypervisor may
hide the function 0, but give access to other functions of the same device
and pci_enumerate_bus() will stop if function 0 isn't available.
 1.2.2.1 28-Apr-2005  tron Pull up revision 1.3 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.3.4.2 30-Dec-2006  yamt sync with head.
 1.3.4.1 21-Jun-2006  yamt sync with head.
 1.3.2.2 29-Apr-2005  kent sync with -current
 1.3.2.1 16-Apr-2005  kent file pci_machdep.h was added on branch kent-audio2 on 2005-04-29 11:28:29 +0000
 1.5.12.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.5.10.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.5.8.1 11-Apr-2006  yamt sync with head
 1.5.6.1 22-Apr-2006  simonb Sync with head.
 1.5.4.1 09-Sep-2006  rpaulo sync with head
 1.6.10.1 22-Oct-2006  yamt sync with head
 1.6.8.1 18-Nov-2006  ad Sync with head.
 1.7.52.1 02-Jun-2008  mjf Sync with HEAD.
 1.9.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.9.4.4 11-Aug-2010  yamt sync with head.
 1.9.4.3 11-Mar-2010  yamt sync with head
 1.9.4.2 19-Aug-2009  yamt sync with head.
 1.9.4.1 04-May-2009  yamt sync with head.
 1.9.2.1 04-Jun-2008  yamt sync with head
 1.10.16.1 21-Apr-2010  matt sync to netbsd-5
 1.10.12.4 02-May-2011  jym Sync with head.
 1.10.12.3 24-Oct-2010  jym Sync with HEAD
 1.10.12.2 01-Nov-2009  jym Sync with HEAD.
 1.10.12.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.10.8.1 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.10.6.1 03-Mar-2009  skrll Sync with HEAD.
 1.13.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.17.4.1 06-Jun-2011  jruoho Sync with HEAD.
 1.17.2.1 21-Apr-2011  rmind sync with head
 1.18.32.1 28-Aug-2017  skrll Sync with HEAD
 1.18.14.1 03-Dec-2017  jdolecek update from HEAD
 1.19.6.1 10-Jun-2019  christos Sync with HEAD
 1.19.2.2 16-Jul-2017  cherry 2739767
 1.19.2.1 16-Jul-2017  cherry file pci_machdep.h was added on branch perseant-stdc-iso10646 on 2017-07-16 06:14:25 +0000
 1.21.4.1 04-Oct-2024  martin Pull up following revision(s) (requested by rin in ticket #928):

sys/external/bsd/drm2/dist/drm/drm_gem.c: revision 1.25
sys/external/bsd/drm2/dist/drm/radeon/radeon_ci_dpm.c: revision 1.7
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/priv.h: revision 1.4
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_acpi.c: revision 1.4
sys/external/bsd/drm2/dist/drm/i915/display/intel_opregion.h: revision 1.6
sys/external/bsd/drm2/dist/drm/i915/i915_drv.h: revision 1.49
sys/external/bsd/drm2/include/linux/mxm-wmi.h: revision 1.1
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/pci/nouveau_nvkm_subdev_pci_pcie.c: revision 1.4
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_base.c: revision 1.13
sys/external/bsd/common/include/linux/bitops.h: revision 1.17
sys/external/bsd/drm2/nouveau/files.nouveau: revision 1.40
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.30
sys/external/bsd/drm2/dist/drm/radeon/radeon_drv.h: revision 1.5
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/pci/nouveau_nvkm_subdev_pci_pcie.c: revision 1.5
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/mxm/nouveau_nvkm_subdev_mxm_base.c: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_gart.c: revision 1.12
sys/external/bsd/drm2/dist/drm/radeon/radeon_rv770.c: revision 1.3
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/disp/nouveau_nvkm_engine_disp_sorgm200.c: revision 1.3
sys/external/bsd/common/include/linux/printk.h: revision 1.14
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/instmem/nouveau_nvkm_subdev_instmem_gk20a.c: revision 1.10
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_vi.c: revision 1.4
sys/external/bsd/drm2/include/linux/acpi.h: revision 1.11
sys/external/bsd/drm2/drm/drm_cdevsw.c: revision 1.31
sys/external/bsd/drm2/dist/drm/radeon/radeon_si.c: revision 1.6
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_acpi.c: revision 1.5
sys/external/bsd/drm2/dist/drm/i915/display/intel_acpi.h: revision 1.5
sys/external/bsd/drm2/include/acpi/video.h: revision 1.3
sys/external/bsd/drm2/dist/drm/radeon/radeon_evergreen.c: revision 1.6
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_acpi.h: revision 1.4
sys/arch/sparc64/include/pci_machdep.h: revision 1.31
sys/arch/sparc64/dev/pci_machdep.c: revision 1.83
sys/external/bsd/drm2/include/linux/kref.h: revision 1.14
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_pci.c: revision 1.12
sys/external/bsd/drm2/linux/linux_dma_buf.c: revision 1.17
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/bios/nouveau_nvkm_subdev_bios_shadowacpi.c: revision 1.4
sys/external/bsd/drm2/drm/drm_module.c: revision 1.32
sys/external/bsd/drm2/dist/drm/i915/i915_gem.h: revision 1.8
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_dmabuf.c: revision 1.7
sys/external/bsd/drm2/include/linux/smp.h: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_si.c: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_device.c: revision 1.20
sys/arch/x86/x86/bus_dma.c: revision 1.91
sys/external/bsd/drm2/radeon/files.radeon: revision 1.40
sys/external/bsd/drm2/include/acpi/acpi_bus.h: revision 1.1
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_drv.h: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_device.c: revision 1.21
sys/external/bsd/common/include/asm/barrier.h: revision 1.20
sys/external/bsd/drm2/include/linux/nbsd-namespace-acpi.h: revision 1.2
sys/external/bsd/common/include/asm/barrier.h: revision 1.21
sys/modules/drmkms/drmkms_pci.h: revision 1.1
sys/external/bsd/drm2/dist/drm/drm_dp_helper.c: revision 1.17
sys/external/bsd/drm2/radeon/radeon_pci.c: revision 1.23
sys/external/bsd/drm2/linux/linux_xa.c: revision 1.4
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.23
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.24
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.25
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.26
sys/dev/pci/pcivar.h: revision 1.120
sys/arch/xen/include/pci_machdep.h: revision 1.24
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.26
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.27
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.27
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.28
sys/external/bsd/drm2/dist/drm/radeon/radeon_cik.c: revision 1.8
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.28
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.29
sys/external/bsd/drm2/include/linux/pci.h: revision 1.57
sys/external/bsd/drm2/include/linux/pci.h: revision 1.58
sys/external/bsd/drm2/dist/drm/radeon/radeon_acpi.c: revision 1.5
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_display.c: revision 1.6
sys/external/bsd/drm2/dist/drm/amd/powerplay/hwmgr/amdgpu_hwmgr.c: revision 1.3
sys/external/bsd/drm2/dist/drm/amd/display/dc/core/amdgpu_dc_stream.c: revision 1.3
share/man/man9/bus_dma.9: revision 1.69
sys/external/bsd/drm2/drm/drm_gem_cma_helper.c: revision 1.15
sys/external/bsd/drm2/dist/drm/radeon/radeon_acpi.c: revision 1.6
sys/external/bsd/drm2/dist/drm/radeon/radeon.h: revision 1.12
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_cik.c: revision 1.7
sys/dev/acpi/acpi_mcfg.c: revision 1.29
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_acpi.c: revision 1.6
sys/external/bsd/drm2/dist/drm/radeon/radeon_r600.c: revision 1.7
sys/external/bsd/drm2/dist/drm/radeon/radeon_bios.c: revision 1.13
sys/modules/amdgpu/Makefile: revision 1.9
sys/external/bsd/drm2/dist/drm/radeon/radeon_bios.c: revision 1.14
sys/external/bsd/common/linux/linux_tasklet.c: revision 1.12
sys/external/bsd/drm2/dist/drm/nouveau/include/nvkm/core/device.h: revision 1.10
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_mman.c: revision 1.23
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu.h: revision 1.9
sys/external/bsd/drm2/include/linux/interval_tree.h: revision 1.14
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_mman.c: revision 1.26
sys/external/bsd/drm2/dist/drm/amd/powerplay/hwmgr/amdgpu_smu7_hwmgr.c: revision 1.5
sys/dev/pci/pci.c: revision 1.168
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_mman.c: revision 1.27
sys/external/bsd/drm2/dist/drm/radeon/radeon_si_dpm.c: revision 1.9
sys/external/bsd/drm2/pci/files.drmkms_pci: revision 1.18
sys/external/bsd/drm2/linux/linux_sync_file.c: revision 1.3
sys/external/bsd/drm2/amdgpu/files.amdgpu: revision 1.31
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_tegra.c: revision 1.4
sys/external/bsd/drm2/dist/drm/drm_gem.c: revision 1.24
sys/arch/xen/xen/xpci_xenbus.c: revision 1.29

drm: Eliminate __HAVE_ATOMIC_AS_MEMBAR conditionals.
Discussed on tech-kern:
https://mail-index.netbsd.org/tech-kern/2023/02/23/msg028729.html

linux asm/barrier.h: Fix !MULTIPROCESSOR build.

remove "nouveau" from a comment. noted by jmcneill.

drm: KASSERT(A && B) -> KASSERT(A); KASSERT(B)
comment a function that has a clear overbounds read but it isn't used.
found by GCC 12.

nix the NetBSD specific GEM_BUG_ON().
avoids GCC 12 warnings, and matches upstream closer.
avoid uninitialised variable usage in drm_gem_cma_create_internal().
in the case nothing has returned 'error', 'nsegs' and the dma info
are (potentially) uninitialised, so consider this an error.
found by GCC 12.

avoid a GCC 12 warning.
there's a 1-element long array and a loop conditional that tries to see
if indexes for it are not identical. as these indexes will always both
be 0, the only valid index, the condition is always false. GCC 12
triggers a strange warning on this code that can never run (see below),
so simply assert the array size is 1 and comment the rest.
amdgpu_dc_stream.c:470:55: error: array subscript [0, 0] is outside array bounds of 'struct dc_writeback_info[1]' [-Werror=array-bounds]
470 | stream->writeback_info[j] = stream->writeback_info[i];

convert a KASSERT() into an if () panic() sequence to appease GCC 12.
OK riastradh@.

drm: Fix conditionals around drmkms_pci and agp.
Kernel should build now with all pci drm drivers stripped out but
DRM_LEGACY still enabled. (Might not be very useful, but it'll
build. Maybe we should also have DRM_LEGACY_PCI so those drivers can
be modloaded later.)

drmkms: Fix module build.
avoid an unlikely array bounds issue picked up by GCC 12.
nvkm_pcie_speed() can return -1, which is then used as an array index,
so make this default return PCIe 1.0 speeds.

drm: enable almost all PCIe functionality
linux_pci.c revisions 1.24 and 1.25 implemented most of the remaining
missing PCIe backends, but only enabled them for some amdgpu portions.
this enables all code marked with "XXX amdgpu pcie", "XXX radeon pcie",
and "XXX pcie speed". for most of it, simply removing #ifndefs __NetBSD__
to enable compliation was required, once the new "bus->max_bus_speed"
member was added to struct pci_bus. add an "always fails" backend for
pci_enable_atomic_ops_to_root() which seems to only be necessary
for virtual GPU functionality (and could be implemented if needed.)
tested on radeon 5450, 7750, R7 240 [radeon], and RX 550 [amdgpu], and
nvidia 750 and 1030 [nouveau].
this still does not quite work on nvidia cards. there are two problems
that remain:
- the call to set the link speed is skipped because the speed is set
to the default value of "-1". nvkm_pcie_set_link() will actually
determine the right value for this and for some cards, calling this
function if the current speed is -1 helps set the link speed. it
may be that on linux other paths we don't have enabled properly
would set this (there's one via debugfs, and a jetson specific one,
though perhaps setting either AC or DC speed values as boot options
(after hooking up these for netbsd) would currently work.
- worse, cards newer than kepler - geforce 900, 1000, and newer, are
all lacking the backing support to set pcie link speed. the GT 1030
card i have been testing with remains at pcie 1.0.

radeon: fix and enable ACPI methods for getting ROM BIOS
The hacky way of getting the BIOS mapped only works on x86. ACPI
should be preferred if available. Makes BIOS reading though VFCT
work on aarch64 with EDK2. (But only if EDK2 has POSTed the GPU.)
XXX amdgpu should get the same treatment.

drm: put_cpu() should enable preemption, not disable it again

drm(4): make pr_debug equivalent to aprint_debug
significantly reduces the default spam from amdgpu(4).

drm: Set CONFIG_ACPI in linux/acpi.h and make it build.

Leave a little ACPI-related functionality disabled for now, like
getting EDID out of ACPI -- needs a bit more work to make this work,
and I don't have hardware to work on that.
Should help with failures of the forms:
- unable to locate a BIOS ROM
- bios: unable to locate usable image
on various machines.

radeon_acpi.c: ifdef out unused function on NetBSD.
Should fix syzkaller build.

drm(4): Fix st_rdev in stat.
dminor->index already has the 64*type adjustment, as allocated in
drm_minor_alloc.
PR kern/58180

linux_sync_file: Fix missing init/fini steps.
Noted by rjs@.
PR kern/58210

ttm: Sync ttm_bo_uvm_fault_idle better with Linux.
PR xsrc/58133
ttm: Undo mistake in previous.

PR xsrc/58133
linux: Add a few more cases to pci_get_class.
Should fix crash on boot with amdgpu now that the ACPI business is
enabled.

i915: Fix dmabuf mmap object.

drm: Fix missing bounds checks in dma buf mmap.

drm_gem.c: Fix sense of assertion.
This is the opposite of WARN_ON.
Noted by rjs@.

drm_gem.c: Enable drm_gem_fence_array_add now that we emulate xa.
linux_xa: Delete and replace collision in xa_store as intended.
Don't free the colliding node that's still in the tree.
Noted by rjs@.

i915_gem_mman.c: Apply mmap types via pmap flags.
This way, userland gets buffers mapped write-combining or uncached as
needed.
PR xsrc/58307

x86: Teach bus_dmamem_map about BUS_DMA_PREFETCHABLE.
PR port-amd64/58308

bus_dma(9): Document BUS_DMA_PREFETCHABLE.
Like BUS_DMA_NOCACHE. Doesn't absolve you of the need for
bus_dmamap_sync, but if you later pass the vaddr to bus_dmamap_load,
the DMA map might notice the mapping is write-combining and use this
to make bus_dmamap_sync cheaper.
PR kern/58309

nouveau_nvkm_subdev_instmem_gk20a.c: Use BUS_DMA_PREFETCHABLE.
Matches Linux's pgprot_writecombine.
Unclear where the appropriate bus_dmamap_sync happens, or is supposed
to happen -- not using it would be wrong, but asking for a
prefetchable mapping may paper over symptoms, at least!

ttm: Sync more with Linux.
Add the original copyright and attribution since this is now,
intentionally, a modified copy of the original and not just roughly
the same algorithm.

ttm: Respect PGO_ALLPAGES.
Not sure this is useful but it reduces XXX's and makes this match
udv_fault better so it's easier to understand.

ttm: Sync cacheability flag logic with Linux.

ttm: Add XXX about readahead fault failures.

pci: Pass cookie through pci_find_device, pci_enumerate_bus, take 2.
New functions pci_find_device1 and pci_enumerate_bus1 have the cookie
argument. Existing symbols pci_find_device and pci_enumerate_bus are
now wrappers for the cookieless version.
This will allow pci_find_device callers to pass a cookie through to
the match function so they can keep state or pass in extra parameters
like b/d/f numbers, which will allow us to nix some horrible kludges
in the Linux PCI API emulation for drm (and, perhaps, Intel wifi).
This change drops the symbol pci_probe_device, in favour of a new
pci_probe_device1 with the cookie argument. But I don't think that
requires a revbump because it's only called by MD pci_enumerate_bus1
implementations, which don't live in modules anyway.
Take 2: Make sure to handle NULL match function.
linux_pci: Nix pci enumeration kludges.
Now that we can pass a cookie through, this stuff will be a little
less fragile.

i915: Omit needless i915_gem_object_pin/unpin_pages cycle in fault.
vm_fault_cpu and vm_fault_gtt, called by i915_gem_fault, already do
the pinning and unpinning internally, so there is no need for
i915_gem_fault to do it.
No functional change intended, except that the transient pin count
will be one lower than before during the fault routine (but it will
still be positive).

i915: Match Linux fault routine return code actions.
Omit needless EINTR interception -- this is now handled by
i915_error_to_vmf_fault.
Earlier revert was over a false alarm -- bisection shows the new
warnings arose from linux_pci.c 1.29 here:
https://mail-index.netbsd.org/source-changes/2024/06/23/msg151929.html

linux_pci: Fix shifto in pci_get_class.
It looks like Linux's pci_get_class also matches the interface part
of the PCI class register (but not the revision part), and I hadn't
noticed that in the previous shim structured differently.

With GCC12 kernel ALL/amd64 triggers "'sor' may be used uninitialized".
If "sublinks & 3" is zero GCC is right and sor[1] may be returned unitialized.
Fix by initializing "sor" to zero to return -1 instead of uninitialized value.
Ok: Taylor R Campbell <riastradh@>

amdgpu: Map BAR 2, not BAR 5, on pre-bonaire chips.
PR kern/58384

amdgpu: Map consecutive pages, not the same one over and over again.
PR kern/58385

linux/bitops: Fix overestimate for BITS_TO_LONGS(9)
Fortunately, this seems harmless except for allocating
excessive buffer memory.
Pointed out by nonaka@, OK riastradh@.
 1.3 02-Jul-2008  drochner remove some obviously obsolete definitions
 1.2 11-Dec-2005  christos branches: 1.2.74; 1.2.78; 1.2.80; 1.2.82;
merge ktrace-lwp.
 1.1 11-Mar-2004  cl branches: 1.1.4;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file pic.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.2.82.1 03-Jul-2008  simonb Sync with head.
 1.2.80.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.2.78.1 04-May-2009  yamt sync with head.
 1.2.74.1 28-Sep-2008  mjf Sync with HEAD.
 1.19 11-Jan-2008  bouyer 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.18 25-Dec-2007  perry Convert many of the uses of __attribute__ to equivalent
__packed, __unused and __dead macros from cdefs.h
 1.17 28-Nov-2007  ad branches: 1.17.2; 1.17.6;
Remove remaining CPUCLASS_386 tests.
 1.16 28-Nov-2007  ad Use the new atomic ops.
 1.15 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.14 15-Nov-2007  ad Remove support for 80386 level CPUs. PR port-i386/36163.
 1.13 17-Oct-2007  garbled branches: 1.13.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.12 04-Sep-2007  bouyer branches: 1.12.4;
Fix build failure when for kernel with options LOCKDEBUG.
Pointed out by Christoph Egger.
 1.11 29-Aug-2007  ad Merge most x86 changes from the vmlocking branch, except the threaded soft
interrupt stuff. This is mostly comprised of changes to the pmap modules to
work on multiprocessor systems without kernel_lock, and changes to speed up
tlb shootdowns.
 1.10 21-Feb-2007  thorpej branches: 1.10.4; 1.10.12; 1.10.16; 1.10.20; 1.10.22;
Replace the Mach-derived boolean_t type with the C99 bool type. A
future commit will replace use of TRUE and FALSE with true and false.
 1.9 16-Feb-2006  perry branches: 1.9.20;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.8 23-Jan-2006  yamt branches: 1.8.2; 1.8.4;
merge xen pmap_enter, pmap_enter_ma, pmap_remap_pages.
ok'ed by Manuel Bouyer.
 1.7 24-Dec-2005  perry branches: 1.7.2;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.6 11-Dec-2005  christos merge ktrace-lwp.
 1.5 10-Sep-2005  bouyer Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.4 09-Mar-2005  bouyer branches: 1.4.2; 1.4.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 24-Apr-2004  cl branches: 1.3.2; 1.3.6; 1.3.8; 1.3.10;
Make bus_space map machine addresses instead of physical addresses.
 1.2 10-Apr-2004  cl add prototype for pmap_kenter_ma
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.8.1 29-Apr-2005  kent sync with -current
 1.3.6.5 09-Mar-2005  bouyer bus_space_map/unmap() should map machine addresses, not physical addresses,
for memory-mapped I/O. So use pmap_kenter_ma() instead of pmap_kenter_pa(),
and introduce pmap_extract_ma() to get the machine address of a mapping.
Now memory-mapped I/O should work outside of the ISA hole (note that PCI
devices are usually mapped in the ISA memory hole anyway).
 1.3.6.4 12-Feb-2005  bouyer Implement pmap_remap_pages(), which remplace one or more pages in a
mapping with different pages, possibly from a foreing domain.
Use this to implement IOCTL_PRIVCMD_MMAP.
 1.3.6.3 17-Dec-2004  bouyer merge changes from i386/include/pmap.h 1.81:
Fix various typos in comments including ones that weren't noticed
for years.

This syncs pmap.h with i386/include/pmap.h 1.82, without 1.80 (NKPTP dynamic
adjustement) which cause the domain to reset when booting.
 1.3.6.2 17-Dec-2004  bouyer Merge in changes from arch/i386/pmap.h 1.82
 1.3.6.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.3.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.3.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.2.2 03-Aug-2004  skrll Sync with HEAD
 1.3.2.1 24-Apr-2004  skrll file pmap.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.4.4.6 21-Jan-2008  yamt sync with head
 1.4.4.5 07-Dec-2007  yamt sync with head
 1.4.4.4 27-Oct-2007  yamt sync with head.
 1.4.4.3 03-Sep-2007  yamt sync with head.
 1.4.4.2 26-Feb-2007  yamt sync with head.
 1.4.4.1 21-Jun-2006  yamt sync with head.
 1.4.2.1 20-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1104):
sys/arch/xen/xen/privcmd.c: revision 1.6
sys/arch/xen/i386/pmap.c: revision 1.14
sys/arch/xen/include/pmap.h: revision 1.5
sys/arch/xen/xen/xennetback.c: revision 1.12 via patch
sys/arch/xen/xen/xbdback.c: revision 1.15
Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.7.2.2 18-Feb-2006  yamt sync with head.
 1.7.2.1 01-Feb-2006  yamt sync with head.
 1.8.4.1 22-Apr-2006  simonb Sync with head.
 1.8.2.1 09-Sep-2006  rpaulo sync with head
 1.9.20.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.10.22.3 23-Mar-2008  matt sync with HEAD
 1.10.22.2 09-Jan-2008  matt sync with HEAD
 1.10.22.1 06-Nov-2007  matt sync with HEAD
 1.10.20.5 03-Dec-2007  joerg Sync with HEAD.
 1.10.20.4 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.10.20.3 21-Nov-2007  joerg Sync with HEAD.
 1.10.20.2 02-Oct-2007  joerg Sync with HEAD.
 1.10.20.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.10.16.2 10-Sep-2007  skrll Sync with HEAD.
 1.10.16.1 03-Sep-2007  skrll Sync with HEAD.
 1.10.12.1 03-Oct-2007  garbled Sync with HEAD
 1.10.4.2 03-Dec-2007  ad Sync with HEAD.
 1.10.4.1 09-Oct-2007  ad Sync with head.
 1.12.4.3 18-Nov-2007  bouyer Sync with HEAD
 1.12.4.2 25-Oct-2007  bouyer Finish sync with HEAD. Especially use the new x86 pmap for xenamd64.
For this:
- rename pmap_pte_set() to pmap_pte_testset()
- make pmap_pte_set() a function or macro for non-atomic PTE write
- define and use pmap_pa2pte()/pmap_pte2pa() to read/write PTE entries
- define pmap_pte_flush() which is a nop in x86 case, and flush the
MMUops queue in the Xen case
 1.12.4.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.13.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.13.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.13.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.17.6.1 11-Dec-2007  bouyer Switch i386 to x86/x86/pmap.c
 1.17.2.1 26-Dec-2007  ad Sync with head.
 1.3 11-Jan-2008  bouyer 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.2 09-Feb-2007  ad branches: 1.2.4; 1.2.26; 1.2.32; 1.2.38;
Merge newlock2 to head.
 1.1 22-Dec-2006  ad branches: 1.1.2;
file rwlock.h was initially added on branch newlock2.
 1.1.2.1 22-Dec-2006  ad Pull in x86 headers.
 1.2.38.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.2.32.1 18-Feb-2008  mjf Sync with HEAD.
 1.2.26.1 23-Mar-2008  matt sync with HEAD
 1.2.4.3 21-Jan-2008  yamt sync with head
 1.2.4.2 26-Feb-2007  yamt sync with head.
 1.2.4.1 09-Feb-2007  yamt file rwlock.h was added on branch yamt-lazymbuf on 2007-02-26 09:08:56 +0000
 1.8 11-Jan-2008  bouyer 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.7 04-Jan-2008  yamt i386:
- make tss per-cpu. this considerably speeds up context switch for,
at least, pentium4, where ltr instruction seems very slow.
i386, xen:
- kill cpu_maxproc.
kvm86:
- adapt to per-cpu tss.
- cleanup and simplify.
- move kvm86_mp_lock to more meaningful place.
- disable preemption during a call.
 1.6 26-Dec-2007  yamt - share idt entry allocation code among x86.
- introduce a function to reserve an idt entry and use it instead of
manipulating idt_allocmap directly.
- rename idt to xen_idt for amd64 xen. add missing #ifdef XEN.
 1.5 25-Dec-2007  perry Convert many of the uses of __attribute__ to equivalent
__packed, __unused and __dead macros from cdefs.h
 1.4 11-Nov-2007  ad branches: 1.4.2; 1.4.6;
Make xen build again.
 1.3 15-Jan-2006  bouyer branches: 1.3.28; 1.3.46; 1.3.48; 1.3.52; 1.3.54;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.2 11-Dec-2005  christos branches: 1.2.2;
merge ktrace-lwp.
 1.1 11-Mar-2004  cl branches: 1.1.4; 1.1.18; 1.1.20;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.20.3 21-Jan-2008  yamt sync with head
 1.1.20.2 15-Nov-2007  yamt sync with head.
 1.1.20.1 21-Jun-2006  yamt sync with head.
 1.1.18.1 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.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 11-Mar-2004  skrll file segments.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.2.2.1 01-Feb-2006  yamt sync with head.
 1.3.54.2 18-Feb-2008  mjf Sync with HEAD.
 1.3.54.1 19-Nov-2007  mjf Sync with HEAD.
 1.3.52.1 13-Nov-2007  bouyer Sync with HEAD
 1.3.48.2 23-Mar-2008  matt sync with HEAD
 1.3.48.1 09-Jan-2008  matt sync with HEAD
 1.3.46.1 11-Nov-2007  joerg Sync with HEAD.
 1.3.28.1 03-Dec-2007  ad Sync with HEAD.
 1.4.6.3 07-Jan-2008  bouyer Move Xen support to i386/i386/machdep.c. two less files in xen/ :)
 1.4.6.2 02-Jan-2008  bouyer Sync with HEAD
 1.4.6.1 11-Dec-2007  bouyer Switch i386 to x86/x86/pmap.c
 1.4.2.1 26-Dec-2007  ad Sync with head.
 1.5 28-Jul-2012  matt Fix some -fno-common fallout.
 1.4 20-Sep-2011  jym branches: 1.4.2;
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.3 17-Oct-2007  garbled branches: 1.3.34;
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.2 23-Sep-2007  bouyer Add NetBSD RCS Id.
 1.1 11-Aug-2006  yamt branches: 1.1.2; 1.1.6; 1.1.8; 1.1.10; 1.1.16; 1.1.22; 1.1.30; 1.1.40; 1.1.42;
"xm shutdown" support for xen3. ok'ed by Manuel Bouyer.
 1.1.42.1 06-Nov-2007  matt sync with HEAD
 1.1.40.1 02-Oct-2007  joerg Sync with HEAD.
 1.1.30.1 03-Oct-2007  garbled Sync with HEAD
 1.1.22.1 09-Oct-2007  ad Sync with head.
 1.1.16.3 27-Oct-2007  yamt sync with head.
 1.1.16.2 30-Dec-2006  yamt sync with head.
 1.1.16.1 11-Aug-2006  yamt file shutdown_xenbus.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.10.2 09-Sep-2006  rpaulo sync with head
 1.1.10.1 11-Aug-2006  rpaulo file shutdown_xenbus.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.1.8.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.1.6.2 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.1.6.1 11-Aug-2006  ghen file shutdown_xenbus.h was added on branch netbsd-3 on 2006-09-08 10:27:36 +0000
 1.1.2.2 11-Aug-2006  yamt sync with head
 1.1.2.1 11-Aug-2006  yamt file shutdown_xenbus.h was added on branch yamt-pdpolicy on 2006-08-11 15:43:16 +0000
 1.3.34.1 27-Aug-2011  jym Rename the functions for suspend to reflect that Xen does not hijack
the ACPI "sleepstate" sysctl(7) node anymore.

Add a boolean value to mark that the save/suspend operation has been
notified by dom0, so as to avoid possible errors where admin would like
to schedule the domain for sleep without dom0 being prepared for that. Fail
with EAGAIN in this case.

Sprinkle some KNF.
 1.4.2.1 30-Oct-2012  yamt sync with head
 1.10 11-Jan-2008  bouyer 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.9 08-Jan-2008  joerg Switch Xen to generic TODR. Tested by Manuel Bouyer.
 1.8 04-Jan-2008  yamt i386:
- make tss per-cpu. this considerably speeds up context switch for,
at least, pentium4, where ltr instruction seems very slow.
i386, xen:
- kill cpu_maxproc.
kvm86:
- adapt to per-cpu tss.
- cleanup and simplify.
- move kvm86_mp_lock to more meaningful place.
- disable preemption during a call.
 1.7 17-Oct-2007  garbled branches: 1.7.2; 1.7.8;
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.6 14-Jul-2007  ad branches: 1.6.10;
Generic soft interrupts are mandatory.
 1.5 12-May-2007  jld Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.4 09-Feb-2007  ad branches: 1.4.2; 1.4.6; 1.4.8; 1.4.14;
Merge newlock2 to head.
 1.3 03-Sep-2006  bjh21 branches: 1.3.2; 1.3.4; 1.3.8; 1.3.10;
Nothing in the kernel now tests __HAVE_NWSCONS, so stop defining it everywhere.
 1.2 07-Jun-2006  kardel branches: 1.2.4; 1.2.6; 1.2.8;
no timecounters yet - provide a local types.h
 1.1 30-Apr-2006  kardel branches: 1.1.2;
file types.h was initially added on branch simonb-timecounters.
 1.1.2.1 30-Apr-2006  kardel - provide no timecounter version of types.h as
XEN derivitives have not yet been converted
 1.2.8.3 14-Sep-2006  yamt sync with head.
 1.2.8.2 26-Jun-2006  yamt sync with head.
 1.2.8.1 07-Jun-2006  yamt file types.h was added on branch yamt-pdpolicy on 2006-06-26 12:45:40 +0000
 1.2.6.7 21-Jan-2008  yamt sync with head
 1.2.6.6 03-Sep-2007  yamt sync with head.
 1.2.6.5 26-Feb-2007  yamt sync with head.
 1.2.6.4 30-Dec-2006  yamt define __HAVE_LAZY_MBUF.
 1.2.6.3 30-Dec-2006  yamt sync with head.
 1.2.6.2 21-Jun-2006  yamt sync with head.
 1.2.6.1 07-Jun-2006  yamt file types.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:15 +0000
 1.2.4.2 19-Jun-2006  chap Sync with head.
 1.2.4.1 07-Jun-2006  chap file types.h was added on branch chap-midi on 2006-06-19 03:45:36 +0000
 1.3.10.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.3.8.1 10-Jun-2007  bouyer Pull up following revision(s) (requested by jld in ticket #711):
sys/arch/xen/xen/clock.c: revision 1.35 via patch
sys/arch/xen/include/types.h: revision 1.5 via patch
sys/arch/xen/i386/machdep.c: revision 1.38 via patch
sys/arch/xen/conf/files.xen: revision 1.60 via patch
sys/arch/xen/include/cpu.h: revision 1.19 via patch
Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.3.4.2 09-Sep-2006  rpaulo sync with head
 1.3.4.1 03-Sep-2006  rpaulo file types.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.3.2.1 29-Dec-2006  ad Checkpoint work in progress.
 1.4.14.2 03-Oct-2007  garbled Sync with HEAD
 1.4.14.1 22-May-2007  matt Update to HEAD.
 1.4.8.1 11-Jul-2007  mjf Sync with head.
 1.4.6.2 15-Jul-2007  ad Sync with head.
 1.4.6.1 27-May-2007  ad Sync with head.
 1.4.2.1 17-May-2007  yamt sync with head.
 1.6.10.3 23-Mar-2008  matt sync with HEAD
 1.6.10.2 09-Jan-2008  matt sync with HEAD
 1.6.10.1 06-Nov-2007  matt sync with HEAD
 1.7.8.2 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.7.8.1 08-Jan-2008  bouyer Sync with HEAD
 1.7.2.1 18-Feb-2008  mjf Sync with HEAD.
 1.1 06-Mar-2007  yamt branches: 1.1.4; 1.1.24;
fix vcpu after recent cpubus changes.
 1.1.24.2 03-Sep-2007  yamt sync with head.
 1.1.24.1 06-Mar-2007  yamt file vcpuvar.h was added on branch yamt-lazymbuf on 2007-09-03 14:31:35 +0000
 1.1.4.2 12-Mar-2007  rmind Sync with HEAD (missed new files in previous).
 1.1.4.1 06-Mar-2007  rmind file vcpuvar.h was added on branch yamt-idlelwp on 2007-03-12 06:14:50 +0000
 1.3 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.2 24-Apr-2004  cl branches: 1.2.2; 1.2.4; 1.2.8; 1.2.10; 1.2.12;
remove trailing empty lines
 1.1 24-Apr-2004  cl Add vga display support.
 1.2.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.10.1 29-Apr-2005  kent sync with -current
 1.2.8.1 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.2.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.2.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.2.4.2 03-Aug-2004  skrll Sync with HEAD
 1.2.4.1 24-Apr-2004  skrll file vga_xenvar.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.2.2.2 22-May-2004  he Pull up revisions 1.1-1.2 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.2.2.1 24-Apr-2004  he file vga_xenvar.h was added on branch netbsd-2-0 on 2004-05-22 15:57:25 +0000
 1.3 11-Jan-2008  bouyer 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.2 18-Oct-2007  yamt branches: 1.2.2; 1.2.8;
merge yamt-x86pmap branch.

- reduce differences between amd64 and i386. notably, share pmap.c
between them. it makes several i386 pmap improvements available to
amd64, including tlb shootdown reduction and bug fixes from Stephan Uphoff.
- implement deferred pmap switching for amd64.
- remove LARGEPAGES option. always use large pages if available.
also, make it work on amd64.
 1.1 22-Nov-2005  yamt branches: 1.1.2; 1.1.4; 1.1.22; 1.1.36; 1.1.54; 1.1.56; 1.1.58; 1.1.60;
simplify memory layout.
 1.1.60.1 25-Oct-2007  bouyer Sync with HEAD.
 1.1.58.1 24-Sep-2007  yamt a hack to make xen kernels at least buildable.
 1.1.56.2 23-Mar-2008  matt sync with HEAD
 1.1.56.1 06-Nov-2007  matt sync with HEAD
 1.1.54.1 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.1.36.1 23-Oct-2007  ad Sync with head.
 1.1.22.4 21-Jan-2008  yamt sync with head
 1.1.22.3 27-Oct-2007  yamt sync with head.
 1.1.22.2 21-Jun-2006  yamt sync with head.
 1.1.22.1 22-Nov-2005  yamt file vmparam.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:15 +0000
 1.1.4.2 11-Dec-2005  christos Sync with head.
 1.1.4.1 22-Nov-2005  christos file vmparam.h was added on branch ktrace-lwp on 2005-12-11 10:28:46 +0000
 1.1.2.2 22-Nov-2005  yamt sync with head.
 1.1.2.1 22-Nov-2005  yamt file vmparam.h was added on branch yamt-readahead on 2005-11-22 16:08:06 +0000
 1.2.8.1 11-Dec-2007  bouyer Switch i386 to x86/x86/pmap.c
 1.2.2.1 18-Feb-2008  mjf Sync with HEAD.
 1.17 02-May-2015  mlelstv Merge dk_intf and dkdriver interfaces.
Merge common disk driver functionality in ld.c with dksubr.c.
Adjust the two previous users of dk_intf (cgd and xbd) to
the changes.

bump kernel version to 7.99.14
 1.16 14-Apr-2015  riastradh Include <sys/rndsource.h> where it is actually used.

I had removed <sys/rnd.h> from files that didn't mention anything of
the rnd(9) API. But they included other files which assumed
<sys/rnd.h> had already been included.
 1.15 20-Mar-2014  skrll branches: 1.15.6;
Mechanically replace simplelock with kmutex_t.
 1.14 02-Feb-2012  tls branches: 1.14.6; 1.14.10;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.13 19-Nov-2011  tls branches: 1.13.2;
First step of random number subsystem rework described in
<20111022023242.BA26F14A158@mail.netbsd.org>. This change includes
the following:

An initial cleanup and minor reorganization of the entropy pool
code in sys/dev/rnd.c and sys/dev/rndpool.c. Several bugs are
fixed. Some effort is made to accumulate entropy more quickly at
boot time.

A generic interface, "rndsink", is added, for stream generators to
request that they be re-keyed with good quality entropy from the pool
as soon as it is available.

The arc4random()/arc4randbytes() implementation in libkern is
adjusted to use the rndsink interface for rekeying, which helps
address the problem of low-quality keys at boot time.

An implementation of the FIPS 140-2 statistical tests for random
number generator quality is provided (libkern/rngtest.c). This
is based on Greg Rose's implementation from Qualcomm.

A new random stream generator, nist_ctr_drbg, is provided. It is
based on an implementation of the NIST SP800-90 CTR_DRBG by
Henric Jungheim. This generator users AES in a modified counter
mode to generate a backtracking-resistant random stream.

An abstraction layer, "cprng", is provided for in-kernel consumers
of randomness. The arc4random/arc4randbytes API is deprecated for
in-kernel use. It is replaced by "cprng_strong". The current
cprng_fast implementation wraps the existing arc4random
implementation. The current cprng_strong implementation wraps the
new CTR_DRBG implementation. Both interfaces are rekeyed from
the entropy pool automatically at intervals justifiable from best
current cryptographic practice.

In some quick tests, cprng_fast() is about the same speed as
the old arc4randbytes(), and cprng_strong() is about 20% faster
than rnd_extract_data(). Performance is expected to improve.

The AES code in src/crypto/rijndael is no longer an optional
kernel component, as it is required by cprng_strong, which is
not an optional kernel component.

The entropy pool output is subjected to the rngtest tests at
startup time; if it fails, the system will reboot. There is
approximately a 3/10000 chance of a false positive from these
tests. Entropy pool _input_ from hardware random numbers is
subjected to the rngtest tests at attach time, as well as the
FIPS continuous-output test, to detect bad or stuck hardware
RNGs; if any are detected, they are detached, but the system
continues to run.

A problem with rndctl(8) is fixed -- datastructures with
pointers in arrays are no longer passed to userspace (this
was not a security problem, but rather a major issue for
compat32). A new kernel will require a new rndctl.

The sysctl kern.arandom() and kern.urandom() nodes are hooked
up to the new generators, but the /dev/*random pseudodevices
are not, yet.

Manual pages for the new kernel interfaces are forthcoming.
 1.12 23-Oct-2009  snj branches: 1.12.12;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.11 16-Apr-2008  cegger branches: 1.11.4; 1.11.18;
device_t / softc split
reviewed, tested and approved by bouyer
 1.10 11-Jan-2008  bouyer branches: 1.10.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.9 05-May-2006  jld branches: 1.9.38; 1.9.44; 1.9.52;
Suspend/resume support for xbd -- keep enough info on live I/O to replay.

The rest of suspend/resume isn't there yet, but the parts that touch
regular usage have been tested. Discussed on port-xen on 2006-04-25;
approved by bouyer@.
 1.8 11-Dec-2005  christos branches: 1.8.4; 1.8.6; 1.8.8; 1.8.10; 1.8.12;
merge ktrace-lwp.
 1.7 09-Mar-2005  bouyer branches: 1.7.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.6 15-Sep-2004  tls branches: 1.6.4; 1.6.6; 1.6.8;
Actually mix samples into the kernel RNG pool. There's a buglet here: all
disk samples are reported as belonging to the first disk.
 1.5 07-May-2004  cl branches: 1.5.2; 1.5.4;
add support for hot-add/-remove of block devices
 1.4 24-Apr-2004  cl Add sysctl nodes for Xen disk cookies used by domain creation tools.
 1.3 24-Apr-2004  cl Allow the block device driver to impersonate wd/sd/cd devices. This allows
for most system seamless migration from a NetBSD/i386 setup to a NetBSD/xen
setup without changing /etc/fstab or creating additional device nodes.
 1.2 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.1 17-Apr-2004  cl add block device driver
 1.5.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.5.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.5.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.5.4.2 03-Aug-2004  skrll Sync with HEAD
 1.5.4.1 07-May-2004  skrll file xbdvar.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.5.2.3 16-Sep-2004  jmc Pullup rev 1.6 (requested by tls in ticket #846)

Actually mix samples into the kernel RNG pool.
 1.5.2.2 22-May-2004  he Pull up revisions 1.1-1.5 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.5.2.1 07-May-2004  he file xbdvar.h was added on branch netbsd-2-0 on 2004-05-22 15:58:45 +0000
 1.6.8.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.6.6.1 29-Apr-2005  kent sync with -current
 1.6.4.2 18-Jan-2005  bouyer xbd_scan_finish() is dead.
 1.6.4.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.7.4.2 21-Jan-2008  yamt sync with head
 1.7.4.1 21-Jun-2006  yamt sync with head.
 1.8.12.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.8.10.1 11-May-2006  elad sync with head
 1.8.8.1 24-May-2006  yamt sync with head.
 1.8.6.1 01-Jun-2006  kardel Sync with head.
 1.8.4.1 09-Sep-2006  rpaulo sync with head
 1.9.52.1 08-Jan-2008  bouyer Make XEN kernels build again.
 1.9.44.1 18-Feb-2008  mjf Sync with HEAD.
 1.9.38.1 23-Mar-2008  matt sync with HEAD
 1.10.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.11.18.1 01-Nov-2009  jym Sync with HEAD.
 1.11.4.1 11-Mar-2010  yamt sync with head
 1.12.12.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.12.12.1 17-Apr-2012  yamt sync with head
 1.13.2.1 18-Feb-2012  mrg merge to -current.
 1.14.10.1 18-May-2014  rmind sync with head
 1.14.6.2 03-Dec-2017  jdolecek update from HEAD
 1.14.6.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.15.6.1 06-Jun-2015  skrll Sync with HEAD
 1.48 24-Mar-2023  bouyer Allow a PVH dom0 to use VGA as console: make xen_pvh_consinit() return 1 if
it handles the console and 0 otherwise (especially when console=tty0 or
console=pc is present on the command line).
In consinit() fallback to native console selection if xen_pvh_consinit()
returns 0.
 1.47 02-May-2020  bouyer branches: 1.47.20;
Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.46 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.45 09-Apr-2020  jdolecek update to __XEN_INTERFACE_VERSION__ 0x0003020a aka Xen 3.2.10

this brings grant memory v2 support:
- status separated from flags - revoking access needs just memory barrier,
no need for expensive cmpxchg16 any more
- sub-page hypervisor copy-only grants, to be used by xennet(4)
- 64-bit frame, i.e. support for DomU RAM >16TB

the grant table is now always allocated on boot to maximum size, it's now
never grown in runtime; switch back to regular kmem_alloc()/kmem_free()

code now requires v2 support, no compatibility for grant version 1 retained -
Xen v2 support predates all currently supported Xen versions

also interface for baloon changed slightly, code updated
 1.44 09-May-2019  bouyer branches: 1.44.8;
sti/cli are not allowed on Xen, we have to clear/set a bit in the
shared page. Revert x86_disable_intr/x86_enable_intr to plain function
calls on XENPV.
While there, clean up unused functions and macros, and change cli()/sti()
macros to x86_disable_intr/x86_enable_intr.
Makes Xen domU boot again
(http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/)
 1.43 04-Feb-2019  cherry Bump up XEN source API compatibility to 0x00030208 from 0x00030201,

but maintain backwards source API compilation compatibility.

ie; sources with config(5)
options __XEN_INTERFACE_VERSION__=0x00030201 # Xen 3.1 interface

should compile and run without problems.

Not that API version 0x00030201 is the lowest version we support now.
 1.42 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.41 10-Oct-2018  cherry In xen_atomic_test_and_clear_bit()

Use the appropriate sized variable for inline assembler.
 1.40 07-Oct-2018  mlelstv Support bootspec.
 1.39 26-Jul-2018  maxv Merge the content of xen_debug.c into xen_machdep.c, there is only one
function.
 1.38 26-Jul-2018  maxv Remove dead code. This looks like a leftover from when our Xen port was
being developed (2004), and it seems to have been copied from the Xen
kernel examples. It can't have any use, so get rid of it. Also remove
vprintk, unused.
 1.37 07-Jul-2016  msaitoh branches: 1.37.10; 1.37.16; 1.37.18;
KNF. Remove extra spaces. No functional change.
 1.36 29-May-2016  bouyer Switch to elf notes for amd64 instead of the old key=value list to describe the
guest requirements and support.
Add infrastructure to query the hypervisor about features support.
For verbose boot, print the features suppoted by the hypervisor for this
guest.
 1.35 20-Sep-2011  jym branches: 1.35.12; 1.35.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.34 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.33 17-Apr-2011  mrg apply some _KERNEL_OPT.
 1.32 29-Jul-2009  cegger branches: 1.32.4; 1.32.6;
remove Xen2 support.
ok bouyer@
 1.31 13-Feb-2009  bouyer Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.30 21-Oct-2008  cegger branches: 1.30.2; 1.30.4; 1.30.8; 1.30.12;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.29 21-Apr-2008  cegger branches: 1.29.2; 1.29.8;
Access Xen's vcpu info structure per-CPU.
Tested on i386 and amd64 (both dom0 and domU) by me.
Xen2 tested (both dom0 and domU) by bouyer.
OK bouyer
 1.28 19-Feb-2008  bouyer branches: 1.28.6; 1.28.8;
The event bitmasks provided by the hypervisor are unsigned long (so 64bits
on amd64). Make sure to use the right type to store and manipulate them.
This fixes amd64, where basically any event channel > 31 was not working
(and you get there after starting/stopping a domU a few times). Things
would occasionally unwedge though the spllower() callbacks.
 1.27 19-Feb-2008  bouyer Fix xenevt to not call softint_schedule() above IPL_HIGH:
Register a ipl callback for IPL_HIGH.
if the current ipl level is too high, just record the event in a bitmap,
and record IPL_HIGH as pending. The callback will process the pending events.
 1.26 11-Jan-2008  bouyer 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.25 22-Nov-2007  bouyer branches: 1.25.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.24 17-Oct-2007  garbled branches: 1.24.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.23 26-Sep-2007  ad branches: 1.23.2;
x86 changes for pcc and LKMs.

- Replace most inline assembly with proper functions. As a side effect
this reduces the size of amd64 GENERIC by about 120kB, and i386 by a
smaller amount. Nearly all of the inlines did something slow, or something
that does not need to be fast.
- Make curcpu() and curlwp functions proper, unless __GNUC__ && _KERNEL.
In that case make them inlines. Makes curlwp LKM and preemption safe.
- Make bus_space and bus_dma more LKM friendly.
- Share a few more files between the ports.
- Other minor changes.
 1.22 23-Sep-2007  bouyer branches: 1.22.2;
Ajust for Xen 3.1.0 public headers. From Christoph Egger in private mail.
 1.21 06-Mar-2006  bouyer branches: 1.21.22; 1.21.30; 1.21.40; 1.21.42;
Add xen_atomic_cmpxchg16(), which exports the cmpxchgw instruction to C.
 1.20 16-Feb-2006  perry branches: 1.20.2;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.19 15-Jan-2006  bouyer branches: 1.19.2; 1.19.4;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.18 24-Dec-2005  perry branches: 1.18.2;
__asm__ -> __asm
__const__ -> const
__inline__ -> inline
__volatile__ -> volatile
 1.17 24-Dec-2005  perry Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.16 11-Dec-2005  christos merge ktrace-lwp.
 1.15 15-Jun-2005  bouyer branches: 1.15.2;
Add support for x86 boot flags:
-s boot single user
-a ask root device
XXX -d is ifdef'd out for now, it cause early panic (no console message).
 1.14 31-May-2005  yamt always provide prototype of printk for now. because:
- currently its implementation is always compiled in.
- there're users which don't honor XENDEBUG.
 1.13 31-May-2005  yamt x86_variable_test_bit: don't remove const qualifier unnecessarily.
 1.12 11-Apr-2005  yamt fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.11 26-Mar-2005  bouyer Don't assume we're on a uniprocessor system (the hypervisor may be running SMP)
and add "lock;" barrier to avoid instruction reordering in event handlings.
This fix the last issue with event loss I was getting on SMP systems.
 1.10 09-Mar-2005  bouyer branches: 1.10.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.9 10-Dec-2004  christos branches: 1.9.2; 1.9.4; 1.9.6;
don't use va_list here; use _BSD_VA_LIST_ because we could be included
before va_list is defined. This is common practice for kernel header
files (syslog.h)
 1.8 14-Jun-2004  cl branches: 1.8.2;
cleanup low-level bit mangling code
 1.7 07-May-2004  cl Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.6 07-May-2004  cl cleanup debugging code
 1.5 26-Apr-2004  cl Move struct xen_netinfo into include/xen.h so that xen12load compiles again.
 1.4 24-Apr-2004  cl Make kernel command line parsing support additional keywords without
having to change existing code which calls the parser.
 1.3 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.2 17-Apr-2004  cl - fix event dispatching for event 0
- use struct trapframe instead of struct pt_regs
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.2 17-Jun-2004  tron Pull up revision 1.8 (requested by cl in ticket #498):
cleanup low-level bit mangling code
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.7 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.8.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.8.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.8.2.5 18-Dec-2004  skrll Sync with HEAD.
 1.8.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.8.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.8.2.2 03-Aug-2004  skrll Sync with HEAD
 1.8.2.1 14-Jun-2004  skrll file xen.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.9.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.9.4.1 29-Apr-2005  kent sync with -current
 1.9.2.6 16-Feb-2005  bouyer Glue xen_shm and the block and network backends to the system.
 1.9.2.5 12-Feb-2005  bouyer Remove prototypes for xenvfr_init and xenmachmem_init
 1.9.2.4 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.9.2.3 18-Jan-2005  bouyer The wbinvd instruction can't be used with xen, it case a kernel trap.
Implement wbinvd() with MMUEXT_FLUSH_CACHE.
Make shared_info_t volatile.
 1.9.2.2 17-Dec-2004  bouyer Merge change from rev 1.9
 1.9.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.10.2.6 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.10.2.5 28-Jun-2005  tron Pull up revision 1.15 (requested by bouyer in ticket #481):
Add support for x86 boot flags:
-s boot single user
-a ask root device
XXX -d is ifdef'd out for now, it cause early panic (no console message).
 1.10.2.4 18-Jun-2005  tron Pull up revision 1.14 (requested by yamt in ticket #462):
always provide prototype of printk for now. because:
- currently its implementation is always compiled in.
- there're users which don't honor XENDEBUG.
 1.10.2.3 18-Jun-2005  tron Pull up revision 1.13 (requested by yamt in ticket #461):
x86_variable_test_bit: don't remove const qualifier unnecessarily.
 1.10.2.2 13-Apr-2005  tron Pull up revision 1.12 (requested by yamt in ticket #146):
fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.10.2.1 30-Mar-2005  tron Pull up revision 1.11 (requested by bouyer in ticket #71):
Don't assume we're on a uniprocessor system (the hypervisor may be running SMP)
and add "lock;" barrier to avoid instruction reordering in event handlings.
This fix the last issue with event loss I was getting on SMP systems.
 1.15.2.5 27-Feb-2008  yamt sync with head.
 1.15.2.4 21-Jan-2008  yamt sync with head
 1.15.2.3 07-Dec-2007  yamt sync with head
 1.15.2.2 27-Oct-2007  yamt sync with head.
 1.15.2.1 21-Jun-2006  yamt sync with head.
 1.18.2.2 18-Feb-2006  yamt sync with head.
 1.18.2.1 01-Feb-2006  yamt sync with head.
 1.19.4.1 22-Apr-2006  simonb Sync with head.
 1.19.2.1 09-Sep-2006  rpaulo sync with head
 1.20.2.1 13-Mar-2006  yamt sync with head.
 1.21.42.3 23-Mar-2008  matt sync with HEAD
 1.21.42.2 09-Jan-2008  matt sync with HEAD
 1.21.42.1 06-Nov-2007  matt sync with HEAD
 1.21.40.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.21.40.1 02-Oct-2007  joerg Sync with HEAD.
 1.21.30.1 03-Oct-2007  garbled Sync with HEAD
 1.21.22.2 03-Dec-2007  ad Sync with HEAD.
 1.21.22.1 09-Oct-2007  ad Sync with head.
 1.22.2.1 06-Oct-2007  yamt sync with head.
 1.23.2.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.24.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.24.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.25.6.1 08-Jan-2008  bouyer Make XEN kernels build again.
 1.28.8.1 18-May-2008  yamt sync with head.
 1.28.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.28.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.29.8.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.29.2.2 19-Aug-2009  yamt sync with head.
 1.29.2.1 04-May-2009  yamt sync with head.
 1.30.12.1 21-Apr-2010  matt sync to netbsd-5
 1.30.8.8 27-Aug-2011  jym Rename the functions for suspend to reflect that Xen does not hijack
the ACPI "sleepstate" sysctl(7) node anymore.

Add a boolean value to mark that the save/suspend operation has been
notified by dom0, so as to avoid possible errors where admin would like
to schedule the domain for sleep without dom0 being prepared for that. Fail
with EAGAIN in this case.

Sprinkle some KNF.
 1.30.8.7 27-Aug-2011  jym (HEAD fix) _BSD_VA_LIST_ => va_list
 1.30.8.6 27-Aug-2011  jym Further sync with HEAD.
 1.30.8.5 02-May-2011  jym Sync with head.
 1.30.8.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.30.8.3 01-Nov-2009  jym Sync with HEAD.
 1.30.8.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.30.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.30.4.1 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.30.2.1 03-Mar-2009  skrll Sync with HEAD.
 1.32.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.32.4.1 21-Apr-2011  rmind sync with head
 1.35.30.1 09-Jul-2016  skrll Sync with HEAD
 1.35.12.1 03-Dec-2017  jdolecek update from HEAD
 1.37.18.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.37.18.1 10-Jun-2019  christos Sync with HEAD
 1.37.16.2 20-Oct-2018  pgoyette Sync with head
 1.37.16.1 28-Jul-2018  pgoyette Sync with HEAD
 1.37.10.2 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1862):

sys/arch/xen/x86/xen_intr.c: revision 1.31 (patch)
sys/arch/xen/include/xen.h (apply patch)

xen_intr.c: Use kpreempt_disable/enable around access to curcpu().

curcpu() is not otherwise guaranteed to be stable at these points.

While here, nix nonsensical membars. This need only be synchronized
with interrupts on the same CPU.

Proposed on port-xen:
https://mail-index.netbsd.org/port-xen/2022/07/13/msg010250.html
 1.37.10.1 13-Oct-2018  martin Pull up following revision(s) (requested by mlelstv in ticket #1057):

sys/arch/xen/x86/autoconf.c: revision 1.20
sys/arch/xen/include/xen.h: revision 1.40

Support bootspec.
 1.44.8.2 20-Apr-2020  bouyer Sync with HEAD
 1.44.8.1 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.47.20.1 30-Mar-2023  martin Pull up following revision(s) (requested by bouyer in ticket #131):

sys/arch/x86/x86/consinit.c: revision 1.36
sys/arch/xen/x86/pvh_consinit.c: revision 1.3
sys/arch/xen/include/xen.h: revision 1.48

Allow a PVH dom0 to use VGA as console: make xen_pvh_consinit() return 1 if
it handles the console and 0 otherwise (especially when console=tty0 or
console=pc is present on the command line).

In consinit() fallback to native console selection if xen_pvh_consinit()
returns 0.
 1.12 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.11 19-Apr-2020  jdolecek change interface for xen_shm_map() so that caller always supplies the VA,
it now fails only if the Xen hypercall fails, in which case the failure
is final

change xbdback to pre-allocate KVA on xbdback attach (and free on detach),
so it has always KVA to map the request pages

remove no longer needed KVA allocation failure handling
 1.10 08-Jan-2019  jdolecek branches: 1.10.10;
remove explicit <machine/param.h> include, code including this already includes
<sys/param.h>
 1.9 19-Oct-2009  bouyer branches: 1.9.62; 1.9.64;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.8 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.7 25-Jun-2006  bouyer branches: 1.7.62; 1.7.76;
Allow xen_shm_map() to map multiple grants in a contigous virtual address
space.
While here garbage-collect unused xen_shm_vaddr2ma()
 1.6 25-May-2006  bouyer branches: 1.6.2;
Convert xen_shm_map() and xen_shm_unmap() to the Xen3 grant table
interface.
 1.5 11-Dec-2005  christos branches: 1.5.4; 1.5.6; 1.5.8; 1.5.14;
merge ktrace-lwp.
 1.4 17-Jul-2005  tls Improvements to xbdback (the domain 0 driver that provides "xbd" virtual
disks to other domains) from Jed Davis, <jld@panix.com>:

* Issue multiple requests when necessary rather than
assuming that arbitrary requests can be mapped into single
contiguous virtual address ranges.

* Don't assume that all data for a request is consecutive
in memory. With some client OSes, it's not.

The above two changes fix data corruption issues with Linux
clients with certain filesystem block sizes.

* Gracefully handle memory or pool allocation failures after
beginning to handle a request from the ring.

* Merge contiguous requests to avoid the "64K turns into 44K + 20K
and doubles the transactions per second at the disk" problem
caused by the 11-page limit caused by the structure of Xen
ring entries. This causes a very slight performance decrease
for sequential 64K I/O if the disk is not already saturated with
requests (about 1%) but halves the transactions per second we
hit the disk with -- or better. It even compensates for bizarre
Linux behaviour like breaking long requests up into 5.5K pieces.

* Probably some stuff I forgot to mention.

Disk throughput (though not latency) is now much, much closer to the
"raw hardware" case than it was before.
 1.3 16-Apr-2005  yamt branches: 1.3.2; 1.3.4;
s/foreing/foreign/g
ok'ed by Manuel Bouyer.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4; 1.2.6;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 16-Feb-2005  bouyer branches: 1.1.2;
file xen_shm.h was initially added on branch bouyer-xen2.
 1.1.2.2 08-Mar-2005  bouyer Improve ressource shortage recovery: if xen_shm_map() fails, register a
callback wich will retry the I/O when xen_shm_unmap() is called.
 1.1.2.1 16-Feb-2005  bouyer Support functions to map/unmap guest's memory into our kernel VM space.
This will be used by the block and network back-end.
The caller provide a list of machine address pages, and we return the
address virtual address they've been mapped to. These mapping are not
registered to the pmap (we don't have physical addresses for these pages),
so change bus_dma(9) to handle these mappings as well.
 1.2.6.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.6.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.6.1 09-Mar-2005  skrll file xen_shm.h was added on branch ktrace-lwp on 2005-04-01 14:29:10 +0000
 1.2.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.4.1 09-Mar-2005  yamt file xen_shm.h was added on branch yamt-km on 2005-03-19 08:33:26 +0000
 1.2.2.2 15-Aug-2005  tron Pull up revision 1.4 (requested by tls in ticket #656):
Improvements to xbdback (the domain 0 driver that provides "xbd" virtual
disks to other domains) from Jed Davis, <jld@panix.com>:
* Issue multiple requests when necessary rather than
assuming that arbitrary requests can be mapped into single
contiguous virtual address ranges.
* Don't assume that all data for a request is consecutive
in memory. With some client OSes, it's not.
The above two changes fix data corruption issues with Linux
clients with certain filesystem block sizes.
* Gracefully handle memory or pool allocation failures after
beginning to handle a request from the ring.
* Merge contiguous requests to avoid the "64K turns into 44K + 20K
and doubles the transactions per second at the disk" problem
caused by the 11-page limit caused by the structure of Xen
ring entries. This causes a very slight performance decrease
for sequential 64K I/O if the disk is not already saturated with
requests (about 1%) but halves the transactions per second we
hit the disk with -- or better. It even compensates for bizarre
Linux behaviour like breaking long requests up into 5.5K pieces.
* Probably some stuff I forgot to mention.
Disk throughput (though not latency) is now much, much closer to the
"raw hardware" case than it was before.
 1.2.2.1 21-Apr-2005  tron Pull up revision 1.3 (requested by yamt in ticket #173):
s/foreing/foreign/g
ok'ed by Manuel Bouyer.
 1.3.4.2 30-Dec-2006  yamt sync with head.
 1.3.4.1 21-Jun-2006  yamt sync with head.
 1.3.2.2 29-Apr-2005  kent sync with -current
 1.3.2.1 16-Apr-2005  kent file xen_shm.h was added on branch kent-audio2 on 2005-04-29 11:28:29 +0000
 1.5.14.1 19-Jun-2006  chap Sync with head.
 1.5.8.1 26-Jun-2006  yamt sync with head.
 1.5.6.1 01-Jun-2006  kardel Sync with head.
 1.5.4.1 09-Sep-2006  rpaulo sync with head
 1.6.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.7.76.1 01-Nov-2009  jym Sync with HEAD.
 1.7.62.2 11-Mar-2010  yamt sync with head
 1.7.62.1 19-Aug-2009  yamt sync with head.
 1.9.64.2 21-Apr-2020  martin Sync with HEAD
 1.9.64.1 10-Jun-2019  christos Sync with HEAD
 1.9.62.1 18-Jan-2019  pgoyette Synch with HEAD
 1.10.10.1 20-Apr-2020  bouyer Sync with HEAD
 1.25 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.24 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.23 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.22 07-Apr-2020  jdolecek branches: 1.22.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.21 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.20 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.19 07-Apr-2020  jdolecek convert the node watch code to use kmem_alloc() instead of malloc()
 1.18 07-Apr-2020  jdolecek make xenbus_watch_path() static, it's not used outside xenbus_client.c
 1.17 07-Apr-2020  jdolecek revert the watch.node change, xenbus_watch_path() and xenbus_watch_path2()
need to use non-constant path
 1.16 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.15 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.14 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.13 07-Dec-2011  cegger branches: 1.13.48;
switch from xen3-public to xen-public.
 1.12 20-Sep-2011  jym branches: 1.12.2; 1.12.6;
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.11 29-Oct-2008  cegger branches: 1.11.6;
make this header standalone
 1.10 24-Oct-2008  jym branches: 1.10.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.9 16-Apr-2008  cegger branches: 1.9.4; 1.9.10;
device_t / softc split
reviewed, tested and approved by bouyer
 1.8 14-Mar-2008  ichiro make compile
- add xenbus_read_ull prototype into include/xenbus.h
 1.7 22-Nov-2007  bouyer branches: 1.7.10; 1.7.14;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.6 25-Jun-2006  bouyer branches: 1.6.6; 1.6.10; 1.6.16; 1.6.26; 1.6.34; 1.6.36; 1.6.40; 1.6.42;
Add a 'base' argument to xenbus_read_ul, so that we can read number is base
other than 10.
 1.5 23-May-2006  bouyer branches: 1.5.2; 1.5.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.4 26-Mar-2006  bouyer branches: 1.4.2; 1.4.4; 1.4.6;
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.3 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.2 15-Mar-2006  bouyer branches: 1.2.2;
Implement watch of xenstore nodes, and install watches for otherend'state
node.
 1.1 06-Mar-2006  bouyer branches: 1.1.2; 1.1.4;
Xenbus public functions
 1.1.4.5 26-Jun-2006  yamt sync with head.
 1.1.4.4 24-May-2006  yamt sync with head.
 1.1.4.3 01-Apr-2006  yamt sync with head.
 1.1.4.2 13-Mar-2006  yamt sync with head.
 1.1.4.1 06-Mar-2006  yamt file xenbus.h was added on branch yamt-pdpolicy on 2006-03-13 09:07:03 +0000
 1.1.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.2.2.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.2.2.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.4.6.1 19-Jun-2006  chap Sync with head.
 1.4.4.3 01-Jun-2006  kardel Sync with head.
 1.4.4.2 22-Apr-2006  simonb Sync with head.
 1.4.4.1 26-Mar-2006  simonb file xenbus.h 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 26-Mar-2006  tron file xenbus.h was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.5.4.5 17-Mar-2008  yamt sync with head.
 1.5.4.4 07-Dec-2007  yamt sync with head
 1.5.4.3 30-Dec-2006  yamt sync with head.
 1.5.4.2 21-Jun-2006  yamt sync with head.
 1.5.4.1 23-May-2006  yamt file xenbus.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:15 +0000
 1.5.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.6.42.1 08-Dec-2007  mjf Sync with HEAD.
 1.6.40.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.36.2 23-Mar-2008  matt sync with HEAD
 1.6.36.1 09-Jan-2008  matt sync with HEAD
 1.6.34.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.6.26.1 04-Sep-2008  skrll Sync with netbsd-4.
 1.6.16.1 03-Dec-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.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.7.14.3 17-Jan-2009  mjf Sync with HEAD.
 1.7.14.2 02-Jun-2008  mjf Sync with HEAD.
 1.7.14.1 03-Apr-2008  mjf Sync with HEAD.
 1.7.10.1 24-Mar-2008  keiichi sync with head.
 1.9.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.9.4.1 04-May-2009  yamt sync with head.
 1.10.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.11.6.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.11.6.2 01-Nov-2009  jym Sync with HEAD.
 1.11.6.1 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.12.6.1 18-Feb-2012  mrg merge to -current.
 1.12.2.1 17-Apr-2012  yamt sync with head
 1.13.48.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.13.48.1 10-Jun-2019  christos Sync with HEAD
 1.22.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.18 09-May-2019  bouyer sti/cli are not allowed on Xen, we have to clear/set a bit in the
shared page. Revert x86_disable_intr/x86_enable_intr to plain function
calls on XENPV.
While there, clean up unused functions and macros, and change cli()/sti()
macros to x86_disable_intr/x86_enable_intr.
Makes Xen domU boot again
(http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/)
 1.17 12-Feb-2019  cherry Move xen event related code which interfaces with the NetBSD interrupt
subsystem into a separate namespace where it can co-exist with the
native equivalent in PVHVM mode.

On PV, we alias and export the native symbols - this means that
although the namespace is different, the semantics must be identical.

Eg: xen_intr_establish_xname() vs. intr_establish_xname().

The specific functions we need in PVHVM are:

- spllower, xen_spllower (for native as well as XEN event spl
despatch/defer)
- xen_disable_intr()/xen_enable_intr() ,
x86_disable_intr()/x86_enable_intr()
- xen_read_psl()/xen_write_psl(),
x86_read_psl()/x86_write_psl()
- intr_establish() et. al, xen_intr_establish() et. al.

This gives us the ability to manage Paravirtualised drivers such as
xbd(4) as well as fully emulated ones such as wd(4)., for eg
 1.16 26-Jul-2018  maxv Retire XENDEBUG_LOW, and switch its only user to XENDEBUG.
 1.15 23-Oct-2009  snj branches: 1.15.62; 1.15.64;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.14 11-Jan-2008  bouyer branches: 1.14.10; 1.14.24;
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.13 22-Nov-2007  bouyer branches: 1.13.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.12 17-Oct-2007  garbled branches: 1.12.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.11 26-Sep-2007  ad branches: 1.11.2;
x86 changes for pcc and LKMs.

- Replace most inline assembly with proper functions. As a side effect
this reduces the size of amd64 GENERIC by about 120kB, and i386 by a
smaller amount. Nearly all of the inlines did something slow, or something
that does not need to be fast.
- Make curcpu() and curlwp functions proper, unless __GNUC__ && _KERNEL.
In that case make them inlines. Makes curlwp LKM and preemption safe.
- Make bus_space and bus_dma more LKM friendly.
- Share a few more files between the ports.
- Other minor changes.
 1.10 16-Feb-2006  perry branches: 1.10.24; 1.10.32; 1.10.42; 1.10.44; 1.10.46;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.9 24-Dec-2005  perry branches: 1.9.2; 1.9.4; 1.9.6;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.8 11-Dec-2005  christos merge ktrace-lwp.
 1.7 31-May-2005  yamt branches: 1.7.2;
constify.
 1.6 26-May-2005  bouyer Always call the xpq_queue*() functions at splvm(), so that it's safe to call
them from interrupt context.
xpq_flush_queue() is called from IPL_NET in if_xennet.c, and
other xpq_queue* functions may be called from interrupt context via
pmap_kenter*(). Should fix port-xen/30153.
Thanks to Jason Thorpe and YAMAMOTO Takashi for enlightments on this issue.
 1.5 22-Apr-2005  yamt don't use block scope static function decl. found by gcc4.
 1.4 09-Mar-2005  bouyer branches: 1.4.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 10-Dec-2004  christos branches: 1.3.2; 1.3.4; 1.3.6;
Move the disable_intr and enable_intr functions from xenfunc.h to cpufunc.h,
because the x86/intr.h needs them and does not include xenfunc.h. Including
xenfunc.h in cpufunc.h is a clear lose because xenfunc.h needs a boatload
of include functions in order to compile.
 1.2 07-May-2004  cl branches: 1.2.2;
replace rdr6/ldr6 with the corresponding hypervisor traps
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revision 1.2 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.2.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.2.2.5 18-Dec-2004  skrll Sync with HEAD.
 1.2.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.2.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.2.2.2 03-Aug-2004  skrll Sync with HEAD
 1.2.2.1 07-May-2004  skrll file xenfunc.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.3.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.4.1 29-Apr-2005  kent sync with -current
 1.3.2.2 17-Dec-2004  bouyer Merge chanages from rev 1.3.
 1.3.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.4.2.3 18-Jun-2005  tron Pull up revision 1.7 (requested by yamt in ticket #464):
constify.
 1.4.2.2 28-May-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #355):
Always call the xpq_queue*() functions at splvm(), so that it's safe to call
them from interrupt context.
xpq_flush_queue() is called from IPL_NET in if_xennet.c, and
other xpq_queue* functions may be called from interrupt context via
pmap_kenter*(). Should fix port-xen/30153.
Thanks to Jason Thorpe and YAMAMOTO Takashi for enlightments on this issue.
 1.4.2.1 01-May-2005  tron Pull up revision 1.5 (requested by yamt in ticket #233):
don't use block scope static function decl. found by gcc4.
 1.7.2.3 21-Jan-2008  yamt sync with head
 1.7.2.2 07-Dec-2007  yamt sync with head
 1.7.2.1 27-Oct-2007  yamt sync with head.
 1.9.6.1 22-Apr-2006  simonb Sync with head.
 1.9.4.1 09-Sep-2006  rpaulo sync with head
 1.9.2.1 18-Feb-2006  yamt sync with head.
 1.10.46.1 06-Oct-2007  yamt sync with head.
 1.10.44.3 23-Mar-2008  matt sync with HEAD
 1.10.44.2 09-Jan-2008  matt sync with HEAD
 1.10.44.1 06-Nov-2007  matt sync with HEAD
 1.10.42.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.10.42.1 02-Oct-2007  joerg Sync with HEAD.
 1.10.32.1 03-Oct-2007  garbled Sync with HEAD
 1.10.24.2 03-Dec-2007  ad Sync with HEAD.
 1.10.24.1 09-Oct-2007  ad Sync with head.
 1.11.2.2 21-Oct-2007  bouyer Factorise some Xen pmap code in x86_xpmap.c.
More xpmap_{ptom,mtop} -> xpmap_{ptom,mtop}_masked

The xenamd64 kernel is now good enough to complete a sysinst install from
xennet to xbd.
 1.11.2.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.12.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.12.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.13.6.1 09-Jan-2008  bouyer Merge xen bits to i386/i386/gdt.c. Convert remaining uses of PTE_* macros to
pmap_pte_* macros/inlines.
Fix think-o in pmap.c for native i386.
 1.14.24.3 29-Mar-2011  jym More sync fixes. And add the mbr_gpt files.
 1.14.24.2 10-Jan-2011  jym Sync with HEAD
 1.14.24.1 01-Nov-2009  jym Sync with HEAD.
 1.14.10.1 11-Mar-2010  yamt sync with head
 1.15.64.1 10-Jun-2019  christos Sync with HEAD
 1.15.62.1 28-Jul-2018  pgoyette Sync with HEAD
 1.12 26-May-2020  bouyer Implement new ioctl, needed by Xen 4.13:
IOCTL_PRIVCMD_MMAPBATCH_V2
IOCTL_PRIVCMD_MMAP_RESOURCE
IOCTL_GNTDEV_MMAP_GRANT_REF
IOCTL_GNTDEV_ALLOC_GRANT_REF
 1.11 07-Jul-2016  msaitoh branches: 1.11.22;
KNF. Remove extra spaces. No functional change.
 1.10 07-Sep-2015  dholland Final bit of PR 41200: headers that declare ioctls should include sys/ioccom.h.
This covers most if not all of the MD headers.

XXX: a lot of the ioctl definitions in some of these files are cutpasted.
 1.9 10-Jan-2011  cegger branches: 1.9.18; 1.9.36;
fix typo in ioctl definition
 1.8 15-Dec-2010  cegger add privcmd ioctl that got introduced with Xen 4
 1.7 03-Sep-2010  cegger match header protection with filename
 1.6 17-Feb-2008  bouyer branches: 1.6.10; 1.6.24; 1.6.30; 1.6.32;
Fix IOCTL_PRIVCMD_HYPERCALL issue which shows up with xen-3.1.3:
some hypercalls results are returned though the error path (depending on
sign, it's an error code or a result), and some results are interpreted in
a special way by the NetBSD kernel (e.g. -1).
Add a 'retval' member to the privcmd_hypercall_t argument, which holds
the hypercall result if it completed without error. The error code
is returned via the usual error path.
Handle the old IOCTL_PRIVCMD_HYPERCALL under COMPAT_40.

While there, make the double-inclusion protection #define match the
convention in xenio3.h and xenio.h.
 1.5 11-Dec-2005  christos branches: 1.5.24; 1.5.40; 1.5.50; 1.5.56;
merge ktrace-lwp.
 1.4 10-Sep-2005  bouyer Fix typo reported by Jed Davis on port-xen:
IOCTL_PRIVCMD_GET_MACH2PHYS_START_MFN is a read, not a write.
 1.3 24-May-2005  yamt branches: 1.3.2;
privcmd_ioctl: don't abuse errno for IOCTL_PRIVCMD_INITDOMAIN_EVTCHN.
renumber the ioctl and keep the old one for compatibility.
PR/30027.
 1.2 09-Mar-2005  bouyer branches: 1.2.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 07-May-2004  cl branches: 1.1.2; 1.1.4; 1.1.8; 1.1.10; 1.1.12;
Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.1.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.10.1 29-Apr-2005  kent sync with -current
 1.1.8.3 09-Mar-2005  bouyer Add CVS Id.
 1.1.8.2 12-Feb-2005  bouyer IOCTL_PRIVCMD_HYPERCALL is _IOWR.
 1.1.8.1 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.1.4.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 07-May-2004  skrll file xenio.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 07-May-2004  he file xenio.h was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.2.2.2 14-Sep-2005  tron Pull up following revision(s) (requested by boyuer in ticket #774):
sys/arch/xen/include/xenio.h: revision 1.4
Fix typo reported by Jed Davis on port-xen:
IOCTL_PRIVCMD_GET_MACH2PHYS_START_MFN is a read, not a write.
 1.2.2.1 28-May-2005  tron Pull up revision 1.3 (requested by yamt in ticket #351):
privcmd_ioctl: don't abuse errno for IOCTL_PRIVCMD_INITDOMAIN_EVTCHN.
renumber the ioctl and keep the old one for compatibility.
PR/30027.
 1.3.2.2 27-Feb-2008  yamt sync with head.
 1.3.2.1 21-Jun-2006  yamt sync with head.
 1.5.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.5.50.1 23-Mar-2008  matt sync with HEAD
 1.5.40.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.5.24.1 23-Mar-2008  jdc Pull up revisions: (requested by bouyer in ticket #1083)
src/sys/arch/xen/include/xenio.h 1.6
src/sys/arch/xen/include/xenio3.h 1.2
src/sys/arch/xen/xen/privcmd.c 1.25

Fix IOCTL_PRIVCMD_HYPERCALL issue which shows up with xen-3.1.3:
some hypercalls results are returned though the error path (depending on
sign, it's an error code or a result), and some results are interpreted in
a special way by the NetBSD kernel (e.g. -1).
Add a 'retval' member to the privcmd_hypercall_t argument, which holds
the hypercall result if it completed without error. The error code
is returned via the usual error path.
Handle the old IOCTL_PRIVCMD_HYPERCALL under COMPAT_40.

While there, make the double-inclusion protection #define match the
convention in xenio3.h and xenio.h.
 1.6.32.1 05-Mar-2011  rmind sync with head
 1.6.30.1 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.6.24.3 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.6.24.2 10-Jan-2011  jym Sync with HEAD
 1.6.24.1 24-Oct-2010  jym Sync with HEAD
 1.6.10.1 09-Oct-2010  yamt sync with head
 1.9.36.2 09-Jul-2016  skrll Sync with HEAD
 1.9.36.1 22-Sep-2015  skrll Sync with HEAD
 1.9.18.1 03-Dec-2017  jdolecek update from HEAD
 1.11.22.1 31-May-2020  martin Pull up following revision(s) (requested by bouyer in ticket #935):

sys/arch/xen/x86/x86_xpmap.c: revision 1.89
sys/arch/x86/include/pmap.h: revision 1.121
sys/arch/xen/xen/privcmd.c: revision 1.58
sys/external/mit/xen-include-public/dist/xen/include/public/memory.h: revision 1.2
sys/arch/xen/include/xenpmap.h: revision 1.44
sys/arch/xen/include/xenio.h: revision 1.12
sys/arch/x86/x86/pmap.c: revision 1.394
(all via patch)

Ajust pmap_enter_ma() for upcoming new Xen privcmd ioctl:
pass flags to xpq_update_foreign()

Introduce a pmap MD flag: PMAP_MD_XEN_NOTR, which cause xpq_update_foreign()
to use the MMU_PT_UPDATE_NO_TRANSLATE flag.
make xpq_update_foreign() return the raw Xen error. This will cause
pmap_enter_ma() to return a negative error number in this case, but the
only user of this code path is privcmd.c and it can deal with it.

Add pmap_enter_gnt()m which maps a set of Xen grant entries at the
specified va in the specified pmap. Use the hooks implemented for EPT to
keep track of mapped grand entries in the pmap, and unmap them
when pmap_remove() is called. This requires pmap_remove() to be split
into a pmap_remove_locked(), to be called from pmap_remove_gnt().

Implement new ioctl, needed by Xen 4.13:
IOCTL_PRIVCMD_MMAPBATCH_V2
IOCTL_PRIVCMD_MMAP_RESOURCE
IOCTL_GNTDEV_MMAP_GRANT_REF
IOCTL_GNTDEV_ALLOC_GRANT_REF

Always enable declarations needed by privcmd.c
 1.4 07-Sep-2015  dholland Final bit of PR 41200: headers that declare ioctls should include sys/ioccom.h.
This covers most if not all of the MD headers.

XXX: a lot of the ioctl definitions in some of these files are cutpasted.
 1.3 03-Sep-2010  cegger branches: 1.3.18; 1.3.36;
match header protection with filename
 1.2 17-Feb-2008  bouyer branches: 1.2.10; 1.2.24; 1.2.30; 1.2.32;
Fix IOCTL_PRIVCMD_HYPERCALL issue which shows up with xen-3.1.3:
some hypercalls results are returned though the error path (depending on
sign, it's an error code or a result), and some results are interpreted in
a special way by the NetBSD kernel (e.g. -1).
Add a 'retval' member to the privcmd_hypercall_t argument, which holds
the hypercall result if it completed without error. The error code
is returned via the usual error path.
Handle the old IOCTL_PRIVCMD_HYPERCALL under COMPAT_40.

While there, make the double-inclusion protection #define match the
convention in xenio3.h and xenio.h.
 1.1 07-May-2006  bouyer branches: 1.1.2; 1.1.6; 1.1.8; 1.1.10; 1.1.14; 1.1.20; 1.1.24; 1.1.40; 1.1.50; 1.1.56;
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.1.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.1.50.1 23-Mar-2008  matt sync with HEAD
 1.1.40.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.1.24.1 23-Mar-2008  jdc Pull up revisions: (requested by bouyer in ticket #1083)
src/sys/arch/xen/include/xenio.h 1.6
src/sys/arch/xen/include/xenio3.h 1.2
src/sys/arch/xen/xen/privcmd.c 1.25

Fix IOCTL_PRIVCMD_HYPERCALL issue which shows up with xen-3.1.3:
some hypercalls results are returned though the error path (depending on
sign, it's an error code or a result), and some results are interpreted in
a special way by the NetBSD kernel (e.g. -1).
Add a 'retval' member to the privcmd_hypercall_t argument, which holds
the hypercall result if it completed without error. The error code
is returned via the usual error path.
Handle the old IOCTL_PRIVCMD_HYPERCALL under COMPAT_40.

While there, make the double-inclusion protection #define match the
convention in xenio3.h and xenio.h.
 1.1.20.2 09-Sep-2006  rpaulo sync with head
 1.1.20.1 07-May-2006  rpaulo file xenio3.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.1.14.3 27-Feb-2008  yamt sync with head.
 1.1.14.2 21-Jun-2006  yamt sync with head.
 1.1.14.1 07-May-2006  yamt file xenio3.h was added on branch yamt-lazymbuf on 2006-06-21 14:58:15 +0000
 1.1.10.2 01-Jun-2006  kardel Sync with head.
 1.1.10.1 07-May-2006  kardel file xenio3.h was added on branch simonb-timecounters on 2006-06-01 22:35:36 +0000
 1.1.8.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.1.8.1 07-May-2006  tron file xenio3.h was added on branch peter-altq on 2006-05-24 15:48:25 +0000
 1.1.6.2 24-May-2006  yamt sync with head.
 1.1.6.1 07-May-2006  yamt file xenio3.h was added on branch yamt-pdpolicy on 2006-05-24 10:57:22 +0000
 1.1.2.2 11-May-2006  elad sync with head
 1.1.2.1 07-May-2006  elad file xenio3.h was added on branch elad-kernelauth on 2006-05-11 23:27:14 +0000
 1.2.32.1 05-Mar-2011  rmind sync with head
 1.2.30.1 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.2.24.1 24-Oct-2010  jym Sync with HEAD
 1.2.10.1 09-Oct-2010  yamt sync with head
 1.3.36.1 22-Sep-2015  skrll Sync with HEAD
 1.3.18.1 03-Dec-2017  jdolecek update from HEAD
 1.2 07-Sep-2015  dholland Final bit of PR 41200: headers that declare ioctls should include sys/ioccom.h.
This covers most if not all of the MD headers.

XXX: a lot of the ioctl definitions in some of these files are cutpasted.
 1.1 15-Dec-2010  cegger branches: 1.1.6; 1.1.20; 1.1.38;
add gnttab ioctl definitions to implement
 1.1.38.1 22-Sep-2015  skrll Sync with HEAD
 1.1.20.1 03-Dec-2017  jdolecek update from HEAD
 1.1.6.2 05-Mar-2011  rmind sync with head
 1.1.6.1 15-Dec-2010  rmind file xenio_gntdev.h was added on branch rmind-uvmplock on 2011-03-05 20:52:33 +0000
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 24-Apr-2004  cl branches: 1.1.2; 1.1.4; 1.1.8; 1.1.10; 1.1.12;
Add keyboard support and wscons config options.
 1.1.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.10.1 29-Apr-2005  kent sync with -current
 1.1.8.1 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.1.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 24-Apr-2004  skrll file xenkbcvar.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 24-Apr-2004  he file xenkbcvar.h was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.2 31-Aug-2022  bouyer Remove XENPV support from xenmem; it's only used for !XENPV at this time.
 1.1 31-Aug-2022  bouyer Work in progress on dom0 PVH support: ioctl support for tools.
Basically, in PVH mode (where XENFEAT_auto_translated_physmap is enabled),
the hypervisor will not map foreing ressources in our virtual address
space for us. Instead, we have to pass it an address in our physical
address space (but not mapped to some RAM) where the ressource will show up
and then enter this PA in pour page table.

For this, introduce xenmem_* which manage the PA space. In PVH mode this
is just allocated from the iomem_ex extent.

With this, I can start a PV domU, and the guest's kernel boots (and
the console works). It hangs because the backend driver can't map the
frontend ressources (yet).

Note that, per https://xenbits.xen.org/docs/unstable/support-matrix.html,
dom0 PVH support is still considered experimental by Xen.
 1.5 01-May-2020  jdolecek make the csum blank/undefer counters per interface
 1.4 22-Mar-2020  jdolecek actually in data_validated case, there is no need to inspect the data for Rx,
simply set the supported csum offload flags to skip the software csum
verification
 1.3 18-Mar-2020  jdolecek use NET[RT]XF_data_validated flag to mark when Tx packet has valid
checksum; this is used to skip software checksum validation on
xennet Rx side when configured for Rx offloading

in Dom0 assume that checksum is valid when the Tx mbuf has no offload flags
- in that case either it's local packet where checksum has just been
computed in software, or forwarded external packet already
verified when received on Dom0

practical offshot of this is that DomU doesn't re-verify checksum of
packets forwarded from external hosts, e.g. via bridge(4)
 1.2 16-Mar-2020  jdolecek drop the disabled M_EXT_ROMAP mbuf code, convert xennet_checksum_fill()
to use in_undefer_cksum() instead of custom code to compute the checksum
for Rx packets, and set csum_data appropriately for eventual hw offloading

make it possible to skip the sw checksum computation by appropriate Rx
flag similarily as we do for Tx

XXX for now, the Rx flag is mostly for testing as it only works for
dom0<->domu, need some further network stack changes to arrange for
the checksum to be eventually computed when packets goes outside xen
 1.1 12-Jul-2006  yamt branches: 1.1.2; 1.1.6; 1.1.10; 1.1.16; 1.1.158;
implement a simple NETTXF_csum_blank/NETRXF_csum_blank workaround
so that we can talk with linux guests at least.
just fill checksum field of received packets if the flag is set.
maybe should be revisited later.
 1.1.158.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.1.16.2 30-Dec-2006  yamt sync with head.
 1.1.16.1 12-Jul-2006  yamt file xennet_checksum.h was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.1.10.2 09-Sep-2006  rpaulo sync with head
 1.1.10.1 12-Jul-2006  rpaulo file xennet_checksum.h was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:44:56 +0000
 1.1.6.2 11-Aug-2006  yamt sync with head
 1.1.6.1 12-Jul-2006  yamt file xennet_checksum.h was added on branch yamt-pdpolicy on 2006-08-11 15:43:16 +0000
 1.1.2.2 13-Jul-2006  gdamore Merge from HEAD.
 1.1.2.1 12-Jul-2006  gdamore file xennet_checksum.h was added on branch gdamore-uart on 2006-07-13 17:49:06 +0000
 1.44 26-May-2020  bouyer Ajust pmap_enter_ma() for upcoming new Xen privcmd ioctl:
pass flags to xpq_update_foreign()
Introduce a pmap MD flag: PMAP_MD_XEN_NOTR, which cause xpq_update_foreign()
to use the MMU_PT_UPDATE_NO_TRANSLATE flag.
make xpq_update_foreign() return the raw Xen error. This will cause
pmap_enter_ma() to return a negative error number in this case, but the
only user of this code path is privcmd.c and it can deal with it.

Add pmap_enter_gnt()m which maps a set of Xen grant entries at the
specified va in the specified pmap. Use the hooks implemented for EPT to
keep track of mapped grand entries in the pmap, and unmap them
when pmap_remove() is called. This requires pmap_remove() to be split
into a pmap_remove_locked(), to be called from pmap_remove_gnt().
 1.43 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.42 30-Oct-2019  maxv branches: 1.42.6;
Switch to new PTE bits.
 1.41 13-Feb-2019  cherry branches: 1.41.4;
Further restrict the scope of XENPV to relevant parts.
 1.40 26-Jul-2018  maxv Remove the non-PAE-i386 code of Xen. The branches are reordered so that
__x86_64__ comes first, eg:

#if defined(PAE)
/* i386+PAE */
#elif defined(__x86_64__)
/* amd64 */
#else
/* i386 */
#endif

becomes

#ifdef __x86_64__
/* amd64 */
#else
/* i386+PAE */
#endif

Tested on i386pae-domU and amd64-dom0.
 1.39 08-Mar-2017  maxv branches: 1.39.12; 1.39.14;
A few changes:
* Use markers to reduce false sharing.
* Remove XENDEBUG_SYNC and several debug messages, they are just useless.
* Remove xen_vcpu_*. They are unused and not optimized: if we really
wanted to flush ranges we should pack the VAs in a mmuext_op array
instead of performing several hypercalls in a loop.
* Start removing PG_k.
* KNF, reorder, simplify and remove stupid comments.
 1.38 06-May-2014  cherry branches: 1.38.4; 1.38.8; 1.38.12;
Use the hypervisor to copy/zero pages. This saves us the extra overheads
of setting up temporary kernel mapping/unmapping.

riz@ reports savings of about 2s on a 120s kernel build.
 1.37 30-Jun-2012  jym branches: 1.37.2; 1.37.4; 1.37.12;
Extend the xpmap API, as described in [1]. This change is mechanical and
avoids exposing the MD phys_to_machine/machine_to_phys tables directly.
Added:

- xpmap_ptom handles PFN (pseudo physical) to MFN (machine frame number)
translations, and is under control of the domain.
- xpmap_mtop is its counterpart (MFN to PFN), and is under control of
hypervisor.

xpmap_ptom_map() map a pseudo-phys address to a machine address
xpmap_ptom_unmap() unmap a pseudo-phys address (invalidation)
xpmap_ptom_isvalid() check for pseudo-phys address validity

The parameters are physical/machine addresses, like bus_dma/bus_space(9).
As x86 MFNs are tracked by u_long (Xen's choice) while machine addresses
can be 64 bits entities (PAE), use ptoa() to avoid truncation when bit
shifting by PAGE_SHIFT.

I kept the same namespace (xpmap_) to avoid code churn.

[1] http://mail-index.netbsd.org/port-xen/2009/05/09/msg004951.html

XXX will document ptoa/atop/trunc_page separately.
 1.36 27-Jun-2012  jym Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.35 24-Jun-2012  jym Enable the map/unmap recursive mapping functions for all Xen ports for
save/restore.

For an unknown reason (to me) Xen refuses to update VM translations
when the entry is pointing back to itself (which is precisely
what our recursive VM model does). So enable the functions that take
care of this, which will avoid all sort of memory corruption upon restore
leading domU to trample upon itself.

Save/restore works again for amd64. The occasional domU frontend corruption is
still present, but is harmless to dom0. Now we have a working shell and
ddb inside domU, that helps debugging a tiny bit.

XXX pull-up to -6.
 1.34 20-Apr-2012  rmind - Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.

- Support up to 256 CPUs on amd64 architecture by default.

Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
 1.33 30-Dec-2011  cherry branches: 1.33.2;
per-cpu shadow directory pages should be updated locally via cross-calls. Do this.
 1.32 23-Nov-2011  jym branches: 1.32.2;
Move Xen-specific functions to Xen pmap. Requested by cherry@.

Un'ifdef XEN in xen_pmap.c, it is always defined there.
 1.31 08-Nov-2011  cherry Expose the PG_k #define pt/pd bit to both xen and "baremetal" x86. This is required, since kernel pages are mapped with user permissions in XEN/amd64 since the VM kernel runs in ring3. Since XEN/i386(including PAE) runs in ring1, supervisor mode is appropriate for these ports. We need to share this since the pmap implementation is still shared. Once the xen implementation is sufficiently independant of the x86 one, this can be made private to xen/include/xenpmap.h
 1.30 06-Nov-2011  cherry [merging from cherry-xenmp] Make the xen MMU op queue locking api private. Implement per-cpu queues.
 1.29 13-Aug-2011  cherry branches: 1.29.2;
remove unnecessary locking overhead for UP
 1.28 10-Aug-2011  cherry Introduce locking primitives for Xen pte operations, and xen helper calls for MP related MMU ops
 1.27 29-Apr-2011  jym branches: 1.27.2;
Apply DRY: xpmap_{mtop,ptom}() can reuse xpmap_{mtop,ptom}_masked() for
the frame number lookup.

No functional change.
 1.26 17-Apr-2011  mrg apply some _KERNEL_OPT.
 1.25 10-Feb-2011  jym Use only one function to pin pages with Xen, and provide macros to
call it for different levels (L1 => L4).

Replace all calls to xpq_queue_pin_table(...) in MD code with these new
functions, with proper #ifdef'ing depending on $MACHINE.

Rationale:
- only one function to modify for logging
- pushes responsibility to caller for chosing the proper pin level, rather
than Xen internal functions; this makes the pin level explicit rather than
implicit.

Boot tested for dom0 i386/amd64, PAE included. No functional change intended.
 1.24 23-Oct-2009  snj branches: 1.24.4; 1.24.6; 1.24.8;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.23 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.22 10-Mar-2009  bouyer More i386PAE fixes:
- x86_round_page, x86_trunc_page, x86_btop and x86_ptob macros are used with
physical addresses; cast to paddr_t instead of u_long. Issue pointed out
by jym@
- machine_to_phys_mapping[] is a long. This is fine as it holds page
frame numbers (and this fits in a 32bit int as physical addresses are
only 36bits), but cast to paddr_t before << PAGE_SHIFT
- xen_start_info.store_mfn is a long; cast it to paddr_t before << PAGE_SHIFT.
should fix issue pointed out by cegger@
 1.21 24-Oct-2008  jym branches: 1.21.2; 1.21.4; 1.21.8; 1.21.12;
- add mfn_to_pfn() and pfn_to_mfn() macros, for rapid conversion between
pseudo-physical and machine frame numbers.

- add HYPERVISOR_crash() for i386 and amd64. Intended to be used by a domain
to notify Xen that it crashed on purpose, and request a dump (if applicable).

No functional changes intended.

Reviewed by Christoph (cegger@).
 1.20 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.19 23-Jan-2008  bouyer branches: 1.19.6; 1.19.10; 1.19.16;
Merge the bouyer-xeni386 branch. This brings in PAE support to NetBSD xeni386
(domU only). PAE support is enabled by 'options PAE', see the new XEN3PAE_DOMU
and INSTALL_XEN3PAE_DOMU kernel config files.

See the comments in arch/i386/include/{pte.h,pmap.h} to see how it works.
In short, we still handle it as a 2-level MMU, with the second level page
directory being 4 pages in size. pmap switching is done by switching the
L2 pages in the L3 entries, instead of loading %cr3. This is almost required
by Xen, which handle the last L2 page (the one mapping 0xc0000000 - 0xffffffff)
in a very special way. But this approach should also work for native PAE
support if ever supported (in fact, the pmap should almost suport native
PAE, what's missing is bootstrap code in locore.S).
 1.18 11-Jan-2008  bouyer 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.17 28-Nov-2007  ad branches: 1.17.6;
Use the new atomic ops.
 1.16 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.15 17-Oct-2006  bouyer branches: 1.15.8; 1.15.14; 1.15.26; 1.15.28; 1.15.32; 1.15.34;
Add ELF_PADDR_OFFSET and VIRT_ENTRY strings to __xen_guest ELF section,
so that our kernels works with newer xen-3 hypervisors; and correct the value
of VIRT_BASE for dom0.
Now that we can embed the values of KERNBASE and KERNTEXTOFF in the binary
for Xen, make the domU memory layout the same as dom0 for Xen3 (making
it the other way round doens't work; probably because of alignement
constraints in the hypervisor). The old domU layout is used if options
XEN_COMPAT_030001 is present in the kernel config file. Enable this the
domU kernel config files for now, in case someone wants to run a NetBSD
domU on an older Xen3 installation.
 1.14 06-Mar-2006  bouyer branches: 1.14.12; 1.14.14;
Implement MULTI_update_va_mapping() and MULTI_update_va_mapping_otherdomain(),
which fills in multicall arguments for __HYPERVISOR_update_va_mapping
and __HYPERVISOR_update_va_mapping_otherdomain dealing with
differences between i386 and amd64.
 1.13 16-Feb-2006  perry branches: 1.13.2;
Change "inline" back to "__inline" in .h files -- C99 is still too
new, and some apps compile things in C89 mode. C89 keywords stay.

As per core@.
 1.12 15-Jan-2006  bouyer branches: 1.12.2; 1.12.4;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.11 24-Dec-2005  perry branches: 1.11.2;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.10 11-Dec-2005  christos merge ktrace-lwp.
 1.9 07-Nov-2005  yamt some assym cleanup.
- move copyin and friends from locore.S to their own file, copy.S.
share it between i386 and xen.
- defparam KERNBASE and kill KERNBASE_LOCORE hack.
- add more symbols to assym.h and use it where appropriate.
 1.8 20-Aug-2005  bouyer Implement xpq_queue_machphys_update(), which queues a request to
update the machine to physical table (to be used after a
MEMOP_increase_reservation).
 1.7 31-May-2005  yamt branches: 1.7.2;
avoid variable shadowing.
 1.6 26-May-2005  bouyer Always call the xpq_queue*() functions at splvm(), so that it's safe to call
them from interrupt context.
xpq_flush_queue() is called from IPL_NET in if_xennet.c, and
other xpq_queue* functions may be called from interrupt context via
pmap_kenter*(). Should fix port-xen/30153.
Thanks to Jason Thorpe and YAMAMOTO Takashi for enlightments on this issue.
 1.5 16-Apr-2005  yamt s/foreing/foreign/g
ok'ed by Manuel Bouyer.
 1.4 09-Mar-2005  bouyer branches: 1.4.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 26-Apr-2004  cl branches: 1.3.2; 1.3.6; 1.3.8; 1.3.10;
Rework the physical<->machine memory mapping: offset physical addresses
by 0x100000 (above the I/O Memory "hole") leaving all physical addresses
below unused, don't perform phys<->mach mapping for addresses below 0x100000
or beyond the real hardware's physical memory.

-> /dev/mem works now as expected and X works in domain0.
 1.2 24-Apr-2004  cl Make bus_space map machine addresses instead of physical addresses.
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.8.1 29-Apr-2005  kent sync with -current
 1.3.6.2 12-Feb-2005  bouyer Implement pmap_remap_pages(), which remplace one or more pages in a
mapping with different pages, possibly from a foreing domain.
Use this to implement IOCTL_PRIVCMD_MMAP.
 1.3.6.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.3.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.3.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.2.2 03-Aug-2004  skrll Sync with HEAD
 1.3.2.1 26-Apr-2004  skrll file xenpmap.h was added on branch ktrace-lwp on 2004-08-03 10:43:11 +0000
 1.4.2.5 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.4 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #696):
sys/arch/xen/i386/xen_machdep.c: revision 1.11
sys/arch/xen/include/xenpmap.h: revision 1.8
Implement xpq_queue_machphys_update(), which queues a request to
update the machine to physical table (to be used after a
MEMOP_increase_reservation).
 1.4.2.3 18-Jun-2005  tron Pull up revision 1.7 (requested by yamt in ticket #460):
avoid variable shadowing.
 1.4.2.2 28-May-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #355):
Always call the xpq_queue*() functions at splvm(), so that it's safe to call
them from interrupt context.
xpq_flush_queue() is called from IPL_NET in if_xennet.c, and
other xpq_queue* functions may be called from interrupt context via
pmap_kenter*(). Should fix port-xen/30153.
Thanks to Jason Thorpe and YAMAMOTO Takashi for enlightments on this issue.
 1.4.2.1 21-Apr-2005  tron Pull up revision 1.5 (requested by yamt in ticket #173):
s/foreing/foreign/g
ok'ed by Manuel Bouyer.
 1.7.2.5 04-Feb-2008  yamt sync with head.
 1.7.2.4 21-Jan-2008  yamt sync with head
 1.7.2.3 07-Dec-2007  yamt sync with head
 1.7.2.2 30-Dec-2006  yamt sync with head.
 1.7.2.1 21-Jun-2006  yamt sync with head.
 1.11.2.2 18-Feb-2006  yamt sync with head.
 1.11.2.1 01-Feb-2006  yamt sync with head.
 1.12.4.1 22-Apr-2006  simonb Sync with head.
 1.12.2.1 09-Sep-2006  rpaulo sync with head
 1.13.2.1 13-Mar-2006  yamt sync with head.
 1.14.14.1 22-Oct-2006  yamt sync with head
 1.14.12.1 18-Nov-2006  ad Sync with head.
 1.15.34.2 18-Feb-2008  mjf Sync with HEAD.
 1.15.34.1 08-Dec-2007  mjf Sync with HEAD.
 1.15.32.3 21-Oct-2007  bouyer Factorise some Xen pmap code in x86_xpmap.c.
More xpmap_{ptom,mtop} -> xpmap_{ptom,mtop}_masked

The xenamd64 kernel is now good enough to complete a sysinst install from
xennet to xbd.
 1.15.32.2 18-Oct-2007  bouyer Introduce xpmap_mtop_masked().
 1.15.32.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.28.2 23-Mar-2008  matt sync with HEAD
 1.15.28.1 09-Jan-2008  matt sync with HEAD
 1.15.26.2 03-Dec-2007  joerg Sync with HEAD.
 1.15.26.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.15.14.1 18-Apr-2007  thorpej Convert i386 and amd64 to the new atomic ops API.
 1.15.8.1 03-Dec-2007  ad Sync with HEAD.
 1.17.6.3 13-Jan-2008  bouyer Make non-PAE kernels build again
 1.17.6.2 13-Jan-2008  bouyer Work in progress on xeni386 PAE support:
Make xeni386 build with a 64bit paddr_t. For this vaddr_t vs paddr_t vs
pointers usages had to be clarified.
If 'options PAE' is present in a Xen3 kernel, switch paddr_t, pd_entry_t
and pt_entry_t to 64bits, and add the PAE entry in the __xen_guest ELF section.
 1.17.6.1 09-Jan-2008  bouyer Merge xen bits to i386/i386/gdt.c. Convert remaining uses of PTE_* macros to
pmap_pte_* macros/inlines.
Fix think-o in pmap.c for native i386.
 1.19.16.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.19.10.3 11-Mar-2010  yamt sync with head
 1.19.10.2 19-Aug-2009  yamt sync with head.
 1.19.10.1 04-May-2009  yamt sync with head.
 1.19.6.1 17-Jan-2009  mjf Sync with HEAD.
 1.21.12.1 21-Apr-2010  matt sync to netbsd-5
 1.21.8.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.21.8.8 02-May-2011  jym Sync with head.
 1.21.8.7 28-Mar-2011  jym Cure sync hiccups. Code with compile errors is not really useful, heh.
 1.21.8.6 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.21.8.5 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.21.8.4 01-Nov-2009  jym Sync with HEAD.
 1.21.8.3 24-Jul-2009  jym - rework the page pinning API, so that now a function is provided for
each level of indirection encountered during virtual memory translations. Update
pmap accordingly. Pinning looks cleaner that way, and it offers the possibility
to pin lower level pages if necessary (NetBSD does not do it currently).

- some fixes and comments to explain how page validation/invalidation take
place during save/restore/migrate under Xen. L2 shadow entries from PAE are now
handled, so basically, suspend/resume works with PAE.

- fixes an issue reported by Christoph (cegger@) for xencons suspend/resume
in dom0.

TODO:

- PAE save/restore is currently limited to single-user only, multi-user
support requires modifications in PAE pmap that should be discussed first. See
the comments about the L2 shadow pages cached in pmap_pdp_cache in this commit.

- grant table bug is still there; do not use the kernels of this branch
to test suspend/resume, unless you want to experience bad crashes in dom0,
and push the big red button.

Now there is light at the end of the tunnel :)

Note: XEN2 kernels will neither build nor work with this branch.
 1.21.8.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.21.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.21.4.1 30-Sep-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1040):
sys/arch/i386/include/param.h: revision 1.71
sys/arch/i386/i386/db_memrw.c: revision 1.25
sys/arch/xen/include/xenpmap.h: revision 1.22
sys/arch/xen/xen/xenevt.c: revision 1.31
More i386PAE fixes:
- x86_round_page, x86_trunc_page, x86_btop and x86_ptob macros are used with
physical addresses; cast to paddr_t instead of u_long. Issue pointed out
by jym@
- machine_to_phys_mapping[] is a long. This is fine as it holds page
frame numbers (and this fits in a 32bit int as physical addresses are
only 36bits), but cast to paddr_t before << PAGE_SHIFT
- xen_start_info.store_mfn is a long; cast it to paddr_t before << PAGE_SHIFT.
should fix issue pointed out by cegger@
 1.21.2.1 28-Apr-2009  skrll Sync with HEAD.
 1.24.8.1 17-Feb-2011  bouyer Sync with HEAD
 1.24.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.24.4.3 31-May-2011  rmind sync with head
 1.24.4.2 21-Apr-2011  rmind sync with head
 1.24.4.1 05-Mar-2011  rmind sync with head
 1.27.2.5 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.27.2.4 09-Sep-2011  cherry make #define PG_k visible on all xen archs
 1.27.2.3 17-Aug-2011  cherry Pullup relevant changes from -current
 1.27.2.2 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.27.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.29.2.5 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.29.2.4 30-Oct-2012  yamt sync with head
 1.29.2.3 23-May-2012  yamt sync with head.
 1.29.2.2 17-Apr-2012  yamt sync with head
 1.29.2.1 10-Nov-2011  yamt sync with head
 1.32.2.2 29-Apr-2012  mrg sync to latest -current.
 1.32.2.1 18-Feb-2012  mrg merge to -current.
 1.33.2.2 02-Jul-2012  jdc Pull up revisions:
src/sys/arch/xen/include/xenpmap.h revision 1.35 via patch
src/sys/arch/xen/x86/xen_pmap.c revision 1.22 via patch
(requested by jym in ticket #372).

Enable the map/unmap recursive mapping functions for all Xen ports for
save/restore.

For an unknown reason (to me) Xen refuses to update VM translations
when the entry is pointing back to itself (which is precisely
what our recursive VM model does). So enable the functions that take
care of this, which will avoid all sort of memory corruption upon restore
leading domU to trample upon itself.

Save/restore works again for amd64. The occasional domU frontend
corruption is
still present, but is harmless to dom0. Now we have a working shell and
ddb inside domU, that helps debugging a tiny bit.

XXX pull-up to -6.
 1.33.2.1 09-May-2012  riz Pull up following revision(s) (requested by rmind in ticket #202):
sys/arch/x86/include/cpuvar.h: revision 1.46
sys/arch/xen/include/xenpmap.h: revision 1.34
sys/arch/i386/include/param.h: revision 1.77
sys/arch/x86/x86/pmap_tlb.c: revision 1.5
sys/arch/x86/x86/pmap_tlb.c: revision 1.6
sys/arch/i386/i386/genassym.cf: revision 1.92
sys/arch/xen/x86/cpu.c: revision 1.91
sys/arch/x86/x86/pmap.c: revision 1.177
sys/arch/xen/x86/xen_pmap.c: revision 1.21
sys/arch/x86/acpi/acpi_wakeup.c: revision 1.31
sys/kern/subr_kcpuset.c: revision 1.5
sys/arch/amd64/include/param.h: revision 1.18
sys/sys/kcpuset.h: revision 1.5
sys/arch/x86/x86/mtrr_i686.c: revision 1.26
sys/arch/x86/x86/mtrr_i686.c: revision 1.27
sys/arch/xen/x86/x86_xpmap.c: revision 1.43
sys/arch/x86/x86/cpu.c: revision 1.98
sys/arch/amd64/amd64/mptramp.S: revision 1.14
sys/kern/sys_sched.c: revision 1.42
sys/arch/amd64/amd64/genassym.cf: revision 1.50
sys/arch/i386/i386/mptramp.S: revision 1.24
sys/arch/x86/include/pmap.h: revision 1.52
sys/arch/x86/include/cpu.h: revision 1.50
- Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.
- Support up to 256 CPUs on amd64 architecture by default.
Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
- pmap_tlb_shootdown: do not overwrite tp_cpumask with pm_cpus, but merge
like pm_kernel_cpus. Remove unecessary intersection with kcpuset_running.
Do not reset tp_userpmap if pmap_kernel().
- Remove pmap_tlb_mailbox_t wrapping, which is pointless after recent changes.
- pmap_tlb_invalidate, pmap_tlb_intr: constify for packet structure.
i686_mtrr_init_first: handle the case when there are no variable-size MTRR
registers available (i686_mtrr_vcnt == 0).
 1.37.12.1 10-Aug-2014  tls Rebase.
 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.12.1 21-Apr-2017  bouyer Sync with HEAD
 1.38.8.1 20-Mar-2017  pgoyette Sync with HEAD
 1.38.4.1 28-Aug-2017  skrll Sync with HEAD
 1.39.14.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.39.14.1 10-Jun-2019  christos Sync with HEAD
 1.39.12.1 28-Jul-2018  pgoyette Sync with HEAD
 1.41.4.1 31-May-2020  martin Pull up following revision(s) (requested by bouyer in ticket #935):

sys/arch/xen/x86/x86_xpmap.c: revision 1.89
sys/arch/x86/include/pmap.h: revision 1.121
sys/arch/xen/xen/privcmd.c: revision 1.58
sys/external/mit/xen-include-public/dist/xen/include/public/memory.h: revision 1.2
sys/arch/xen/include/xenpmap.h: revision 1.44
sys/arch/xen/include/xenio.h: revision 1.12
sys/arch/x86/x86/pmap.c: revision 1.394
(all via patch)

Ajust pmap_enter_ma() for upcoming new Xen privcmd ioctl:
pass flags to xpq_update_foreign()

Introduce a pmap MD flag: PMAP_MD_XEN_NOTR, which cause xpq_update_foreign()
to use the MMU_PT_UPDATE_NO_TRANSLATE flag.
make xpq_update_foreign() return the raw Xen error. This will cause
pmap_enter_ma() to return a negative error number in this case, but the
only user of this code path is privcmd.c and it can deal with it.

Add pmap_enter_gnt()m which maps a set of Xen grant entries at the
specified va in the specified pmap. Use the hooks implemented for EPT to
keep track of mapped grand entries in the pmap, and unmap them
when pmap_remove() is called. This requires pmap_remove() to be split
into a pmap_remove_locked(), to be called from pmap_remove_gnt().

Implement new ioctl, needed by Xen 4.13:
IOCTL_PRIVCMD_MMAPBATCH_V2
IOCTL_PRIVCMD_MMAP_RESOURCE
IOCTL_GNTDEV_MMAP_GRANT_REF
IOCTL_GNTDEV_ALLOC_GRANT_REF

Always enable declarations needed by privcmd.c
 1.42.6.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.8 16-Jul-2024  riastradh xen: Don't hotpatch away LOCK prefix in xen_mb, even on UP boots.

Both xen_mb and membar_sync are designed to provide store-before-load
ordering, but xen_mb has to provide it in synchronizing guest with
hypervisor, while membar_sync only has to provide it in synchronizing
one (guest) CPU with another (guest) CPU.

It is safe to hotpatch away the LOCK prefix in membar_sync on a
uniprocessor boot because membar_sync is only designed to coordinate
between normal memory on multiple CPUs, and is never necessary when
there's only one CPU involved.

But xen_mb is used to coordinate between the guest and the `device'
implemented by a hypervisor, which might be running on another
_physical_ CPU even if the NetBSD guest only sees one `CPU', i.e.,
one _virtual_ CPU. So even on `uniprocessor' boots, xen_mb must
still issue an instruction with store-before-load ordering on
multiprocessor systems, such as a LOCK ADD (or MFENCE, but MFENCE is
costlier for no benefit here).

No need to change xen_wmb (release ordering, load/store-before-store)
or xen_rmb (acquire ordering, load-before-load/store) because every
x86 store is a store-release and every x86 load is a load-acquire,
even on multiprocessor systems, so there's no hotpatching involved
anyway.

PR kern/57199
 1.7 25-Feb-2023  riastradh branches: 1.7.6;
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
 1.6 25-Apr-2020  bouyer branches: 1.6.20;
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.5 21-Apr-2020  jdolecek add blkif_x86_{32,64}_request_indirect types
 1.4 07-Apr-2019  bouyer branches: 1.4.4; 1.4.12;
blkif_x86_{32,64}_* are not identical to blkif_*, internal fields have
different alignements and this change their sizes. Copy them back from
their netbsd-8 definitions.
Fixes PR port-xen/54099
 1.3 02-Feb-2019  cherry Fix build. A multiline macro needs 'line continuation'.
 1.2 02-Feb-2019  cherry Remove mb(), rmb() and wmb() from the kernel namespace.

These are introduced by external/bsd/common/include/asm/barrier.h

The purpose of barrier.h is to bridge the use of linux API calls
within code which uses them, such as drm code. The XEN api implicitly
uses these calls which are linuxisms within io/ring.h

This diff undos the damage.

The correct fix is to modify io/ring.h to not assume that all OSs that
XEN runs on has these functions, and to appropriately conditionally via
#ifdef __NetBSD__/#endif use the appropriate NetBSD functions. These
changes then need to be pushed upstream.
 1.1 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.4.12.1 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.4.4.3 21-Apr-2020  martin Sync with HEAD
 1.4.4.2 10-Jun-2019  christos Sync with HEAD
 1.4.4.1 07-Apr-2019  christos file xenring.h was added on branch phil-wifi on 2019-06-10 22:06:54 +0000
 1.6.20.2 20-Jul-2024  martin Pull up following revision(s) (requested by riastradh in ticket #764):

common/lib/libc/arch/i386/atomic/atomic.S: revision 1.37
sys/arch/xen/include/xenring.h: revision 1.8
sys/arch/i386/i386/cpufunc.S: revision 1.52
sys/arch/amd64/amd64/cpufunc.S: revision 1.68
sys/arch/xen/include/hypervisor.h: revision 1.60
common/lib/libc/arch/x86_64/atomic/atomic.S: revision 1.30

xen: Don't hotpatch away LOCK prefix in xen_mb, even on UP boots.

Both xen_mb and membar_sync are designed to provide store-before-load
ordering, but xen_mb has to provide it in synchronizing guest with
hypervisor, while membar_sync only has to provide it in synchronizing
one (guest) CPU with another (guest) CPU.

It is safe to hotpatch away the LOCK prefix in membar_sync on a
uniprocessor boot because membar_sync is only designed to coordinate
between normal memory on multiple CPUs, and is never necessary when
there's only one CPU involved.

But xen_mb is used to coordinate between the guest and the `device'
implemented by a hypervisor, which might be running on another
_physical_ CPU even if the NetBSD guest only sees one `CPU', i.e.,
one _virtual_ CPU. So even on `uniprocessor' boots, xen_mb must
still issue an instruction with store-before-load ordering on
multiprocessor systems, such as a LOCK ADD (or MFENCE, but MFENCE is
costlier for no benefit here).

No need to change xen_wmb (release ordering, load/store-before-store)
or xen_rmb (acquire ordering, load-before-load/store) because every
x86 store is a store-release and every x86 load is a load-acquire,
even on multiprocessor systems, so there's no hotpatching involved
anyway.

PR kern/57199
 1.6.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.7.6.1 02-Aug-2025  perseant Sync with HEAD
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file bus_private.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file bus_private.h was added on branch matt-armv6 on 2008-01-09 01:50:08 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file bus_private.h was added on branch mjf-devfs on 2007-11-22 16:16:59 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file bus_private.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:12 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file bus_private.h was added on branch jmcneill-pm on 2007-11-27 19:36:10 +0000
 1.14 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.13 21-Apr-2020  jdolecek convert to newer HYPERVISOR_physdev_op() interface, now command and the
arg are separate arguments - this is needed for newer physdev_op commands

remove code for PHYSDEVOP_IRQ_UNMASK_NOTIFY, it is obsolete since
interface version 0x00030202 and is unsupported by newer versions of Xen

confirmed working on amd64 Dom0, i386 compile-tested only
 1.12 10-Feb-2019  cherry branches: 1.12.10;
Catchup hypercall interfaces for HYPERVISOR_sched_op which use
arguments to __XEN_INTERFACE_VERSION__ >= 0x00030201

We've been using the sched_op_compat API with sched_op arguments.

fixes PR port-xen/53965
 1.11 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.10 24-Jan-2019  cherry The event_channel_op hypercall uses a newer API since
__XEN_INTERFACE_VERSION__ 0x00030202

Since hvm_op only supports event_channel_op via the newer API, we
can't get away with our current event_channel_op_compat shim.

We thus introduce the new API to our internal hypercall C API
interface.

This change should have no effect on the PV kernels, since they will
continue to use the pre 0x00030202 API.
 1.9 24-Jan-2019  cherry hvm_op returns a signed value.

The pattern is that a hypercall which returns a value < 0 may imply an
error.
 1.8 07-Dec-2011  cegger branches: 1.8.46; 1.8.48;
switch from xen3-public to xen-public.
 1.7 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.6 30-Mar-2011  jym branches: 1.6.2; 1.6.4; 1.6.8;
Add the HYPERVISOR_sysctl() hypercall.

Although the hypercall arguments (like struct sysctl_readconsole) are not
compatible between different XEN_SYSCTL_INTERFACE_VERSIONs (one of the
reasons why the sysctl calls should only be used by xentools directly),
it's still practical to have when one wants to query Xen's dmesg from
ddb(4) in case of a panic.

Note: additional code is needed for readconsole() functionality, but adding
the hypercall should not cause any harm.
 1.5 13-Nov-2008  cegger branches: 1.5.4; 1.5.8; 1.5.10;
prepare move to new interface
 1.4 24-Oct-2008  jym branches: 1.4.2;
- add mfn_to_pfn() and pfn_to_mfn() macros, for rapid conversion between
pseudo-physical and machine frame numbers.

- add HYPERVISOR_crash() for i386 and amd64. Intended to be used by a domain
to notify Xen that it crashed on purpose, and request a dump (if applicable).

No functional changes intended.

Reviewed by Christoph (cegger@).
 1.3 25-Aug-2008  cegger Add machine check hypercall.
There will be one file where this will be used. In the initialization a hypervisor version check will verify, if this feature is usable or not.
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16; 1.2.22; 1.2.26; 1.2.28; 1.2.32;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file hypercalls.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.32.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.2.32.1 19-Oct-2008  haad Sync with HEAD.
 1.2.28.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.2.26.1 04-May-2009  yamt sync with head.
 1.2.22.2 17-Jan-2009  mjf Sync with HEAD.
 1.2.22.1 28-Sep-2008  mjf Sync with HEAD.
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file hypercalls.h was added on branch matt-armv6 on 2008-01-09 01:50:09 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file hypercalls.h was added on branch mjf-devfs on 2007-11-22 16:16:59 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file hypercalls.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:12 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file hypercalls.h was added on branch jmcneill-pm on 2007-11-27 19:36:10 +0000
 1.4.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.5.10.1 06-Jun-2011  jruoho Sync with HEAD.
 1.5.8.1 21-Apr-2011  rmind sync with head
 1.5.4.5 02-May-2011  jym Sync with head.
 1.5.4.4 30-Mar-2011  jym Sync with my commits in HEAD.
 1.5.4.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.4.2 01-Nov-2009  jym Sync with HEAD.
 1.5.4.1 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.6.8.1 18-Feb-2012  mrg merge to -current.
 1.6.4.1 17-Apr-2012  yamt sync with head
 1.6.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.8.48.1 10-Jun-2019  christos Sync with HEAD
 1.8.46.1 26-Jan-2019  pgoyette Sync with HEAD
 1.12.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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file i82093var.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file i82093var.h was added on branch matt-armv6 on 2008-01-09 01:50:09 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file i82093var.h was added on branch mjf-devfs on 2007-11-22 16:17:00 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file i82093var.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:12 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file i82093var.h was added on branch jmcneill-pm on 2007-11-27 19:36:11 +0000
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file i82489var.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file i82489var.h was added on branch matt-armv6 on 2008-01-09 01:50:09 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file i82489var.h was added on branch mjf-devfs on 2007-11-22 16:17:00 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file i82489var.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:13 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file i82489var.h was added on branch jmcneill-pm on 2007-11-27 19:36:12 +0000
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file intr.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file intr.h was added on branch matt-armv6 on 2008-01-09 01:50:10 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file intr.h was added on branch mjf-devfs on 2007-11-22 16:17:01 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file intr.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:13 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file intr.h was added on branch jmcneill-pm on 2007-11-27 19:36:12 +0000
 1.3 11-Jan-2008  bouyer 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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.10; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file intrdefs.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file intrdefs.h was added on branch matt-armv6 on 2008-01-09 01:50:10 +0000
 1.2.14.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.14.1 22-Nov-2007  mjf file intrdefs.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.10.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.2.6.3 21-Jan-2008  yamt sync with head
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file intrdefs.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:13 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file intrdefs.h was added on branch jmcneill-pm on 2007-11-27 19:36:13 +0000
 1.3 11-Jan-2008  bouyer 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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.10; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file mpacpi.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file mpacpi.h was added on branch matt-armv6 on 2008-01-09 01:50:10 +0000
 1.2.14.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.14.1 22-Nov-2007  mjf file mpacpi.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.10.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.2.6.3 21-Jan-2008  yamt sync with head
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file mpacpi.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:13 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file mpacpi.h was added on branch jmcneill-pm on 2007-11-27 19:36:14 +0000
 1.3 11-Jan-2008  bouyer 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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.10; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file mutex.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file mutex.h was added on branch matt-armv6 on 2008-01-09 01:50:10 +0000
 1.2.14.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.14.1 22-Nov-2007  mjf file mutex.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.10.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.2.6.3 21-Jan-2008  yamt sync with head
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file mutex.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:14 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file mutex.h was added on branch jmcneill-pm on 2007-11-27 19:36:14 +0000
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file pci_machdep.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file pci_machdep.h was added on branch matt-armv6 on 2008-01-09 01:50:11 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file pci_machdep.h was added on branch mjf-devfs on 2007-11-22 16:17:02 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file pci_machdep.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:14 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file pci_machdep.h was added on branch jmcneill-pm on 2007-11-27 19:36:15 +0000
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file pic.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file pic.h was added on branch matt-armv6 on 2008-01-09 01:50:11 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file pic.h was added on branch mjf-devfs on 2007-11-22 16:17:02 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file pic.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:14 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file pic.h was added on branch jmcneill-pm on 2007-11-27 19:36:15 +0000
 1.3 11-Jan-2008  bouyer 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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.10; 1.2.14; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file rwlock.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file rwlock.h was added on branch matt-armv6 on 2008-01-09 01:50:11 +0000
 1.2.14.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.14.1 22-Nov-2007  mjf file rwlock.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.10.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.2.6.3 21-Jan-2008  yamt sync with head
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file rwlock.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:15 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file rwlock.h was added on branch jmcneill-pm on 2007-11-27 19:36:16 +0000
 1.4 25-Aug-2023  riastradh xen: Provide definitions or ifdefs to make drm build in XEN3_DOM0.

No idea if it works, but it builds now.

PR port-xen/49330
 1.3 14-Feb-2019  cherry Snag the final bits of PV only code to conditionally compile under
-DXENPV

This completes the bifurcation.

The next step is to add -DXENPVHVM code.
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.6; 1.2.14; 1.2.16; 1.2.114;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 19-Nov-2007  bouyer branches: 1.1.2;
file vmparam.h was initially added on branch bouyer-xenamd64.
 1.1.2.1 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 2: move xenamd64/include to xen/include/amd64
 1.2.114.1 10-Jun-2019  christos Sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file vmparam.h was added on branch matt-armv6 on 2008-01-09 01:50:11 +0000
 1.2.14.2 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.2.14.1 22-Nov-2007  bouyer file vmparam.h was added on branch mjf-devfs on 2007-11-22 16:17:03 +0000
 1.2.6.2 07-Dec-2007  yamt sync with head
 1.2.6.1 22-Nov-2007  yamt file vmparam.h was added on branch yamt-lazymbuf on 2007-12-07 17:27:15 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file vmparam.h was added on branch jmcneill-pm on 2007-11-27 19:36:16 +0000
 1.2 11-Jan-2008  bouyer branches: 1.2.2; 1.2.4; 1.2.12;
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.1 10-Jan-2008  bouyer branches: 1.1.2;
file bus_private.h was initially added on branch bouyer-xeni386.
 1.1.2.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.12.2 23-Mar-2008  matt sync with HEAD
 1.2.12.1 11-Jan-2008  matt file bus_private.h was added on branch matt-armv6 on 2008-03-23 02:04:30 +0000
 1.2.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.4.1 11-Jan-2008  mjf file bus_private.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.2.2 21-Jan-2008  yamt sync with head
 1.2.2.1 11-Jan-2008  yamt file bus_private.h was added on branch yamt-lazymbuf on 2008-01-21 09:40:28 +0000
 1.21 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.20 21-Apr-2020  jdolecek convert to newer HYPERVISOR_physdev_op() interface, now command and the
arg are separate arguments - this is needed for newer physdev_op commands

remove code for PHYSDEVOP_IRQ_UNMASK_NOTIFY, it is obsolete since
interface version 0x00030202 and is unsupported by newer versions of Xen

confirmed working on amd64 Dom0, i386 compile-tested only
 1.19 10-Feb-2019  cherry branches: 1.19.10;
Catchup hypercall interfaces for HYPERVISOR_sched_op which use
arguments to __XEN_INTERFACE_VERSION__ >= 0x00030201

We've been using the sched_op_compat API with sched_op arguments.

fixes PR port-xen/53965
 1.18 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.17 24-Jan-2019  cherry The event_channel_op hypercall uses a newer API since
__XEN_INTERFACE_VERSION__ 0x00030202

Since hvm_op only supports event_channel_op via the newer API, we
can't get away with our current event_channel_op_compat shim.

We thus introduce the new API to our internal hypercall C API
interface.

This change should have no effect on the PV kernels, since they will
continue to use the pre 0x00030202 API.
 1.16 26-Jul-2018  maxv Remove the non-PAE-i386 code of Xen. The branches are reordered so that
__x86_64__ comes first, eg:

#if defined(PAE)
/* i386+PAE */
#elif defined(__x86_64__)
/* amd64 */
#else
/* i386 */
#endif

becomes

#ifdef __x86_64__
/* amd64 */
#else
/* i386+PAE */
#endif

Tested on i386pae-domU and amd64-dom0.
 1.15 27-Jun-2012  jym branches: 1.15.38; 1.15.40;
Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.14 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.13 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.12 07-Jun-2011  bouyer branches: 1.12.2; 1.12.6;
Don't call psignal() without holding proc_lock. This is the cause of
the reboot of PR port-xen/45028
Now that Xen2 is gone, handle FPU context switches the same way as
amd64. This makes all tests in /usr/tests/lib/libc/ieeefp pass.
 1.11 30-Mar-2011  jym branches: 1.11.2;
Add the HYPERVISOR_sysctl() hypercall.

Although the hypercall arguments (like struct sysctl_readconsole) are not
compatible between different XEN_SYSCTL_INTERFACE_VERSIONs (one of the
reasons why the sysctl calls should only be used by xentools directly),
it's still practical to have when one wants to query Xen's dmesg from
ddb(4) in case of a panic.

Note: additional code is needed for readconsole() functionality, but adding
the hypercall should not cause any harm.
 1.10 19-Oct-2009  bouyer branches: 1.10.4; 1.10.6;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.9 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.8 13-Nov-2008  cegger branches: 1.8.4;
add platform_op hypercall (already exists for amd64)
 1.7 13-Nov-2008  cegger prepare move to new interface
 1.6 24-Oct-2008  jym branches: 1.6.2;
- add mfn_to_pfn() and pfn_to_mfn() macros, for rapid conversion between
pseudo-physical and machine frame numbers.

- add HYPERVISOR_crash() for i386 and amd64. Intended to be used by a domain
to notify Xen that it crashed on purpose, and request a dump (if applicable).

No functional changes intended.

Reviewed by Christoph (cegger@).
 1.5 21-Oct-2008  cegger catch up with amd64: add hvm_op hypercall
 1.4 25-Aug-2008  cegger Add machine check hypercall.
There will be one file where this will be used. In the initialization a hypervisor version check will verify, if this feature is usable or not.
 1.3 23-Jan-2008  bouyer branches: 1.3.2; 1.3.8; 1.3.10; 1.3.14; 1.3.16; 1.3.20;
Merge the bouyer-xeni386 branch. This brings in PAE support to NetBSD xeni386
(domU only). PAE support is enabled by 'options PAE', see the new XEN3PAE_DOMU
and INSTALL_XEN3PAE_DOMU kernel config files.

See the comments in arch/i386/include/{pte.h,pmap.h} to see how it works.
In short, we still handle it as a 2-level MMU, with the second level page
directory being 4 pages in size. pmap switching is done by switching the
L2 pages in the L3 entries, instead of loading %cr3. This is almost required
by Xen, which handle the last L2 page (the one mapping 0xc0000000 - 0xffffffff)
in a very special way. But this approach should also work for native PAE
support if ever supported (in fact, the pmap should almost suport native
PAE, what's missing is bootstrap code in locore.S).
 1.2 11-Jan-2008  bouyer branches: 1.2.2;
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.1 10-Jan-2008  bouyer branches: 1.1.2;
file hypercalls.h was initially added on branch bouyer-xeni386.
 1.1.2.4 20-Jan-2008  bouyer Remove debug printk()
 1.1.2.3 13-Jan-2008  bouyer Make non-PAE kernels build again.
 1.1.2.2 13-Jan-2008  bouyer Work in progress on xeni386 PAE support:
Make xeni386 build with a 64bit paddr_t. For this vaddr_t vs paddr_t vs
pointers usages had to be clarified.
If 'options PAE' is present in a Xen3 kernel, switch paddr_t, pd_entry_t
and pt_entry_t to 64bits, and add the PAE entry in the __xen_guest ELF section.
 1.1.2.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.2.3 04-Feb-2008  yamt sync with head.
 1.2.2.2 21-Jan-2008  yamt sync with head
 1.2.2.1 11-Jan-2008  yamt file hypercalls.h was added on branch yamt-lazymbuf on 2008-01-21 09:40:29 +0000
 1.3.20.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.3.20.1 19-Oct-2008  haad Sync with HEAD.
 1.3.16.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.3.14.3 11-Mar-2010  yamt sync with head
 1.3.14.2 19-Aug-2009  yamt sync with head.
 1.3.14.1 04-May-2009  yamt sync with head.
 1.3.10.2 23-Mar-2008  matt sync with HEAD
 1.3.10.1 23-Jan-2008  matt file hypercalls.h was added on branch matt-armv6 on 2008-03-23 02:04:30 +0000
 1.3.8.2 17-Jan-2009  mjf Sync with HEAD.
 1.3.8.1 28-Sep-2008  mjf Sync with HEAD.
 1.3.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.3.2.1 23-Jan-2008  mjf file hypercalls.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.6.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.8.4.6 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.8.4.5 02-May-2011  jym Sync with head.
 1.8.4.4 30-Mar-2011  jym Sync with my commits in HEAD.
 1.8.4.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.8.4.2 01-Nov-2009  jym Sync with HEAD.
 1.8.4.1 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.10.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.10.4.2 12-Jun-2011  rmind sync with head
 1.10.4.1 21-Apr-2011  rmind sync with head
 1.11.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.12.6.1 18-Feb-2012  mrg merge to -current.
 1.12.2.2 30-Oct-2012  yamt sync with head
 1.12.2.1 17-Apr-2012  yamt sync with head
 1.15.40.1 10-Jun-2019  christos Sync with HEAD
 1.15.38.2 26-Jan-2019  pgoyette Sync with HEAD
 1.15.38.1 28-Jul-2018  pgoyette Sync with HEAD
 1.19.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.2 11-Jan-2008  bouyer branches: 1.2.2; 1.2.4; 1.2.12;
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.1 10-Jan-2008  bouyer branches: 1.1.2;
file i82093var.h was initially added on branch bouyer-xeni386.
 1.1.2.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.12.2 23-Mar-2008  matt sync with HEAD
 1.2.12.1 11-Jan-2008  matt file i82093var.h was added on branch matt-armv6 on 2008-03-23 02:04:30 +0000
 1.2.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.4.1 11-Jan-2008  mjf file i82093var.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.2.2 21-Jan-2008  yamt sync with head
 1.2.2.1 11-Jan-2008  yamt file i82093var.h was added on branch yamt-lazymbuf on 2008-01-21 09:40:29 +0000
 1.2 11-Jan-2008  bouyer branches: 1.2.2; 1.2.4; 1.2.12;
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.1 10-Jan-2008  bouyer branches: 1.1.2;
file i82489var.h was initially added on branch bouyer-xeni386.
 1.1.2.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.12.2 23-Mar-2008  matt sync with HEAD
 1.2.12.1 11-Jan-2008  matt file i82489var.h was added on branch matt-armv6 on 2008-03-23 02:04:30 +0000
 1.2.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.4.1 11-Jan-2008  mjf file i82489var.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.2.2 21-Jan-2008  yamt sync with head
 1.2.2.1 11-Jan-2008  yamt file i82489var.h was added on branch yamt-lazymbuf on 2008-01-21 09:40:29 +0000
 1.2 11-Jan-2008  bouyer branches: 1.2.2; 1.2.4; 1.2.12;
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.1 10-Jan-2008  bouyer branches: 1.1.2;
file intr.h was initially added on branch bouyer-xeni386.
 1.1.2.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.12.2 23-Mar-2008  matt sync with HEAD
 1.2.12.1 11-Jan-2008  matt file intr.h was added on branch matt-armv6 on 2008-03-23 02:04:30 +0000
 1.2.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.4.1 11-Jan-2008  mjf file intr.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.2.2 21-Jan-2008  yamt sync with head
 1.2.2.1 11-Jan-2008  yamt file intr.h was added on branch yamt-lazymbuf on 2008-01-21 09:40:30 +0000
 1.2 11-Jan-2008  bouyer branches: 1.2.2; 1.2.4; 1.2.12;
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.1 10-Jan-2008  bouyer branches: 1.1.2;
file pci_machdep.h was initially added on branch bouyer-xeni386.
 1.1.2.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.12.2 23-Mar-2008  matt sync with HEAD
 1.2.12.1 11-Jan-2008  matt file pci_machdep.h was added on branch matt-armv6 on 2008-03-23 02:04:30 +0000
 1.2.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.4.1 11-Jan-2008  mjf file pci_machdep.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.2.2 21-Jan-2008  yamt sync with head
 1.2.2.1 11-Jan-2008  yamt file pci_machdep.h was added on branch yamt-lazymbuf on 2008-01-21 09:40:30 +0000
 1.2 11-Jan-2008  bouyer branches: 1.2.2; 1.2.4; 1.2.12;
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.1 10-Jan-2008  bouyer branches: 1.1.2;
file pic.h was initially added on branch bouyer-xeni386.
 1.1.2.1 10-Jan-2008  bouyer Change xeni386 build to match xenamd64:
- machine/ points to xen/include/i386
- remove includes that were identical to the i386/include/ ones; the build
will find them though the xen-ma/machine link.
- include xen files using xen/ not machine/
 1.2.12.2 23-Mar-2008  matt sync with HEAD
 1.2.12.1 11-Jan-2008  matt file pic.h was added on branch matt-armv6 on 2008-03-23 02:04:30 +0000
 1.2.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.4.1 11-Jan-2008  mjf file pic.h was added on branch mjf-devfs on 2008-02-18 21:05:20 +0000
 1.2.2.2 21-Jan-2008  yamt sync with head
 1.2.2.1 11-Jan-2008  yamt file pic.h was added on branch yamt-lazymbuf on 2008-01-21 09:40:30 +0000
 1.26 17-Oct-2023  bouyer Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.
Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen
when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.
x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console
xen/x86/consinit.c: support genfb as possible console
xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.
xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.
 1.25 02-May-2020  bouyer branches: 1.25.20;
Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.24 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.23 24-May-2019  nonaka branches: 1.23.8;
Added drivers for Hyper-V Synthetic Keyboard and Video device.
 1.22 28-Jan-2019  bad Sprinkle DPRINTF #ifdef DEBUG_GEOM and set booted_method like arch/x86/x86/x86_autoconf.c

As discussed 1 week ago on port-xen.
 1.21 22-Dec-2018  cherry This change modifies the mainbus(4) entry point for all x86 sub-archs
in the following way:

i) It provides a unified entry point in
x86/x86/mainbus.c:mainbus_attach()
ii) It carves out the preliminary bus attachment sequence that is
common to all sub-archs into
x86/x86/mainbus.c: x86_cpubus_attach()
iii) It consolidates the remaining pathways as internal callee
functions so that these may be called piecemeal if required. A
special usecase of this is XEN PVHVM which may need to call the
native configure path, the xen configure path, or both.
iv) It moves the driver private data structures from
i386/i386_mainbus.c to an x86/ level one. This allows for other
sub-arch's to do similar, if needed. (They do not at the moment).
v) For dom0 kernels, it enables 'acpi0 at mainbus?' and
'acpi0 at hypervisorbus'. This serves two purposes:
a) To demonstrate the possibility of dynamic configuration tree
traversal ordering changes.
b) To allow for the common acpi_check(self, "acpibus") call in
x86/mainbus.c to not barf when it is called from the dom0 attach
path. We allow for the acpi0 device to be a child of mainbus with
the changes to amd64/conf/XEN3_DOM0 and i386/conf/XEN3PAE_DOM0
without actually probing further in the code. This path will later
be pursued in a PVHVM boot codepath.

There should be no operative changes with this change. If there are,
please complain loudly.
 1.20 07-Oct-2018  mlelstv Support bootspec.
 1.19 29-Jul-2017  maxv branches: 1.19.2; 1.19.4;
Remove the remaining parts of compat_oldboot.
 1.18 23-May-2017  nonaka branches: 1.18.2;
x86: hypervisor detection from FreeBSD for x2APIC support.
 1.17 03-Apr-2014  christos branches: 1.17.6;
Change findroot() to cpu_bootconf() since this is what it does. Remove bogus
comment.
 1.16 03-Oct-2012  dsl branches: 1.16.2;
Remove all references to KVM86.
It was only ever used by APMBIOS - and then only if an option was selected.
Probably didn't work well at all!
 1.15 29-Jul-2012  mlelstv branches: 1.15.2;
Do not call setroot() from MD code and from MI code, which has
unwanted sideeffects in the RB_ASKNAME case. This fixes PR/46732.

No longer wrap MD cpu_rootconf(), as hp300 port stores reboot information
as a side effect. Instead call MI rootconf() from MD code which makes
rootconf() now a wrapper to setroot().

Adjust several MD routines to set the global booted_device,booted_partition
variables instead of passing partial information to setroot().

Make cpu_rootconf(9) describe the calling order.
 1.14 10-Jun-2012  mlelstv Make detection of root on wedges (dk(4)) machine independent. Remove
MD code for x86, xen, sparc64.
 1.13 27-Nov-2009  rmind branches: 1.13.12; 1.13.18;
- Use uvm_lwp_setuarea() instead of directly setting address to lwp_t::l_addr.
- Replace most remaining uses of l_addr with uvm_lwp_getuarea() or lwp_getpcb().
- Amend assembly in ports where it accesses PCB via struct user.
- Rename L_ADDR to L_PCB in few places. Reduce sys/user.h inclusions.
 1.12 21-Nov-2009  rmind Catch-up Xen and usermode with lwp_getpcb() and unbreak Xen build.
 1.11 06-Nov-2009  dyoung Use deviter(9) instead of accessing alldevs directly.
 1.10 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.9 12-Feb-2009  cegger Make Dom0/DomU boot with root-on-nfs when 'bootdev' parameter is missing or wrong.
In this case, we get prompted for the root device.
Make sure that nfs_bootstatic_callback is initialized or we will miss the nfsroot bootparameter,
causing a boot failure even when root device is correct.
 1.8 18-Dec-2008  cegger branches: 1.8.2;
remove unused malloc.h
 1.7 27-Oct-2008  cegger branches: 1.7.2;
change nfs boot behaviour to automatically try next boot method if boot information are incomplete to succeed.
That way, it is possible combine static and dhcp boot:
For example, to boot diskless you can specify the nfs-server and the rootpath statically. All other information will be taken via dhcp.

Patch has been presented on port-xen, tech-kern and tech-net:
http://mail-index.netbsd.org/port-xen/2008/10/24/msg004488.html
http://mail-index.netbsd.org/tech-kern/2008/10/24/msg003255.html
http://mail-index.netbsd.org/tech-net/2008/10/24/msg000864.html

No comments, no objections.
 1.6 24-Oct-2008  cegger branches: 1.6.2;
findroot(): set booted_device also when specifying a network device to bootdev.
Useful for booting with root on nfs.
 1.5 24-Oct-2008  cegger struct device * -> device_t
 1.4 21-Oct-2008  cegger introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.3 06-Apr-2008  cegger branches: 1.3.4; 1.3.10;
use aprint_*_dev and device_xname
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.8; 1.2.16; 1.2.22;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file autoconf.c was initially added on branch bouyer-xenamd64.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.22.2 17-Jan-2009  mjf Sync with HEAD.
 1.2.22.1 02-Jun-2008  mjf Sync with HEAD.
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file autoconf.c was added on branch matt-armv6 on 2008-01-09 01:50:13 +0000
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file autoconf.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:15 +0000
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file autoconf.c was added on branch vmlocking on 2007-12-03 19:04:37 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file autoconf.c was added on branch jmcneill-pm on 2007-11-27 19:36:17 +0000
 1.3.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.3.4.3 11-Mar-2010  yamt sync with head
 1.3.4.2 19-Aug-2009  yamt sync with head.
 1.3.4.1 04-May-2009  yamt sync with head.
 1.6.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.6.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.7.2.1 23-Feb-2009  snj Pull up following revision(s) (requested by cegger in ticket #460):
sys/arch/xen/x86/autoconf.c: revision 1.9
Make Dom0/DomU boot with root-on-nfs when 'bootdev' parameter is missing
or wrong.
In this case, we get prompted for the root device.
Make sure that nfs_bootstatic_callback is initialized or we will miss
the nfsroot bootparameter, causing a boot failure even when root device
is correct.
 1.8.2.3 24-Oct-2010  jym Sync with HEAD
 1.8.2.2 01-Nov-2009  jym Sync with HEAD.
 1.8.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.13.18.2 08-Aug-2012  martin Pull up following revision(s) (requested by mlelstv in ticket #466):
sys/arch/amiga/amiga/autoconf.c: revision 1.113
sys/arch/rs6000/rs6000/autoconf.c: revision 1.4
sys/arch/emips/emips/autoconf.c: revision 1.6
sys/arch/sandpoint/sandpoint/autoconf.c: revision 1.27
sys/arch/evbmips/alchemy/autoconf.c: revision 1.18
sys/arch/sgimips/sgimips/autoconf.c: revision 1.43
sys/arch/atari/atari/autoconf.c: revision 1.63
sys/arch/powerpc/oea/ofw_autoconf.c: revision 1.17
sys/arch/mmeye/mmeye/autoconf.c: revision 1.9
distrib/sets/lists/comp/mi: revision 1.1771
sys/arch/mipsco/mipsco/autoconf.c: revision 1.25
sys/arch/iyonix/iyonix/autoconf.c: revision 1.14
sys/arch/hp300/hp300/autoconf.c: revision 1.100
sys/kern/init_main.c: revision 1.445
sys/arch/pmax/pmax/autoconf.c: revision 1.79
sys/arch/netwinder/netwinder/autoconf.c: revision 1.11
sys/arch/dreamcast/dreamcast/autoconf.c: revision 1.10
sys/arch/ibmnws/ibmnws/autoconf.c: revision 1.12
sys/arch/evbppc/ev64260/autoconf.c: revision 1.17
sys/arch/evbmips/gdium/autoconf.c: revision 1.5
sys/arch/algor/algor/autoconf.c: revision 1.21
share/man/man9/Makefile: revision 1.367
sys/arch/ews4800mips/ews4800mips/autoconf.c: revision 1.9
sys/arch/amigappc/amigappc/autoconf.c: revision 1.5
sys/arch/x86/x86/x86_autoconf.c: revision 1.65
sys/arch/acorn26/acorn26/autoconf.c: revision 1.9
sys/arch/mvmeppc/mvmeppc/autoconf.c: revision 1.13
sys/arch/vax/vax/autoconf.c: revision 1.94
sys/arch/usermode/dev/cpu.c: revision 1.72
sys/arch/evbppc/virtex/autoconf.c: revision 1.5
sys/arch/next68k/next68k/autoconf.c: revision 1.26
sys/arch/mac68k/mac68k/autoconf.c: revision 1.73
sys/arch/ia64/ia64/autoconf.c: revision 1.6
sys/arch/evbppc/obs405/obs405_autoconf.c: revision 1.6
share/man/man9/cpu_rootconf.9: revision 1.7
sys/arch/landisk/landisk/autoconf.c: revision 1.6
sys/arch/evbmips/malta/autoconf.c: revision 1.16
sys/arch/sun3/sun3/autoconf.c: revision 1.76
sys/arch/evbppc/explora/autoconf.c: revision 1.13
sys/arch/sun3/sun3/autoconf.c: revision 1.77
sys/arch/evbmips/loongson/autoconf.c: revision 1.3
sys/arch/evbmips/atheros/autoconf.c: revision 1.11
sys/arch/sparc64/sparc64/autoconf.c: revision 1.188
sys/arch/acorn32/acorn32/autoconf.c: revision 1.18
sys/arch/evbarm/evbarm/autoconf.c: revision 1.13
sys/arch/cobalt/cobalt/autoconf.c: revision 1.30
sys/arch/mvme68k/mvme68k/autoconf.c: revision 1.46
sys/arch/hp700/hp700/autoconf.c: revision 1.48
sys/arch/evbmips/adm5120/autoconf.c: revision 1.5
sys/arch/hpcmips/hpcmips/autoconf.c: revision 1.25
sys/arch/alpha/alpha/autoconf.c: revision 1.52
sys/arch/sparc/sparc/autoconf.c: revision 1.244
sys/arch/evbppc/pmppc/autoconf.c: revision 1.7
sys/arch/bebox/bebox/autoconf.c: revision 1.25
sys/arch/luna68k/luna68k/autoconf.c: revision 1.13
sys/arch/hpcarm/hpcarm/autoconf.c: revision 1.20
sys/arch/evbppc/walnut/autoconf.c: revision 1.21
sys/arch/cesfic/cesfic/autoconf.c: revision 1.26
sys/arch/cats/cats/autoconf.c: revision 1.17
sys/arch/x68k/x68k/autoconf.c: revision 1.67
sys/arch/news68k/news68k/autoconf.c: revision 1.21
sys/arch/arc/arc/autoconf.c: revision 1.34
sys/arch/evbsh3/evbsh3/autoconf.c: revision 1.11
sys/sys/conf.h: revision 1.143
sys/arch/evbmips/rasoc/autoconf.c: revision 1.3
sys/arch/hpcsh/hpcsh/autoconf.c: revision 1.26
sys/arch/sun68k/sun68k/autoconf.c: revision 1.29
sys/arch/evbmips/rmixl/autoconf.c: revision 1.6
sys/arch/zaurus/zaurus/autoconf.c: revision 1.12
sys/arch/xen/x86/autoconf.c: revision 1.15
sys/arch/evbppc/mpc85xx/autoconf.c: revision 1.6
sys/arch/shark/shark/autoconf.c: revision 1.18
sys/arch/prep/prep/autoconf.c: revision 1.25
sys/arch/newsmips/newsmips/autoconf.c: revision 1.36
sys/arch/sbmips/sbmips/autoconf.c: revision 1.8
Do not call setroot() from MD code and from MI code, which has
unwanted sideeffects in the RB_ASKNAME case. This fixes PR/46732.
No longer wrap MD cpu_rootconf(), as hp300 port stores reboot information
as a side effect. Instead call MI rootconf() from MD code which makes
rootconf() now a wrapper to setroot().
Adjust several MD routines to set the global booted_device,booted_partition
variables instead of passing partial information to setroot().
Make cpu_rootconf(9) describe the calling order.
add rootconf(9) as a link to cpu_rootconf(9)
make this compile again
 1.13.18.1 05-Jul-2012  riz Pull up following revision(s) (requested by mlelstv in ticket #402):
sys/dev/vnd.c: revision 1.221
sys/kern/init_main.c: revision 1.443
sys/kern/init_main.c: revision 1.444
sys/dev/dkwedge/dk.c: revision 1.64
sys/arch/x86/x86/x86_autoconf.c: revision 1.63
sys/arch/sparc64/sparc64/autoconf.c: revision 1.187
sys/sys/device.h: revision 1.141
sys/dev/dkwedge/dkwedge_bsdlabel.c: revision 1.17
sys/kern/kern_subr.c: revision 1.213
sys/arch/zaurus/zaurus/autoconf.c: revision 1.11
sys/arch/xen/x86/autoconf.c: revision 1.14
sys/sys/disk.h: revision 1.57
Use the label's packname to create wedge names instead of the classic
device names. Fall back to classic device names when the label has an
empty name or the default name 'fictitious'.
autodiscover wedges
Make detection of root on wedges (dk(4)) machine independent. Remove
MD code for x86, xen, sparc64.
Make detection of root on wedges (dk(4)) machine independent. Remove
MD code for zaurus.
Do not try to find the wedge we booted from if opendisk(booted_device)
failed.
 1.13.12.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.13.12.1 30-Oct-2012  yamt sync with head
 1.15.2.3 03-Dec-2017  jdolecek update from HEAD
 1.15.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.15.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.16.2.1 18-May-2014  rmind sync with head
 1.17.6.1 28-Aug-2017  skrll Sync with HEAD
 1.18.2.2 12-Jun-2019  martin Pull up following revision(s) (requested by nonaka in ticket #1280):

sys/arch/x86/x86/consinit.c: revision 1.29
sys/dev/hyperv/vmbusvar.h: revision 1.2
sys/dev/hyperv/genfb_vmbusvar.h: revision 1.1
sys/arch/x86/x86/x86_autoconf.c: revision 1.78
sys/arch/x86/x86/identcpu.c: revision 1.91
sys/arch/x86/x86/hyperv.c: revision 1.2
sys/arch/x86/x86/hyperv.c: revision 1.3
sys/arch/x86/x86/hyperv.c: revision 1.4
sys/arch/i386/conf/GENERIC: revision 1.1207
sys/dev/wscons/wsconsio.h: revision 1.123
sys/arch/x86/x86/hypervvar.h: revision 1.1
sys/arch/amd64/conf/GENERIC: revision 1.528
sys/dev/hyperv/files.hyperv: revision 1.2
sys/arch/x86/include/autoconf.h: revision 1.6
sys/dev/hyperv/hyperv_common.c: revision 1.2
sys/arch/xen/x86/autoconf.c: revision 1.23
sys/arch/x86/pci/pci_machdep.c: revision 1.86
sys/dev/hyperv/hvkbd.c: revision 1.1
sys/dev/hyperv/hypervvar.h: revision 1.2
sys/dev/acpi/vmbus_acpi.c: revision 1.2
sys/dev/hyperv/vmbus.c: revision 1.3
sys/dev/hyperv/hvkbdvar.h: revision 1.1
sys/dev/hyperv/genfb_vmbus.c: revision 1.1

Added drivers for Hyper-V Synthetic Keyboard and Video device.

Avoid undefined reference to `hyperv_guid_video' without vmbus(4).

Avoid undefined reference to `hyperv_is_gen1' without hyperv(4).

Use efi_probe().
 1.18.2.1 13-Oct-2018  martin Pull up following revision(s) (requested by mlelstv in ticket #1057):

sys/arch/xen/x86/autoconf.c: revision 1.20
sys/arch/xen/include/xen.h: revision 1.40

Support bootspec.
 1.19.4.1 10-Jun-2019  christos Sync with HEAD
 1.19.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.19.2.1 20-Oct-2018  pgoyette Sync with head
 1.23.8.2 12-Apr-2020  bouyer remove stray 'else'
 1.23.8.1 08-Apr-2020  bouyer Remove VM_GUEST_XEN and define only Xen subtypes:
VM_GUEST_XENPV
VM_GUEST_XENPVH
VM_GUEST_XENHVM
VM_GUEST_XENPVHVM

Set vm_guest in the start routine, if it is hypervisor-specific (e.g Xen PV).
If vm_guest was not set early and we detect Xen in identify_hypervisor(),
assume it is VM_GUEST_XENHVM. Refine to VM_GUEST_PVXENHVM in
hypervisor_match().
 1.25.20.1 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #428):

sys/arch/xen/xen/xen_machdep.c: revision 1.28
sys/arch/x86/pci/pci_machdep.c: revision 1.97
sys/arch/xen/xen/genfb_xen.c: revision 1.1
sys/arch/xen/xen/genfb_xen.c: revision 1.2
sys/arch/xen/include/hypervisor.h: revision 1.59
sys/arch/i386/conf/XEN3PAE_DOM0: revision 1.41 (patch)
sys/arch/x86/x86/genfb_machdep.c: revision 1.22
sys/arch/xen/x86/consinit.c: revision 1.18
sys/arch/xen/x86/autoconf.c: revision 1.26
sys/external/mit/xen-include-public/dist/xen/include/public/platform.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.188
sys/arch/x86/x86/consinit.c: revision 1.37
sys/arch/xen/conf/files.xen: revision 1.189
sys/arch/x86/x86/consinit.c: revision 1.38
sys/external/mit/xen-include-public/dist/xen/include/public/xen.h: revision 1.2
sys/arch/x86/include/genfb_machdep.h: revision 1.7
sys/arch/xen/x86/pvh_consinit.c: revision 1.5
sys/arch/xen/x86/pvh_consinit.c: revision 1.6
sys/arch/amd64/conf/XEN3_DOM0: revision 1.201

Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.

Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.

Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen

when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.

x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console

xen/x86/consinit.c: support genfb as possible console

xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.

xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.

Make sure to always fall back to xen_early_console, even for dom0

Enable genfb in DOM0 kernels

Add ext_lfb_base to dom0_vga_console_info, from recent Xen. We know if it's
present or not by checking dom0.info_size

Add XENPF_get_dom0_console, which gets a dom0_vga_console_info stucture
from the hypervisor. To be used by PVH dom0 kernels.

XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.

Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.5 16-Apr-2005  yamt tweak x86 bus_dma code so that it can be used by xen port.

- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.4 01-Apr-2005  yamt branches: 1.4.2;
merge yamt-km branch.
- don't use managed mappings/backing objects for wired memory allocations.
save some resources like pv_entry. also fix (most of) PR/27030.
- simplify kernel memory management API.
- simplify pmap bootstrap of some ports.
- some related cleanups.
 1.3 10-Mar-2005  matt branches: 1.3.2;
Update to new bus_dma semantics.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 20-Jan-2005  bouyer branches: 1.1.2; 1.1.4;
file bus_dma.c was initially added on branch bouyer-xen2.
 1.1.4.2 19-Mar-2005  yamt (re-)convert arch/xen to the new API.
XXX except for xbdback.c and xennetback.c, because i'm not sure
what they're doing.
 1.1.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.2.2 16-Feb-2005  bouyer Support functions to map/unmap guest's memory into our kernel VM space.
This will be used by the block and network back-end.
The caller provide a list of machine address pages, and we return the
address virtual address they've been mapped to. These mapping are not
registered to the pmap (we don't have physical addresses for these pages),
so change bus_dma(9) to handle these mappings as well.
 1.1.2.1 20-Jan-2005  bouyer bus_dma(9) for xen. Derived from arch/x86/x86/bus_dma.c.
bounce buffers not supported yet, because xen doesn't have an interface
to request memory in a specific range (this means that DMA on ISA won't
be supported), but I've left the code commented out because xen will
likely provide an appropriate hypercall in the future.
 1.3.2.1 21-Apr-2005  tron Pull up file removal (requested by yamt in ticket #175):
tweak x86 bus_dma code so that it can be used by xen port.
- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.4.2.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.4.2.2 01-Apr-2005  skrll Sync with HEAD.
 1.4.2.1 01-Apr-2005  skrll file bus_dma.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.9 26-Sep-2007  ad x86 changes for pcc and LKMs.

- Replace most inline assembly with proper functions. As a side effect
this reduces the size of amd64 GENERIC by about 120kB, and i386 by a
smaller amount. Nearly all of the inlines did something slow, or something
that does not need to be fast.
- Make curcpu() and curlwp functions proper, unless __GNUC__ && _KERNEL.
In that case make them inlines. Makes curlwp LKM and preemption safe.
- Make bus_space and bus_dma more LKM friendly.
- Share a few more files between the ports.
- Other minor changes.
 1.8 04-Mar-2007  christos branches: 1.8.2; 1.8.10; 1.8.18; 1.8.20; 1.8.22;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.7 22-Feb-2007  thorpej TRUE -> true, FALSE -> false
 1.6 15-Jan-2006  bouyer branches: 1.6.24;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.5 24-Nov-2005  yamt branches: 1.5.2;
bus_dmamem_map: honour BUS_DMA_NOWAIT. noted by Manuel Bouyer.
bus_space_map: always do NOWAIT allocation as it used to be before yamt-km.

we have too many copies!
 1.4 01-Apr-2005  yamt branches: 1.4.2; 1.4.8;
merge yamt-km branch.
- don't use managed mappings/backing objects for wired memory allocations.
save some resources like pv_entry. also fix (most of) PR/27030.
- simplify kernel memory management API.
- simplify pmap bootstrap of some ports.
- some related cleanups.
 1.3 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.2 26-Apr-2004  cl branches: 1.2.2; 1.2.4; 1.2.8; 1.2.10; 1.2.12;
Rework the physical<->machine memory mapping: offset physical addresses
by 0x100000 (above the I/O Memory "hole") leaving all physical addresses
below unused, don't perform phys<->mach mapping for addresses below 0x100000
or beyond the real hardware's physical memory.

-> /dev/mem works now as expected and X works in domain0.
 1.1 24-Apr-2004  cl Make bus_space map machine addresses instead of physical addresses.
 1.2.12.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.12.1 13-Feb-2005  yamt - use new apis.
- simplify bootstrap and pvpage allocation.
- remove no longer needed .globl decls.
 1.2.10.1 29-Apr-2005  kent sync with -current
 1.2.8.2 09-Mar-2005  bouyer bus_space_map/unmap() should map machine addresses, not physical addresses,
for memory-mapped I/O. So use pmap_kenter_ma() instead of pmap_kenter_pa(),
and introduce pmap_extract_ma() to get the machine address of a mapping.
Now memory-mapped I/O should work outside of the ISA hole (note that PCI
devices are usually mapped in the ISA memory hole anyway).
 1.2.8.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.2.4.6 11-Dec-2005  christos Sync with head.
 1.2.4.5 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.2.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.2.4.2 03-Aug-2004  skrll Sync with HEAD
 1.2.4.1 26-Apr-2004  skrll file bus_space.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.2.2.2 22-May-2004  he Pull up revisions 1.1-1.2 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.2.2.1 26-Apr-2004  he file bus_space.c was added on branch netbsd-2-0 on 2004-05-22 15:57:25 +0000
 1.4.8.1 29-Nov-2005  yamt sync with head.
 1.4.2.4 27-Oct-2007  yamt sync with head.
 1.4.2.3 03-Sep-2007  yamt sync with head.
 1.4.2.2 26-Feb-2007  yamt sync with head.
 1.4.2.1 21-Jun-2006  yamt sync with head.
 1.5.2.1 01-Feb-2006  yamt sync with head.
 1.6.24.2 12-Mar-2007  rmind Sync with HEAD.
 1.6.24.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.8.22.1 06-Oct-2007  yamt sync with head.
 1.8.20.1 06-Nov-2007  matt sync with HEAD
 1.8.18.1 02-Oct-2007  joerg Sync with HEAD.
 1.8.10.1 03-Oct-2007  garbled Sync with HEAD
 1.8.2.1 12-Oct-2007  ad Fix merge errors.
 1.18 17-Oct-2023  bouyer Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.
Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen
when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.
x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console
xen/x86/consinit.c: support genfb as possible console
xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.
xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.
 1.17 22-Jul-2023  mrg xen: declare 'default_consinfo' as extern in a header

this makes pvh_consinit.c actually compile with CONS_OVERRIDE set.
i didn't see any good header to add to (bootinfo.h and cpu.h both
seem to be poor choices but were considered), hence the new one
with just this definition.
 1.16 13-Oct-2012  jdc branches: 1.16.68;
Adapt to the changed signature of pckbc_cnattach().
 1.15 01-Jul-2011  dyoung branches: 1.15.2; 1.15.12;
#include <sys/bus.h> instead of <machine/bus.h>.
 1.14 28-Apr-2010  dyoung On x86, change the bus_space_tag_t to a pointer to a struct
bus_space_tag. For now, bus_space_tag's only member is
bst_type, the type of space, which is either X86_BUS_SPACE_IO
or X86_BUS_SPACE_MEM. In the future, new bus_space_tag members
will refer to override-functions installed by a new function,
bus_space_tag_create(9).

Add pointers to constant struct bus_space_tag, x86_bus_space_io and
x86_bus_space_mem. Use them to replace most uses of X86_BUS_SPACE_IO
and X86_BUS_SPACE_MEM.

Add an x86-specific bus_space_is_equal(9) implementation that compares
the two tags' bst_type.
 1.13 18-Mar-2009  cegger branches: 1.13.2; 1.13.4;
Ansify function definitions w/o arguments. Generated with sed.
 1.12 16-Mar-2009  cegger ansify function definitions
 1.11 21-Oct-2008  cegger branches: 1.11.2; 1.11.8;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.10 22-Nov-2007  bouyer branches: 1.10.14; 1.10.18; 1.10.24;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.9 14-Nov-2007  ad Remove pccons.
 1.8 29-Jan-2007  hubertf branches: 1.8.6; 1.8.22; 1.8.24; 1.8.28; 1.8.30;
Remove more duplicate headers.
Patch by Slava Semushin <slava.semushin@gmail.com>

Again, this was tested by comparing obj files from a pristine and a patched
source tree against an i386/ALL kernel, and also for src/sbin/fsck_ffs,
src/sbin/fsdb and src/usr.sbin/makefs. Only changes in assert() line numbers
were detected in 'objdump -d' output.
 1.7 09-Dec-2006  bouyer Remove extra ) causing compile failure when CONS_OVERRIDE is defined.
From Hideo Masuda in PR port-xen/35217.
 1.6 11-Dec-2005  christos branches: 1.6.20; 1.6.22; 1.6.24;
merge ktrace-lwp.
 1.5 16-Jun-2005  bouyer branches: 1.5.2;
Allow compiling a domain0 kernel with vga but without pckbc, and add
console support for USB keyboard. Problem pointed out by Karl Janmar on
port-xen.
 1.4 09-Mar-2005  bouyer branches: 1.4.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 24-Apr-2004  cl branches: 1.3.2; 1.3.6; 1.3.8; 1.3.10;
Enable keyboard and vga display as console when running as domain-0.
 1.2 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.8.1 29-Apr-2005  kent sync with -current
 1.3.6.1 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.3.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.3.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.2.2 03-Aug-2004  skrll Sync with HEAD
 1.3.2.1 24-Apr-2004  skrll file consinit.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.4.2.2 11-Dec-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1600):
sys/arch/xen/x86/consinit.c: revision 1.7
Remove extra ) causing compile failure when CONS_OVERRIDE is defined.
From Hideo Masuda in PR port-xen/35217.
 1.4.2.1 28-Jun-2005  tron branches: 1.4.2.1.2; 1.4.2.1.4;
Pull up revision 1.5 (requested by bouyer in ticket #482):
Allow compiling a domain0 kernel with vga but without pckbc, and add
console support for USB keyboard. Problem pointed out by Karl Janmar on
port-xen.
 1.4.2.1.4.1 11-Dec-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1600):
sys/arch/xen/x86/consinit.c: revision 1.7
Remove extra ) causing compile failure when CONS_OVERRIDE is defined.
From Hideo Masuda in PR port-xen/35217.
 1.4.2.1.2.1 11-Dec-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1600):
sys/arch/xen/x86/consinit.c: revision 1.7
Remove extra ) causing compile failure when CONS_OVERRIDE is defined.
From Hideo Masuda in PR port-xen/35217.
 1.5.2.4 07-Dec-2007  yamt sync with head
 1.5.2.3 15-Nov-2007  yamt sync with head.
 1.5.2.2 26-Feb-2007  yamt sync with head.
 1.5.2.1 30-Dec-2006  yamt sync with head.
 1.6.24.1 09-Dec-2006  riz Pull up following revision(s) (requested by bouyer in ticket #266):
sys/arch/xen/x86/consinit.c: revision 1.7
Remove extra ) causing compile failure when CONS_OVERRIDE is defined.
From Hideo Masuda in PR port-xen/35217.
 1.6.22.1 10-Dec-2006  yamt sync with head.
 1.6.20.2 01-Feb-2007  ad Sync with head.
 1.6.20.1 12-Jan-2007  ad Sync with head.
 1.8.30.2 08-Dec-2007  mjf Sync with HEAD.
 1.8.30.1 19-Nov-2007  mjf Sync with HEAD.
 1.8.28.2 18-Nov-2007  bouyer Sync with HEAD
 1.8.28.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.24.1 09-Jan-2008  matt sync with HEAD
 1.8.22.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.8.22.1 21-Nov-2007  joerg Sync with HEAD.
 1.8.6.1 03-Dec-2007  ad Sync with HEAD.
 1.10.24.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.10.18.2 11-Aug-2010  yamt sync with head.
 1.10.18.1 04-May-2009  yamt sync with head.
 1.10.14.1 17-Jan-2009  mjf Sync with HEAD.
 1.11.8.4 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.11.8.3 24-Oct-2010  jym Sync with HEAD
 1.11.8.2 01-Nov-2009  jym Sync with HEAD.
 1.11.8.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.11.2.1 28-Apr-2009  skrll Sync with HEAD.
 1.13.4.1 30-May-2010  rmind sync with head
 1.13.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.15.12.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.15.2.1 30-Oct-2012  yamt sync with head
 1.16.68.1 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #428):

sys/arch/xen/xen/xen_machdep.c: revision 1.28
sys/arch/x86/pci/pci_machdep.c: revision 1.97
sys/arch/xen/xen/genfb_xen.c: revision 1.1
sys/arch/xen/xen/genfb_xen.c: revision 1.2
sys/arch/xen/include/hypervisor.h: revision 1.59
sys/arch/i386/conf/XEN3PAE_DOM0: revision 1.41 (patch)
sys/arch/x86/x86/genfb_machdep.c: revision 1.22
sys/arch/xen/x86/consinit.c: revision 1.18
sys/arch/xen/x86/autoconf.c: revision 1.26
sys/external/mit/xen-include-public/dist/xen/include/public/platform.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.188
sys/arch/x86/x86/consinit.c: revision 1.37
sys/arch/xen/conf/files.xen: revision 1.189
sys/arch/x86/x86/consinit.c: revision 1.38
sys/external/mit/xen-include-public/dist/xen/include/public/xen.h: revision 1.2
sys/arch/x86/include/genfb_machdep.h: revision 1.7
sys/arch/xen/x86/pvh_consinit.c: revision 1.5
sys/arch/xen/x86/pvh_consinit.c: revision 1.6
sys/arch/amd64/conf/XEN3_DOM0: revision 1.201

Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.

Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.

Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen

when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.

x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console

xen/x86/consinit.c: support genfb as possible console

xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.

xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.

Make sure to always fall back to xen_early_console, even for dom0

Enable genfb in DOM0 kernels

Add ext_lfb_base to dom0_vga_console_info, from recent Xen. We know if it's
present or not by checking dom0.info_size

Add XENPF_get_dom0_console, which gets a dom0_vga_console_info stucture
from the hypervisor. To be used by PVH dom0 kernels.

XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.

Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.145 25-Feb-2023  riastradh xen/x86/cpu.c: Nix trailing whitespace.

No functional change intended.
 1.144 25-Feb-2023  riastradh 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.
 1.143 25-Feb-2023  riastradh x86: Assert kpreempt_disabled() in cpu_load_pmap.

No functional change intended. Just makes it easier to audit
curcpu() usage.
 1.142 20-Aug-2022  riastradh branches: 1.142.4;
x86: Split most of pmap.h into pmap_private.h or vmparam.h.

This way pmap.h only contains the MD definition of the MI pmap(9)
API, which loads of things in the kernel rely on, so changing x86
pmap internals no longer requires recompiling the entire kernel every
time.

Callers needing these internals must now use machine/pmap_private.h.
Note: This is not x86/pmap_private.h because it contains three parts:

1. CPU-specific (different for i386/amd64) definitions used by...

2. common definitions, including Xenisms like xpmap_ptetomach,
further used by...

3. more CPU-specific inlines for pmap_pte_* operations

So {amd64,i386}/pmap_private.h defines 1, includes x86/pmap_private.h
for 2, and then defines 3. Maybe we should split that out into a new
pmap_pte.h to reduce this trouble.

No functional change intended, other than that some .c files must
include machine/pmap_private.h when previously uvm/uvm_pmap.h
polluted the namespace with pmap internals.

Note: This migrates part of i386/pmap.h into i386/vmparam.h --
specifically the parts that are needed for several constants defined
in vmparam.h:

VM_MAXUSER_ADDRESS
VM_MAX_ADDRESS
VM_MAX_KERNEL_ADDRESS
VM_MIN_KERNEL_ADDRESS

Since i386 needs PDP_SIZE in vmparam.h, I added it there on amd64
too, just to keep things parallel.
 1.141 07-Aug-2021  thorpej Merge thorpej-cfargs2.
 1.140 24-Apr-2021  thorpej branches: 1.140.8;
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.139 14-Jul-2020  yamaguchi branches: 1.139.4;
Introduce per-cpu IDTs

This is realized by following modifications:
- Add IDT pages and its allocation maps for each cpu in "struct cpu_info"
- Load per-cpu IDTs at cpu_init_idt(struct cpu_info*)
- Copy the IDT entries for cpu0 to other CPUs at attach
- These are, for example, exceptions, db, system calls, etc.

And, added a kernel option named PCPU_IDT to enable the feature.
 1.138 08-Jul-2020  jdolecek initalize ci_kfpu_spl, to avoid triggering KASSERT() in fpu_kern_enter()

Follows revision 1.177 of sys/arch/x86/x86/cpu.c:
"""
Add a small API for in-kernel FPU operations.

fpu_kern_enter();
/* do FPU stuff */
fpu_kern_leave();
"""

With this DomU kernel boots with:
[ 2.0000571] aes: Intel AES-NI
 1.137 27-Jun-2020  jdolecek avoid excessive stack usage in mp_cpu_start(), this is called after VM
init so kmem(9) can be used
 1.136 21-May-2020  ad - Recalibrate the APIC timer using the TSC, once the TSC has in turn been
recalibrated using the HPET. This gets the clock interrupt firing more
closely to HZ.

- Undo change with recent Xen merge and go back to starting the clocks in
initclocks() on the boot CPU, and in cpu_hatch() on secondary CPUs.

- On reflection don't use HPET delay any more, it works very well but means
going over the bus. It's enough to use HPET to calibrate the TSC and
APIC.

Tested on amd64 native, xen and xen PVH.
 1.135 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.134 21-Apr-2020  ad Follow convention and put entire predicate inside __predict_false()
 1.133 24-Feb-2020  rin branches: 1.133.4;
0x%p --> %p for non-external codes.
 1.132 13-Jan-2020  bouyer Don't call cpu_switchto() before idle_loop(), it should not be needed any more.
While there, assume (and KASSERT) that curlwp == ci->ci_data.cpu_idlelwp,
this saves a lwp_getpcb() call.
 1.131 23-Nov-2019  ad branches: 1.131.2;
cpu_need_resched():

- Remove all code that should be MI, leaving the bare minimum under arch/.
- Make the required actions very explicit.
- Pass in LWP pointer for convenience.
- When a trap is required on another CPU, have the IPI set it locally.
- Expunge cpu_did_resched().
 1.130 12-Oct-2019  maxv Rewrite the FPU code on x86. This greatly simplifies the logic and removes
the dependency on IPL_HIGH. NVMM is updated accordingly. Posted on
port-amd64 a week ago.

Bump the kernel version to 9.99.16.
 1.129 09-Mar-2019  maxv Start replacing the x86 PTE bits.
 1.128 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.127 03-Sep-2018  riastradh Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.126 12-Aug-2018  maxv Introduce PDIR_SLOT_USERLIM, which indicates the limit of the user slots.
Use it instead of PDIR_SLOT_PTE when we just want to iterate over the
user slots. Also use it in SVS, I had hardcoded 255 because there was no
proper define (which there now is).
 1.125 27-Jul-2018  maxv Reduce the size of the blocks. No functional change.
 1.124 26-Jul-2018  maxv Remove the non-PAE-i386 code of Xen. The branches are reordered so that
__x86_64__ comes first, eg:

#if defined(PAE)
/* i386+PAE */
#elif defined(__x86_64__)
/* amd64 */
#else
/* i386 */
#endif

becomes

#ifdef __x86_64__
/* amd64 */
#else
/* i386+PAE */
#endif

Tested on i386pae-domU and amd64-dom0.
 1.123 24-Jul-2018  bouyer Sync cpu_boot_secondary_processors() with x86/x86/cpu.c:
explicitely wait for all CPUs to be registered in kcpuset_running.
 1.122 23-Jun-2018  jdolecek branches: 1.122.2;
re-do the XEN XSAVE support, this time to leave all probe code in
cpu_probe_fpu(), and have XEN cpu_init() just act

the xen probe is now guarded by XEN_USE_XSAVE option and XSAVE
support is thus still disabled by default (same as before), so it
wouldn't interfere with maxv's eager fpu rototil, while still
allowing testing for others

PR kern/50332
 1.121 23-Jun-2018  maxv Revert the rest of jdolecek's changes. This puts us back in a clean,
sensical state.
 1.120 22-Jun-2018  maxv Revert jdolecek's changes related to FXSAVE. They just didn't make any
sense and were trying to hide a real bug, which is, that there is for some
reason a wrong stack alignment that causes FXSAVE to fault in
fpuinit_mxcsr_mask. As seen in current-users@ yesterday, rdi % 16 = 8. And
as seen several months ago, as well.

The rest of the changes in XSAVE are wrong too, but I'll let him fix these
ones.
 1.119 20-Jun-2018  jdolecek as a stop-gap, make fpuinit_mxcsr_mask() for native independant of
XSAVE as it should be, only xen case checks the flag now; need to
investigate further why exactly the fault happens for the xen
no-xsave case

pointed out by maxv
 1.118 19-Jun-2018  jdolecek fix FPU initialization on Xen to allow e.g. AVX when supported by hardware;
only use XSAVE when the the CPUID OSXSAVE bit is set, as this seems to be
reliable indication

tested with Xen 4.2.6 DOM0/DOMU on Intel CPU, without and with no-xsave flag,
so should work also on those AMD CPUs, which have XSAVE disabled by default;
also tested with Xen DOM0 4.8.3

fixes PR kern/50332 by Torbjorn Granlund; sorry it took three years to address

XXX pullup netbsd-8
 1.117 13-Jan-2018  bouyer branches: 1.117.2;
Needs cpu_init_tss() for application processor too.
 1.116 11-Nov-2017  maxv Recommit

http://mail-index.netbsd.org/source-changes/2017/11/08/msg089525.html

but use __INITIAL_MXCSR_MASK__ on Xen until someone figures out what's
wrong with the Xen fpu.
 1.115 11-Nov-2017  bouyer Revert http://mail-index.netbsd.org/source-changes/2017/11/08/msg089525.html,
it breaks Xen:
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/amd64/201711082340Z_anita.txt
 1.114 11-Nov-2017  riastradh No externs in .c files!
 1.113 08-Nov-2017  maxv Call fpuinit_mxcsr_mask in cpu_init, after cr4 is initialized, but before
touching xcr0. Then use clts/stts instead of modifying cr0, and enable the
mxcsr_mask detection on Xen.
 1.112 17-Sep-2017  maxv Remove TRAPLOG from i386. Nowadays there are better instrumentation tools,
in both software and hardware.
 1.111 06-Jul-2017  bouyer gdt_prepframes() is called with a number of pages, don't convert to a number
of pages again. This didn't fail because we're called with only one page, and
the conversion from '1' to pages resulted in 1 again.
 1.110 23-Mar-2017  maxv branches: 1.110.6;
Remove PG_k completely.
 1.109 11-Feb-2017  maxv Instead of using a global array with per-cpu indexes, embed the tmp VAs
into cpu_info directly. This concerns only {i386, Xen-i386, Xen-amd64},
because amd64 already has a direct map that is way faster than that.

There are two major issues with the global array: maxcpus entries are
allocated while it is unlikely that common i386 machines have so many
cpus, and the base VA of these entries is not cache-line-aligned, which
mostly guarantees cache-line-thrashing each time the VAs are entered.

Now the number of tmp VAs allocated is proportionate to the number of CPUs
attached (which therefore reduces memory consumption), and the base is
properly aligned.

On my 3-core AMD, the number of DC_refills_L2 events triggered when
performing 5x10^6 calls to pmap_zero_page on two dedicated cores is on
average divided by two with this patch.

Discussed on tech-kern a little.
 1.108 05-Feb-2017  maxv Rename ldt->ldtstore and gdt->gdtstore on i386. It reduces the diff with
amd64, and makes it easier to track down these variables on nxr - 'ldt'
and 'gdt' being common keywords.
 1.107 02-Feb-2017  maxv Use __read_mostly on these variables, to reduce the probability of false
sharing.
 1.106 22-Jan-2017  maxv Import xpmap_pg_nx, and put it in the per-cpu recursive slot on amd64.
 1.105 25-Nov-2016  maxv branches: 1.105.2;
KNF a little
 1.104 07-Jul-2016  msaitoh branches: 1.104.2;
KNF. Remove extra spaces. No functional change.
 1.103 13-Dec-2015  christos need definition
 1.102 13-Dec-2015  christos fix the build.
 1.101 08-Dec-2014  msaitoh Modify around cpu_identify() to not to break the dmesg of cpus with AB_VERBOSE
or AB_DEBUG.
 1.100 27-Nov-2014  bouyer branches: 1.100.2;
Revert sys/arch/x86/x86/pmap.c 1.185; a CPU needs to get pmap updates,
especially for pmap_kernel(), as soon as it is up.
Instead move all pmap-related cpu_info initialisations, including
initializing ci_kpm_mtx, in cpu_attach_common() from cpu_init()
(ci_pmap and ci_tlbstate as already initialized in cpu_attach_common()).
 1.99 18-Oct-2014  snj src is too big these days to tolerate superfluous apostrophes. It's
"its", people!
 1.98 12-Feb-2014  dsl branches: 1.98.4;
Change i386 to use x86/fpu.c instead of i386/isa/npx.c
This changes the trap10 and trap13 code to call directly into fpu.c,
removing all the code for T_ARITHTRAP, T_XMM and T_FPUNDA from i386/trap.c
Not all of the code thate appeared to handle fpu traps was ever called!
Most of the changes just replace the include of machine/npx.h with x86/fpu.h
(or remove it entirely).
 1.97 11-Feb-2014  dsl Move sys/arch/amd64/amd64/fpu.c and sys/arch/amd64/include/fpu.h
into sys/arch/x86 in preparation for using the same code for i386.
 1.96 26-Jan-2014  dsl Remove support for 'external' floating point units and the MS-DOS
compatible method of handling floating point exceptions.
Make kernel support for teh fpu non-optional (486SX should still work).
Only 386 cpus support external fpu, and i386 support was removed years ago.
This means that the npx code no longer uses port 0xf0 or interupt 13.
All the "npx at isa" lines go from the configs, arch/i386/isa/npx.c
is now mandatory for all i386 kernels.
I've renamed npxinit() to fpuinit() and npxinit_cpu() to fpuinit_cpu()
to match the very similar amd64 functions.
The fpu of the boot cpu is now initialised by a direct call from
cpu_configure(), this enables FP emulation for a 486SX.
(for amd64 the cr0 values are set in locore.S and similar).
This fixes a long-standing bug in linux_setregs() - which did not
save the fpu regsiters if they were active.
I've test booted a single cpu i386 kernel (using anita).
amd64 builds - none of teh changes should affect it.
The i386 XEN kernels build, but I'm not sure where they set cr0, and
it might have got lost!
 1.95 01-Dec-2013  christos revert fpu/pcu changes until we figure out what's wrong; they cause random
freezes
 1.94 23-Oct-2013  drochner Use the MI "pcu" framework for bookkeeping of npx/fpu states on x86.
This reduces the amount of MD code enormously, and makes it easier
to implement support for newer CPU features which require more fpu
state, or for fpu usage by the kernel.
For access to FPU state across CPUs, an xcall kthread is used now
rather than a dedicated IPI.
No user visible changes intended.
 1.93 24-Jun-2012  jym branches: 1.93.2; 1.93.4;
Update comment: we stopped using xcall to sync PTP between CPUs.
pmap_kpm_sync_xcall => xen_kpm_sync
 1.92 06-Jun-2012  rmind Few fixes for Xen:
- cpu_load_pmap: use atomic kcpuset(9) operations; fixes rare crashes.
- Add kcpuset_copybits(9) and replace xen_kcpuset2bits(). Avoids incorrect
ncpu problem in early boot. Also, micro-optimises xen_mcast_invlpg() and
xen_mcast_tlbflush() routines.

Tested by chs@.
 1.91 20-Apr-2012  rmind - Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.

- Support up to 256 CPUs on amd64 architecture by default.

Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
 1.90 11-Mar-2012  jym Typo fix.
 1.89 25-Feb-2012  bouyer The code assumes that ci_index is also the Xen's cpunum, and that
cpunum is less than XEN_LEGACY_MAX_VCPUS. KASSERT both.
 1.88 24-Feb-2012  bouyer Don't maintain ci_cpumask for physical CPUs, it's not used.
 1.87 24-Feb-2012  bouyer Get rid of phycpus_attached bitmask; it's maintained but not used and
will limit the number of physical CPUs to 32 without good reasons.
 1.86 24-Feb-2012  cherry (xen) - remove the (*xpq_cpu)() shim.We hasten the %fs/%gs setup process during boot.Although this is hacky, it lets us use the non-xen specificpmap_pte_xxx() functions in pmap code (and others).
 1.85 23-Feb-2012  cherry Cleanup.

- Remove cruft from native x86 origin.
- Remove access to privileged MSRs.
- Cleanup stale comments.
 1.84 23-Feb-2012  cherry cpu_load_pmap() should not be used to load pmap_kernel(), since in the
x86 model, its mappings are shared across pmaps. KASSERT() for this
and remove unused codepaths.
 1.83 22-Feb-2012  bouyer use pmap_protect() instead of pmap_kenter_pa() to remap R/O an exiting
page. This gets rid of the last "mapping already present" warnings.
 1.82 21-Feb-2012  bouyer Avoid early use of xen_kpm_sync(); locks are not available at this time.
Don't call cpu_init() twice.

Makes LOCKDEBUG kernels boot again
 1.81 17-Feb-2012  bouyer Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.

2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.

To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.

to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.

While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
 1.80 13-Feb-2012  jym branches: 1.80.2;
PAT flags are not under control of Xen domains currently, so there is no
point in enabling them.

Avoids:
- a warning logged by hypervisor when a domain attempts to modify the PAT
MSR.
- an error during domain resuming, where a PAT flag has been set on a page
while the hypervisor does not allow it.

ok releng@.
 1.79 28-Jan-2012  cherry Update comments to remove references to alternate pte space.
 1.78 28-Jan-2012  cherry stop using alternate pde mapping in xen pmap
 1.77 09-Jan-2012  cherry revert previous commit. DIAGNOSTIC should only do strict checks, not muffle current ones
 1.76 06-Jan-2012  cherry Address those pesky DIAGNOSTIC messages. \n
Take a performance hit at fork() for not DTRT. \n
Note: Only applicable for kernels built with "options DIAGNOSTIC" \n
 1.75 04-Jan-2012  cherry Use macro PDP_SIZE instead of numeric constant, for unshared PAE L3 entries.
Thanks jym@
 1.74 30-Dec-2011  cherry Never cut-paste code from email!
Use the right count (0 -> 2) of l3 unshared userland entries for per-cpu initialisation.
 1.73 30-Dec-2011  cherry Force pae l3 page allocation for new vcpus to be < 4G, so they fit in 32bits
 1.72 30-Dec-2011  cherry per-cpu shadow directory pages should be updated locally via cross-calls. Do this.
 1.71 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.70 06-Nov-2011  cherry branches: 1.70.4;
[merging from cherry-xenmp] make pmap_kernel() shadow PMD per-cpu and MP aware.
 1.69 06-Nov-2011  cherry [merging from cherry-xenmp] Make the xen MMU op queue locking api private. Implement per-cpu queues.
 1.68 20-Oct-2011  jruoho branches: 1.68.2;
Remove code that is commented out and out-of-sync with x86. If Xen needs to
use cpu_resume(), cpu_suspend(), or cpu_shutdown() in the future, it is
better to expose these from x86 rather than duplicate code.
 1.67 06-Oct-2011  mrg remove a check against uvmexp.ncolors that is done inside uvm_page_recolor()
already anyway.
 1.66 28-Sep-2011  jruoho Call cpufreq_suspend(9) and cpufreq_resume(9) during suspend/resume.
 1.65 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.64 16-Aug-2011  dholland Fix broken build.
 1.63 15-Aug-2011  cherry Do not panic() on xen_send_ipi() sent to a cpu not yet running.
x86 MP boot depends on this strange behaviour.
 1.62 13-Aug-2011  cherry MP probing and startup code
 1.61 11-Aug-2011  cherry Hide the MD details of specific IPIs behind semantically pleasing functions. This cleans up a couple of #ifdef XEN/#endif pairs
 1.60 16-Jul-2011  rmind Initialise cpus_running to 1 on Xen, as it was done on x86.

Problem analysed by hannken@. Fixes PR/45062.
 1.59 15-Jun-2011  rmind Few XEN fixes:
- cpu_load_pmap: perform tlbflush() after xen_set_user_pgd().
- xen_pmap_bootstrap: perform xpq_queue_tlb_flush() in the end.
- pmap_tlb_shootdown: do not check PG_G for Xen.
 1.58 15-Jun-2011  rmind - cpu_hatch: call tlbflushg(), just to make sure that TLB is clean.
- xen_bootstrap_tables: call xpq_queue_tlb_flush() for safety.
- Initialise cpus_attached and ci_cpumask for primary CPU.
 1.57 12-Jun-2011  rmind Welcome to 5.99.53! Merge rmind-uvmplock branch:

- Reorganize locking in UVM and provide extra serialisation for pmap(9).
New lock order: [vmpage-owner-lock] -> pmap-lock.

- Simplify locking in some pmap(9) modules by removing P->V locking.

- Use lock object on vmobjlock (and thus vnode_t::v_interlock) to share
the locks amongst UVM objects where necessary (tmpfs, layerfs, unionfs).

- Rewrite and optimise x86 TLB shootdown code, make it simpler and cleaner.
Add TLBSTATS option for x86 to collect statistics about TLB shootdowns.

- Unify /dev/mem et al in MI code and provide required locking (removes
kernel-lock on some ports). Also, avoid cache-aliasing issues.

Thanks to Andrew Doran and Joerg Sonnenberger, as their initial patches
formed the core changes of this branch.
 1.56 26-Feb-2011  jruoho branches: 1.56.2;
Use config_defer(9) for cpu_rescan() in cpu_attach().
Also mark few local functions as static.
 1.55 24-Feb-2011  jruoho Catch up with x86 on cpufeaturebus.
 1.54 24-Feb-2011  jruoho Move PowerNow! to the cpufeaturebus.
 1.53 24-Feb-2011  jruoho Add cpufeaturebus and est(4) for Xen.
 1.52 14-Nov-2010  bouyer branches: 1.52.2; 1.52.4;
Boot vs AP processors don't make sense for physical CPUs, these are
handled by the hypervisor and all CPUs are running when the dom0 is started.
In addition, we don't have a reliable way to determine the boot CPU as
- we may not be running on the boot CPU
- we don't have access to the lapic id
So simplify by ignoring the information and assign phycpu_info_primary to the
first attached CPU.
 1.51 06-Nov-2010  uebayasi Machine dependent code is considered as part of UVM. Include
internal API header.
 1.50 03-Nov-2010  jruoho Fill cpu_info::ci_acpiid also on Xen.
 1.49 20-Aug-2010  jruoho Revert all previous changes that were made naively believing that the
existing CPU power management implementations could peacefully coexist with
the acpicpu(4) driver. The following options can not be used with acpicpu(4):
ENHANCED_SPEEDSTEP, INTEL_ONDEMAND_CLOCKMOD, POWERNOW_K7, and POWERNOW_K8.
 1.48 09-Aug-2010  jruoho Revert the previous changes to EST. The used hack had an obvious flaw:
the acpicpu(4) driver should attach even if the existing frequency management
code fails to attach, mainly because ACPI is the only proper way to deal
with EST on new Intel system.

Use a more drastic hack to deal with this: when acpicpu(4) attachs, it tears
down any existing sysctl(8) controls and installs identical ones in place.
Upon detachment, the initialization function of the existing EST is called.
 1.47 24-Jul-2010  jym Welcome PAE inside i386 current.

This patch is inspired by work previously done by Jeremy Morse, ported by me
to -current, merged with the work previously done for port-xen, together with
additionals fixes and improvements.

PAE option is disabled by default in GENERIC (but will be enabled in ALL in
the next few days).

In quick, PAE switches the CPU to a mode where physical addresses become
36 bits (64 GiB). Virtual address space remains at 32 bits (4 GiB). To cope
with the increased size of the physical address, they are manipulated as
64 bits variables by kernel and MMU.

When supported by the CPU, it also allows the use of the NX/XD bit that
provides no-execution right enforcement on a per physical page basis.

Notes:

- reworked locore.S

- introduce cpu_load_pmap(), used to switch pmap for the curcpu. Due to the
different handling of pmap mappings with PAE vs !PAE, Xen vs native, details
are hidden within this function. This helps calling it from assembly,
as some features, like BIOS calls, switch to pmap_kernel before mapping
trampoline code in low memory.

- some changes in bioscall and kvm86_call, to reflect the above.

- the L3 is "pinned" per-CPU, and is only manipulated by a
reduced set of functions within pmap. To track the L3, I added two
elements to struct cpu_info, namely ci_l3_pdirpa (PA of the L3), and
ci_l3_pdir (the L3 VA). Rest of the code considers that it runs "just
like" a normal i386, except that the L2 is 4 pages long (PTP_LEVELS is
still 2).

- similar to the ci_pae_l3_pdir{,pa} variables, amd64's xen_current_user_pgd
becomes an element of cpu_info (slowly paving the way for MP world).

- bootinfo_source struct declaration is modified, to cope with paddr_t size
change with PAE (it is not correct to assume that bs_addr is a paddr_t when
compiled with PAE - it should remain 32 bits). bs_addrs is now a
void * array (in bootloader's code under i386/stand/, the bs_addrs
is a physaddr_t, which is an unsigned long).

- fixes in multiboot code (same reason as bootinfo): paddr_t size
change. I used Elf32_* types, use RELOC() where necessary, and move the
memcpy() functions out of the if/else if (I do not expect sym and str tables
to overlap with ELF).

- 64 bits atomic functions for pmap

- all pmap_pdirpa access are now done through the pmap_pdirpa macro. It
hides the L3/L2 stuff from PAE, as well as the pm_pdirpa change in
struct pmap (it now becomes a PDP_SIZE array, with or without PAE).

- manipulation of recursive mappings ( PDIR_SLOT_{,A}PTEs ) is done via
loops on PDP_SIZE.

See also http://mail-index.netbsd.org/port-i386/2010/07/17/msg002062.html

No objection raised on port-i386@ and port-xen@R for about a week.

XXX kvm(3) will be fixed in another patch to properly handle both PAE and !PAE
kernel dumps (VA => PA macros are slightly different, and need proper 64 bits
PA support in kvm_i386).

XXX Mixing PAE and !PAE modules may lead to unwanted/unexpected results. This
cannot be solved easily, and needs lots of thinking before being declared
safe (paddr_t/bus_addr_t size handling, PD/PT macros abstractions).
 1.46 06-Jul-2010  cegger Turn PMAP_NOCACHE into MI flag.
Add MI flags PMAP_WRITE_COMBINE, PMAP_WRITE_BACK, PMAP_NOCACHE_OVR.
Update pmap(9) manpage.

hppa: Remove MD PMAP_NOCACHE flag as it exists as MI flag
mips: Rename MD PMAP_NOCACHE to PGC_NOCACHE.

x86: Implement new MI flags using Page-Attribute Tables.
x86: Implement BUS_SPACE_MAP_PREFETCHABLE.

Patch presented on tech-kern@:
http://mail-index.netbsd.org/tech-kern/2010/06/30/msg008458.html

No comments on this last version.
 1.45 28-Jun-2010  rmind mp_cpu_start: although fragment is commented out, add pmap_update(), just
in case somebody would come up with a clever idea to copy-paste that.
 1.44 04-May-2010  jym Enable the NX bit feature for Xen i386pae and amd64 kernels.

Tested with Xen 3.1 and Xen 3.3, dom0 and domU, by bouyer@ and jym@.

Ok bouyer@.
 1.43 18-Apr-2010  jym This patch fixes the NX regression issue observed on amd64 kernels, where
per-page execution right was disabled (therefore leading to the inability
of the kernel to detect fraudulent use of memory mappings marked as not
being executable).

- replace cpu_feature and ci_feature_flags variables by cpu_feature and
ci_feat_val arrays. This makes it cleaner and brings kernel code closer
to the design of cpuctl(8). A warning will be raised for each CPU that
does not expose the same features as the Boot Processor (BP).

- the blacklist of CPU features is now a macro defined in the
specialreg.h header, instead of hardcoding it inside MD initialization
code; fix comments.

- replace checks against CPUID_TSC with the cpu_hascounter() function.

- clean up the code in init_x86_64(), as cpu_feature variables are set
inside cpu_probe().

- use cpu_init_msrs() for i386. It will be eventually used later for NX
feature under i386 PAE kernels.

- remove code that checks for CPUID_NOX in amd64 mptramp.S, this is already
performed by cpu_hatch() through cpu_init_msrs().

- remove cpu_signature and feature_flags members from struct mpbios_proc
(they were never used).

This patch was tested with i386 MONOLITHIC, XEN3PAE_DOM0 and XEN3_DOM0 under
a native i386 host, and amd64 GENERIC, XEN3_DOM0 via QEMU virtual machines.

XXX Should kernel rev be bumped?

XXX A similar patch should be pulled-up for NetBSD-5, hopefully tomorrow.
 1.42 03-Mar-2010  jym branches: 1.42.2;
Use roundup2() instead of hardcoding the CACHE_LINE_SIZE rounding
operation.
 1.41 24-Feb-2010  dyoung A pointer typedef entails trading too much flexibility to declare const
and non-const types, and the kernel uses both const and non-const
PMF qualifiers and device suspensors, so change the pmf_qual_t and
device_suspensor_t typedefs from "pointers to const" to non-pointer,
non-const types.
 1.40 08-Jan-2010  dyoung branches: 1.40.2;
Expand PMF_FN_* macros.
 1.39 27-Nov-2009  rmind - Use uvm_lwp_setuarea() instead of directly setting address to lwp_t::l_addr.
- Replace most remaining uses of l_addr with uvm_lwp_getuarea() or lwp_getpcb().
- Amend assembly in ports where it accesses PCB via struct user.
- Rename L_ADDR to L_PCB in few places. Reduce sys/user.h inclusions.
 1.38 24-Nov-2009  cegger Remove X86_MAXPROCS. This fixes PR port-xen/41755.
This also reduces diff to x86/x86/cpu.c as a nice side effect.
'looks good' bouyer@
 1.37 21-Nov-2009  rmind Catch-up Xen and usermode with lwp_getpcb() and unbreak Xen build.
 1.36 07-Nov-2009  cegger Add a flags argument to pmap_kenter_pa(9).
Patch showed on tech-kern@ http://mail-index.netbsd.org/tech-kern/2009/11/04/msg006434.html
No objections.
 1.35 22-Sep-2009  cegger fix botch with merging in changes from x86/x86/cpu.c:

don't use wbinvd(). Xen flushes the cache for us.
This makes DomU boot again.
Spotted by bouyer@.
 1.34 30-Jul-2009  cegger from x86/x86/cpu.c:
- use atomic operations to set flags
- Align struct cpu_info to 64b.
 1.33 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.32 08-Jun-2009  cegger from sys/arch/x86/x86/cpu.c:

Implement -1 (RB_MD1) for physical CPUs in the Dom0.
 1.31 23-Dec-2008  cegger branches: 1.31.2;
catch up with x86/x86/cpu.c: move from malloc to kmem
 1.30 06-Nov-2008  cegger Link cpus in the order they are attaching and not in inverse order.
 1.29 31-Oct-2008  rmind - Avoid the race with CPU online/offline state changes, when setting the
affinity (cpu_lock protects these operations now).
- Disallow setting of state of CPU to to offline, if there are bound LWPs,
which have no CPU to migrate.
- Disallow setting of affinity for the LWP(s), if all CPUs in the dynamic
CPU-set are offline.
- sched_setaffinity: fix invalid check of kcpuset_isset().
- Rename cpu_setonline() to cpu_setstate().

Should fix PR/39349.
 1.28 22-Aug-2008  bouyer branches: 1.28.2; 1.28.4;
printf()->aprint_debug_dev() to match x86/cpu.c
 1.27 28-May-2008  ad branches: 1.27.4;
Give it a private X86_MAXPROCS def. XXX
 1.26 16-May-2008  bouyer call x86_cpu_idle_init(), avoid null function pointer call (cpu_idle()) when
scheduling starts.
cleanup printfs of vcpu
 1.25 11-May-2008  ad Fix typo.
 1.24 11-May-2008  ad Don't reload LDTR unless a new value, which only happens for USER_LDT.
 1.23 11-May-2008  ad Stop using APIC IDs to identify CPUs for software purposes. Allows for
APIC IDs beyond 31, which has been possible for some time now.
 1.22 11-May-2008  ad Share cpu.h between the x86 ports.
 1.21 11-May-2008  ad Update xen for identcpu changes.
 1.20 10-May-2008  ad Make xen build after tsc changes.
 1.19 09-May-2008  joerg Make cpu_idle a macro calling a function pointer on x86.
Select the Xen idle routine for Xen, mwait if supported by the CPU and
it is not AMD and halt otherwise. As reported by Christoph Egger,
AMD Barcelona keeps the CPU in C0 state with MWAIT, contrary to HLT,
which uses C1 and therefore much less power.
 1.18 28-Apr-2008  martin branches: 1.18.2;
Remove clause 3 and 4 from TNF licenses
 1.17 24-Apr-2008  cegger branches: 1.17.2;
keep up with x86/x86/cpu.c, rev. 1.33:
Gracefully handle a condition where apic id >= X86_MAXPROCS rather than panicing.
 1.16 21-Apr-2008  cegger Access Xen's vcpu info structure per-CPU.
Tested on i386 and amd64 (both dom0 and domU) by me.
Xen2 tested (both dom0 and domU) by bouyer.
OK bouyer
 1.15 18-Apr-2008  cegger branches: 1.15.2;
g/c unused ioapic_bsp_id.
Per discussion with bouyer.
 1.14 17-Apr-2008  bouyer Do not set ioapic_bsp_id in cpu_attach_common(). It's already initialized
in cpu_attach(), and doing it here will overwrite the cpu_number of the
physical CPU with the one from the virtual CPU (which is always 0).
XXX is ioapic_bsp_id read somewhere ?
 1.13 17-Apr-2008  yamt cpu_debug_dump: s/curproc/curlwp/ in a message.
 1.12 17-Apr-2008  cegger reduce diff to x86/x86/cpu.c
 1.11 13-Apr-2008  cegger reduce diff to x86/x86/cpu.c
 1.10 13-Apr-2008  cegger - device_t/softc split
- ansify
 1.9 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.8 16-Jan-2008  dogcow branches: 1.8.6;
cargo-cult copy cpu_offline_md; fixes compile on i386/x86_64
 1.7 11-Jan-2008  bouyer 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.6 04-Jan-2008  yamt branches: 1.6.2;
i386:
- make tss per-cpu. this considerably speeds up context switch for,
at least, pentium4, where ltr instruction seems very slow.
i386, xen:
- kill cpu_maxproc.
kvm86:
- adapt to per-cpu tss.
- cleanup and simplify.
- move kvm86_mp_lock to more meaningful place.
- disable preemption during a call.
 1.5 18-Dec-2007  joerg Add new IPI for saving CPU state explicitly, share high-level part of
ACPI wakeup code and teach it how to start the APs again. As a side
effect the CPU_START interface allows choosing between different
bootstrap codes more easily now.
 1.4 12-Dec-2007  bouyer Initialize ci_idepth in cpu_info_primary, makes LOCKDEBUG kernels boot.
 1.3 10-Dec-2007  bouyer branches: 1.3.2;
Make Xen kernels build again.
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.6; 1.2.8; 1.2.10;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file cpu.c was initially added on branch bouyer-xenamd64.
 1.1.4.3 18-Feb-2008  mjf Sync with HEAD.
 1.1.4.2 27-Dec-2007  mjf Sync with HEAD.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.4 18-Nov-2007  bouyer Ignore MTRRs for now, make kernel build again.
 1.1.2.3 18-Nov-2007  bouyer Sync with HEAD
 1.1.2.2 25-Oct-2007  bouyer Finish sync with HEAD. Especially use the new x86 pmap for xenamd64.
For this:
- rename pmap_pte_set() to pmap_pte_testset()
- make pmap_pte_set() a function or macro for non-atomic PTE write
- define and use pmap_pa2pte()/pmap_pte2pa() to read/write PTE entries
- define pmap_pte_flush() which is a nop in x86 case, and flush the
MMUops queue in the Xen case
 1.1.2.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.2.10.2 13-Dec-2007  yamt sync with head.
 1.2.10.1 11-Dec-2007  yamt sync with head.
 1.2.8.3 21-Jan-2008  yamt sync with head
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file cpu.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:16 +0000
 1.2.6.1 26-Dec-2007  ad Sync with head.
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file cpu.c was added on branch vmlocking on 2007-12-03 19:04:38 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file cpu.c was added on branch jmcneill-pm on 2007-11-27 19:36:18 +0000
 1.3.2.5 19-Jan-2008  bouyer Sync with HEAD
 1.3.2.4 08-Jan-2008  bouyer Sync with HEAD
 1.3.2.3 06-Jan-2008  bouyer Merge needed changes to genassym.cf and locore.S for xeni386 back to
arch/i386. Switch xeni386 to use the arch/i386 cpu.h.
 1.3.2.2 02-Jan-2008  bouyer Sync with HEAD
 1.3.2.1 13-Dec-2007  bouyer Sync with HEAD
 1.6.2.3 23-Mar-2008  matt sync with HEAD
 1.6.2.2 09-Jan-2008  matt sync with HEAD
 1.6.2.1 04-Jan-2008  matt file cpu.c was added on branch matt-armv6 on 2008-01-09 01:50:13 +0000
 1.8.6.3 17-Jan-2009  mjf Sync with HEAD.
 1.8.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.8.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.15.2.2 04-Jun-2008  yamt sync with head
 1.15.2.1 18-May-2008  yamt sync with head.
 1.17.2.7 09-Oct-2010  yamt sync with head
 1.17.2.6 11-Aug-2010  yamt sync with head.
 1.17.2.5 11-Mar-2010  yamt sync with head
 1.17.2.4 19-Aug-2009  yamt sync with head.
 1.17.2.3 20-Jun-2009  yamt sync with head
 1.17.2.2 04-May-2009  yamt sync with head.
 1.17.2.1 16-May-2008  yamt sync with head.
 1.18.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.18.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.27.4.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.27.4.1 19-Oct-2008  haad Sync with HEAD.
 1.28.4.3 22-Nov-2010  riz Pull up following revision(s) (requested by bouyer in ticket #1475):
sys/arch/xen/x86/cpu.c: revision 1.52
Boot vs AP processors don't make sense for physical CPUs, these are
handled by the hypervisor and all CPUs are running when the dom0 is started.
In addition, we don't have a reliable way to determine the boot CPU as
- we may not be running on the boot CPU
- we don't have access to the lapic id
So simplify by ignoring the information and assign phycpu_info_primary to the
first attached CPU.
 1.28.4.2 22-Apr-2010  snj Apply patch (requested by jym in ticket #1380):
Fix the NX regression issue observed on amd64 kernels, where per-page
execution right was disabled (therefore leading to the inability
of the kernel to detect fraudulent use of memory mappings marked as not
being executable).
 1.28.4.1 13-Nov-2008  snj branches: 1.28.4.1.2; 1.28.4.1.4;
Pull up following revision(s) (requested by rmind in ticket #48):
sys/kern/kern_cpu.c: revision 1.37
sys/arch/x86/x86/cpu.c: revision 1.58
sys/arch/xen/x86/cpu.c: revision 1.29
sys/sys/cpu.h: revision 1.24
sys/kern/sys_sched.c: revision 1.31
- Avoid the race with CPU online/offline state changes, when setting the
affinity (cpu_lock protects these operations now).
- Disallow setting of state of CPU to to offline, if there are bound LWPs,
which have no CPU to migrate.
- Disallow setting of affinity for the LWP(s), if all CPUs in the dynamic
CPU-set are offline.
- sched_setaffinity: fix invalid check of kcpuset_isset().
- Rename cpu_setonline() to cpu_setstate().
Should fix PR/39349.
 1.28.4.1.4.1 20-May-2011  matt bring matt-nb5-mips64 up to date with netbsd-5-1-RELEASE (except compat).
 1.28.4.1.2.1 23-Apr-2010  snj Apply patch (requested by jym in ticket #1380):
Fix the NX regression issue observed on amd64 kernels, where per-page
execution right was disabled (therefore leading to the inability
of the kernel to detect fraudulent use of memory mappings marked as not
being executable).
 1.28.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.31.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.31.2.8 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.31.2.7 10-Jan-2011  jym Sync with HEAD
 1.31.2.6 24-Oct-2010  jym Sync with HEAD
 1.31.2.5 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.31.2.4 01-Nov-2009  jym Sync with HEAD.
 1.31.2.3 23-Jul-2009  jym Sync with HEAD.
 1.31.2.2 18-Jun-2009  cegger register physical CPUs with pmf.
No suspend/resume handlers needed since the hypervisor itself handles them.
ok @jym
 1.31.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.40.2.7 09-Nov-2010  uebayasi Sync with HEAD.
 1.40.2.6 09-Nov-2010  uebayasi Sync with HEAD.
 1.40.2.5 06-Nov-2010  uebayasi Sync with HEAD.
 1.40.2.4 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.40.2.3 26-Aug-2010  uebayasi Fix build.
 1.40.2.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.40.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.42.2.4 05-Mar-2011  rmind sync with head
 1.42.2.3 03-Jul-2010  rmind sync with head
 1.42.2.2 31-May-2010  rmind - Split off Xen versions of pmap_map_ptes/pmap_unmap_ptes into Xen pmap,
also move pmap_apte_flush() with pmap_unmap_apdp() there.
- Make Xen buildable.
 1.42.2.1 30-May-2010  rmind sync with head
 1.52.4.1 05-Mar-2011  bouyer Sync with HEAD
 1.52.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.56.2.12 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.56.2.11 09-Sep-2011  cherry fix amd64 boot.
 1.56.2.10 01-Sep-2011  cherry fix %cr3 init. from mhitch@, tested by riz@ & mhitch@
 1.56.2.9 30-Aug-2011  cherry Add per-cpu mmu queues
 1.56.2.8 26-Aug-2011  cherry Name the L4 per-cpu pointer appropriately.
User cr3 should point to the per-cpu L4, not the user pmap pdir
 1.56.2.7 20-Aug-2011  cherry PAE MP support (preliminary), amd64 per-cpu L4 model redesigned, i386 pmap_pa_start/end fixup
 1.56.2.6 17-Aug-2011  cherry Pullup relevant changes from -current
 1.56.2.5 07-Aug-2011  cherry Fix XEN3PAE_DOMx build
 1.56.2.4 31-Jul-2011  cherry grow MP support for i386. boots to single user
 1.56.2.3 16-Jul-2011  cherry Introduce a per-cpu "shadow" for pmap_kernel()'s L4 page
 1.56.2.2 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.56.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.68.2.5 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.68.2.4 30-Oct-2012  yamt sync with head
 1.68.2.3 23-May-2012  yamt sync with head.
 1.68.2.2 17-Apr-2012  yamt sync with head
 1.68.2.1 10-Nov-2011  yamt sync with head
 1.70.4.5 29-Apr-2012  mrg sync to latest -current.
 1.70.4.4 05-Apr-2012  mrg sync to latest -current.
 1.70.4.3 04-Mar-2012  mrg sync to latest -current.
 1.70.4.2 24-Feb-2012  mrg sync to -current.
 1.70.4.1 18-Feb-2012  mrg merge to -current.
 1.80.2.5 12-Jun-2012  riz Pull up following revision(s) (requested by rmind in ticket #314):
sys/arch/xen/x86/cpu.c: revision 1.92
sys/kern/subr_kcpuset.c: revision 1.6
sys/sys/kcpuset.h: revision 1.6
sys/arch/xen/x86/x86_xpmap.c: revision 1.44
Few fixes for Xen:
- cpu_load_pmap: use atomic kcpuset(9) operations; fixes rare crashes.
- Add kcpuset_copybits(9) and replace xen_kcpuset2bits(). Avoids incorrect
ncpu problem in early boot. Also, micro-optimises xen_mcast_invlpg() and
xen_mcast_tlbflush() routines.
Tested by chs@.
 1.80.2.4 09-May-2012  riz Pull up following revision(s) (requested by rmind in ticket #202):
sys/arch/x86/include/cpuvar.h: revision 1.46
sys/arch/xen/include/xenpmap.h: revision 1.34
sys/arch/i386/include/param.h: revision 1.77
sys/arch/x86/x86/pmap_tlb.c: revision 1.5
sys/arch/x86/x86/pmap_tlb.c: revision 1.6
sys/arch/i386/i386/genassym.cf: revision 1.92
sys/arch/xen/x86/cpu.c: revision 1.91
sys/arch/x86/x86/pmap.c: revision 1.177
sys/arch/xen/x86/xen_pmap.c: revision 1.21
sys/arch/x86/acpi/acpi_wakeup.c: revision 1.31
sys/kern/subr_kcpuset.c: revision 1.5
sys/arch/amd64/include/param.h: revision 1.18
sys/sys/kcpuset.h: revision 1.5
sys/arch/x86/x86/mtrr_i686.c: revision 1.26
sys/arch/x86/x86/mtrr_i686.c: revision 1.27
sys/arch/xen/x86/x86_xpmap.c: revision 1.43
sys/arch/x86/x86/cpu.c: revision 1.98
sys/arch/amd64/amd64/mptramp.S: revision 1.14
sys/kern/sys_sched.c: revision 1.42
sys/arch/amd64/amd64/genassym.cf: revision 1.50
sys/arch/i386/i386/mptramp.S: revision 1.24
sys/arch/x86/include/pmap.h: revision 1.52
sys/arch/x86/include/cpu.h: revision 1.50
- Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.
- Support up to 256 CPUs on amd64 architecture by default.
Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
- pmap_tlb_shootdown: do not overwrite tp_cpumask with pm_cpus, but merge
like pm_kernel_cpus. Remove unecessary intersection with kcpuset_running.
Do not reset tp_userpmap if pmap_kernel().
- Remove pmap_tlb_mailbox_t wrapping, which is pointless after recent changes.
- pmap_tlb_invalidate, pmap_tlb_intr: constify for packet structure.
i686_mtrr_init_first: handle the case when there are no variable-size MTRR
registers available (i686_mtrr_vcnt == 0).
 1.80.2.3 24-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #45):
sys/arch/xen/x86/cpu.c: revision 1.87
sys/arch/xen/x86/cpu.c: revision 1.88
Get rid of phycpus_attached bitmask; it's maintained but not used and
will limit the number of physical CPUs to 32 without good reasons.
Don't maintain ci_cpumask for physical CPUs, it's not used.
 1.80.2.2 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #31):
sys/arch/x86/x86/pmap.c: revision 1.166
sys/arch/xen/x86/cpu.c: revision 1.83
- Make pmap_write_protect() work with pmap_kernel() too ((va & L2_FRAME)
strips the high bits of a LP64 address)
- use pmap_protect() in pmap_pdp_ctor() to remap the PDP read-only instead
of (ab)using pmap_kenter_pa(). No more "mapping already present" on
console with DIAGNOSTIC kernels
- make sure to zero the whole PDP (NTOPLEVEL_PDES doens't include
high-level entries on i386 and i386PAE, reserved by Xen). Not sure
how it has worked before
- remove an always-true test (&& pmap != pmap_kernel(); we KASSERT that
at the function entry).
use pmap_protect() instead of pmap_kenter_pa() to remap R/O an exiting
page. This gets rid of the last "mapping already present" warnings.
 1.80.2.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #29):
sys/arch/xen/x86/x86_xpmap.c: revision 1.39
sys/arch/xen/include/hypervisor.h: revision 1.37
sys/arch/xen/include/intr.h: revision 1.34
sys/arch/xen/x86/xen_ipi.c: revision 1.10
sys/arch/x86/x86/cpu.c: revision 1.97
sys/arch/x86/include/cpu.h: revision 1.48
sys/uvm/uvm_map.c: revision 1.315
sys/arch/x86/x86/pmap.c: revision 1.165
sys/arch/xen/x86/cpu.c: revision 1.81
sys/arch/x86/x86/pmap.c: revision 1.167
sys/arch/xen/x86/cpu.c: revision 1.82
sys/arch/x86/x86/pmap.c: revision 1.168
sys/arch/xen/x86/xen_pmap.c: revision 1.17
sys/uvm/uvm_km.c: revision 1.122
sys/uvm/uvm_kmguard.c: revision 1.10
sys/arch/x86/include/pmap.h: revision 1.50
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.
2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.
To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.
to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.
While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
When using uvm_km_pgremove_intrsafe() make sure mappings are removed
before returning the pages to the free pool. Otherwise, under Xen,
a page which still has a writable mapping could be allocated for
a PDP by another CPU and the hypervisor would refuse it (this is
PR port-xen/45975).
For this, move the pmap_kremove() calls inside uvm_km_pgremove_intrsafe(),
and do pmap_kremove()/uvm_pagefree() in batch of (at most) 16 entries
(as suggested by Chuck Silvers on tech-kern@, see also
http://mail-index.netbsd.org/tech-kern/2012/02/17/msg012727.html and
followups).
Avoid early use of xen_kpm_sync(); locks are not available at this time.
Don't call cpu_init() twice.
Makes LOCKDEBUG kernels boot again
Revert pmap_pte_flush() -> xpq_flush_queue() in previous.
 1.93.4.1 18-May-2014  rmind sync with head
 1.93.2.2 03-Dec-2017  jdolecek update from HEAD
 1.93.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.98.4.2 07-Mar-2016  msaitoh Pullup the following revision to fix build break caused by ticket #1118:

sys/arch/xen/x86/cpu.c 1.102-1.103

Increase the number of entries of cpu_features from 5 to 7.
 1.98.4.1 04-Aug-2015  snj branches: 1.98.4.1.2;
Pull up following revision(s) (requested by prlw1 in ticket #934):
sys/arch/xen/x86/cpu.c: revision 1.100
Move all pmap-related cpu_info initialisations, including
initializing ci_kpm_mtx, in cpu_attach_common() from cpu_init()
(ci_pmap and ci_tlbstate as already initialized in cpu_attach_common()).
 1.98.4.1.2.1 20-Mar-2018  martin Additionally pull up the following for ticket #1118:

sys/arch/xen/x86/cpu.c 1.102-1.103

to unbreak the build (adjust cpu_feature declaration to changes in generic
x86 code).
 1.100.2.6 28-Aug-2017  skrll Sync with HEAD
 1.100.2.5 05-Feb-2017  skrll Sync with HEAD
 1.100.2.4 05-Dec-2016  skrll Sync with HEAD
 1.100.2.3 09-Jul-2016  skrll Sync with HEAD
 1.100.2.2 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.100.2.1 06-Apr-2015  skrll Sync with HEAD
 1.104.2.3 26-Apr-2017  pgoyette Sync with HEAD
 1.104.2.2 20-Mar-2017  pgoyette Sync with HEAD
 1.104.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.105.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.110.6.1 13-Mar-2018  martin Pullup the following revisions via patch, requested by maxv in ticket #629:

sys/arch/amd64/amd64/genassym.cf 1.63,1.64
sys/arch/amd64/amd64/locore.S 1.144
sys/arch/amd64/amd64/machdep.c 1.281-1.283
sys/arch/i386/i386/genassym.cf 1.105-1.106
sys/arch/i386/i386/locore.S 1.155
sys/arch/i386/i386/machdep.c 1.802 (adapted),1.803
sys/arch/x86/include/cpu.h 1.85
sys/arch/x86/x86/intr.c 1.115-1.116
sys/arch/x86/x86/pmap.c 1.275
sys/arch/x86/x86/sys_machdep.c 1.45
sys/arch/xen/x86/cpu.c 1.117

Stop sharing the double-fault stack.
Merge the TSS structures into one single cpu_tss structure, and
allocate it dynamically.
 1.117.2.3 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.117.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.117.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.122.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.122.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.122.2.1 10-Jun-2019  christos Sync with HEAD
 1.131.2.2 29-Feb-2020  ad Sync with head.
 1.131.2.1 17-Jan-2020  ad Sync with head.
 1.133.4.1 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.139.4.1 02-Apr-2021  thorpej config_found_ia() -> config_found() w/ CFARG_IATTR.
 1.140.8.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.142.4.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.46 01-Mar-2023  riastradh xen/x86: Need kpreempt_disable/enable around curcpu() access.

This is called with `hardware' interrupts enabled (between sti and
cli), so presumably preemption is possible here.

XXX pullup-8
XXX pullup-9
XXX pullup-10
 1.45 07-Sep-2022  knakahara branches: 1.45.4;
NetBSD/x86: Raise the number of interrupt sources per CPU from 32 to 56.

There has been no objection for three years.
https://mail-index.netbsd.org/port-amd64/2019/09/22/msg003012.html
Implemented by nonaka@n.o, updated by me.
 1.44 20-Aug-2022  riastradh x86: Split most of pmap.h into pmap_private.h or vmparam.h.

This way pmap.h only contains the MD definition of the MI pmap(9)
API, which loads of things in the kernel rely on, so changing x86
pmap internals no longer requires recompiling the entire kernel every
time.

Callers needing these internals must now use machine/pmap_private.h.
Note: This is not x86/pmap_private.h because it contains three parts:

1. CPU-specific (different for i386/amd64) definitions used by...

2. common definitions, including Xenisms like xpmap_ptetomach,
further used by...

3. more CPU-specific inlines for pmap_pte_* operations

So {amd64,i386}/pmap_private.h defines 1, includes x86/pmap_private.h
for 2, and then defines 3. Maybe we should split that out into a new
pmap_pte.h to reduce this trouble.

No functional change intended, other than that some .c files must
include machine/pmap_private.h when previously uvm/uvm_pmap.h
polluted the namespace with pmap internals.

Note: This migrates part of i386/pmap.h into i386/vmparam.h --
specifically the parts that are needed for several constants defined
in vmparam.h:

VM_MAXUSER_ADDRESS
VM_MAX_ADDRESS
VM_MAX_KERNEL_ADDRESS
VM_MIN_KERNEL_ADDRESS

Since i386 needs PDP_SIZE in vmparam.h, I added it there on amd64
too, just to keep things parallel.
 1.43 31-May-2022  bouyer When we have pending events in stipending(), evt_set_pending() has to set
the ih_pending flag for each handler too. Xen/i386 should be stable again.
 1.42 31-May-2022  bouyer Revert previous; evt_set_pending() will set ret to 1 if needed to this was
not our bug.
 1.41 31-May-2022  bouyer stipending(): if we're going to process some interrupts don't return 0.
Hopefully fixes random hang seen in i386 Xen PV.

The bug has been there ~forever but was masked by the fact that spllower()
did call event handlers much more often.
 1.40 19-May-2022  bouyer Restore de EOI mechanism for pirq, using the newer hypervisor interface.
It is needed.
Hopefully fixes kern/56291, kern/56793, kern/55667
 1.39 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.38 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.37 21-Apr-2020  jdolecek convert to newer HYPERVISOR_physdev_op() interface, now command and the
arg are separate arguments - this is needed for newer physdev_op commands

remove code for PHYSDEVOP_IRQ_UNMASK_NOTIFY, it is obsolete since
interface version 0x00030202 and is unsupported by newer versions of Xen

confirmed working on amd64 Dom0, i386 compile-tested only
 1.36 09-May-2019  bouyer branches: 1.36.2; 1.36.8;
sti/cli are not allowed on Xen, we have to clear/set a bit in the
shared page. Revert x86_disable_intr/x86_enable_intr to plain function
calls on XENPV.
While there, clean up unused functions and macros, and change cli()/sti()
macros to x86_disable_intr/x86_enable_intr.
Makes Xen domU boot again
(http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/)
 1.35 12-Feb-2019  cherry conditionally include XENPV specific code.

This explicitly excludes PV only functionality that would be wrong to
attempt to use in other modes, for eg: p2m table management.
 1.34 25-Dec-2018  cherry Excise XEN specific code out of x86/x86/intr.c into xen/x86/xen_intr.c

While at it, separate the source function tracking so that the interrupt
paths are truly independant.

Use weak symbol exporting to provision for future PVHVM co-existence
of both files, but with independant paths. Introduce assembler code
such that in a unified scenario, native interrupts get first priority
in spllower(), followed by XEN event callbacks. IPL management and
semantics are unchanged - native handlers and xen callbacks are
expected to maintain their ipl related semantics.

In summary, after this commit, native and XEN now have completely
unrelated interrupt handling mechanisms, including
intr_establish_xname() and assembler stubs and intr handler
management.

Happy Christmas!
 1.33 19-Nov-2018  kre Hide differences between i386 and amd64 interrupt frames so XEN does
not need to know there is one. Hopefully unbreak i386 build.
 1.32 18-Nov-2018  cherry On Xen, copy just the bits we need from the trapframe for hardclock(9)
and statclock(9).

Current, the macros that use the trapframe are:
CLKF_USERMODE()
CLKF_PC()
CLKF_INTR()

Of these, CLKF_INTR() already ignores the frame and uses the ci_idepth
variable to do its job.

Convert the two remaining ones to do this, but only for XEN.
 1.31 18-Nov-2018  cherry Save the interrupt trap/clockframe to a per-cpu copy.

We can use this copy to pass on the trapframe to hardclock(9) from
within the xen timer handler. This delinks the current dependency
between MD code and the handler, which is specially prototyped to take
the clockframe unlike any other handler.

This change has performance implications, as each interrupt entry will
copy the entire trapframe over to the per-cpu cached copy. This can be
mitigated by selectively copying just the parts of the clockframe that
are used by hardclock() et. al.

Tested on amd64 XEN domU
 1.30 17-Nov-2018  cherry Use hypervisor provided interface to unmask specific ports.

Although at first glance this looks suboptimal, the unmask operation
fast path does not use hypervisor_unmask_event(). Instead, it directly
operates on the mask and pending bit arrays to provide what would
effectively be an "auto mask/eoi" semantic.

This change is thus not in the fast path, and has the advantage of
performance improvements since cross CPU state updates etc. is handled
within the hypervisor instead of domU IPIs.
 1.29 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.28 21-Sep-2014  bouyer branches: 1.28.12; 1.28.18; 1.28.20;
Make Xen kernels compile without DIAGNOSTIC
 1.27 13-Jan-2013  bouyer branches: 1.27.12;
Re-apply
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039125.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039126.html
they're not involved in i386 domU hang shown by ATF.
 1.26 12-Jan-2013  bouyer Revert these commits from november 2012:
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039125.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039126.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039142.html

they cause a i386PAE domU to hang while running ATF tests, as shown in
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/

(we should pay more attention to test results, myself first).
 1.25 12-Jan-2013  bouyer Back out this commit:
http://mail-index.netbsd.org/source-changes/2012/12/28/msg039950.html
which cause a panic when running tests on amd64, as shown on:
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/
(i386 hangs for unrelated reasons).
 1.24 28-Dec-2012  cherry Simplify the xen event handler callback by:
- moving the interrupt handler callback traversal into a separate
function.
- using evt_iterate_bits() to scan through the pending bitfield
- removing cross-cpu pending actions - events recieved on the wrong
vcpu are re-routed via hypervisor_send_event().
- simplifying nested while() loops by encapsulating them in
equivalent functions.

Many thanks for multiple reviews by bouyer@ and jym@
 1.23 25-Nov-2012  cherry Make hypervisor_set_ipending() and its consumers cpu unaware. This syncs syntax with semantics
 1.22 10-Nov-2012  cherry Remove e a redundant layer of function calling in the event handling path
 1.21 27-Dec-2011  cherry branches: 1.21.6;
Optimise branch predict hint for the intended use-case (cross cpu event notification)
 1.20 27-Dec-2011  cherry Do not touch pending flags across vcpus
 1.19 26-Dec-2011  cherry Do not fiddle with the event masks of non-local vcpus when unmasking events across vcpus
 1.18 03-Dec-2011  bouyer branches: 1.18.2;
hypervisor_unmask_event(): don't check/update evtchn_pending_sel for the
current CPU, but for any CPU which may accept this event.
xen/xenevt.c: more use of atomic ops and locks where appropriate, and some
other SMP fixes. Handle all events on the primary CPU (may be revisited
later). Set/clear ci_evtmask[] for watched events.

This should fix the problems on dom0 kernels reported by jym@
 1.17 19-Nov-2011  cherry [merging from cherry-xenmp] bring in bouyer@'s changes via:
http://mail-index.netbsd.org/source-changes/2011/10/22/msg028271.html
From the Log:
Log Message:
Various interrupt fixes, mainly:
keep a per-cpu mask of enabled events, and use it to get pending events.
A cpu-specific event (all of them at this time) should not be ever masked
by another CPU, because it may prevent the target CPU from seeing it
(the clock events all fires at once for example).
 1.16 20-Sep-2011  jym branches: 1.16.2;
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.15 10-Aug-2011  cherry refactor the bitstring/mask operations to be behind an API. Make pending interrupt marking cpu aware.
 1.14 30-Mar-2011  jym branches: 1.14.2;
Fix a year old bug that was only fixed in jym-xensuspend branch, but
not in HEAD:
- use uvm_km_alloc() instead of kmem_alloc() to enforce alignement when
allocating p2m_frame pages (xentools can only deal with page-aligned
addresses)
- do not use paddr_t for p2m_frame_list_list with PAE, xentools expect
32 bits PFNs even with 64 bits PTE.

Required to make ``xm dump-core'' work as expected.
 1.13 23-Oct-2009  snj branches: 1.13.4; 1.13.6;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.12 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.11 21-Oct-2008  cegger branches: 1.11.8;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.10 16-Sep-2008  bouyer Implement the arch-dependent p2m frame lists list. This adds support for
'xm dump-core' for NetBSD domUs.
From Jean-Yves Migeon (jean-yves dot migeon at espci dot fr)
 1.9 01-Jul-2008  bouyer branches: 1.9.2;
Raise ci_idepth (and switch to interrupt stack on i386) becore calling
xenevt_event().
 1.8 21-Apr-2008  cegger branches: 1.8.2; 1.8.4; 1.8.6;
Access Xen's vcpu info structure per-CPU.
Tested on i386 and amd64 (both dom0 and domU) by me.
Xen2 tested (both dom0 and domU) by bouyer.
OK bouyer
 1.7 14-Apr-2008  cegger branches: 1.7.2;
- use POSIX integer types
- ansify functions
 1.6 19-Feb-2008  bouyer branches: 1.6.6;
The event bitmasks provided by the hypervisor are unsigned long (so 64bits
on amd64). Make sure to use the right type to store and manipulate them.
This fixes amd64, where basically any event channel > 31 was not working
(and you get there after starting/stopping a domU a few times). Things
would occasionally unwedge though the spllower() callbacks.
 1.5 19-Feb-2008  bouyer Fix xenevt to not call softint_schedule() above IPL_HIGH:
Register a ipl callback for IPL_HIGH.
if the current ipl level is too high, just record the event in a bitmap,
and record IPL_HIGH as pending. The callback will process the pending events.
 1.4 20-Dec-2007  ad branches: 1.4.2;
- Make __cpu_simple_lock and similar real functions and patch at runtime.
- Remove old x86 atomic ops.
- Drop text alignment back to 16 on i386 (really, this time).
- Minor cleanup.
 1.3 12-Dec-2007  bouyer cleanup the debug event handler to not use the IPL system at all. Fix
debug event storm on XEN2.
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.6; 1.2.8; 1.2.10; 1.2.12;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file hypervisor_machdep.c was initially added on branch bouyer-xenamd64.
 1.1.4.2 27-Dec-2007  mjf Sync with HEAD.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.12.2 02-Jan-2008  bouyer Sync with HEAD
 1.2.12.1 13-Dec-2007  bouyer cleanup the way debug event is handled: make it bypass the IPL system
completely, it's called by shortcuts in the normal path because we want it to
be always called, even if the IPL is high.
Fix debug even recursion on XEN2
 1.2.10.1 13-Dec-2007  yamt sync with head.
 1.2.8.4 27-Feb-2008  yamt sync with head.
 1.2.8.3 21-Jan-2008  yamt sync with head
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file hypervisor_machdep.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:17 +0000
 1.2.6.1 26-Dec-2007  ad Sync with head.
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file hypervisor_machdep.c was added on branch vmlocking on 2007-12-03 19:04:40 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file hypervisor_machdep.c was added on branch jmcneill-pm on 2007-11-27 19:36:19 +0000
 1.4.2.3 23-Mar-2008  matt sync with HEAD
 1.4.2.2 09-Jan-2008  matt sync with HEAD
 1.4.2.1 20-Dec-2007  matt file hypervisor_machdep.c was added on branch matt-armv6 on 2008-01-09 01:50:14 +0000
 1.6.6.4 17-Jan-2009  mjf Sync with HEAD.
 1.6.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.6.6.2 02-Jul-2008  mjf Sync with HEAD.
 1.6.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.7.2.1 18-May-2008  yamt sync with head.
 1.8.6.1 03-Jul-2008  simonb Sync with head.
 1.8.4.2 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.8.4.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.8.2.3 11-Mar-2010  yamt sync with head
 1.8.2.2 19-Aug-2009  yamt sync with head.
 1.8.2.1 04-May-2009  yamt sync with head.
 1.9.2.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.9.2.1 19-Oct-2008  haad Sync with HEAD.
 1.11.8.10 27-Aug-2011  jym Rename the functions for suspend to reflect that Xen does not hijack
the ACPI "sleepstate" sysctl(7) node anymore.

Add a boolean value to mark that the save/suspend operation has been
notified by dom0, so as to avoid possible errors where admin would like
to schedule the domain for sleep without dom0 being prepared for that. Fail
with EAGAIN in this case.

Sprinkle some KNF.
 1.11.8.9 27-Aug-2011  jym Further sync with HEAD.
 1.11.8.8 07-May-2011  jym KNF.
 1.11.8.7 02-May-2011  jym Sync with head.
 1.11.8.6 30-Mar-2011  jym Sync with my commits in HEAD.
 1.11.8.5 29-Mar-2011  jym More sync fixes. And add the mbr_gpt files.
 1.11.8.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.11.8.3 01-Nov-2009  jym Sync with HEAD.
 1.11.8.2 29-May-2009  jym - use uvm_km_alloc() instead of kmem_alloc() to enforce alignement when
allocating p2m_frame pages (xentools can only deal with page-aligned addresses)
- *sigh* do not use paddr_t for p2m_frame_list_list with PAE, xentools expect
32 bits addresses even with 64 bits PTE...
 1.11.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.13.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.13.4.1 21-Apr-2011  rmind sync with head
 1.14.2.6 22-Oct-2011  bouyer Various interrupt fixes, mainly:
keep a per-cpu mask of enabled events, and use it to get pending events.
A cpu-specific event (all of them at this time) should not be ever masked
by another CPU, because it may prevent the target CPU from seeing it
(the clock events all fires at once for example).
 1.14.2.5 18-Sep-2011  cherry Use an IPI to re-route events to the cpu where the handler has been registered
 1.14.2.4 22-Aug-2011  cherry Do not trust the hypervisor to route events to the right cpu. Enforce this in stipending()
 1.14.2.3 17-Aug-2011  cherry Pullup relevant changes from -current
 1.14.2.2 04-Aug-2011  cherry first cut at per-cpu event handling
 1.14.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.16.2.2 16-Jan-2013  yamt sync with (a bit old) head
 1.16.2.1 17-Apr-2012  yamt sync with head
 1.18.2.1 18-Feb-2012  mrg merge to -current.
 1.21.6.3 03-Dec-2017  jdolecek update from HEAD
 1.21.6.2 25-Feb-2013  tls resync with head
 1.21.6.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.27.12.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.28.20.1 10-Jun-2019  christos Sync with HEAD
 1.28.18.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.28.18.1 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.28.12.1 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1864):

sys/arch/xen/x86/hypervisor_machdep.c: revision 1.46 (patch)

xen/x86: Need kpreempt_disable/enable around curcpu() access.

This is called with `hardware' interrupts enabled (between sti and
cli), so presumably preemption is possible here.
 1.36.8.7 25-Apr-2020  bouyer sync with bouyer-xenpvh-base2 (HEAD)
 1.36.8.6 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.36.8.5 19-Apr-2020  bouyer Move xen_ipi.c to XENPV only.
Make sure we don't need to send events to remote CPUs (outside of IPIs)
 1.36.8.4 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.36.8.3 16-Apr-2020  bouyer amd64: Xhypervisor_pvhvm_callback has to be in text.user for SVS.
Thanks to maxv@ for helping me with this.
Enable SVS again.
While there, increase ci_idepth before calling do_hypervisor_callback,
and don't touch ci_idepth while looping over pending events.
 1.36.8.2 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.36.8.1 12-Apr-2020  bouyer Get rid of xen-specific ci_x* interrupt handling:
- use the general SIR mechanism, reserving 3 more slots for IPL_VM, IPL_SCHED
and IPL_HIGH
- remove specific handling from C sources, or change to ipending
- convert IPL number to SIR number in various places
- Remove XUNMASK/XPENDING in assembly or change to IUNMASK/IPENDING
- remove Xen-specific ci_xsources, ci_xmask, ci_xunmask, ci_xpending from
struct cpu_info
- for now remove a KASSERT that there are no pending interrupts in
idle_block(). We can get there with some software interrupts pending
in autoconf XXX needs to be looked at.
 1.36.2.1 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1681):

sys/arch/xen/x86/hypervisor_machdep.c: revision 1.46 (patch)

xen/x86: Need kpreempt_disable/enable around curcpu() access.

This is called with `hardware' interrupts enabled (between sti and
cli), so presumably preemption is possible here.
 1.45.4.1 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #269):

sys/arch/xen/x86/hypervisor_machdep.c: revision 1.46

xen/x86: Need kpreempt_disable/enable around curcpu() access.

This is called with `hardware' interrupts enabled (between sti and
cli), so presumably preemption is possible here.
 1.4 09-May-2008  joerg Make cpu_idle a macro calling a function pointer on x86.
Select the Xen idle routine for Xen, mwait if supported by the CPU and
it is not AMD and halt otherwise. As reported by Christoph Egger,
AMD Barcelona keeps the CPU in C0 state with MWAIT, contrary to HLT,
which uses C1 and therefore much less power.
 1.3 02-Dec-2007  ad branches: 1.3.2; 1.3.6; 1.3.14; 1.3.20; 1.3.22; 1.3.24; 1.3.26;
Don't clear ci_want_resched in MD code; it's done in mi_switch().
 1.2 22-Nov-2007  bouyer branches: 1.2.2;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file idle_machdep.c was initially added on branch bouyer-xenamd64.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.2.3 03-Dec-2007  joerg Sync with HEAD.
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file idle_machdep.c was added on branch jmcneill-pm on 2007-11-27 19:36:20 +0000
 1.3.26.1 23-Jun-2008  wrstuden Remove files removed on branch. Updating using patch has its
drawbacks. :-)
 1.3.24.1 16-May-2008  yamt sync with head.
 1.3.22.1 17-Jun-2008  yamt fix merge botches
 1.3.20.1 02-Jun-2008  mjf Sync with HEAD.
 1.3.14.2 09-Jan-2008  matt sync with HEAD
 1.3.14.1 02-Dec-2007  matt file idle_machdep.c was added on branch matt-armv6 on 2008-01-09 01:50:14 +0000
 1.3.6.2 07-Dec-2007  yamt sync with head
 1.3.6.1 02-Dec-2007  yamt file idle_machdep.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:17 +0000
 1.3.2.2 03-Dec-2007  ad Sync with HEAD.
 1.3.2.1 02-Dec-2007  ad file idle_machdep.c was added on branch vmlocking on 2007-12-03 19:04:40 +0000
 1.34 04-Nov-2017  cherry Retire xen/x86/intr.c and use the new xen specific glue in x86/x86/intr.c

The purpose of this change is to expose the x86/include/intr.h API
to drivers. Specifically the following functions:

void *intr_establish_xname(...);
void *intr_establish(...);
void intr_disestablish(...);

while maintaining the old API from xen/include/evtchn.h, specifically
the following functions:

int event_set_handler(...);
int event_remove_handler(...);

This is so that if things break, we can keep using the old API until
everything stabilises. This is a stepping stone towards getting the
actual XEN event callback path rework code in place - which can be
done opaquely behind the intr.h API - NetBSD/XEN specific drivers that
have been ported to the intr.h API should then work without
significant further modifications.
 1.33 04-Nov-2017  cherry On XEN dom0, the function xen/x86/intr.c:xen_intr_map() is used to map
hardware interrupts to XEN callbacks called 'events'. This function
combines both the allocation and the binding.

This change is the first part of breaking up that combination into
xen_pirq_alloc() and the binding will happen as part of the
pic_addroute() callback of a new pseudo PIC_XEN

This code will be added later on.
 1.32 16-Jul-2017  cherry branches: 1.32.2;
Remove the xen specific interrupt type for the x86 intr_handle_t
For this to work, we use the evtchn.c:get_pirq_to_evtchn() glue
function to make things easier.
 1.31 23-May-2017  nonaka x86: Add preliminary x2APIC support.

x2APIC is used only when x2APIC is enabled in BIOS/UEFI.
LAPIC ID is not supported above 256.
 1.30 17-Oct-2016  jdolecek provide stub intr xname establish for xen
 1.29 13-Aug-2011  cherry branches: 1.29.12; 1.29.30; 1.29.34;
Remove spurious header.
Thanks rmind@
 1.28 11-Aug-2011  cherry Make event/interrupt handling MP aware
 1.27 19-Mar-2010  dyoung branches: 1.27.6;
Cosmetic: abbreviate: use `pc' instead of `pci_chipset_tag'.
 1.26 18-Aug-2009  jmcneill branches: 1.26.2; 1.26.4;
Switch to ACPICA 20090730, and update for API changes.
 1.25 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.24 03-Jun-2009  cegger Interrupt handling in Xen 3.5 changed. There's no longer
a hardcoded upper limit. So *our* upper limit of 200 may be different from machine to machine now.
So just retry if the hypercall failed.
 1.23 22-Apr-2009  ad Make xen kernels build again.
 1.22 10-Mar-2009  bouyer When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.21 05-Sep-2008  tron branches: 1.21.2; 1.21.4; 1.21.8; 1.21.12;
Compile NetBSD/amd64 kernels with "-Wextra". Patches contributed by
Juan RP in PR port-amd64/39266.
 1.20 03-Jul-2008  drochner branches: 1.20.2;
Remove "struct device" from "struct pic", where it was only real
for ioapics and faked up for others. Add it to "struct ioapic_softc"
for now, until device/softc get split.
This required all typecasts between "struct pic" and "struct ioapic_softc"
to be replaced, I hope I got them all.
functionally tested on i386, compile-tested on xen, untested on amd64
 1.19 30-May-2008  ad branches: 1.19.2;
Add a 'known_mpsafe' argument to intr_establish().
 1.18 11-May-2008  ad Stop using APIC IDs to identify CPUs for software purposes. Allows for
APIC IDs beyond 31, which has been possible for some time now.
 1.17 11-Jan-2008  bouyer branches: 1.17.6; 1.17.8; 1.17.10; 1.17.12;
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.16 20-Dec-2007  ad - Make __cpu_simple_lock and similar real functions and patch at runtime.
- Remove old x86 atomic ops.
- Drop text alignment back to 16 on i386 (really, this time).
- Minor cleanup.
 1.15 03-Dec-2007  ad branches: 1.15.2; 1.15.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.14 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.13 29-Jan-2007  hubertf branches: 1.13.6; 1.13.12; 1.13.22; 1.13.24; 1.13.28; 1.13.30;
Remove more duplicate headers.
Patch by Slava Semushin <slava.semushin@gmail.com>

Again, this was tested by comparing obj files from a pristine and a patched
source tree against an i386/ALL kernel, and also for src/sbin/fsck_ffs,
src/sbin/fsdb and src/usr.sbin/makefs. Only changes in assert() line numbers
were detected in 'objdump -d' output.
 1.12 08-Dec-2006  yamt - pass intrframe by-pointer, not by-value.
- make i386 and xen use per-cpu interrupt stack.

xen part is reviewed by Manuel Bouyer.
 1.11 15-Oct-2006  yamt include machine/mpconfig.h so that these files can be compiled
with ACPI but without MPBIOS.
 1.10 12-Oct-2006  yamt intr_establish: add a missing ";" in the case of NIOAPIC==0.
 1.9 28-Sep-2006  bouyer Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.8 09-Apr-2006  bouyer branches: 1.8.8; 1.8.10;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.7 11-Dec-2005  christos branches: 1.7.4; 1.7.6; 1.7.8; 1.7.10; 1.7.12;
merge ktrace-lwp.
 1.6 16-Apr-2005  bouyer branches: 1.6.2;
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.5 11-Apr-2005  yamt fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.4 09-Mar-2005  bouyer branches: 1.4.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.3 23-Oct-2004  yamt branches: 1.3.4; 1.3.6; 1.3.8;
don't reference kernel_lock directly.
 1.2 11-Apr-2004  cl branches: 1.2.2;
catch up with arch/x86/x86/intr.c
1.15/kochi
use designated initializer for struct pic initializers.
just for readability.

update the xenev_pic initializer as well
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revision 1.2 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.2.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.2.2.5 02-Nov-2004  skrll Sync with HEAD.
 1.2.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.2.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.2.2.2 03-Aug-2004  skrll Sync with HEAD
 1.2.2.1 11-Apr-2004  skrll file intr.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.3.8.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.3.6.1 29-Apr-2005  kent sync with -current
 1.3.4.2 18-Jan-2005  bouyer Remove unused code.
 1.3.4.1 17-Dec-2004  bouyer Sync with arch/x86/x86/intr.c 1.20.
 1.4.2.2 28-Apr-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4.2.1 13-Apr-2005  tron Pull up revision 1.5 (requested by yamt in ticket #146):
fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.6.2.5 21-Jan-2008  yamt sync with head
 1.6.2.4 07-Dec-2007  yamt sync with head
 1.6.2.3 26-Feb-2007  yamt sync with head.
 1.6.2.2 30-Dec-2006  yamt sync with head.
 1.6.2.1 21-Jun-2006  yamt sync with head.
 1.7.12.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.7.10.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.7.8.1 11-Apr-2006  yamt sync with head
 1.7.6.1 22-Apr-2006  simonb Sync with head.
 1.7.4.1 09-Sep-2006  rpaulo sync with head
 1.8.10.2 10-Dec-2006  yamt sync with head.
 1.8.10.1 22-Oct-2006  yamt sync with head
 1.8.8.3 01-Feb-2007  ad Sync with head.
 1.8.8.2 12-Jan-2007  ad Sync with head.
 1.8.8.1 18-Nov-2006  ad Sync with head.
 1.13.30.3 18-Feb-2008  mjf Sync with HEAD.
 1.13.30.2 27-Dec-2007  mjf Sync with HEAD.
 1.13.30.1 08-Dec-2007  mjf Sync with HEAD.
 1.13.28.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.13.24.2 23-Mar-2008  matt sync with HEAD
 1.13.24.1 09-Jan-2008  matt sync with HEAD
 1.13.22.2 09-Dec-2007  jmcneill Sync with HEAD.
 1.13.22.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.13.12.1 18-Apr-2007  thorpej Convert i386 and amd64 to the new atomic ops API.
 1.13.6.1 03-Dec-2007  ad Sync with HEAD.
 1.15.6.2 06-Jan-2008  bouyer Merge needed changes to genassym.cf and locore.S for xeni386 back to
arch/i386. Switch xeni386 to use the arch/i386 cpu.h.
 1.15.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.15.2.1 26-Dec-2007  ad Sync with head.
 1.17.12.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.17.12.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.17.10.5 11-Aug-2010  yamt sync with head.
 1.17.10.4 19-Aug-2009  yamt sync with head.
 1.17.10.3 20-Jun-2009  yamt sync with head
 1.17.10.2 04-May-2009  yamt sync with head.
 1.17.10.1 16-May-2008  yamt sync with head.
 1.17.8.2 04-Jun-2008  yamt sync with head
 1.17.8.1 18-May-2008  yamt sync with head.
 1.17.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.17.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.19.2.1 03-Jul-2008  simonb Sync with head.
 1.20.2.1 19-Oct-2008  haad Sync with HEAD.
 1.21.12.1 21-Apr-2010  matt sync to netbsd-5
 1.21.8.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.21.8.4 24-Oct-2010  jym Sync with HEAD
 1.21.8.3 01-Nov-2009  jym Sync with HEAD.
 1.21.8.2 06-Jun-2009  jym As requested by cegger@, apply the following patch to jym-xensuspend branch:

Interrupt handling in Xen 3.5 changed. There's no longer
a hardcoded upper limit. So *our* upper limit of 200 may be different from machine to machine now.
So just retry if the hypercall failed.
 1.21.8.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.21.4.1 04-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/x86/intr.c: revision 1.22
sys/arch/xen/xen/isa_machdep.c: revision 1.14
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.9
sys/arch/xen/xen/pciide_machdep.c: revision 1.12
When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.21.2.1 28-Apr-2009  skrll Sync with HEAD.
 1.26.4.1 30-May-2010  rmind sync with head
 1.26.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.27.6.2 17-Aug-2011  cherry Pullup relevant changes from -current
 1.27.6.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.29.34.1 04-Nov-2016  pgoyette Sync with HEAD
 1.29.30.2 28-Aug-2017  skrll Sync with HEAD
 1.29.30.1 05-Dec-2016  skrll Sync with HEAD
 1.29.12.1 03-Dec-2017  jdolecek update from HEAD
 1.32.2.2 16-Jul-2017  cherry 2739767
 1.32.2.1 16-Jul-2017  cherry file intr.c was added on branch perseant-stdc-iso10646 on 2017-07-16 06:14:25 +0000
 1.20 22-Dec-2018  cherry Move mainbus(4) driver files in various x86 sub-archs to name prefixed
versions. This allows us to further modularise them by unifying common
bus probe code in x86/x86/mainbus.c to be introduced next.

This commit has no functional changes. It is done for ease of
visibility of newer diffs in the queue.
 1.19 23-May-2017  nonaka branches: 1.19.8; 1.19.10;
x86: Add preliminary x2APIC support.

x2APIC is used only when x2APIC is enabled in BIOS/UEFI.
LAPIC ID is not supported above 256.
 1.18 03-Mar-2014  dsl branches: 1.18.6;
Use the global pci_mode to avoid 'set but not used' warnings from gcc 4.8.3.
 1.17 31-Jan-2014  bouyer Move back call to pci_mode_detect() outside of #ifdef PCI_BUS_FIXUP.
Even if mode is not used, the call to pci_mode_detect() is mandatory to
initialize the PCI subsystem.
Fix "panic booting -current DOM0" reported by Patrick Welche on port-xen.
 1.16 06-Nov-2013  mrg - move variables inside their #ifdef use
- remove unused and set-but-unused variables
- use __USE() in a particularly ugly case

with these, and a couple of other changes, amd64 gcc 4.8.1 world
is able to complete build.sh release.
 1.15 20-Sep-2011  jym branches: 1.15.2; 1.15.12; 1.15.16;
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.14 01-Jul-2011  dyoung #include <sys/bus.h> instead of <machine/bus.h>.
 1.13 12-Nov-2010  dholland Build fix for xen domu + PCI, from Juho Salminen in PR 44083.
 1.12 07-Aug-2010  cegger acpi_madt.h is gone
 1.11 28-Apr-2010  dyoung On x86, change the bus_space_tag_t to a pointer to a struct
bus_space_tag. For now, bus_space_tag's only member is
bst_type, the type of space, which is either X86_BUS_SPACE_IO
or X86_BUS_SPACE_MEM. In the future, new bus_space_tag members
will refer to override-functions installed by a new function,
bus_space_tag_create(9).

Add pointers to constant struct bus_space_tag, x86_bus_space_io and
x86_bus_space_mem. Use them to replace most uses of X86_BUS_SPACE_IO
and X86_BUS_SPACE_MEM.

Add an x86-specific bus_space_is_equal(9) implementation that compares
the two tags' bst_type.
 1.10 15-Feb-2010  dyoung branches: 1.10.2;
Don't use the global variable pci_mode, but use a local copy of
the return value of pci_mode_detect(), instead.
 1.9 18-Aug-2009  jmcneill branches: 1.9.2;
Switch to ACPICA 20090730, and update for API changes.
 1.8 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.7 18-Jan-2009  bouyer branches: 1.7.2;
The Xen PCI_BUS_FIXUP/PCI_ADDR_FIXUP has rotted, catch up with x86 changes
in this area. Patch provided by FUKAUMI Naoki in PR#40356.
 1.6 09-Nov-2008  cegger Nuke last parameter from mpaci_scan_apics() and mpbios_scan().
It is unused.
 1.5 21-Oct-2008  cegger branches: 1.5.2; 1.5.4;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 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 11-Jan-2008  bouyer branches: 1.3.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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.8; 1.2.12; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file mainbus.c was initially added on branch bouyer-xenamd64.
 1.1.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file mainbus.c was added on branch matt-armv6 on 2008-01-09 01:50:15 +0000
 1.2.12.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.2.8.3 21-Jan-2008  yamt sync with head
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file mainbus.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:18 +0000
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file mainbus.c was added on branch vmlocking on 2007-12-03 19:04:41 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file mainbus.c was added on branch jmcneill-pm on 2007-11-27 19:36:21 +0000
 1.3.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.3.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.4.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.4.4.4 11-Aug-2010  yamt sync with head.
 1.4.4.3 11-Mar-2010  yamt sync with head
 1.4.4.2 19-Aug-2009  yamt sync with head.
 1.4.4.1 04-May-2009  yamt sync with head.
 1.5.4.1 22-Jan-2009  snj Pull up following revision(s) (requested by bouyer in ticket #286):
sys/arch/xen/conf/files.xen: revision 1.92
sys/arch/xen/x86/mainbus.c: revision 1.7 via patch
sys/arch/xen/xen/hypervisor.c: revision 1.43
The Xen PCI_BUS_FIXUP/PCI_ADDR_FIXUP has rotted, catch up with x86 changes
in this area. Patch provided by FUKAUMI Naoki in PR#40356.
 1.5.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.7.2.6 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.7.2.5 10-Jan-2011  jym Sync with HEAD
 1.7.2.4 24-Oct-2010  jym Sync with HEAD
 1.7.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.7.2.2 01-Nov-2009  jym Sync with HEAD.
 1.7.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.9.2.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.9.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.10.2.2 05-Mar-2011  rmind sync with head
 1.10.2.1 30-May-2010  rmind sync with head
 1.15.16.1 18-May-2014  rmind sync with head
 1.15.12.2 03-Dec-2017  jdolecek update from HEAD
 1.15.12.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.15.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.18.6.1 28-Aug-2017  skrll Sync with HEAD
 1.19.10.1 10-Jun-2019  christos Sync with HEAD
 1.19.8.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.25 18-Aug-2025  andvar Fix various typos, mainly in comments:
s/invaid/invalid/
s/instad/instead/
s/wich/with/
s/tranform/transform/
s/tranmist/transmit/
s/tranceiver/transceiver/
s/Tranparent/Transparent/
s/tranlated/translated/
s/tranfer/transfer/
s/tranmissions/transmissions/
s/condtions/conditions/
s/Recient/Recent/
 1.24 26-May-2022  bouyer aprint_debug(): if a hypercall fail, print the return code.
 1.23 24-May-2022  bouyer Remove useless info from debug printf, fix format warning on i386
 1.22 24-May-2022  bouyer - msipic_construct_msix_pic(): set mp_table_base to memaddr (without
table_offset), this is what Xen wants
while there use pci_conf_write16() in msi_set_msictl_enablebit() too,
for consistency (it seems that Xen accepts the 32bit write at this point,
but this may change).

- xen_map_msix_pirq(): don't forget to set map_irq.table_base in the
MSI-X case, otherwise Xen maps it as MSI
- call pic_hwunmask() after pirq_establish() in msi/msix case, to make sure
the msi-x vector is unmasked.

Now MSI-X works with Xen so stop disabling it in pci_attach_hook().
 1.21 23-May-2022  bouyer Work in progress on MSI/MSI-X on Xen (MSI works on my hardware, more work
needed for MSI-X):
- Xen silently rejects 32 bits writes to MSI configuration registers
(especially when setting PCI_MSI_CTL_MSI_ENABLE/PCI_MSIX_CTL_ENABLE),
it expects 16 bits writes. So introduce a pci_conf_write16(),
only available on XENPV (and working only for mode 1 without
PCI_OVERRIDE_CONF_WRITE) and use it to enable MSI or MSI-X on XENPV.
- for multi-MSI vectors, Xen allocates all of them in a single hypercall,
so it's not convenient to do it at intr_establish() time.
So do it at alloc() time and register the pirqs in the msipic structure.
xen_pic_to_gsi() now just returns the values cached in the msipic.
As a bonus, if the PHYSDEVOP_map_pirq hypercall fails we can fail
the alloc() and we don't need the xen_pci_msi*_probe() hacks.

options NO_PCI_MSI_MSIX still on by default for XEN3_DOM0.
 1.20 01-Aug-2020  jdolecek adjust includes to pull __HAVE_PCI_MSI_MSIX properly
 1.19 19-Jul-2020  jdolecek add #ifdef __HAVE_PCI_MSI_MSIX so this still compiles with NO_PCI_MSI_MSIX
 1.18 19-Jul-2020  jdolecek for Xen MSI, fallback to INTx when PHYSDEVOP_map_pirq fails for the device

apparently Xen requires VT-d to be enabled in BIOS for PHYSDEVOP_map_pirq
to work, this change makes it work on systems with VT-d disabled or missing

adresses the panic part of PR port-xen/55285 by Patrick Welche
 1.17 23-May-2020  jdolecek switch back to PHYSDEVOP_alloc_irq_vector for non-MSI interrupts - on my
computer it works the same as PHYSDEVOP_map_pirq, but seems it doesn't
on other systems

fixes PR port-xen/55285 for Patrick Welche, but not yet for another system
by Frank Kardel
 1.16 15-May-2020  jdolecek use short for irq2port[] to save memory (4KB), it only needs to store
numbers <= NR_EVENT_CHANNELS (2048)
 1.15 15-May-2020  jdolecek only call PHYSDEVOP_map_pirq for a shared interrupt once, same as previous code

fixes boot problem reported privately by Frank Kardel and Patrick Welche
 1.14 04-May-2020  jdolecek add support for using MSI for XenPV Dom0

use PHYSDEVOP_map_pirq to get the pirq/gsi for MSI/MSI-X, switch also INTx
to use it instead of PHYSDEVOP_alloc_irq_vector

MSI confirmed working with single-vector MSI for wm(4), ahcisata(4), bge(4)

XXX added some provision for MSI-X, but it doesn't actually work (no interrupts
delivered), needs some further investigation; disable MSI-X for XENPV
via flag in x86/pci/pci_machdep.c
 1.13 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.12 21-Apr-2020  jdolecek convert to newer HYPERVISOR_physdev_op() interface, now command and the
arg are separate arguments - this is needed for newer physdev_op commands

remove code for PHYSDEVOP_IRQ_UNMASK_NOTIFY, it is obsolete since
interface version 0x00030202 and is unsupported by newer versions of Xen

confirmed working on amd64 Dom0, i386 compile-tested only
 1.11 07-Apr-2020  jdolecek branches: 1.11.2;
remove <sys/malloc.h> include, not used here
 1.10 13-Feb-2019  cherry Catchup with struct intrstub; unification.

This should fix dom0 build breakage.
 1.9 10-Oct-2018  cherry Do not export the 'irq<->vector' abstraction outside of pintr.c
anymore. We now think of them as a unified thing called 'gsi',
which is generated by mpacpi/mpbios
 1.8 10-Oct-2018  cherry Since GSIs are invented by the mpbios/mpacpi interrupt routing probe code,
it's possible for shared GSIs to popup even outside the original
legacy_irq range.

Relax this latter, older assumption.

Thanks to Brad Spencer for extensive trialing on interesting hardware.
 1.7 07-Oct-2018  cherry Switch over to a "GSI" concept for guest irqs.

On XEN there is a namespace called GSI which includes:

i) legacy_irq (0 - 16)
ii) "gsi" (16-nr_irqs_gsi)
iii) msi

We try to mirror this in guest space, but are mindful that legacy_irq
is 1:1 bound to actual hardware legacy_irq. Apart from this, XEN doesn't
really care what number scheme we use, as long as it doesn't encroach
on the MSI space, which is TBD for us.

Thus we trust the mpbios.c/mpacpi.c code to correctly map the pic,pin
tuples into the correct global gsi space, which we then register with
xen. As we now do, we allow for duplicate gsi registrations, in case
any hardware shares the same (pic,pin);

This enables us to now use the (pic,pin) tuple as the canonical reference
for device interrupt addresses, and leave any global mappings to specific
code. Thus xen_pic_to_gsi().

Note that this requires separate support for MSI, which I will get around to
once things stabilise - however the API change facilitates this nicely.

I note that the msi addroute() function does not use the "pin" parameter.
This can be made use of, to encode the gsi number, for XEN. This is however
TBD.

We further tweak the xen_vec_alloc() code to be uniform for the NIOAPICS
and other cases, and ensure that i8259.c DTRT wrt to route().

This will allow us to use pic->pic_addroute() without needing to worry about
pic specific issues.

The next step is to consolidate the pic_addroute() XEN related #ifdefs into
a -DXEN specific file, so that we don't clutter x86/ code with #ifdef XENs.

This change has functional implications, and there is likely breakage coming
especially on bespoke platforms that I haven't been able to test yet.

I am especially interested in bug reports from platforms with legacy (esp. i386)
and with multiple ioapics.
 1.6 06-Oct-2018  cherry Change the name of xen_pirq_alloc() to xen_vec_alloc() to reflect
its actual job.

The idea is that we will strip this down until it is as close to
idt_vec_alloc() as possible.
 1.5 06-Oct-2018  cherry Move the pic->pic_addroute() call from within pintr.c:xen_pirq_alloc() to
intr.c:intr_establish_xname()

xen_pirq_alloc() now returns a vector value, as is intended by
the semantics of the call to the hypervisor, PHYSDEVOP_ASSIGN_VECTOR.

This also brings our usage closer to native.
 1.4 06-Oct-2018  cherry Teach intr_establish_xname() for XEN to tolerate shared legacy_irq
registrations.

The current XEN code has not been able to tolerate shared legacy_irq
requests in xen_pirq_alloc(). This was never a problem because:

i) The only potential callpath with shared legacy_irq was
isa_intr_establish_xname().
ii) The other callpath, namely pci_intr_establish_xname() passed
legacy_irq to intr_establish_xname(). However, this was ignored,
and a value of zero was passed to xen_pirq_alloc() which in
turn, allocated a new irq value, thus effectively demultiplexing
any shared legacy_irq value intended across randomly allocated
new irq values.
iii) Presumably on all platforms that XEN runs on, the isa callpath
mentioned in i) never had shared irqs, and thus this was never
a problem.

Except:
We now use a unified path for both isa_intr_establish_xname() and
pci_intr_establish_xname(). This means that now, intr_establish_xname()
which is a callee of both, needs to have a way to discern who the caller
was, and decide to pass on or discard the legacy_irq value, to preserve
the old semantics. However, this is impossible to do so, because the
callpath doesn't explicitly provide a mechanism for this discernment.

This is however not a problem, because from XEN's point of view, a
repeat registration of an irq is only a warning. legacy_irq is the only
case in which this repeat should occur, per the current implementation of
xen_pirq_alloc(). We thus tweak the KASSERT()s to tolerate a repeat value
in the legacy_irq, while maintaining the original intent of the KASSERT()
which was to ensure that existing unrelated irq registrations should never
be overwritten.

Once we re-organise XEN specific code and unify with the native
intr_establish_xname() path, we will not run into this problem, because
legacy_irq will be treated as the pin number of the i8259 pic
exactly as it is now treated in native.

In short, this commit should fix some of the panics being seen on
-current for certain configurations of hardware on which dom0 runs.
 1.3 17-Feb-2018  maxv branches: 1.3.2; 1.3.4;
Rename i8259_stubs -> legacy_stubs. We will want the entries to have the
same name, eg:

legacy_stubs
-> Xintr_legacy0, Xrecurse_legacy0, Xresume_legacy0
-> Xintr_legacy1, Xrecurse_legacy1, Xresume_legacy1
...
 1.2 13-Dec-2017  bouyer Fixes for physical interrupts on Xen:
- do not cast int * to intr_handle_t *, they're not the same size
- legacy_irq is not always -1 for ioapic interrupts, test pic_type instead
- change irq2port[] to hold (port + 1) so that 0 is an invalid value
- add KASSERTs to make sure vect, port or irq values extracted from arrays are
valid (or that they are invalid before write)
- for the !ioapic case, we still need to do PHYSDEVOP_ASSIGN_VECTOR and
bind_pirq_to_evtch().

now XEN3_DOM0 boots again
 1.1 04-Nov-2017  cherry branches: 1.1.2;
On XEN dom0, the function xen/x86/intr.c:xen_intr_map() is used to map
hardware interrupts to XEN callbacks called 'events'. This function
combines both the allocation and the binding.

This change is the first part of breaking up that combination into
xen_pirq_alloc() and the binding will happen as part of the
pic_addroute() callback of a new pseudo PIC_XEN

This code will be added later on.
 1.1.2.2 03-Dec-2017  jdolecek update from HEAD
 1.1.2.1 04-Nov-2017  jdolecek file pintr.c was added on branch tls-maxphys on 2017-12-03 11:36:51 +0000
 1.3.4.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.3.4.1 10-Jun-2019  christos Sync with HEAD
 1.3.2.1 20-Oct-2018  pgoyette Sync with head
 1.11.2.1 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.6 17-Oct-2023  bouyer Make sure to always fall back to xen_early_console, even for dom0
 1.5 16-Oct-2023  bouyer Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.
 1.4 22-Jul-2023  mrg xen: declare 'default_consinfo' as extern in a header

this makes pvh_consinit.c actually compile with CONS_OVERRIDE set.
i didn't see any good header to add to (bootinfo.h and cpu.h both
seem to be poor choices but were considered), hence the new one
with just this definition.
 1.3 24-Mar-2023  bouyer Allow a PVH dom0 to use VGA as console: make xen_pvh_consinit() return 1 if
it handles the console and 0 otherwise (especially when console=tty0 or
console=pc is present on the command line).
In consinit() fallback to native console selection if xen_pvh_consinit()
returns 0.
 1.2 03-May-2020  bouyer branches: 1.2.20;
Hanble dom0 console. This one doesn't need a ring to be mapped, and
can be used earlier.
 1.1 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.2.20.2 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #428):

sys/arch/xen/xen/xen_machdep.c: revision 1.28
sys/arch/x86/pci/pci_machdep.c: revision 1.97
sys/arch/xen/xen/genfb_xen.c: revision 1.1
sys/arch/xen/xen/genfb_xen.c: revision 1.2
sys/arch/xen/include/hypervisor.h: revision 1.59
sys/arch/i386/conf/XEN3PAE_DOM0: revision 1.41 (patch)
sys/arch/x86/x86/genfb_machdep.c: revision 1.22
sys/arch/xen/x86/consinit.c: revision 1.18
sys/arch/xen/x86/autoconf.c: revision 1.26
sys/external/mit/xen-include-public/dist/xen/include/public/platform.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.188
sys/arch/x86/x86/consinit.c: revision 1.37
sys/arch/xen/conf/files.xen: revision 1.189
sys/arch/x86/x86/consinit.c: revision 1.38
sys/external/mit/xen-include-public/dist/xen/include/public/xen.h: revision 1.2
sys/arch/x86/include/genfb_machdep.h: revision 1.7
sys/arch/xen/x86/pvh_consinit.c: revision 1.5
sys/arch/xen/x86/pvh_consinit.c: revision 1.6
sys/arch/amd64/conf/XEN3_DOM0: revision 1.201

Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.

Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.

Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen

when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.

x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console

xen/x86/consinit.c: support genfb as possible console

xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.

xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.

Make sure to always fall back to xen_early_console, even for dom0

Enable genfb in DOM0 kernels

Add ext_lfb_base to dom0_vga_console_info, from recent Xen. We know if it's
present or not by checking dom0.info_size

Add XENPF_get_dom0_console, which gets a dom0_vga_console_info stucture
from the hypervisor. To be used by PVH dom0 kernels.

XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.

Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.2.20.1 30-Mar-2023  martin Pull up following revision(s) (requested by bouyer in ticket #131):

sys/arch/x86/x86/consinit.c: revision 1.36
sys/arch/xen/x86/pvh_consinit.c: revision 1.3
sys/arch/xen/include/xen.h: revision 1.48

Allow a PVH dom0 to use VGA as console: make xen_pvh_consinit() return 1 if
it handles the console and 0 otherwise (especially when console=tty0 or
console=pc is present on the command line).

In consinit() fallback to native console selection if xen_pvh_consinit()
returns 0.
 1.93 11-May-2024  andvar s/boostrap/bootstrap/ in comment, warning message and documentation.
 1.92 20-Aug-2022  riastradh x86: Split most of pmap.h into pmap_private.h or vmparam.h.

This way pmap.h only contains the MD definition of the MI pmap(9)
API, which loads of things in the kernel rely on, so changing x86
pmap internals no longer requires recompiling the entire kernel every
time.

Callers needing these internals must now use machine/pmap_private.h.
Note: This is not x86/pmap_private.h because it contains three parts:

1. CPU-specific (different for i386/amd64) definitions used by...

2. common definitions, including Xenisms like xpmap_ptetomach,
further used by...

3. more CPU-specific inlines for pmap_pte_* operations

So {amd64,i386}/pmap_private.h defines 1, includes x86/pmap_private.h
for 2, and then defines 3. Maybe we should split that out into a new
pmap_pte.h to reduce this trouble.

No functional change intended, other than that some .c files must
include machine/pmap_private.h when previously uvm/uvm_pmap.h
polluted the namespace with pmap internals.

Note: This migrates part of i386/pmap.h into i386/vmparam.h --
specifically the parts that are needed for several constants defined
in vmparam.h:

VM_MAXUSER_ADDRESS
VM_MAX_ADDRESS
VM_MAX_KERNEL_ADDRESS
VM_MIN_KERNEL_ADDRESS

Since i386 needs PDP_SIZE in vmparam.h, I added it there on amd64
too, just to keep things parallel.
 1.91 11-May-2022  bouyer In bootstrap, after switching to a new page table make sure that
now-unused memory is unmapped.
 1.90 06-Sep-2020  riastradh Fix fallout from previous uvm.h cleanup.

- pmap(9) needs uvm/uvm_extern.h.

- x86/pmap.h is not usable on its own; it is only usable if included
via uvm/uvm_extern.h (-> uvm/uvm_pmap.h -> machine/pmap.h).

- Make nvmm.h and nvmm_internal.h standalone.
 1.89 26-May-2020  bouyer Ajust pmap_enter_ma() for upcoming new Xen privcmd ioctl:
pass flags to xpq_update_foreign()
Introduce a pmap MD flag: PMAP_MD_XEN_NOTR, which cause xpq_update_foreign()
to use the MMU_PT_UPDATE_NO_TRANSLATE flag.
make xpq_update_foreign() return the raw Xen error. This will cause
pmap_enter_ma() to return a negative error number in this case, but the
only user of this code path is privcmd.c and it can deal with it.

Add pmap_enter_gnt()m which maps a set of Xen grant entries at the
specified va in the specified pmap. Use the hooks implemented for EPT to
keep track of mapped grand entries in the pmap, and unmap them
when pmap_remove() is called. This requires pmap_remove() to be split
into a pmap_remove_locked(), to be called from pmap_remove_gnt().
 1.88 06-May-2020  bouyer xpq_queue_* use per-cpu queue; splvm() is enough to protect them.
remove the XXX SMP comments.
 1.87 06-May-2020  bouyer KASSERT() that the per-cpu queues are run at IPL_VM after boot.
 1.86 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.85 30-Oct-2019  maxv Switch to new PTE bits.
 1.84 09-Mar-2019  maxv branches: 1.84.4;
Start replacing the x86 PTE bits.
 1.83 07-Mar-2019  maxv Drop PG_RO, PG_KR and PG_PROT, they are useless and create confusion.
 1.82 04-Feb-2019  cherry Bump up XEN source API compatibility to 0x00030208 from 0x00030201,

but maintain backwards source API compilation compatibility.

ie; sources with config(5)
options __XEN_INTERFACE_VERSION__=0x00030201 # Xen 3.1 interface

should compile and run without problems.

Not that API version 0x00030201 is the lowest version we support now.
 1.81 29-Jul-2018  maxv Reduce the confusion, rename a bunch of variables and reorg a little.
Tested on i386PAE-domU and amd64-dom0.
 1.80 27-Jul-2018  maxv Try to reduce the confusion, rename:

l2_4_count -> PDIRSZ
count -> nL2
bootstrap_tables -> our_tables
init_tables -> xen_tables

No functional change.
 1.79 26-Jul-2018  maxv Remove the non-PAE-i386 code of Xen. The branches are reordered so that
__x86_64__ comes first, eg:

#if defined(PAE)
/* i386+PAE */
#elif defined(__x86_64__)
/* amd64 */
#else
/* i386 */
#endif

becomes

#ifdef __x86_64__
/* amd64 */
#else
/* i386+PAE */
#endif

Tested on i386pae-domU and amd64-dom0.
 1.78 26-Jul-2018  maxv Retire XENDEBUG_LOW, and switch its only user to XENDEBUG.
 1.77 26-Jul-2018  maxv Merge the blocks. No functional change.
 1.76 26-Jul-2018  maxv Simplify the conditions; (PTP_LEVELS > 3) and (PTP_LEVELS > 2) are for
amd64, so use ifdef __x86_64__. No functional change.
 1.75 24-Jun-2018  jdolecek branches: 1.75.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.74 16-Sep-2017  maxv branches: 1.74.2;
Move xpq_idx into cpu_info, to prevent false sharing between CPUs. Saves
10s when doing a './build.sh -j 3 kernel=GENERIC' on xen-amd64-domU.
 1.73 18-Mar-2017  maxv Style, and remove debug code that does not work anyway.
 1.72 08-Mar-2017  maxv A few changes:
* Use markers to reduce false sharing.
* Remove XENDEBUG_SYNC and several debug messages, they are just useless.
* Remove xen_vcpu_*. They are unused and not optimized: if we really
wanted to flush ranges we should pack the VAs in a mmuext_op array
instead of performing several hypercalls in a loop.
* Start removing PG_k.
* KNF, reorder, simplify and remove stupid comments.
 1.71 02-Feb-2017  maxv Use __read_mostly on these variables, to reduce the probability of false
sharing.
 1.70 22-Jan-2017  maxv Export xpmap_pg_nx, and put it in the page table pages. It does not change
anything, since Xen removes the X bit on these; but it is better for
consistency.
 1.69 06-Jan-2017  maxv branches: 1.69.2;
Remove a few #if 0s, and explain what we are doing on PAE: the last two PAs
are entered in reversed order.
 1.68 16-Dec-2016  maxv The way the xen dummy page is taken care of makes absolutely no sense at
all, with magic offsets here and there in different layers of the system.
It is just blind luck that everything has always worked as expected so
far.

Due to this wrong design we have a problem now: we allocate one physical
page for lapic, and it happens to overlap with the dummy page, which
causes the system to crash.

Fix this by keeping the dummy va directly in a variable instead of magic
offsets. The asm locore now increments the first pa to hide the dummy page
to machdep and pmap.
 1.67 15-Nov-2016  maxv Mmh, apparently I didn't properly test my previous change since it does not
compile anymore
 1.66 15-Nov-2016  maxv Keep simplifying that stuff. Also, replace plX_pi(KERNTEXTOFF) by
LX_SLOT_KERNBASE: the base address is KERNBASE, and we just start mapping
from KERNTEXTOFF. For symmetry with the normal amd64, does not change
anything.
 1.65 11-Nov-2016  maxv Rename xen_pmap_bootstrap to xen_locore, it really has nothing to do with
pmap and is just a C version of what amd64 and i386 do in asm.
 1.64 11-Nov-2016  maxv Start simplifying the Xen locore: rename and reorder several things, remove
awful debug messages, use unsigned counters, fix typos and KNF.
 1.63 01-Nov-2016  maxv Map the PTE space as non-executable on PAE. The same is already done on
amd64.
 1.62 01-Nov-2016  maxv Map the remaining pages as non-executable. Only text should have X.
 1.61 25-Aug-2016  bouyer Revert to 1.59 (adding back the W^X kernel mapings), and move the data+bss
mapping late so that mappings that should be RO (such as page tables) won't
be made RW by accident.
 1.60 23-Aug-2016  bouyer Stopgap measure: revert to rev 1.56. starting with 1.57 an i386PAE Xen
kernel doesn't boot:
(XEN) mm.c:2394:d139v0 Bad type (saw 5400000000000001 != exp 7000000000000000) for mfn 1136f5 (pfn 621)
(XEN) mm.c:887:d139v0 Could not get page type PGT_writable_page
(XEN) mm.c:939:d139v0 Error getting mfn 1136f5 (pfn 621) from L1 entry 00000001136f5003 for l1e_owner=139, pg_owner=139
(XEN) mm.c:1254:d139v0 Failure in alloc_l1_table: entry 33
(XEN) mm.c:2141:d139v0 Error while validating mfn 112f57 (pfn dbf) for type 1000000000000000: caf=8000000000000003 taf=1000000000000001
(XEN) mm.c:947:d139v0 Attempt to create linear p.t. with write perms
(XEN) mm.c:1330:d139v0 Failure in alloc_l2_table: entry 3
(XEN) mm.c:2141:d139v0 Error while validating mfn 112f5b (pfn dbb) for type 2200000000000000: caf=8000000000000003 taf=2200000000000001
(XEN) mm.c:1412:d139v0 Failure in alloc_l3_table: entry 3
(XEN) mm.c:2141:d139v0 Error while validating mfn 112f60 (pfn db6) for type 3000000000000000: caf=8000000000000003 taf=3000000000000001
(XEN) mm.c:3044:d139v0 Error while pinning mfn 112f60
(XEN) traps.c:459:d139v0 Unhandled bkpt fault/trap [#3] on VCPU 0 [ec=0000]
(XEN) domain_crash_sync called from entry.S: fault at ffff82d080231894 compat_create_bounce_frame+0xda/0xf2
 1.59 11-Aug-2016  maxv Make the I/O area non-executable on Xen.
 1.58 03-Aug-2016  maxv Map the recursive slot and page table pages as non-executable on Xen. Same
as normal x86.
 1.57 02-Aug-2016  maxv Map the kernel text, rodata and data+bss independently on Xen, with
respectively RX, R and RW.
 1.56 02-Aug-2016  maxv Use PG_RO instead of a magic zero.
 1.55 02-Aug-2016  maxv KNF, and use PAGE_SIZE instead of NBPG.
 1.54 29-May-2016  bouyer branches: 1.54.2;
Switch to elf notes for amd64 instead of the old key=value list to describe the
guest requirements and support.
Add infrastructure to query the hypervisor about features support.
For verbose boot, print the features suppoted by the hypervisor for this
guest.
 1.53 06-May-2014  cherry branches: 1.53.4;
Use the hypervisor to copy/zero pages. This saves us the extra overheads
of setting up temporary kernel mapping/unmapping.

riz@ reports savings of about 2s on a 120s kernel build.
 1.52 10-Nov-2013  jnemeth branches: 1.52.2;
Change xpq_flush_cache to just do WBINVD letting the hypervisor trap and
handle it as MMUEXT_FLUSH_CACHE is a privileged hypervisor operation.
 1.51 08-Nov-2013  christos fix unused variable warnings
 1.50 06-Nov-2013  mrg - move variables inside their #ifdef use
- remove unused and set-but-unused variables
- use __USE() in a particularly ugly case

with these, and a couple of other changes, amd64 gcc 4.8.1 world
is able to complete build.sh release.
 1.49 16-Sep-2012  rmind branches: 1.49.2;
Rename kcpuset_copybits() to kcpuset_export_u32() and thus be more specific
about the interface.
 1.48 21-Aug-2012  bouyer branches: 1.48.2;
Redo previous the correct way: Xen expects a u_long * for vcpumask,
so use 2 uint32_t on LP64.
 1.47 21-Aug-2012  rmind Fix Xen build. Make xcpumask uint32_t, fits 32 CPUs (can increase).
 1.46 30-Jun-2012  jym Extend the xpmap API, as described in [1]. This change is mechanical and
avoids exposing the MD phys_to_machine/machine_to_phys tables directly.
Added:

- xpmap_ptom handles PFN (pseudo physical) to MFN (machine frame number)
translations, and is under control of the domain.
- xpmap_mtop is its counterpart (MFN to PFN), and is under control of
hypervisor.

xpmap_ptom_map() map a pseudo-phys address to a machine address
xpmap_ptom_unmap() unmap a pseudo-phys address (invalidation)
xpmap_ptom_isvalid() check for pseudo-phys address validity

The parameters are physical/machine addresses, like bus_dma/bus_space(9).
As x86 MFNs are tracked by u_long (Xen's choice) while machine addresses
can be 64 bits entities (PAE), use ptoa() to avoid truncation when bit
shifting by PAGE_SHIFT.

I kept the same namespace (xpmap_) to avoid code churn.

[1] http://mail-index.netbsd.org/port-xen/2009/05/09/msg004951.html

XXX will document ptoa/atop/trunc_page separately.
 1.45 27-Jun-2012  jym Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.44 06-Jun-2012  rmind Few fixes for Xen:
- cpu_load_pmap: use atomic kcpuset(9) operations; fixes rare crashes.
- Add kcpuset_copybits(9) and replace xen_kcpuset2bits(). Avoids incorrect
ncpu problem in early boot. Also, micro-optimises xen_mcast_invlpg() and
xen_mcast_tlbflush() routines.

Tested by chs@.
 1.43 20-Apr-2012  rmind - Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.

- Support up to 256 CPUs on amd64 architecture by default.

Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
 1.42 02-Mar-2012  bouyer MMUEXT_INVLPG_MULTI and MMUEXT_TLB_FLUSH_MULTI use a long as cpu mask,
not uint32_t, so pass a pointer of the right type.
While there, cleanup includes and delete local, redundant define of PG_k.
 1.41 24-Feb-2012  cherry (xen) - remove the (*xpq_cpu)() shim.We hasten the %fs/%gs setup process during boot.Although this is hacky, it lets us use the non-xen specificpmap_pte_xxx() functions in pmap code (and others).
 1.40 23-Feb-2012  bouyer On Xen, there is variable-sized Xen data after the kernel's text+data+bss
(this include the physical->machine table).
(vaddr_t)(KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2) is after text+data+bss but,
on a domU with lots of RAM (more than 4GB) (so large
xpmap_phys_to_machine_mapping table) this can point to some of Xen's data
setup at bootstrap (either the xpmap_phys_to_machine_mapping table,
some page shared with the hypervisor, or our kernel page table). Using it for
early_zerop will cause of these pages to be unmapped after bootstrap.
This will cause a kernel page fault for the domU, either immediatly or
eventually much later, depending on where early_zerop points to.
To fix this, account for early_zerop when building the bootstrap pages,
and its VA from here.

May fix PR port-xen/38699
 1.39 17-Feb-2012  bouyer Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.

2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.

To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.

to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.

While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
 1.38 12-Jan-2012  cherry branches: 1.38.2;
relocate pte_lock initialisation to the earliest points after %fs is first usable in the XEN bootpath
 1.37 09-Jan-2012  cherry Make cross-cpu pte access MP safe.
XXX: review cases of use of pmap_set_pte() vs direct use of xpq_queue_pte_update()
 1.36 06-Nov-2011  cherry branches: 1.36.4;
[merging from cherry-xenmp] make pmap_kernel() shadow PMD per-cpu and MP aware.
 1.35 06-Nov-2011  cherry [merging from cherry-xenmp] Make the xen MMU op queue locking api private. Implement per-cpu queues.
 1.34 20-Sep-2011  jym branches: 1.34.2;
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 21-Aug-2011  jym Merge err printf with the panic(9) message.

Also fix the if () {...} statement with braces, to avoid calling panic()
every time. Hi cherry!
 1.32 13-Aug-2011  cherry Call the right function
(fix for an egregious error)
 1.31 13-Aug-2011  cherry Add locking around ops to the hypervisor MMU "queue".
 1.30 13-Aug-2011  cherry remove unnecessary locking overhead for UP
 1.29 10-Aug-2011  cherry Introduce locking primitives for Xen pte operations, and xen helper calls for MP related MMU ops
 1.28 15-Jun-2011  rmind Few XEN fixes:
- cpu_load_pmap: perform tlbflush() after xen_set_user_pgd().
- xen_pmap_bootstrap: perform xpq_queue_tlb_flush() in the end.
- pmap_tlb_shootdown: do not check PG_G for Xen.
 1.27 15-Jun-2011  rmind - cpu_hatch: call tlbflushg(), just to make sure that TLB is clean.
- xen_bootstrap_tables: call xpq_queue_tlb_flush() for safety.
- Initialise cpus_attached and ci_cpumask for primary CPU.
 1.26 08-May-2011  jym branches: 1.26.2;
Print the PGD address in the debug message.
 1.25 29-Mar-2011  jym Typo fix.
 1.24 10-Feb-2011  jym Use only one function to pin pages with Xen, and provide macros to
call it for different levels (L1 => L4).

Replace all calls to xpq_queue_pin_table(...) in MD code with these new
functions, with proper #ifdef'ing depending on $MACHINE.

Rationale:
- only one function to modify for logging
- pushes responsibility to caller for chosing the proper pin level, rather
than Xen internal functions; this makes the pin level explicit rather than
implicit.

Boot tested for dom0 i386/amd64, PAE included. No functional change intended.
 1.23 20-Dec-2010  jym branches: 1.23.2; 1.23.4;
Now, get the return error too, in case that could help with EC2
troubleshooting...
 1.22 19-Dec-2010  jym Need the successful count (for AMI debugging)
 1.21 24-Jul-2010  jym Welcome PAE inside i386 current.

This patch is inspired by work previously done by Jeremy Morse, ported by me
to -current, merged with the work previously done for port-xen, together with
additionals fixes and improvements.

PAE option is disabled by default in GENERIC (but will be enabled in ALL in
the next few days).

In quick, PAE switches the CPU to a mode where physical addresses become
36 bits (64 GiB). Virtual address space remains at 32 bits (4 GiB). To cope
with the increased size of the physical address, they are manipulated as
64 bits variables by kernel and MMU.

When supported by the CPU, it also allows the use of the NX/XD bit that
provides no-execution right enforcement on a per physical page basis.

Notes:

- reworked locore.S

- introduce cpu_load_pmap(), used to switch pmap for the curcpu. Due to the
different handling of pmap mappings with PAE vs !PAE, Xen vs native, details
are hidden within this function. This helps calling it from assembly,
as some features, like BIOS calls, switch to pmap_kernel before mapping
trampoline code in low memory.

- some changes in bioscall and kvm86_call, to reflect the above.

- the L3 is "pinned" per-CPU, and is only manipulated by a
reduced set of functions within pmap. To track the L3, I added two
elements to struct cpu_info, namely ci_l3_pdirpa (PA of the L3), and
ci_l3_pdir (the L3 VA). Rest of the code considers that it runs "just
like" a normal i386, except that the L2 is 4 pages long (PTP_LEVELS is
still 2).

- similar to the ci_pae_l3_pdir{,pa} variables, amd64's xen_current_user_pgd
becomes an element of cpu_info (slowly paving the way for MP world).

- bootinfo_source struct declaration is modified, to cope with paddr_t size
change with PAE (it is not correct to assume that bs_addr is a paddr_t when
compiled with PAE - it should remain 32 bits). bs_addrs is now a
void * array (in bootloader's code under i386/stand/, the bs_addrs
is a physaddr_t, which is an unsigned long).

- fixes in multiboot code (same reason as bootinfo): paddr_t size
change. I used Elf32_* types, use RELOC() where necessary, and move the
memcpy() functions out of the if/else if (I do not expect sym and str tables
to overlap with ELF).

- 64 bits atomic functions for pmap

- all pmap_pdirpa access are now done through the pmap_pdirpa macro. It
hides the L3/L2 stuff from PAE, as well as the pm_pdirpa change in
struct pmap (it now becomes a PDP_SIZE array, with or without PAE).

- manipulation of recursive mappings ( PDIR_SLOT_{,A}PTEs ) is done via
loops on PDP_SIZE.

See also http://mail-index.netbsd.org/port-i386/2010/07/17/msg002062.html

No objection raised on port-i386@ and port-xen@R for about a week.

XXX kvm(3) will be fixed in another patch to properly handle both PAE and !PAE
kernel dumps (VA => PA macros are slightly different, and need proper 64 bits
PA support in kvm_i386).

XXX Mixing PAE and !PAE modules may lead to unwanted/unexpected results. This
cannot be solved easily, and needs lots of thinking before being declared
safe (paddr_t/bus_addr_t size handling, PD/PT macros abstractions).
 1.20 15-Jul-2010  jym With Xen, PDPpaddr should contain a guest physical address (== PFN).
 1.19 26-Feb-2010  jym branches: 1.19.2;
Fixes regarding paddr_t/pd_entry_t types in MD x86 code, exposed by PAE:

- NBPD_* macros are set to the types that better match their architecture
(UL for i386 and amd64, ULL for i386 PAE) - will revisit when paddr_t is
set to 64 bits for i386 non-PAE.

- type fixes in printf/printk messages (Use PRIxPADDR when printing paddr_t
values, instead of %lx - paddr_t/pd_entry_t being 64 bits with PAE)

- remove casts that are no more needed now that Xen2 support has been dropped

Some fixes are from jmorse@ patches for PAE.

Compile + tested for i386 GENERIC and XEN3 kernels. Only compile tested for
amd64.

Reviewed by bouyer@.

See also http://mail-index.netbsd.org/tech-kern/2010/02/22/msg007373.html
 1.18 12-Feb-2010  jym Starting with Xen 3 API, MMU_EXTENDED_COMMAND (tlb flush, cache flush, page
pinning/unpinning, set_ldt, invlpg) operations cannot be queued in the
xpq_queue[] any more, as they use their own specific hypercall, mmuext_op().

Their associated xpq_queue_*() functions already call xpq_flush_queue()
before issuing the mmuext_op() hypercall, which makes these xpq_flush_queue()
calls not necessary.

Rapidly discussed with bouyer@ in private mail. XEN3_DOM0/XEN3PAE_DOM0 tested
through a build.sh release, amd64 was only compile tested. No regression
expected.
 1.17 23-Oct-2009  snj branches: 1.17.2;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.16 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.15 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.14 23-Jul-2009  jym Fix typos in comments and __PRINTKs.
 1.13 20-Jun-2009  cegger sprintf -> snprintf. Wrap long lines.
 1.12 13-Nov-2008  cegger branches: 1.12.4;
Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.11 24-Oct-2008  jym branches: 1.11.2; 1.11.4;
- 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.10 21-Oct-2008  cegger introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.9 05-Sep-2008  tron Compile NetBSD/amd64 kernels with "-Wextra". Patches contributed by
Juan RP in PR port-amd64/39266.
 1.8 14-Apr-2008  cegger branches: 1.8.4; 1.8.6; 1.8.10;
- use POSIX integer types
- ansify functions
 1.7 17-Feb-2008  bouyer branches: 1.7.6;
The informations about console and store page number are long, so avoid
overflow on i386PAE when converting to machine address. Fix booting
XEN3PAE kernels when xen maps it above 4Gb.
 1.6 23-Jan-2008  bouyer Merge the bouyer-xeni386 branch. This brings in PAE support to NetBSD xeni386
(domU only). PAE support is enabled by 'options PAE', see the new XEN3PAE_DOMU
and INSTALL_XEN3PAE_DOMU kernel config files.

See the comments in arch/i386/include/{pte.h,pmap.h} to see how it works.
In short, we still handle it as a 2-level MMU, with the second level page
directory being 4 pages in size. pmap switching is done by switching the
L2 pages in the L3 entries, instead of loading %cr3. This is almost required
by Xen, which handle the last L2 page (the one mapping 0xc0000000 - 0xffffffff)
in a very special way. But this approach should also work for native PAE
support if ever supported (in fact, the pmap should almost suport native
PAE, what's missing is bootstrap code in locore.S).
 1.5 15-Jan-2008  bouyer Allocate one more L2 slot in xen_pmap_bootstrap() for i386.
pmap_bootstrap()/init386() wants to map a few additionnal things after
first_avail that we didn't account for, before pmap_growkernel() is
used/functionnal, and if the loaded kernel is close to the end of
the last L2 slot we loose. Should fix port-xen/37761 by YAMAMOTO Takashi.

Fix a XENPRINTF() so that low debug builds again.
 1.4 11-Jan-2008  bouyer 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.3 23-Nov-2007  bouyer branches: 1.3.2; 1.3.4; 1.3.8; 1.3.12; 1.3.16;
xpq_flush_queue(): cast values to u_int64_t and use PRIx64 in printf().
Fix build of i386 Xen kernels, reported by Hisashi T Fujinaka.
 1.2 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.1 21-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file x86_xpmap.c was initially added on branch bouyer-xenamd64.
 1.1.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.6 22-Nov-2007  bouyer Disable debug messages
 1.1.2.5 21-Nov-2007  bouyer When HYPERVISOR_mmu_update_self() fails in xpq_flush_queue(), dump the content
of the queue.
 1.1.2.4 19-Nov-2007  bouyer Get rid of arch/xenamd64, step 3: merge xenamd64/amd64/xpmap.c in
xen/x86/x86_xpmap.c
 1.1.2.3 26-Oct-2007  bouyer Make amd64, i386 and xen kernels build and work again.
 1.1.2.2 25-Oct-2007  bouyer Finish sync with HEAD. Especially use the new x86 pmap for xenamd64.
For this:
- rename pmap_pte_set() to pmap_pte_testset()
- make pmap_pte_set() a function or macro for non-atomic PTE write
- define and use pmap_pa2pte()/pmap_pte2pa() to read/write PTE entries
- define pmap_pte_flush() which is a nop in x86 case, and flush the
MMUops queue in the Xen case
 1.1.2.1 21-Oct-2007  bouyer Factorise some Xen pmap code in x86_xpmap.c.
More xpmap_{ptom,mtop} -> xpmap_{ptom,mtop}_masked

The xenamd64 kernel is now good enough to complete a sysinst install from
xennet to xbd.
 1.3.16.3 23-Mar-2008  matt sync with HEAD
 1.3.16.2 09-Jan-2008  matt sync with HEAD
 1.3.16.1 23-Nov-2007  matt file x86_xpmap.c was added on branch matt-armv6 on 2008-01-09 01:50:15 +0000
 1.3.12.14 20-Jan-2008  bouyer Remove debug printk()
 1.3.12.13 19-Jan-2008  bouyer Sync with HEAD
 1.3.12.12 18-Jan-2008  bouyer Fix APDP handling. A XEN i386PAE kernel now boots multiuser
 1.3.12.11 17-Jan-2008  bouyer - Fix L2_SLOT_APTE value (not sure how I got this value but it was definitively
wrong)
- Use global variable for the PAE L3 page adresses, so that pmap.c can get it
from the bootstrap code
- Extent the size of our virtual PDP from 3 to 4 pages, so that pmap->pm_pdir[]
is contigous for the whole VA range. The last page is a shadow of
the kernel's real PDP (L3[3]).
- make pm_pdirpa an array of 4 paddr_t if using PAE. introduce a
pmap_pdirpa macro to get the physical address of a given PD entry.
- fix pmap_map_pte

The kernel now boots single-user. fsck will cause a kernel fault in
pmap_pdes_invalid() on exit.
 1.3.12.10 15-Jan-2008  bouyer Snapshot of work in progress: an Xen i386PAE kernel boots and start init
on a amd64 dom0, but panics when init forks.
This code needs a lot of cleanup, and the pmap handling is minimal to
allow init to start. It's a proof of concept of how PAE on Xen can work.

For PAE guest, the Xen MMU handling differs in some significant way
from the i386 or amd64 Xen.
The L3 page has only 4 entries, the last one mapping 0xc0000000->0xffffffff
(which happens to be our kenrel VM range, that's cool). The L2 page
pointed to by this last entry is handled specially by Xen because it
contains some Xen private mapping, including a recursive mapping. So this
page can only be pointed to by exactly one L3 entry, and nothing else
(it can't be part of a recursive mapping for example). In addition, it
would waste too much VA space to do recursive mapping at the L3 level.

We do pmap switching at the L# level, instead of doing it though %cr3.
%cr3 is static, as is L3[3] which contains only kenrel mappings.
pmap_load() does pmap switching though the first 3 entries for L3.

PTE mapping is done though 4 contigous L2 entries; the last one pointing
to a shadow of L3[3]. This way we can consider we have a 2-level VM system,
but with the L2 being 4 pages in size instead of one. The plx_i()
macros can be used with it to access the PTE without changes.

This can be reused as is for native PAE support (without the L3[3] shadow
which wouldn't be needed here)
 1.3.12.9 13-Jan-2008  bouyer Add i386PAE suport for bootstrap.
 1.3.12.8 13-Jan-2008  bouyer Work in progress on xeni386 PAE support:
Make xeni386 build with a 64bit paddr_t. For this vaddr_t vs paddr_t vs
pointers usages had to be clarified.
If 'options PAE' is present in a Xen3 kernel, switch paddr_t, pd_entry_t
and pt_entry_t to 64bits, and add the PAE entry in the __xen_guest ELF section.
 1.3.12.7 11-Jan-2008  bouyer Ops, fix XENPRINTK usage.
 1.3.12.6 11-Jan-2008  bouyer printk -> XENPRINTK
 1.3.12.5 09-Jan-2008  bouyer Merge xen bits to i386/i386/gdt.c. Convert remaining uses of PTE_* macros to
pmap_pte_* macros/inlines.
Fix think-o in pmap.c for native i386.
 1.3.12.4 05-Jan-2008  bouyer Make it build on for XEN2_*
 1.3.12.3 15-Dec-2007  bouyer Switch xen/i386 to the x86 xen_pmap_bootstrap().
 1.3.12.2 15-Dec-2007  bouyer Cleanup xen_pmap_bootstrap() and make it build on i386.
 1.3.12.1 11-Dec-2007  bouyer Switch i386 to x86/x86/pmap.c
 1.3.8.5 27-Feb-2008  yamt sync with head.
 1.3.8.4 04-Feb-2008  yamt sync with head.
 1.3.8.3 21-Jan-2008  yamt sync with head
 1.3.8.2 07-Dec-2007  yamt sync with head
 1.3.8.1 23-Nov-2007  yamt file x86_xpmap.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:18 +0000
 1.3.4.2 03-Dec-2007  ad Sync with HEAD.
 1.3.4.1 23-Nov-2007  ad file x86_xpmap.c was added on branch vmlocking on 2007-12-03 19:04:42 +0000
 1.3.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.3.2.1 23-Nov-2007  joerg file x86_xpmap.c was added on branch jmcneill-pm on 2007-11-27 19:36:22 +0000
 1.7.6.3 17-Jan-2009  mjf Sync with HEAD.
 1.7.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.7.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.8.10.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.8.10.1 19-Oct-2008  haad Sync with HEAD.
 1.8.6.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.8.4.5 11-Aug-2010  yamt sync with head.
 1.8.4.4 11-Mar-2010  yamt sync with head
 1.8.4.3 19-Aug-2009  yamt sync with head.
 1.8.4.2 18-Jul-2009  yamt sync with head.
 1.8.4.1 04-May-2009  yamt sync with head.
 1.11.4.1 24-Feb-2012  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1729):
sys/arch/x86/x86/pmap.c: revision 1.170 via patch
sys/arch/xen/x86/x86_xpmap.c: revision 1.40 via patch

Fix random kernel panic on domains with large memory.
May fix PR port-xen/38699
 1.11.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.12.4.15 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.4.14 02-May-2011  jym Sync with head.
 1.12.4.13 30-Mar-2011  jym Sync with my commits in HEAD.
 1.12.4.12 29-Mar-2011  jym More sync fixes. And add the mbr_gpt files.
 1.12.4.11 28-Mar-2011  jym Cure sync hiccups. Code with compile errors is not really useful, heh.
 1.12.4.10 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.12.4.9 10-Jan-2011  jym Sync with HEAD
 1.12.4.8 24-Oct-2010  jym Sync with HEAD
 1.12.4.7 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.4.6 01-Nov-2009  jym Sync with HEAD.
 1.12.4.5 24-Jul-2009  jym - rework the page pinning API, so that now a function is provided for
each level of indirection encountered during virtual memory translations. Update
pmap accordingly. Pinning looks cleaner that way, and it offers the possibility
to pin lower level pages if necessary (NetBSD does not do it currently).

- some fixes and comments to explain how page validation/invalidation take
place during save/restore/migrate under Xen. L2 shadow entries from PAE are now
handled, so basically, suspend/resume works with PAE.

- fixes an issue reported by Christoph (cegger@) for xencons suspend/resume
in dom0.

TODO:

- PAE save/restore is currently limited to single-user only, multi-user
support requires modifications in PAE pmap that should be discussed first. See
the comments about the L2 shadow pages cached in pmap_pdp_cache in this commit.

- grant table bug is still there; do not use the kernels of this branch
to test suspend/resume, unless you want to experience bad crashes in dom0,
and push the big red button.

Now there is light at the end of the tunnel :)

Note: XEN2 kernels will neither build nor work with this branch.
 1.12.4.4 23-Jul-2009  jym Sync with HEAD.
 1.12.4.3 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.12.4.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.12.4.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.17.2.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.17.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.19.2.3 31-May-2011  rmind sync with head
 1.19.2.2 21-Apr-2011  rmind sync with head
 1.19.2.1 05-Mar-2011  rmind sync with head
 1.23.4.1 17-Feb-2011  bouyer Sync with HEAD
 1.23.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.26.2.12 21-Oct-2011  bouyer Make this build without 'options MULTIPROCESSOR'
 1.26.2.11 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.26.2.10 18-Sep-2011  cherry Make the xpq locking per-cpu
 1.26.2.9 09-Sep-2011  cherry fix amd64 boot.
 1.26.2.8 30-Aug-2011  cherry Add per-cpu mmu queues
 1.26.2.7 20-Aug-2011  cherry PAE MP support (preliminary), amd64 per-cpu L4 model redesigned, i386 pmap_pa_start/end fixup
 1.26.2.6 17-Aug-2011  cherry Pullup relevant changes from -current
 1.26.2.5 31-Jul-2011  cherry grow MP support for i386. boots to single user
 1.26.2.4 16-Jul-2011  cherry Introduce a per-cpu "shadow" for pmap_kernel()'s L4 page
 1.26.2.3 27-Jun-2011  cherry Add xpq locking around xpq_queue_tlb_flush()
 1.26.2.2 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.26.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.34.2.5 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.34.2.4 30-Oct-2012  yamt sync with head
 1.34.2.3 23-May-2012  yamt sync with head.
 1.34.2.2 17-Apr-2012  yamt sync with head
 1.34.2.1 10-Nov-2011  yamt sync with head
 1.36.4.6 29-Apr-2012  mrg sync to latest -current.
 1.36.4.5 06-Mar-2012  mrg sync to -current
 1.36.4.4 06-Mar-2012  mrg sync to -current
 1.36.4.3 04-Mar-2012  mrg sync to latest -current.
 1.36.4.2 24-Feb-2012  mrg sync to -current.
 1.36.4.1 18-Feb-2012  mrg merge to -current.
 1.38.2.5 12-Jun-2012  riz Pull up following revision(s) (requested by rmind in ticket #314):
sys/arch/xen/x86/cpu.c: revision 1.92
sys/kern/subr_kcpuset.c: revision 1.6
sys/sys/kcpuset.h: revision 1.6
sys/arch/xen/x86/x86_xpmap.c: revision 1.44
Few fixes for Xen:
- cpu_load_pmap: use atomic kcpuset(9) operations; fixes rare crashes.
- Add kcpuset_copybits(9) and replace xen_kcpuset2bits(). Avoids incorrect
ncpu problem in early boot. Also, micro-optimises xen_mcast_invlpg() and
xen_mcast_tlbflush() routines.
Tested by chs@.
 1.38.2.4 09-May-2012  riz Pull up following revision(s) (requested by rmind in ticket #202):
sys/arch/x86/include/cpuvar.h: revision 1.46
sys/arch/xen/include/xenpmap.h: revision 1.34
sys/arch/i386/include/param.h: revision 1.77
sys/arch/x86/x86/pmap_tlb.c: revision 1.5
sys/arch/x86/x86/pmap_tlb.c: revision 1.6
sys/arch/i386/i386/genassym.cf: revision 1.92
sys/arch/xen/x86/cpu.c: revision 1.91
sys/arch/x86/x86/pmap.c: revision 1.177
sys/arch/xen/x86/xen_pmap.c: revision 1.21
sys/arch/x86/acpi/acpi_wakeup.c: revision 1.31
sys/kern/subr_kcpuset.c: revision 1.5
sys/arch/amd64/include/param.h: revision 1.18
sys/sys/kcpuset.h: revision 1.5
sys/arch/x86/x86/mtrr_i686.c: revision 1.26
sys/arch/x86/x86/mtrr_i686.c: revision 1.27
sys/arch/xen/x86/x86_xpmap.c: revision 1.43
sys/arch/x86/x86/cpu.c: revision 1.98
sys/arch/amd64/amd64/mptramp.S: revision 1.14
sys/kern/sys_sched.c: revision 1.42
sys/arch/amd64/amd64/genassym.cf: revision 1.50
sys/arch/i386/i386/mptramp.S: revision 1.24
sys/arch/x86/include/pmap.h: revision 1.52
sys/arch/x86/include/cpu.h: revision 1.50
- Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.
- Support up to 256 CPUs on amd64 architecture by default.
Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
- pmap_tlb_shootdown: do not overwrite tp_cpumask with pm_cpus, but merge
like pm_kernel_cpus. Remove unecessary intersection with kcpuset_running.
Do not reset tp_userpmap if pmap_kernel().
- Remove pmap_tlb_mailbox_t wrapping, which is pointless after recent changes.
- pmap_tlb_invalidate, pmap_tlb_intr: constify for packet structure.
i686_mtrr_init_first: handle the case when there are no variable-size MTRR
registers available (i686_mtrr_vcnt == 0).
 1.38.2.3 05-Mar-2012  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #80):
sys/arch/xen/x86/x86_xpmap.c: revision 1.42
sys/arch/x86/include/specialreg.h: revision 1.56
sys/arch/amd64/amd64/machdep.c: revision 1.179
sys/arch/i386/i386/locore.S: revision 1.97
sys/arch/i386/i386/machdep.c: revision 1.723 via patch
sys/arch/x86/include/cpu.h: revision 1.49

Fix possible FPU registers corruption on context switches.
Fix type of pointers passed to some hypercalls.
 1.38.2.2 23-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #39):
sys/arch/x86/x86/pmap.c: revision 1.170
sys/arch/xen/x86/x86_xpmap.c: revision 1.40
On Xen, there is variable-sized Xen data after the kernel's text+data+bss
(this include the physical->machine table).
(vaddr_t)(KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2) is after text+data+bss but,
on a domU with lots of RAM (more than 4GB) (so large
xpmap_phys_to_machine_mapping table) this can point to some of Xen's data
setup at bootstrap (either the xpmap_phys_to_machine_mapping table,
some page shared with the hypervisor, or our kernel page table). Using it for
early_zerop will cause of these pages to be unmapped after bootstrap.
This will cause a kernel page fault for the domU, either immediatly or
eventually much later, depending on where early_zerop points to.
To fix this, account for early_zerop when building the bootstrap pages,
and its VA from here.
May fix PR port-xen/38699
 1.38.2.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #29):
sys/arch/xen/x86/x86_xpmap.c: revision 1.39
sys/arch/xen/include/hypervisor.h: revision 1.37
sys/arch/xen/include/intr.h: revision 1.34
sys/arch/xen/x86/xen_ipi.c: revision 1.10
sys/arch/x86/x86/cpu.c: revision 1.97
sys/arch/x86/include/cpu.h: revision 1.48
sys/uvm/uvm_map.c: revision 1.315
sys/arch/x86/x86/pmap.c: revision 1.165
sys/arch/xen/x86/cpu.c: revision 1.81
sys/arch/x86/x86/pmap.c: revision 1.167
sys/arch/xen/x86/cpu.c: revision 1.82
sys/arch/x86/x86/pmap.c: revision 1.168
sys/arch/xen/x86/xen_pmap.c: revision 1.17
sys/uvm/uvm_km.c: revision 1.122
sys/uvm/uvm_kmguard.c: revision 1.10
sys/arch/x86/include/pmap.h: revision 1.50
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.
2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.
To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.
to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.
While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
When using uvm_km_pgremove_intrsafe() make sure mappings are removed
before returning the pages to the free pool. Otherwise, under Xen,
a page which still has a writable mapping could be allocated for
a PDP by another CPU and the hypervisor would refuse it (this is
PR port-xen/45975).
For this, move the pmap_kremove() calls inside uvm_km_pgremove_intrsafe(),
and do pmap_kremove()/uvm_pagefree() in batch of (at most) 16 entries
(as suggested by Chuck Silvers on tech-kern@, see also
http://mail-index.netbsd.org/tech-kern/2012/02/17/msg012727.html and
followups).
Avoid early use of xen_kpm_sync(); locks are not available at this time.
Don't call cpu_init() twice.
Makes LOCKDEBUG kernels boot again
Revert pmap_pte_flush() -> xpq_flush_queue() in previous.
 1.48.2.3 03-Dec-2017  jdolecek update from HEAD
 1.48.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.48.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.49.2.1 18-May-2014  rmind sync with head
 1.52.2.1 10-Aug-2014  tls Rebase.
 1.53.4.5 28-Aug-2017  skrll Sync with HEAD
 1.53.4.4 05-Feb-2017  skrll Sync with HEAD
 1.53.4.3 05-Dec-2016  skrll Sync with HEAD
 1.53.4.2 05-Oct-2016  skrll Sync with HEAD
 1.53.4.1 09-Jul-2016  skrll Sync with HEAD
 1.54.2.4 20-Mar-2017  pgoyette Sync with HEAD
 1.54.2.3 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.54.2.2 04-Nov-2016  pgoyette Sync with HEAD
 1.54.2.1 06-Aug-2016  pgoyette Sync with HEAD
 1.69.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.74.2.3 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.74.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.74.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.75.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.75.2.1 10-Jun-2019  christos Sync with HEAD
 1.84.4.2 13-May-2022  martin Pull up following revision(s) (requested by bouyer in ticket #1444):

sys/arch/xen/x86/x86_xpmap.c: revision 1.91

In bootstrap, after switching to a new page table make sure that
now-unused memory is unmapped.
 1.84.4.1 31-May-2020  martin Pull up following revision(s) (requested by bouyer in ticket #935):

sys/arch/xen/x86/x86_xpmap.c: revision 1.89
sys/arch/x86/include/pmap.h: revision 1.121
sys/arch/xen/xen/privcmd.c: revision 1.58
sys/external/mit/xen-include-public/dist/xen/include/public/memory.h: revision 1.2
sys/arch/xen/include/xenpmap.h: revision 1.44
sys/arch/xen/include/xenio.h: revision 1.12
sys/arch/x86/x86/pmap.c: revision 1.394
(all via patch)

Ajust pmap_enter_ma() for upcoming new Xen privcmd ioctl:
pass flags to xpq_update_foreign()

Introduce a pmap MD flag: PMAP_MD_XEN_NOTR, which cause xpq_update_foreign()
to use the MMU_PT_UPDATE_NO_TRANSLATE flag.
make xpq_update_foreign() return the raw Xen error. This will cause
pmap_enter_ma() to return a negative error number in this case, but the
only user of this code path is privcmd.c and it can deal with it.

Add pmap_enter_gnt()m which maps a set of Xen grant entries at the
specified va in the specified pmap. Use the hooks implemented for EPT to
keep track of mapped grand entries in the pmap, and unmap them
when pmap_remove() is called. This requires pmap_remove() to be split
into a pmap_remove_locked(), to be called from pmap_remove_gnt().

Implement new ioctl, needed by Xen 4.13:
IOCTL_PRIVCMD_MMAPBATCH_V2
IOCTL_PRIVCMD_MMAP_RESOURCE
IOCTL_GNTDEV_MMAP_GRANT_REF
IOCTL_GNTDEV_ALLOC_GRANT_REF

Always enable declarations needed by privcmd.c
 1.34 14-May-2024  andvar fix recently committed typos by msaitoh in few more places, as well as few more.
mainly s/contigous/contiguous/ and s/miliseconds/milliseconds/ in comments.
 1.33 20-Aug-2022  riastradh x86: Split most of pmap.h into pmap_private.h or vmparam.h.

This way pmap.h only contains the MD definition of the MI pmap(9)
API, which loads of things in the kernel rely on, so changing x86
pmap internals no longer requires recompiling the entire kernel every
time.

Callers needing these internals must now use machine/pmap_private.h.
Note: This is not x86/pmap_private.h because it contains three parts:

1. CPU-specific (different for i386/amd64) definitions used by...

2. common definitions, including Xenisms like xpmap_ptetomach,
further used by...

3. more CPU-specific inlines for pmap_pte_* operations

So {amd64,i386}/pmap_private.h defines 1, includes x86/pmap_private.h
for 2, and then defines 3. Maybe we should split that out into a new
pmap_pte.h to reduce this trouble.

No functional change intended, other than that some .c files must
include machine/pmap_private.h when previously uvm/uvm_pmap.h
polluted the namespace with pmap internals.

Note: This migrates part of i386/pmap.h into i386/vmparam.h --
specifically the parts that are needed for several constants defined
in vmparam.h:

VM_MAXUSER_ADDRESS
VM_MAX_ADDRESS
VM_MAX_KERNEL_ADDRESS
VM_MIN_KERNEL_ADDRESS

Since i386 needs PDP_SIZE in vmparam.h, I added it there on amd64
too, just to keep things parallel.
 1.32 06-May-2020  bouyer Make MP-safe: make sure the xpq_queue* are flushed before making the
pages visible to UVM.
 1.31 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.30 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.29 09-Apr-2020  jdolecek update to __XEN_INTERFACE_VERSION__ 0x0003020a aka Xen 3.2.10

this brings grant memory v2 support:
- status separated from flags - revoking access needs just memory barrier,
no need for expensive cmpxchg16 any more
- sub-page hypervisor copy-only grants, to be used by xennet(4)
- 64-bit frame, i.e. support for DomU RAM >16TB

the grant table is now always allocated on boot to maximum size, it's now
never grown in runtime; switch back to regular kmem_alloc()/kmem_free()

code now requires v2 support, no compatibility for grant version 1 retained -
Xen v2 support predates all currently supported Xen versions

also interface for baloon changed slightly, code updated
 1.28 03-Sep-2018  riastradh branches: 1.28.10;
Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.27 24-Jun-2018  jdolecek branches: 1.27.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.26 30-Jun-2012  jym branches: 1.26.38;
Use setter to set xenguest_handles.
 1.25 30-Jun-2012  jym Extend the xpmap API, as described in [1]. This change is mechanical and
avoids exposing the MD phys_to_machine/machine_to_phys tables directly.
Added:

- xpmap_ptom handles PFN (pseudo physical) to MFN (machine frame number)
translations, and is under control of the domain.
- xpmap_mtop is its counterpart (MFN to PFN), and is under control of
hypervisor.

xpmap_ptom_map() map a pseudo-phys address to a machine address
xpmap_ptom_unmap() unmap a pseudo-phys address (invalidation)
xpmap_ptom_isvalid() check for pseudo-phys address validity

The parameters are physical/machine addresses, like bus_dma/bus_space(9).
As x86 MFNs are tracked by u_long (Xen's choice) while machine addresses
can be 64 bits entities (PAE), use ptoa() to avoid truncation when bit
shifting by PAGE_SHIFT.

I kept the same namespace (xpmap_) to avoid code churn.

[1] http://mail-index.netbsd.org/port-xen/2009/05/09/msg004951.html

XXX will document ptoa/atop/trunc_page separately.
 1.24 27-Jun-2012  jym Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.23 01-Jul-2011  dyoung branches: 1.23.2;
#include <sys/bus.h> instead of <machine/bus.h>.
 1.22 12-Nov-2010  njoly branches: 1.22.6;
Include uvm.h not uvm_extern.h following recent changes.
 1.21 22-Mar-2010  bouyer bus_dmamem_alloc() may not get a boundary smaller than size, but
it's perfectly valid for bus_dmamap_create() to do so (a contigous
transfers will then split in multiple segment).
Fix _xen_bus_dmamem_alloc_range() and _bus_dmamem_alloc_range() to
allow a boundary limit smaller than size:
- compute appropriate boundary for uvm_pglistalloc(), wich doesn't
accept boundary < size
- also take care of boundary when deciding to start a new segment.
While there, remove useless boundary argument to _xen_alloc_contig().
Fix the boundary-related issue of PR port-amd64/42980
 1.20 09-Mar-2010  jym branches: 1.20.2;
Although Xen's documentation states that the address_bits field is not used
by XENMEM_decrease_reservation, it is checked by the hypervisor. In certain
circumstances (stack leak), the field could have an improper value, leading
to a fail of the hypercall.

Set it to 0 ("no addressing restriction") to avoid that.

Patch tested by Sam Fourman and haad@.

This should fix the rare "failed allocating DMA memory" encountered
under NetBSD dom0. Will ask for a pull-up.
 1.19 02-Mar-2010  jym Catch the return value from the XENMEM_decrease_reservation hypercall,
and not some error value stored earlier.

While here, fix a typo in a comment.
 1.18 27-Feb-2010  jym Make a XENMEM_decrease_reservation DEBUG printf() more meaningful.
 1.17 12-Feb-2010  jym Starting with Xen 3 API, MMU_EXTENDED_COMMAND (tlb flush, cache flush, page
pinning/unpinning, set_ldt, invlpg) operations cannot be queued in the
xpq_queue[] any more, as they use their own specific hypercall, mmuext_op().

Their associated xpq_queue_*() functions already call xpq_flush_queue()
before issuing the mmuext_op() hypercall, which makes these xpq_flush_queue()
calls not necessary.

Rapidly discussed with bouyer@ in private mail. XEN3_DOM0/XEN3PAE_DOM0 tested
through a build.sh release, amd64 was only compile tested. No regression
expected.
 1.16 23-Jan-2010  cegger branches: 1.16.2;
fix address overflow with 32bit PAE.
Reported and tested by Mark Davies on port-xen@.
 1.15 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.14 24-Jan-2009  bouyer branches: 1.14.2;
Properly check the return value of HYPERVISOR_memory_op(): it returns
the number of successfull operations, so a return value of 0 is also
a failure.
 1.13 18-Dec-2008  cegger remove unused malloc.h
 1.12 13-Nov-2008  cegger Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.11 04-Jun-2008  ad branches: 1.11.4; 1.11.6; 1.11.8; 1.11.12;
vm_page: put TAILQ_ENTRY into a union with LIST_ENTRY, so we can use both.
 1.10 28-Apr-2008  martin branches: 1.10.2;
Remove clause 3 and 4 from TNF licenses
 1.9 24-Feb-2007  bouyer branches: 1.9.42; 1.9.44; 1.9.46;
Properly honnor bus_dma bus address range restriction for Xen3 (the Xen2
interface doesn't allow it), so that .e.g bus_dma_subregion() has a chance
to work. Unfortunably a stock Xen hypervisor won't allow a upper bound less
than 2^31 (2GB) so devices like bce(4) will need a hacked hypervisor to
work properly.
 1.8 03-Sep-2006  bouyer branches: 1.8.6; 1.8.8;
Wrap some printfs in #ifdef DEBUG, as we should not leak memory any more when
bus_dma memory allocation fails.
 1.7 28-Aug-2006  bouyer Some bus_dma(9) fixes for Xen:
- Attempt to gracefully recover from a failed decrease_reservation or
increase_reservation, by avoiding physical memory loss.
- always store a machine address in ds_addr; this avoids some mistakes
where machine address would in some case be freed at physical address, or
mapped as physical address.
 1.6 15-Jan-2006  bouyer branches: 1.6.2; 1.6.6; 1.6.16; 1.6.18;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.5 24-Dec-2005  perry branches: 1.5.2;
__asm__ -> __asm
__const__ -> const
__inline__ -> inline
__volatile__ -> volatile
 1.4 11-Dec-2005  christos merge ktrace-lwp.
 1.3 22-Aug-2005  bouyer branches: 1.3.2; 1.3.8;
Fix a memory leak. Thanks to YAMAMOTO Takashi for the notice.
 1.2 20-Aug-2005  bouyer Also properly check the alignement and boundary constraints.
 1.1 20-Aug-2005  bouyer Deal with the machine address space being non-contigous in bus_dmamem_alloc():
- Define _BUS_AVAIL_END to 0xffffffff, as we don't have an easy way to
find the upper bound for our machine address space (and this can change
when we swap pages with the hypervisor).
- implement _xen_bus_dmamem_alloc_range(), which will request a contigous
set of pages to the hypervisor if the pages returned by uvm_pglistalloc()
don't fit the constraints.
We can't deal with the low/high constraints yet, because Xen doesn't offer a
way to get pages in a specific ranges of addresses.

Based on patches from Dave Thompson (in private mail), with heavy hacking
by me.
 1.3.8.2 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.8.1 22-Aug-2005  skrll file xen_bus_dma.c was added on branch ktrace-lwp on 2005-11-10 14:00:34 +0000
 1.3.2.5 16-Sep-2006  ghen Pull up following revision(s) (requested by bouyer in ticket #1510):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.7
sys/arch/xen/x86/xen_bus_dma.c: revision 1.8
sys/arch/x86/include/bus_private.h: revision 1.6
sys/arch/x86/x86/bus_dma.c: revision 1.30
sys/arch/xen/include/bus_private.h: revision 1.7
Some bus_dma(9) fixes for Xen:
- Attempt to gracefully recover from a failed decrease_reservation or
increase_reservation, by avoiding physical memory loss.
- always store a machine address in ds_addr; this avoids some mistakes
where machine address would in some case be freed at physical address, or
mapped as physical address.
Wrap some printfs in #ifdef DEBUG, as we should not leak memory any more when
bus_dma memory allocation fails.
 1.3.2.4 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #696):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.3
Fix a memory leak. Thanks to YAMAMOTO Takashi for the notice.
 1.3.2.3 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #696):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.2
Also properly check the alignement and boundary constraints.
 1.3.2.2 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #696):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.1
sys/arch/xen/include/bus_private.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.28
Deal with the machine address space being non-contigous in bus_dmamem_alloc():
- Define _BUS_AVAIL_END to 0xffffffff, as we don't have an easy way to
find the upper bound for our machine address space (and this can change
when we swap pages with the hypervisor).
- implement _xen_bus_dmamem_alloc_range(), which will request a contigous
set of pages to the hypervisor if the pages returned by uvm_pglistalloc()
don't fit the constraints.
We can't deal with the low/high constraints yet, because Xen doesn't offer a
way to get pages in a specific ranges of addresses.
Based on patches from Dave Thompson (in private mail), with heavy hacking
by me.
 1.3.2.1 22-Aug-2005  tron file xen_bus_dma.c was added on branch netbsd-3 on 2005-08-25 20:49:54 +0000
 1.5.2.1 01-Feb-2006  yamt sync with head.
 1.6.18.1 14-Sep-2006  riz Pull up following revision(s) (requested by bouyer in ticket #150):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.7
sys/arch/xen/x86/xen_bus_dma.c: revision 1.8
sys/arch/x86/include/bus_private.h: revision 1.6
sys/arch/x86/x86/bus_dma.c: revision 1.30
sys/arch/xen/include/bus_private.h: revision 1.7
Some bus_dma(9) fixes for Xen:
- Attempt to gracefully recover from a failed decrease_reservation or
increase_reservation, by avoiding physical memory loss.
- always store a machine address in ds_addr; this avoids some mistakes
where machine address would in some case be freed at physical address, or
mapped as physical address.
Wrap some printfs in #ifdef DEBUG, as we should not leak memory any more when
bus_dma memory allocation fails.
 1.6.16.4 26-Feb-2007  yamt sync with head.
 1.6.16.3 30-Dec-2006  yamt sync with head.
 1.6.16.2 21-Jun-2006  yamt sync with head.
 1.6.16.1 15-Jan-2006  yamt file xen_bus_dma.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.6.6.2 14-Sep-2006  yamt sync with head.
 1.6.6.1 03-Sep-2006  yamt sync with head.
 1.6.2.1 09-Sep-2006  rpaulo sync with head
 1.8.8.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.8.6.1 13-Jun-2010  riz Pull up following revision(s) (requested by jym in ticket #1388):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.20
Although Xen's documentation states that the address_bits field is not used
by XENMEM_decrease_reservation, it is checked by the hypervisor. In certain
circumstances (stack leak), the field could have an improper value, leading
to a fail of the hypercall.
Set it to 0 ("no addressing restriction") to avoid that.
Patch tested by Sam Fourman and haad@.
This should fix the rare "failed allocating DMA memory" encountered
under NetBSD dom0. Will ask for a pull-up.
 1.9.46.5 11-Aug-2010  yamt sync with head.
 1.9.46.4 11-Mar-2010  yamt sync with head
 1.9.46.3 19-Aug-2009  yamt sync with head.
 1.9.46.2 04-May-2009  yamt sync with head.
 1.9.46.1 16-May-2008  yamt sync with head.
 1.9.44.2 17-Jun-2008  yamt sync with head.
 1.9.44.1 18-May-2008  yamt sync with head.
 1.9.42.3 17-Jan-2009  mjf Sync with HEAD.
 1.9.42.2 05-Jun-2008  mjf Sync with HEAD.

Also fix build.
 1.9.42.1 02-Jun-2008  mjf Sync with HEAD.
 1.10.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.11.12.1 21-Apr-2010  matt sync to netbsd-5
 1.11.8.3 19-Nov-2010  riz Pull up following revision(s) (requested by bouyer in ticket #1348):
sys/arch/x86/x86/bus_dma.c: revision 1.54
sys/arch/xen/x86/xen_bus_dma.c: revision 1.21
bus_dmamem_alloc() may not get a boundary smaller than size, but
it's perfectly valid for bus_dmamap_create() to do so (a contigous
transfers will then split in multiple segment).
Fix _xen_bus_dmamem_alloc_range() and _bus_dmamem_alloc_range() to
allow a boundary limit smaller than size:
- compute appropriate boundary for uvm_pglistalloc(), wich doesn't
accept boundary < size
- also take care of boundary when deciding to start a new segment.
While there, remove useless boundary argument to _xen_alloc_contig().
Fix the boundary-related issue of PR port-amd64/42980
 1.11.8.2 29-Mar-2010  snj Pull up following revision(s) (requested by jym in ticket #1334):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.20
Although Xen's documentation states that the address_bits field is not used
by XENMEM_decrease_reservation, it is checked by the hypervisor. In certain
circumstances (stack leak), the field could have an improper value, leading
to a fail of the hypercall.
Set it to 0 ("no addressing restriction") to avoid that.
Patch tested by Sam Fourman and haad@.
This should fix the rare "failed allocating DMA memory" encountered
under NetBSD dom0.
 1.11.8.1 30-Jan-2010  snj Pull up following revision(s) (requested by bouyer in ticket #1271):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.16
sys/arch/xen/xen/xengnt.c: revision 1.17 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.33
fix address overflow with 32bit PAE.
Reported and tested by Mark Davies on port-xen@.
 1.11.6.2 03-Mar-2009  skrll Sync with HEAD.
 1.11.6.1 19-Jan-2009  skrll Sync with HEAD.
 1.11.4.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.14.2.6 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.14.2.5 10-Jan-2011  jym Sync with HEAD
 1.14.2.4 24-Oct-2010  jym Sync with HEAD
 1.14.2.3 01-Nov-2009  jym Sync with HEAD.
 1.14.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.14.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.16.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.20.2.2 05-Mar-2011  rmind sync with head
 1.20.2.1 30-May-2010  rmind sync with head
 1.22.6.2 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.22.6.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.23.2.1 30-Oct-2012  yamt sync with head
 1.26.38.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.26.38.1 25-Jun-2018  pgoyette Sync with HEAD
 1.27.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.27.2.1 10-Jun-2019  christos Sync with HEAD
 1.28.10.2 20-Apr-2020  bouyer We need xenbus_bus_dma_tag for PVHVM too, but without phys->machine translation
 1.28.10.1 20-Apr-2020  bouyer Sync with HEAD
 1.1 22-Jul-2023  mrg xen: declare 'default_consinfo' as extern in a header

this makes pvh_consinit.c actually compile with CONS_OVERRIDE set.
i didn't see any good header to add to (bootinfo.h and cpu.h both
seem to be poor choices but were considered), hence the new one
with just this definition.
 1.31 25-Feb-2023  riastradh xen_intr.c: Use kpreempt_disable/enable around access to curcpu().

curcpu() is not otherwise guaranteed to be stable at these points.

While here, nix nonsensical membars. This need only be synchronized
with interrupts on the same CPU.

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

XXX pullup-8 (in __sti/__cli, __save/restore_flags in include/xen.h)
XXX pullup-9
XXX pullup-10
 1.30 24-May-2022  bouyer branches: 1.30.4;
- msipic_construct_msix_pic(): set mp_table_base to memaddr (without
table_offset), this is what Xen wants
while there use pci_conf_write16() in msi_set_msictl_enablebit() too,
for consistency (it seems that Xen accepts the 32bit write at this point,
but this may change).

- xen_map_msix_pirq(): don't forget to set map_irq.table_base in the
MSI-X case, otherwise Xen maps it as MSI
- call pic_hwunmask() after pirq_establish() in msi/msix case, to make sure
the msi-x vector is unmasked.

Now MSI-X works with Xen so stop disabling it in pci_attach_hook().
 1.29 09-Aug-2021  andvar s/alway /always/
 1.28 01-Aug-2020  jdolecek adjust includes to pull __HAVE_PCI_MSI_MSIX properly
 1.27 07-May-2020  bouyer Change event_set_handler() to take the target CPU parameter. If ci is NULL,
event_set_handler() will choose the CPU and bind the event.
If ci is not NULL the caller is responsible for binding the event.
Use a IPI xcall to register the handlers if needed.
pull in a hack from x86 to force pirq handlers to be mpsafe if registered at
a level != IPL_VM. This is for the com at isa interrupt handler, which
registers at IPL_HIGH and has to way to tell it's mpsafe (taking
KERNEL_LOCK at IPL_HIGH causes deadlocks on MP systems).
 1.26 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.25 04-May-2020  jdolecek add support for using MSI for XenPV Dom0

use PHYSDEVOP_map_pirq to get the pirq/gsi for MSI/MSI-X, switch also INTx
to use it instead of PHYSDEVOP_alloc_irq_vector

MSI confirmed working with single-vector MSI for wm(4), ahcisata(4), bge(4)

XXX added some provision for MSI-X, but it doesn't actually work (no interrupts
delivered), needs some further investigation; disable MSI-X for XENPV
via flag in x86/pci/pci_machdep.c
 1.24 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.23 21-Apr-2020  jdolecek adjust so that this at least compiles and links with __HAVE_PCI_MSI_MSIX
 1.22 13-Apr-2020  bouyer By default, events are bound to CPU 0 (exept for IPIs and VTIMERs which
are bound to a different CPU at creation time).
Recent MI changes caused the scheduler to choose a different CPU when
probing and attaching xennet devices (I guess it's the xenbus thread which
runs on a different CPU). This cause the callback to be called on a different
CPU than the one expected by the kernel, and the event is ignored.
It is handled when the clock causes the callback to be called on the right
CPU, which is why xennet still run, but slowly.

Change event_set_handler() to do a EVTCHNOP_bind_vcpu if requested to,
and make sure we don't do it for IPIs and VIRQs (for theses, the op fails).
 1.21 06-Apr-2020  jdolecek branches: 1.21.2;
add known_mpsafe parameter also to pirq_establish(), and pass the parameter
to underlying event_set_handler()
 1.20 06-Apr-2020  jdolecek remove restriction on interrupt level for MP-safe interrupt handlers
 1.19 03-Apr-2020  ad Attach xen IPI event counters.
 1.18 23-Dec-2019  thorpej Provide XEN stubs for intr_mask() / intr_unmask().
 1.17 07-Jun-2019  cherry branches: 1.17.2;
Fix build for the XEN3_PVHVM kernel by conditoinally compiling
redundant functions x86_enable_intr()/x86_disable_intr()
 1.16 09-May-2019  bouyer sti/cli are not allowed on Xen, we have to clear/set a bit in the
shared page. Revert x86_disable_intr/x86_enable_intr to plain function
calls on XENPV.
While there, clean up unused functions and macros, and change cli()/sti()
macros to x86_disable_intr/x86_enable_intr.
Makes Xen domU boot again
(http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/)
 1.15 14-Feb-2019  cherry Welcome XENPVHVM mode.

It is UP only, has xbd(4) and xennet(4) as PV drivers.

The console is com0 at isa and the native portion is very
rudimentary AT architecture, so is probably suboptimal to
run without PV support.
 1.14 12-Feb-2019  cherry Move xen event related code which interfaces with the NetBSD interrupt
subsystem into a separate namespace where it can co-exist with the
native equivalent in PVHVM mode.

On PV, we alias and export the native symbols - this means that
although the namespace is different, the semantics must be identical.

Eg: xen_intr_establish_xname() vs. intr_establish_xname().

The specific functions we need in PVHVM are:

- spllower, xen_spllower (for native as well as XEN event spl
despatch/defer)
- xen_disable_intr()/xen_enable_intr() ,
x86_disable_intr()/x86_enable_intr()
- xen_read_psl()/xen_write_psl(),
x86_read_psl()/x86_write_psl()
- intr_establish() et. al, xen_intr_establish() et. al.

This gives us the ability to manage Paravirtualised drivers such as
xbd(4) as well as fully emulated ones such as wd(4)., for eg
 1.13 26-Dec-2018  cherry Xen can use the native splraise(9) functions.

There is no need for a slower C version.
 1.12 25-Dec-2018  cherry fix i386 build - missed sources migration in previous commit.

allow xen_intr.c to build by bringing in static support functions for
-D INTRSTACKSIZE

This should fix the i386 build now.
 1.11 25-Dec-2018  cherry Excise XEN specific code out of x86/x86/intr.c into xen/x86/xen_intr.c

While at it, separate the source function tracking so that the interrupt
paths are truly independant.

Use weak symbol exporting to provision for future PVHVM co-existence
of both files, but with independant paths. Introduce assembler code
such that in a unified scenario, native interrupts get first priority
in spllower(), followed by XEN event callbacks. IPL management and
semantics are unchanged - native handlers and xen callbacks are
expected to maintain their ipl related semantics.

In summary, after this commit, native and XEN now have completely
unrelated interrupt handling mechanisms, including
intr_establish_xname() and assembler stubs and intr handler
management.

Happy Christmas!
 1.10 24-Dec-2018  cherry 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.9 16-Jan-2009  jym branches: 1.9.58; 1.9.64; 1.9.66;
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.8 01-Jul-2008  bouyer branches: 1.8.4;
spllower(): return immediatly if ci->ci_ilevel <= nlevel, as the asm
versions do.
 1.7 25-May-2008  bouyer branches: 1.7.2;
Add a KASSERT(): Xspllower() will reenable interrupts, so make sure it's
not wrong to do so.
 1.6 24-May-2008  bouyer G/C dead code: remove now-unused softintr-related code.
 1.5 28-Apr-2008  martin branches: 1.5.2;
Remove clause 3 and 4 from TNF licenses
 1.4 21-Apr-2008  cegger branches: 1.4.2;
Access Xen's vcpu info structure per-CPU.
Tested on i386 and amd64 (both dom0 and domU) by me.
Xen2 tested (both dom0 and domU) by bouyer.
OK bouyer
 1.3 14-Apr-2008  cegger branches: 1.3.2;
- use POSIX integer types
- ansify functions
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.8; 1.2.16; 1.2.22;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file xen_intr.c was initially added on branch bouyer-xenamd64.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.22.3 17-Jan-2009  mjf Sync with HEAD.
 1.2.22.2 02-Jul-2008  mjf Sync with HEAD.
 1.2.22.1 02-Jun-2008  mjf Sync with HEAD.
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file xen_intr.c was added on branch matt-armv6 on 2008-01-09 01:50:16 +0000
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file xen_intr.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:18 +0000
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file xen_intr.c was added on branch vmlocking on 2007-12-03 19:04:44 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file xen_intr.c was added on branch jmcneill-pm on 2007-11-27 19:36:22 +0000
 1.3.2.2 04-Jun-2008  yamt sync with head
 1.3.2.1 18-May-2008  yamt sync with head.
 1.4.2.2 04-May-2009  yamt sync with head.
 1.4.2.1 16-May-2008  yamt sync with head.
 1.5.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.5.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.7.2.1 03-Jul-2008  simonb Sync with head.
 1.8.4.1 19-Jan-2009  skrll Sync with HEAD.
 1.9.66.4 21-Apr-2020  martin Sync with HEAD
 1.9.66.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.9.66.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.9.66.1 10-Jun-2019  christos Sync with HEAD
 1.9.64.2 18-Jan-2019  pgoyette Synch with HEAD
 1.9.64.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.9.58.1 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1862):

sys/arch/xen/x86/xen_intr.c: revision 1.31 (patch)
sys/arch/xen/include/xen.h (apply patch)

xen_intr.c: Use kpreempt_disable/enable around access to curcpu().

curcpu() is not otherwise guaranteed to be stable at these points.

While here, nix nonsensical membars. This need only be synchronized
with interrupts on the same CPU.

Proposed on port-xen:
https://mail-index.netbsd.org/port-xen/2022/07/13/msg010250.html
 1.17.2.1 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #1679):

sys/arch/xen/x86/xen_intr.c: revision 1.31

xen_intr.c: Use kpreempt_disable/enable around access to curcpu().

curcpu() is not otherwise guaranteed to be stable at these points.

While here, nix nonsensical membars. This need only be synchronized
with interrupts on the same CPU.

Proposed on port-xen:
https://mail-index.netbsd.org/port-xen/2022/07/13/msg010250.html
 1.21.2.10 25-Apr-2020  bouyer sync with bouyer-xenpvh-base2 (HEAD)
 1.21.2.9 20-Apr-2020  bouyer channel %d -> chan %d, for the benefit of 'systat vm'
 1.21.2.8 20-Apr-2020  bouyer Sync with HEAD
 1.21.2.7 19-Apr-2020  bouyer Add per-PIC callbacks for interrupt_get_devname(), interrupt_get_assigned()
and interrupt_get_count(). Implement Xen-specific callbacks for
PIC_XEN and use the x86 one for others.
In event_set_handler(), call intr_allocate_io_intrsource() so that
events appears in interrupt list (intrctl list).
 1.21.2.6 19-Apr-2020  bouyer Add a struct pic * member to struct intrhand.
This will be used for interrupt_get_count()
For Xen remplace pic_type with a pointer to the pic, and add a pointer
to intrhand, in struct pintrhand
Make event_set_handler return the pointer to struct intrhand.
Don't allocate a fake intrhand in xen_intr_establish_xname(), use the
one returned by event_set_handler().
 1.21.2.5 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.21.2.4 14-Apr-2020  bouyer Remove spllower alias, xen_spllower is gone
 1.21.2.3 12-Apr-2020  bouyer We need to call x86_init_preempt() for all CPUs now.
 1.21.2.2 12-Apr-2020  bouyer Get rid of xen-specific ci_x* interrupt handling:
- use the general SIR mechanism, reserving 3 more slots for IPL_VM, IPL_SCHED
and IPL_HIGH
- remove specific handling from C sources, or change to ipending
- convert IPL number to SIR number in various places
- Remove XUNMASK/XPENDING in assembly or change to IUNMASK/IPENDING
- remove Xen-specific ci_xsources, ci_xmask, ci_xunmask, ci_xpending from
struct cpu_info
- for now remove a KASSERT that there are no pending interrupts in
idle_block(). We can get there with some software interrupts pending
in autoconf XXX needs to be looked at.
 1.21.2.1 11-Apr-2020  bouyer Include ci_isources[] for XenPV too.
Adjust spllower() to XenPV needs, and switch XenPV to the native spllower().
Remove xen_spllower().
 1.30.4.1 31-Jul-2023  martin Pull up following revision(s) (requested by riastradh in ticket #267):

sys/arch/xen/x86/xen_intr.c: revision 1.31

xen_intr.c: Use kpreempt_disable/enable around access to curcpu().

curcpu() is not otherwise guaranteed to be stable at these points.

While here, nix nonsensical membars. This need only be synchronized
with interrupts on the same CPU.

Proposed on port-xen:
https://mail-index.netbsd.org/port-xen/2022/07/13/msg010250.html
 1.42 06-Nov-2023  rin xen_ipi: valid_ipimask: Sprinkle __diagused to fix clang !DIAGNOSTIC build
 1.41 06-Aug-2023  riastradh xen/x86: Get the right intrframe pointer in ddb ipi.

This was broken with the transition from evtchn_set_handler to
intr_establish_xname in 2017, remained broken with the transition
from intr_establish_xname to xen_intr_establish_xname in 2018, and
still remained broken when xen_intr_establish_xname was changed back
to evtchn_set_handler in 2020.

The mechanism is grody -- instead of a secret second argument to the
interrupt handler, the intrframe pointer should be replaced by a
struct cpu_info member that is saved and restored by the interrupt
handler calling logic. But we should make sure the replacement
actually works first -- which is not trivial in part because the
users are hidden behind sketchy function pointer casts.

With any luck, this will make `mach cpu N' work in ddb on Xen.

XXX pullup-10
XXX pullup-9 (by patch)
 1.40 05-Jan-2022  christos branches: 1.40.4;
remove DIAGNOSTIC so that function is defined for KASSERTMSG. Hope that the
compiler removes it.
 1.39 07-May-2020  bouyer Change event_set_handler() to take the target CPU parameter. If ci is NULL,
event_set_handler() will choose the CPU and bind the event.
If ci is not NULL the caller is responsible for binding the event.
Use a IPI xcall to register the handlers if needed.
pull in a hack from x86 to force pirq handlers to be mpsafe if registered at
a level != IPL_VM. This is for the com at isa interrupt handler, which
registers at IPL_HIGH and has to way to tell it's mpsafe (taking
KERNEL_LOCK at IPL_HIGH causes deadlocks on MP systems).
 1.38 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.37 21-Apr-2020  ad Remove spurious reference to XEN_IPI_KICK - it represents the absence of
a specific IPI type.
 1.36 13-Apr-2020  bouyer By default, events are bound to CPU 0 (exept for IPIs and VTIMERs which
are bound to a different CPU at creation time).
Recent MI changes caused the scheduler to choose a different CPU when
probing and attaching xennet devices (I guess it's the xenbus thread which
runs on a different CPU). This cause the callback to be called on a different
CPU than the one expected by the kernel, and the event is ignored.
It is handled when the clock causes the callback to be called on the right
CPU, which is why xennet still run, but slowly.

Change event_set_handler() to do a EVTCHNOP_bind_vcpu if requested to,
and make sure we don't do it for IPIs and VIRQs (for theses, the op fails).
 1.35 01-Dec-2019  ad branches: 1.35.6;
Fix false sharing problems with cpu_info. Identified with tprof(8).
This was a very nice win in my tests on a 48 CPU box.

- Reorganise cpu_data slightly according to usage.
- Put cpu_onproc into struct cpu_info alongside ci_curlwp (now is ci_onproc).
- On x86, put some items in their own cache lines according to usage, like
the IPI bitmask and ci_want_resched.
 1.34 23-Nov-2019  ad cpu_need_resched():

- Remove all code that should be MI, leaving the bare minimum under arch/.
- Make the required actions very explicit.
- Pass in LWP pointer for convenience.
- When a trap is required on another CPU, have the IPI set it locally.
- Expunge cpu_did_resched().
 1.33 12-Oct-2019  maxv Rewrite the FPU code on x86. This greatly simplifies the logic and removes
the dependency on IPL_HIGH. NVMM is updated accordingly. Posted on
port-amd64 a week ago.

Bump the kernel version to 9.99.16.
 1.32 02-Feb-2019  cherry branches: 1.32.4;
Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.31 27-Jan-2019  dholland fix duplicated chunk from merge
 1.30 27-Jan-2019  pgoyette Merge the [pgoyette-compat] branch
 1.29 24-Dec-2018  cherry 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.28 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.27 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.26 24-Jul-2018  bouyer Fix what looks like a typo in xen_send_ipi():
ci != NULL || ci != curcpu()
is always true
 1.25 24-Jun-2018  jdolecek branches: 1.25.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.24 23-Jun-2018  jdolecek make compile without DDB

PR port-xen/50282
 1.23 06-Nov-2017  cherry branches: 1.23.2;
Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.22 15-Aug-2017  maxv Remove unused arg, to have the same definition as amd64.
 1.21 12-Aug-2017  maxv Remove vm86.

Pass 3.
 1.20 07-Jul-2016  msaitoh branches: 1.20.10;
KNF. Remove extra spaces. No functional change.
 1.19 07-Feb-2015  joerg valid_ipimask is only used under DIAGNOSTIC, so only define it then.
 1.18 19-May-2014  rmind branches: 1.18.2; 1.18.4;
Implement MI IPI interface with cross-call support.
 1.17 12-Feb-2014  dsl branches: 1.17.2;
Change i386 to use x86/fpu.c instead of i386/isa/npx.c
This changes the trap10 and trap13 code to call directly into fpu.c,
removing all the code for T_ARITHTRAP, T_XMM and T_FPUNDA from i386/trap.c
Not all of the code thate appeared to handle fpu traps was ever called!
Most of the changes just replace the include of machine/npx.h with x86/fpu.h
(or remove it entirely).
 1.16 11-Feb-2014  dsl Move sys/arch/amd64/amd64/fpu.c and sys/arch/amd64/include/fpu.h
into sys/arch/x86 in preparation for using the same code for i386.
 1.15 26-Jan-2014  dsl Remove support for 'external' floating point units and the MS-DOS
compatible method of handling floating point exceptions.
Make kernel support for teh fpu non-optional (486SX should still work).
Only 386 cpus support external fpu, and i386 support was removed years ago.
This means that the npx code no longer uses port 0xf0 or interupt 13.
All the "npx at isa" lines go from the configs, arch/i386/isa/npx.c
is now mandatory for all i386 kernels.
I've renamed npxinit() to fpuinit() and npxinit_cpu() to fpuinit_cpu()
to match the very similar amd64 functions.
The fpu of the boot cpu is now initialised by a direct call from
cpu_configure(), this enables FP emulation for a 486SX.
(for amd64 the cr0 values are set in locore.S and similar).
This fixes a long-standing bug in linux_setregs() - which did not
save the fpu regsiters if they were active.
I've test booted a single cpu i386 kernel (using anita).
amd64 builds - none of teh changes should affect it.
The i386 XEN kernels build, but I'm not sure where they set cr0, and
it might have got lost!
 1.14 01-Dec-2013  christos revert fpu/pcu changes until we figure out what's wrong; they cause random
freezes
 1.13 23-Oct-2013  drochner Use the MI "pcu" framework for bookkeeping of npx/fpu states on x86.
This reduces the amount of MD code enormously, and makes it easier
to implement support for newer CPU features which require more fpu
state, or for fpu usage by the kernel.
For access to FPU state across CPUs, an xcall kthread is used now
rather than a dedicated IPI.
No user visible changes intended.
 1.12 14-Sep-2013  joerg GC max_cpus.
 1.11 27-Dec-2012  cherry branches: 1.11.2;
Remove unused header evtchn.h from intr.h
 1.10 17-Feb-2012  bouyer branches: 1.10.2;
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.

2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.

To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.

to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.

While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
 1.9 30-Dec-2011  cherry branches: 1.9.2;
Remove spurious (debug) printf()
 1.8 28-Dec-2011  cherry Remove temporary variable definition that is unused in non DIAGNOSTIC builds.
 1.7 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.6 07-Nov-2011  cherry branches: 1.6.4;
Add an ipi callback to force hypervisor callback. this is useful to "re-route" interrupts to a given vcpu
 1.5 27-Sep-2011  jym branches: 1.5.2;
Modify *ASSERTMSG() so they are now used as variadic macros. The main goal
is to provide routines that do as KASSERT(9) says: append a message
to the panic format string when the assertion triggers, with optional
arguments.

Fix call sites to reflect the new definition.

Discussed on tech-kern@. See
http://mail-index.netbsd.org/tech-kern/2011/09/07/msg011427.html
 1.4 15-Aug-2011  cherry branches: 1.4.2;
invert buggy ci_flag test
 1.3 10-Aug-2011  cherry KNF police (rmind@ :-)
 1.2 10-Aug-2011  cherry xen ipi infrastructure
 1.1 03-Jun-2011  cherry branches: 1.1.2;
file xen_ipi.c was initially added on branch cherry-xenmp.
 1.1.2.4 18-Sep-2011  cherry Use an IPI to re-route events to the cpu where the handler has been registered
 1.1.2.3 17-Aug-2011  cherry Pullup relevant changes from -current
 1.1.2.2 31-Jul-2011  cherry grow MP support for i386. boots to single user
 1.1.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.4.2.2 27-Aug-2011  jym Add/remove files, like in HEAD.
 1.4.2.1 15-Aug-2011  jym file xen_ipi.c was added on branch jym-xensuspend on 2011-08-27 15:59:49 +0000
 1.5.2.4 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.5.2.3 23-Jan-2013  yamt sync with head
 1.5.2.2 17-Apr-2012  yamt sync with head
 1.5.2.1 10-Nov-2011  yamt sync with head
 1.6.4.1 18-Feb-2012  mrg merge to -current.
 1.9.2.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #29):
sys/arch/xen/x86/x86_xpmap.c: revision 1.39
sys/arch/xen/include/hypervisor.h: revision 1.37
sys/arch/xen/include/intr.h: revision 1.34
sys/arch/xen/x86/xen_ipi.c: revision 1.10
sys/arch/x86/x86/cpu.c: revision 1.97
sys/arch/x86/include/cpu.h: revision 1.48
sys/uvm/uvm_map.c: revision 1.315
sys/arch/x86/x86/pmap.c: revision 1.165
sys/arch/xen/x86/cpu.c: revision 1.81
sys/arch/x86/x86/pmap.c: revision 1.167
sys/arch/xen/x86/cpu.c: revision 1.82
sys/arch/x86/x86/pmap.c: revision 1.168
sys/arch/xen/x86/xen_pmap.c: revision 1.17
sys/uvm/uvm_km.c: revision 1.122
sys/uvm/uvm_kmguard.c: revision 1.10
sys/arch/x86/include/pmap.h: revision 1.50
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.
2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.
To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.
to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.
While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
When using uvm_km_pgremove_intrsafe() make sure mappings are removed
before returning the pages to the free pool. Otherwise, under Xen,
a page which still has a writable mapping could be allocated for
a PDP by another CPU and the hypervisor would refuse it (this is
PR port-xen/45975).
For this, move the pmap_kremove() calls inside uvm_km_pgremove_intrsafe(),
and do pmap_kremove()/uvm_pagefree() in batch of (at most) 16 entries
(as suggested by Chuck Silvers on tech-kern@, see also
http://mail-index.netbsd.org/tech-kern/2012/02/17/msg012727.html and
followups).
Avoid early use of xen_kpm_sync(); locks are not available at this time.
Don't call cpu_init() twice.
Makes LOCKDEBUG kernels boot again
Revert pmap_pte_flush() -> xpq_flush_queue() in previous.
 1.10.2.3 03-Dec-2017  jdolecek update from HEAD
 1.10.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.10.2.1 25-Feb-2013  tls resync with head
 1.11.2.1 18-May-2014  rmind sync with head
 1.17.2.1 10-Aug-2014  tls Rebase.
 1.18.4.2 28-Aug-2017  skrll Sync with HEAD
 1.18.4.1 06-Apr-2015  skrll Sync with HEAD
 1.18.2.1 08-Feb-2015  snj Pull up following revision(s) (requested by joerg in ticket #498):
sys/arch/xen/x86/xen_ipi.c: revision 1.19
valid_ipimask is only used under DIAGNOSTIC, so only define it then.
 1.20.10.1 30-Apr-2021  martin Pull up following revision(s) (requested by kre in ticket #1675):

sys/arch/xen/x86/xen_ipi.c: revision 1.24 (patch)

make compile without DDB
PR port-xen/50282
 1.23.2.4 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.23.2.3 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.23.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.23.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.25.2.4 21-Apr-2020  martin Sync with HEAD
 1.25.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.25.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.25.2.1 10-Jun-2019  christos Sync with HEAD
 1.32.4.1 10-Aug-2023  sborrill Pull up the following revisions(s) (requested by riastradh in ticket #1713):
sys/arch/xen/x86/xen_ipi.c: revision 1.41 via patch

xen/x86: Fix 'mach cpu N' in ddb by passing the right pointer to a
struct intrframe to IPI handlers.
 1.35.6.7 25-Apr-2020  bouyer sync with bouyer-xenpvh-base2 (HEAD)
 1.35.6.6 20-Apr-2020  bouyer Misc fixes after merge
 1.35.6.5 20-Apr-2020  bouyer Sync with HEAD
 1.35.6.4 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.35.6.3 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.35.6.2 12-Apr-2020  bouyer Add XEN_IPI_KPREEMPT to list of valid IPIs
 1.35.6.1 11-Apr-2020  bouyer Move softint and preemtion-related functions out of x86/x86/intr.c to
its own file, x86/x86/x86_softintr.c
Add x86/x86/x86_softintr.c for native and XenPV
Make sure XenPV also check ci_ioending, which is used for softints.
Switch XenPV to fast softints and allow kernel preemption.
kpreempt_disable() before calling pmap_changeprot_local()
run xen_wallclock_time() and xen_global_systime_ns() at splshed() to
avoid being interrupted.

XXX amd64 lock stubs are racy for XPENDING
 1.40.4.2 14-Dec-2023  martin Pull up following revision(s) (requested by rin in ticket #494):

sys/arch/xen/x86/xen_ipi.c: revision 1.42
sys/dev/nvmm/x86/nvmm_x86_vmx.c: revision 1.86

xen_ipi: valid_ipimask: Sprinkle __diagused to fix clang !DIAGNOSTIC build

nvmm_x86_vmx: vmx_vmptrst: Sprinkle __diagused to fix clang !DIAGNOSTIC build
 1.40.4.1 10-Aug-2023  sborrill Pull up the following revisions(s) (requested by riastradh in ticket #318):
sys/arch/xen/x86/xen_ipi.c: revision 1.41

xen/x86: Fix 'mach cpu N' in ddb by passing the right pointer to a
struct intrframe to IPI handlers.
 1.10 07-Aug-2021  thorpej Merge thorpej-cfargs2.
 1.9 24-Apr-2021  thorpej branches: 1.9.8;
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.8 02-May-2020  bouyer branches: 1.8.4;
Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.7 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.6 14-Feb-2019  cherry branches: 1.6.4; 1.6.12;
Welcome XENPVHVM mode.

It is UP only, has xbd(4) and xennet(4) as PV drivers.

The console is com0 at isa and the native portion is very
rudimentary AT architecture, so is probably suboptimal to
run without PV support.
 1.5 13-Feb-2019  cherry Conditionally compile a conditionally used variable.
 1.4 22-Dec-2018  maxv branches: 1.4.2;
Style, once again.
 1.3 22-Dec-2018  cherry This change modifies the mainbus(4) entry point for all x86 sub-archs
in the following way:

i) It provides a unified entry point in
x86/x86/mainbus.c:mainbus_attach()
ii) It carves out the preliminary bus attachment sequence that is
common to all sub-archs into
x86/x86/mainbus.c: x86_cpubus_attach()
iii) It consolidates the remaining pathways as internal callee
functions so that these may be called piecemeal if required. A
special usecase of this is XEN PVHVM which may need to call the
native configure path, the xen configure path, or both.
iv) It moves the driver private data structures from
i386/i386_mainbus.c to an x86/ level one. This allows for other
sub-arch's to do similar, if needed. (They do not at the moment).
v) For dom0 kernels, it enables 'acpi0 at mainbus?' and
'acpi0 at hypervisorbus'. This serves two purposes:
a) To demonstrate the possibility of dynamic configuration tree
traversal ordering changes.
b) To allow for the common acpi_check(self, "acpibus") call in
x86/mainbus.c to not barf when it is called from the dom0 attach
path. We allow for the acpi0 device to be a child of mainbus with
the changes to amd64/conf/XEN3_DOM0 and i386/conf/XEN3PAE_DOM0
without actually probing further in the code. This path will later
be pursued in a PVHVM boot codepath.

There should be no operative changes with this change. If there are,
please complain loudly.
 1.2 22-Dec-2018  cherry Don't forget pedigree. Re-introduce old RCS Id tags from the originals
 1.1 22-Dec-2018  cherry Move mainbus(4) driver files in various x86 sub-archs to name prefixed
versions. This allows us to further modularise them by unifying common
bus probe code in x86/x86/mainbus.c to be introduced next.

This commit has no functional changes. It is done for ease of
visibility of newer diffs in the queue.
 1.4.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.4.2.1 22-Dec-2018  pgoyette file xen_mainbus.c was added on branch pgoyette-compat on 2018-12-26 14:01:46 +0000
 1.6.12.3 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.6.12.2 16-Apr-2020  bouyer Don't try to attach hypervisor on non Xen; no more error messages about
hypervisor when booting on bare metal.
 1.6.12.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.6.4.2 10-Jun-2019  christos Sync with HEAD
 1.6.4.1 14-Feb-2019  christos file xen_mainbus.c was added on branch phil-wifi on 2019-06-10 22:06:56 +0000
 1.8.4.1 02-Apr-2021  thorpej config_found_ia() -> config_found() w/ CFARG_IATTR.
 1.9.8.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.41 25-Aug-2023  riastradh xen: Provide definitions or ifdefs to make drm build in XEN3_DOM0.

No idea if it works, but it builds now.

PR port-xen/49330
 1.40 20-Aug-2022  riastradh x86: Split most of pmap.h into pmap_private.h or vmparam.h.

This way pmap.h only contains the MD definition of the MI pmap(9)
API, which loads of things in the kernel rely on, so changing x86
pmap internals no longer requires recompiling the entire kernel every
time.

Callers needing these internals must now use machine/pmap_private.h.
Note: This is not x86/pmap_private.h because it contains three parts:

1. CPU-specific (different for i386/amd64) definitions used by...

2. common definitions, including Xenisms like xpmap_ptetomach,
further used by...

3. more CPU-specific inlines for pmap_pte_* operations

So {amd64,i386}/pmap_private.h defines 1, includes x86/pmap_private.h
for 2, and then defines 3. Maybe we should split that out into a new
pmap_pte.h to reduce this trouble.

No functional change intended, other than that some .c files must
include machine/pmap_private.h when previously uvm/uvm_pmap.h
polluted the namespace with pmap internals.

Note: This migrates part of i386/pmap.h into i386/vmparam.h --
specifically the parts that are needed for several constants defined
in vmparam.h:

VM_MAXUSER_ADDRESS
VM_MAX_ADDRESS
VM_MAX_KERNEL_ADDRESS
VM_MIN_KERNEL_ADDRESS

Since i386 needs PDP_SIZE in vmparam.h, I added it there on amd64
too, just to keep things parallel.
 1.39 06-Sep-2020  riastradh Fix fallout from previous uvm.h cleanup.

- pmap(9) needs uvm/uvm_extern.h.

- x86/pmap.h is not usable on its own; it is only usable if included
via uvm/uvm_extern.h (-> uvm/uvm_pmap.h -> machine/pmap.h).

- Make nvmm.h and nvmm_internal.h standalone.
 1.38 19-Jul-2020  maxv don't include opt_user_ldt.h when it is not needed
 1.37 07-Jan-2020  ad Correction to previous.
 1.36 07-Jan-2020  ad pmap_extract_ma(): don't need to take pm_lock for pmap_kernel().
 1.35 04-Jan-2020  ad x86 pmap improvements, reducing system time during a build by about 15% on
my test machine:

- Replace the global pv_hash with a per-pmap record of dynamically allocated
pv entries. The data structure used for this can be changed easily, and
has no special concurrency requirements. For now go with radixtree.

- Change pmap_pdp_cache back into a pool; cache the page directory with the
pmap, and avoid contention on pmaps_lock by adjusting the global list in
the pool_cache ctor & dtor. Align struct pmap and its lock, and update
some comments.

- Simplify pv_entry lists slightly. Allow both PP_EMBEDDED and dynamically
allocated entries to co-exist on a single page. This adds a pointer to
struct vm_page on x86, but shrinks pv_entry to 32 bytes (which also gets
it nicely aligned).

- More elegantly solve the chicken-and-egg problem introduced into the pmap
with radixtree lookup for pages, where we need PTEs mapped and page
allocations to happen under a single hold of the pmap's lock. While here
undo some cut-n-paste.

- Don't adjust pmap_kernel's stats with atomics, because its mutex is now
held in the places the stats are changed.
 1.34 15-Dec-2019  ad uvm_pagerealloc() can now block because of radixtree manipulation, so defer
freeing PTPs until pmap_unmap_ptes(), where we still have the pmap locked
but can finally tolerate context switches again.

To be revisited soon: pmap_map_ptes() seems broken WRT other pmap load.

Reported-by: syzbot+689fb7dab41abff8e75a@syzkaller.appspotmail.com
Reported-by: syzbot+3e7bbf37d37d451b25d7@syzkaller.appspotmail.com
Reported-by: syzbot+689fb7dab41abff8e75a@syzkaller.appspotmail.com
Reported-by: syzbot+689fb7dab41abff8e75a@syzkaller.appspotmail.com
Reported-by: syzbot+3e7bbf37d37d451b25d7@syzkaller.appspotmail.com
 1.33 08-Dec-2019  ad Merge x86 pmap changes from yamt-pagecache:

- Deal better with the multi-level pmap object locking kludge.
- Handle uvm_pagealloc() being able to block.
 1.32 30-Oct-2019  maxv Switch to new PTE bits.
 1.31 10-Mar-2019  maxv Two changes:

* Allow large pages to be passed in pmap_pdes_valid, this happens under
DDB when it reads RIP (.text), called via pmap_extract.

* Invert a branch in pmap_extract, so that 'l_cpu' is not touched if we're
dealing with the kernel pmap.

This fixes 'boot -d'.
 1.30 09-Mar-2019  maxv Start replacing the x86 PTE bits.
 1.29 07-Mar-2019  maxv Drop PG_RO, PG_KR and PG_PROT, they are useless and create confusion.
 1.28 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.27 26-Jul-2018  maxv Remove the non-PAE-i386 code of Xen. The branches are reordered so that
__x86_64__ comes first, eg:

#if defined(PAE)
/* i386+PAE */
#elif defined(__x86_64__)
/* amd64 */
#else
/* i386 */
#endif

becomes

#ifdef __x86_64__
/* amd64 */
#else
/* i386+PAE */
#endif

Tested on i386pae-domU and amd64-dom0.
 1.26 23-Mar-2017  maxv branches: 1.26.12; 1.26.14;
Remove PG_k completely.
 1.25 26-Dec-2016  cherry branches: 1.25.2;
In the MP case,
do not attempt to pmap_tlb_shootdown() after a pmap_kenter_ma() during
boot. pmap_tlb_shootdown() assumes post boot. Instead invalidate the
entry on the local CPU only.

XXX: to DTRT, probably this assumption needs re-examination.
XXX: The tradeoff is a (predicted) single word size comparison
penalty, so perhaps a decision needs performance stats.
 1.24 13-Dec-2016  kamil Torn down KSTACK_CHECK_DR0, i386-only feature to detect stack overflow

This feature was intended to detect stack overflow with CPU Debug Registers
(x86). It was never ported to other ports, neither amd64 and should be
adapted for SMP...

Currently there might be better ways to detect stack overflows like page
mapping protection. Since the number of Debug Registers is restricted
(4 on x86), torn it down completely.

This interface introduced helper functions for Debug Registers, they will
be replaced with the new <x86/dbregs.h> interface.

KSTACK_CHECK_DR0 was disabled by default and won't affect ordinary users.

Sponsored by <The NetBSD Foundation>
 1.23 21-Nov-2016  ozaki-r Sweep unnecessary xcall.h inclusions
 1.22 24-Jun-2012  jym branches: 1.22.2; 1.22.14; 1.22.16; 1.22.20;
Enable the map/unmap recursive mapping functions for all Xen ports for
save/restore.

For an unknown reason (to me) Xen refuses to update VM translations
when the entry is pointing back to itself (which is precisely
what our recursive VM model does). So enable the functions that take
care of this, which will avoid all sort of memory corruption upon restore
leading domU to trample upon itself.

Save/restore works again for amd64. The occasional domU frontend corruption is
still present, but is harmless to dom0. Now we have a working shell and
ddb inside domU, that helps debugging a tiny bit.

XXX pull-up to -6.
 1.21 20-Apr-2012  rmind - Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.

- Support up to 256 CPUs on amd64 architecture by default.

Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
 1.20 11-Mar-2012  jym Split the map/unmap code from the sync/flush code: move xpq_flush_queue()
calls after pmap_{,un}map_recursive_entries() so that pmap's handlers
handle the flush themselves.

Now pmap_{,un}map_recursive_entries() do what their names imply, nothing more.

Fix pmap_xen_suspend()'s comment: APDPs are now gone.

pmap's handlers are called deep during kernel save/restore. We already
are at IPL_VM + kpreemption disabled. No need to wrap the xpq_flush_queue()
with splvm/splx.
 1.19 02-Mar-2012  bouyer Add some more KASSERT()
 1.18 24-Feb-2012  cherry (xen) - remove the (*xpq_cpu)() shim.We hasten the %fs/%gs setup process during boot.Although this is hacky, it lets us use the non-xen specificpmap_pte_xxx() functions in pmap code (and others).
 1.17 17-Feb-2012  bouyer Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.

2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.

To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.

to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.

While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
 1.16 28-Jan-2012  cherry branches: 1.16.2;
stop using alternate pde mapping in xen pmap
 1.15 22-Jan-2012  cherry Do not clobber pmap_kernel()'s pdir unnecessarily while syncing per-cpu pdirs
 1.14 19-Jan-2012  bouyer add a missing splvm()/splx() to protect the xpq queue.
 1.13 09-Jan-2012  cherry Harden cross-cpu L3 sync - avoid optimisations that may race.
Update ci->ci_kpm_pdir from user pmap, not global pmap_kernel() entry which may get clobbered by other CPUs.
XXX: Look into why we use pmap_kernel() userspace entries at all.
 1.12 30-Dec-2011  cherry per-cpu shadow directory pages should be updated locally via cross-calls. Do this.
 1.11 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.10 23-Nov-2011  jym branches: 1.10.2;
Move Xen-specific functions to Xen pmap. Requested by cherry@.

Un'ifdef XEN in xen_pmap.c, it is always defined there.
 1.9 20-Nov-2011  jym Expose pmap_pdp_cache publicly to x86/xen pmap. Provide suspend/resume
callbacks for Xen pmap.

Turn static internal callbacks of pmap_pdp_cache.

XXX the implementation of pool_cache_invalidate(9) is still wrong, and
IMHO this needs fixing before -6. See
http://mail-index.netbsd.org/tech-kern/2011/11/18/msg011924.html
 1.8 08-Nov-2011  cherry Expose the PG_k #define pt/pd bit to both xen and "baremetal" x86. This is required, since kernel pages are mapped with user permissions in XEN/amd64 since the VM kernel runs in ring3. Since XEN/i386(including PAE) runs in ring1, supervisor mode is appropriate for these ports. We need to share this since the pmap implementation is still shared. Once the xen implementation is sufficiently independant of the x86 one, this can be made private to xen/include/xenpmap.h
 1.7 06-Nov-2011  cherry [merging from cherry-xenmp] Make the xen MMU op queue locking api private. Implement per-cpu queues.
 1.6 18-Oct-2011  jym branches: 1.6.2;
Move Xen specific functions out of x86 native pmap to xen_pmap.c.

Provide a wrapper to trigger pmap pool_cache(9) invalidations without
exposing the caches to outside world.
 1.5 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.4 13-Aug-2011  cherry Add locking around ops to the hypervisor MMU "queue".
 1.3 12-Jun-2011  rmind Welcome to 5.99.53! Merge rmind-uvmplock branch:

- Reorganize locking in UVM and provide extra serialisation for pmap(9).
New lock order: [vmpage-owner-lock] -> pmap-lock.

- Simplify locking in some pmap(9) modules by removing P->V locking.

- Use lock object on vmobjlock (and thus vnode_t::v_interlock) to share
the locks amongst UVM objects where necessary (tmpfs, layerfs, unionfs).

- Rewrite and optimise x86 TLB shootdown code, make it simpler and cleaner.
Add TLBSTATS option for x86 to collect statistics about TLB shootdowns.

- Unify /dev/mem et al in MI code and provide required locking (removes
kernel-lock on some ports). Also, avoid cache-aliasing issues.

Thanks to Andrew Doran and Joerg Sonnenberger, as their initial patches
formed the core changes of this branch.
 1.2 01-Feb-2011  chuck branches: 1.2.2;
udpate license clauses on my code to match the new-style BSD licenses.
based on diff that rmind@ sent me.

no functional change with this commit.
 1.1 10-May-2010  dyoung branches: 1.1.2; 1.1.4; 1.1.6; 1.1.8; 1.1.10; 1.1.12;
Provide pmap_enter_ma(), pmap_extract_ma(), pmap_kenter_ma() in all x86
kernels, and use them in the bus_space(9) implementation instead of ugly
Xen #ifdef-age. In a non-Xen kernel, the _ma() functions either call or
alias the equivalent _pa() functions.

Reviewed on port-xen@netbsd.org and port-i386@netbsd.org. Passes
rmind@'s and bouyer@'s inspection. Tested on i386 and on Xen DOMU /
DOM0.
 1.1.12.1 08-Feb-2011  bouyer Sync with HEAD
 1.1.10.1 06-Jun-2011  jruoho Sync with HEAD.
 1.1.8.4 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.1.8.3 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.1.8.2 24-Oct-2010  jym Sync with HEAD
 1.1.8.1 10-May-2010  jym file xen_pmap.c was added on branch jym-xensuspend on 2010-10-24 22:48:22 +0000
 1.1.6.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.1.6.1 10-May-2010  uebayasi file xen_pmap.c was added on branch uebayasi-xip on 2010-08-17 06:45:35 +0000
 1.1.4.2 11-Aug-2010  yamt sync with head.
 1.1.4.1 10-May-2010  yamt file xen_pmap.c was added on branch yamt-nfs-mp on 2010-08-11 22:52:59 +0000
 1.1.2.4 05-Mar-2011  rmind sync with head
 1.1.2.3 31-May-2010  rmind - Split off Xen versions of pmap_map_ptes/pmap_unmap_ptes into Xen pmap,
also move pmap_apte_flush() with pmap_unmap_apdp() there.
- Make Xen buildable.
 1.1.2.2 30-May-2010  rmind sync with head
 1.1.2.1 10-May-2010  rmind file xen_pmap.c was added on branch rmind-uvmplock on 2010-05-30 05:17:14 +0000
 1.2.2.5 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.2.2.4 22-Aug-2011  cherry Remove spurious locks
 1.2.2.3 20-Aug-2011  cherry PAE MP support (preliminary), amd64 per-cpu L4 model redesigned, i386 pmap_pa_start/end fixup
 1.2.2.2 16-Jul-2011  cherry Introduce a per-cpu "shadow" for pmap_kernel()'s L4 page
 1.2.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.6.2.4 30-Oct-2012  yamt sync with head
 1.6.2.3 23-May-2012  yamt sync with head.
 1.6.2.2 17-Apr-2012  yamt sync with head
 1.6.2.1 10-Nov-2011  yamt sync with head
 1.10.2.6 29-Apr-2012  mrg sync to latest -current.
 1.10.2.5 05-Apr-2012  mrg sync to latest -current.
 1.10.2.4 06-Mar-2012  mrg sync to -current
 1.10.2.3 06-Mar-2012  mrg sync to -current
 1.10.2.2 04-Mar-2012  mrg sync to latest -current.
 1.10.2.1 18-Feb-2012  mrg merge to -current.
 1.16.2.3 02-Jul-2012  jdc Pull up revisions:
src/sys/arch/xen/include/xenpmap.h revision 1.35 via patch
src/sys/arch/xen/x86/xen_pmap.c revision 1.22 via patch
(requested by jym in ticket #372).

Enable the map/unmap recursive mapping functions for all Xen ports for
save/restore.

For an unknown reason (to me) Xen refuses to update VM translations
when the entry is pointing back to itself (which is precisely
what our recursive VM model does). So enable the functions that take
care of this, which will avoid all sort of memory corruption upon restore
leading domU to trample upon itself.

Save/restore works again for amd64. The occasional domU frontend
corruption is
still present, but is harmless to dom0. Now we have a working shell and
ddb inside domU, that helps debugging a tiny bit.

XXX pull-up to -6.
 1.16.2.2 09-May-2012  riz Pull up following revision(s) (requested by rmind in ticket #202):
sys/arch/x86/include/cpuvar.h: revision 1.46
sys/arch/xen/include/xenpmap.h: revision 1.34
sys/arch/i386/include/param.h: revision 1.77
sys/arch/x86/x86/pmap_tlb.c: revision 1.5
sys/arch/x86/x86/pmap_tlb.c: revision 1.6
sys/arch/i386/i386/genassym.cf: revision 1.92
sys/arch/xen/x86/cpu.c: revision 1.91
sys/arch/x86/x86/pmap.c: revision 1.177
sys/arch/xen/x86/xen_pmap.c: revision 1.21
sys/arch/x86/acpi/acpi_wakeup.c: revision 1.31
sys/kern/subr_kcpuset.c: revision 1.5
sys/arch/amd64/include/param.h: revision 1.18
sys/sys/kcpuset.h: revision 1.5
sys/arch/x86/x86/mtrr_i686.c: revision 1.26
sys/arch/x86/x86/mtrr_i686.c: revision 1.27
sys/arch/xen/x86/x86_xpmap.c: revision 1.43
sys/arch/x86/x86/cpu.c: revision 1.98
sys/arch/amd64/amd64/mptramp.S: revision 1.14
sys/kern/sys_sched.c: revision 1.42
sys/arch/amd64/amd64/genassym.cf: revision 1.50
sys/arch/i386/i386/mptramp.S: revision 1.24
sys/arch/x86/include/pmap.h: revision 1.52
sys/arch/x86/include/cpu.h: revision 1.50
- Convert x86 MD code, mainly pmap(9) e.g. TLB shootdown code, to use
kcpuset(9) and thus replace hardcoded CPU bitmasks. This removes the
limitation of maximum CPUs.
- Support up to 256 CPUs on amd64 architecture by default.
Bug fixes, improvements, completion of Xen part and testing on 64-core
AMD Opteron(tm) Processor 6282 SE (also, as Xen HVM domU with 128 CPUs)
by Manuel Bouyer.
- pmap_tlb_shootdown: do not overwrite tp_cpumask with pm_cpus, but merge
like pm_kernel_cpus. Remove unecessary intersection with kcpuset_running.
Do not reset tp_userpmap if pmap_kernel().
- Remove pmap_tlb_mailbox_t wrapping, which is pointless after recent changes.
- pmap_tlb_invalidate, pmap_tlb_intr: constify for packet structure.
i686_mtrr_init_first: handle the case when there are no variable-size MTRR
registers available (i686_mtrr_vcnt == 0).
 1.16.2.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #29):
sys/arch/xen/x86/x86_xpmap.c: revision 1.39
sys/arch/xen/include/hypervisor.h: revision 1.37
sys/arch/xen/include/intr.h: revision 1.34
sys/arch/xen/x86/xen_ipi.c: revision 1.10
sys/arch/x86/x86/cpu.c: revision 1.97
sys/arch/x86/include/cpu.h: revision 1.48
sys/uvm/uvm_map.c: revision 1.315
sys/arch/x86/x86/pmap.c: revision 1.165
sys/arch/xen/x86/cpu.c: revision 1.81
sys/arch/x86/x86/pmap.c: revision 1.167
sys/arch/xen/x86/cpu.c: revision 1.82
sys/arch/x86/x86/pmap.c: revision 1.168
sys/arch/xen/x86/xen_pmap.c: revision 1.17
sys/uvm/uvm_km.c: revision 1.122
sys/uvm/uvm_kmguard.c: revision 1.10
sys/arch/x86/include/pmap.h: revision 1.50
Apply patch proposed in PR port-xen/45975 (this does not solve the exact
problem reported here but is part of the solution):
xen_kpm_sync() is not working as expected,
leading to races between CPUs.
1 the check (xpq_cpu != &x86_curcpu) is always false because we
have different x86_curcpu symbols with different addresses in the kernel.
Fortunably, all addresses dissaemble to the same code.
Because of this we always use the code intended for bootstrap, which doesn't
use cross-calls or lock.
2 once 1 above is fixed, xen_kpm_sync() will use xcalls to sync other CPUs,
which cause it to sleep and pmap.c doesn't like that. It triggers this
KASSERT() in pmap_unmap_ptes():
KASSERT(pmap->pm_ncsw == curlwp->l_ncsw);
3 pmap->pm_cpus is not safe for the purpose of xen_kpm_sync(), which
needs to know on which CPU a pmap is loaded *now*:
pmap->pm_cpus is cleared before cpu_load_pmap() is called to switch
to a new pmap, leaving a window where a pmap is still in a CPU's
ci_kpm_pdir but not in pm_cpus. As a virtual CPU may be preempted
by the hypervisor at any time, it can be large enough to let another
CPU free the PTP and reuse it as a normal page.
To fix 2), avoid cross-calls and IPIs completely, and instead
use a mutex to update all CPU's ci_kpm_pdir from the local CPU.
It's safe because we just need to update the table page, a tlbflush IPI will
happen later. As a side effect, we don't need a different code for bootstrap,
fixing 1). The mutex added to struct cpu needs a small headers reorganisation.
to fix 3), introduce a pm_xen_ptp_cpus which is updated from
cpu_pmap_load(), whith the ci_kpm_mtx mutex held. Checking it with
ci_kpm_mtx held will avoid overwriting the wrong pmap's ci_kpm_pdir.
While there I removed the unused pmap_is_active() function;
and added some more details to DIAGNOSTIC panics.
When using uvm_km_pgremove_intrsafe() make sure mappings are removed
before returning the pages to the free pool. Otherwise, under Xen,
a page which still has a writable mapping could be allocated for
a PDP by another CPU and the hypervisor would refuse it (this is
PR port-xen/45975).
For this, move the pmap_kremove() calls inside uvm_km_pgremove_intrsafe(),
and do pmap_kremove()/uvm_pagefree() in batch of (at most) 16 entries
(as suggested by Chuck Silvers on tech-kern@, see also
http://mail-index.netbsd.org/tech-kern/2012/02/17/msg012727.html and
followups).
Avoid early use of xen_kpm_sync(); locks are not available at this time.
Don't call cpu_init() twice.
Makes LOCKDEBUG kernels boot again
Revert pmap_pte_flush() -> xpq_flush_queue() in previous.
 1.22.20.2 26-Apr-2017  pgoyette Sync with HEAD
 1.22.20.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.22.16.3 28-Aug-2017  skrll Sync with HEAD
 1.22.16.2 05-Feb-2017  skrll Sync with HEAD
 1.22.16.1 05-Dec-2016  skrll Sync with HEAD
 1.22.14.1 17-Apr-2017  snj Pull up following revision(s) (requested by khorben in ticket #1367):
sys/arch/amd64/conf/XEN3_DOM0: revision 1.126
sys/arch/i386/conf/XEN3_DOM0: revision 1.104
sys/arch/xen/x86/xen_pmap.c: revision 1.25
In the MP case,
do not attempt to pmap_tlb_shootdown() after a pmap_kenter_ma() during
boot. pmap_tlb_shootdown() assumes post boot. Instead invalidate the
entry on the local CPU only.
XXX: to DTRT, probably this assumption needs re-examination.
XXX: The tradeoff is a (predicted) single word size comparison
penalty, so perhaps a decision needs performance stats.
xen dom0 SMP is now bootable again.
--
add the 'options MULTIPROCESSOR' in respective configs, but mark them
experimental - and thus disabled by default.
 1.22.2.1 03-Dec-2017  jdolecek update from HEAD
 1.25.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.26.14.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.26.14.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.26.14.1 10-Jun-2019  christos Sync with HEAD
 1.26.12.1 28-Jul-2018  pgoyette Sync with HEAD
 1.18 01-Sep-2022  bouyer Add PVH support for backend drivers grant operation.
Now a domU in a PVH dom0 boots multiuser.
 1.17 21-Feb-2021  jdolecek in xen_shm_map(), make sure to unmap any successfully mapped pages
before returning failure if there is partial failure

fix detection of partial failure - GNTTABOP_map_grant_ref can actually return
zero for partial failure, so we need to always check all the entries
to detect it

previously, DIAGNOSTIC kernel triggered panic() for partial failure,
and non-DIAGNOSTIC kernel did not detect it at all, leading to Dom0 page
fault later; since the mapping failure can be triggered by malicious
DomU via bad grant reference, it's important to expect the calls
to fail, and handle it gracefully without crashing Dom0

part of fixes for XSA-362
 1.16 25-Apr-2020  bouyer branches: 1.16.2;
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.15 19-Apr-2020  jdolecek change interface for xen_shm_map() so that caller always supplies the VA,
it now fails only if the Xen hypercall fails, in which case the failure
is final

change xbdback to pre-allocate KVA on xbdback attach (and free on detach),
so it has always KVA to map the request pages

remove no longer needed KVA allocation failure handling
 1.14 13-Apr-2020  chs slightly change and fix the semantics of pool_set*wat(), pool_sethardlimit()
and pool_prime() (and their pool_cache_* counterparts):

- the pool_set*wat() APIs are supposed to specify thresholds for the count of
free items in the pool before pool pages are automatically allocated or freed
during pool_get() / pool_put(), whereas pool_sethardlimit() and pool_prime()
are supposed to specify minimum and maximum numbers of total items
in the pool (both free and allocated). these were somewhat conflated
in the existing code, so separate them as they were intended.

- change pool_prime() to take an absolute number of items to preallocate
rather than an increment over whatever was done before, and wait for
any memory allocations to succeed. since pool_prime() can no longer fail
after this, change its return value to void and adjust all callers.

- pool_setlowat() is documented as not immediately attempting to allocate
any memory, but it was changed some time ago to immediately try to allocate
up to the lowat level, so just fix the manpage to describe the current
behaviour.

- add a pool_cache_prime() to complete the API set.
 1.13 27-Jan-2019  pgoyette branches: 1.13.4; 1.13.10;
Merge the [pgoyette-compat] branch
 1.12 27-Jul-2018  maxv style, localify global variables, etc, no real functional change
 1.11 24-Jun-2018  jdolecek branches: 1.11.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.10 02-Sep-2011  dyoung branches: 1.10.52;
Report vmem(9) errors out-of-band so that we can use vmem(9) to manage
ranges that include the least and the greatest vmem_addr_t. Update
vmem(9) uses throughout the kernel. Slightly expand on the tests in
subr_vmem.c, which still pass. I've been running a kernel with this
patch without any trouble.
 1.9 31-Jul-2011  jym Fix typo in comment.
 1.8 28-Mar-2010  snj Spell "enough" properly.
 1.7 19-Oct-2009  bouyer branches: 1.7.2; 1.7.4;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.6 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.5 16-Mar-2009  cegger ansify function definitions
 1.4 18-Dec-2008  cegger branches: 1.4.2;
remove unused malloc.h
 1.3 17-Feb-2008  bouyer branches: 1.3.6; 1.3.10; 1.3.18;
Add missing __KERNEL_RCSID()
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.8; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file xen_shm_machdep.c was initially added on branch bouyer-xenamd64.
 1.1.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file xen_shm_machdep.c was added on branch matt-armv6 on 2008-01-09 01:50:16 +0000
 1.2.8.3 27-Feb-2008  yamt sync with head.
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file xen_shm_machdep.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:19 +0000
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file xen_shm_machdep.c was added on branch vmlocking on 2007-12-03 19:04:45 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file xen_shm_machdep.c was added on branch jmcneill-pm on 2007-11-27 19:36:23 +0000
 1.3.18.2 28-Apr-2009  skrll Sync with HEAD.
 1.3.18.1 19-Jan-2009  skrll Sync with HEAD.
 1.3.10.4 11-Aug-2010  yamt sync with head.
 1.3.10.3 11-Mar-2010  yamt sync with head
 1.3.10.2 19-Aug-2009  yamt sync with head.
 1.3.10.1 04-May-2009  yamt sync with head.
 1.3.6.1 17-Jan-2009  mjf Sync with HEAD.
 1.4.2.4 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.4.2.3 24-Oct-2010  jym Sync with HEAD
 1.4.2.2 01-Nov-2009  jym Sync with HEAD.
 1.4.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.7.4.1 30-May-2010  rmind sync with head
 1.7.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.10.52.2 28-Jul-2018  pgoyette Sync with HEAD
 1.10.52.1 25-Jun-2018  pgoyette Sync with HEAD
 1.11.2.2 21-Apr-2020  martin Sync with HEAD
 1.11.2.1 10-Jun-2019  christos Sync with HEAD
 1.13.10.1 20-Apr-2020  bouyer Sync with HEAD
 1.13.4.1 23-Feb-2021  martin Pull up following revision(s) (requested by jdolecek in ticket #1210):

sys/arch/xen/x86/xen_shm_machdep.c: revision 1.17 (via patch)

in xen_shm_map(), make sure to unmap any successfully mapped pages
before returning failure if there is partial failure
fix detection of partial failure - GNTTABOP_map_grant_ref can actually re=
turn

zero for partial failure, so we need to always check all the entries
to detect it

previously, kernel triggered panic() for partial failure, leading to
Dom0 page fault later; since the mapping failure can be triggered by
malicious DomU via bad grant reference, it's important to expect
the calls to fail, and handle it gracefully without crashing Dom0

part of fixes for XSA-362
 1.16.2.1 03-Apr-2021  thorpej Sync with HEAD.
 1.29 20-Aug-2022  riastradh x86: Split most of pmap.h into pmap_private.h or vmparam.h.

This way pmap.h only contains the MD definition of the MI pmap(9)
API, which loads of things in the kernel rely on, so changing x86
pmap internals no longer requires recompiling the entire kernel every
time.

Callers needing these internals must now use machine/pmap_private.h.
Note: This is not x86/pmap_private.h because it contains three parts:

1. CPU-specific (different for i386/amd64) definitions used by...

2. common definitions, including Xenisms like xpmap_ptetomach,
further used by...

3. more CPU-specific inlines for pmap_pte_* operations

So {amd64,i386}/pmap_private.h defines 1, includes x86/pmap_private.h
for 2, and then defines 3. Maybe we should split that out into a new
pmap_pte.h to reduce this trouble.

No functional change intended, other than that some .c files must
include machine/pmap_private.h when previously uvm/uvm_pmap.h
polluted the namespace with pmap internals.

Note: This migrates part of i386/pmap.h into i386/vmparam.h --
specifically the parts that are needed for several constants defined
in vmparam.h:

VM_MAXUSER_ADDRESS
VM_MAX_ADDRESS
VM_MAX_KERNEL_ADDRESS
VM_MIN_KERNEL_ADDRESS

Since i386 needs PDP_SIZE in vmparam.h, I added it there on amd64
too, just to keep things parallel.
 1.28 06-May-2020  bouyer xpq_queue_* use per-cpu queue; splvm() is enough to protect them.
remove the XXX SMP comments.
 1.27 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.26 04-May-2019  kre branches: 1.26.8;

More of maxv's "switch to proper types" - hopefully unbreak i386 build.
 1.25 04-May-2019  maxv More inlined ASM. While here switch to proper types.
 1.24 06-Jan-2019  cherry Rollback http://mail-index.netbsd.org/source-changes/2018/12/22/msg101629.html

This change breaks module loading due to weak alias being unsupported
in the kernel module linker.

Requested by maxv@ and others as it affects their work.

No immediate decision on a replacement method is available, but other options
suggested include pre-processing, conditional compilation (#ifdef etc) and other
source level methods to avoid linktime decision making.
 1.23 22-Dec-2018  cherry Introduce a weak alias method of exporting different implementations
of the same API.

For eg: the amd64 native implementation of invlpg() now becomes
amd64_invlpg() with a weak symbol export of invlpg(), while the XEN
implementation becomes xen_invlpg(), also weakly exported as invlpg()

Note that linking in both together without having an override function
named invlpg() would be a mistake, as we have limited control over
which of the two options would emerge as the finally exported invlpg()
resulting in a potential situation where the wrong function is finally
exported. This change avoids this situation.

We should however include an override function invlpg() in that case,
such that it is able to then pass on the call to the appropriate
backing function (amd64_invlpg() in the case of native, and
xen_invlpg() in the case of under XEN virtualisation) at runtime.

This change does not introduce such a function and therefore does not
alter builds to include native as well as XEN implementations in the
same binary. This will be done later, with the introduction of XEN
PVHVM mode, where precisely such a runtime switch is required.

There are no operational changes introduced by this change.
 1.22 18-Oct-2018  cherry Zero out page table memory for IDT before use.
To copy the IDT entry before registration, de-reference the indexed
value, not the first entry.
Add a MAX_XEN_IDT value for max entries we expect and KASSERT() for
this as a sanity check.
 1.21 23-Sep-2018  cherry Fix for i386, functionality intended in:
http://mail-index.netbsd.org/source-changes/2018/09/23/msg099357.html

This should fix the build for both GENERIC and XEN3PAE_DOM0

This has not been boot tested on native or xen3pae

Notes: pmap_changeprot_local() seems to be x86_64 only.
I was a bit surprised by this initially, but I suspect that the table
protections are enforced via ring0/ring1 fencing rather than page protections

the gdt registration code in i386 is still messy. I will leave it as is
for now - to avoid a rabbit hole.
 1.20 23-Sep-2018  cherry Make XEN use the same api as native, for idt vector allocation
and registration.

lidt() placed in xenfunc() on maxv@ suggestion.

There should be no functional change due to this commit.

Tested on amd64 native and XEN.
 1.19 26-Jul-2018  maxv Remove dead code.
 1.18 24-Jun-2018  jdolecek branches: 1.18.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.17 15-Oct-2017  maxv branches: 1.17.2;
Add setusergs on Xen, and simplify.
 1.16 05-Feb-2017  maxv Rename ldt->ldtstore and gdt->gdtstore on i386. It reduces the diff with
amd64, and makes it easier to track down these variables on nxr - 'ldt'
and 'gdt' being common keywords.
 1.15 13-Dec-2016  kamil branches: 1.15.2;
Switch x86 CPU Debug Register types from vaddr_t to register_t

This is more opaque and appropriate type, as vaddr_t is meant to be used
for vitual address value. Not all DR on x86 are used to represent virtual
address (DR6 and DR7 are definitely not).

No functional change intended.

Change suggested by <christos>

Sponsored by <The NetBSD Foundation>
 1.14 27-Nov-2016  kamil Add accessors for available x86 Debug Registers

There are 8 Debug Registers on i386 (available at least since 80386) and 16
on AMD64. Currently DR4 and DR5 are reserved on both cpu-families and
DR9-DR15 are still reserved on AMD64. Therefore add accessors for DR0-DR3,
DR6-DR7 for all ports.

Debug Registers x86:
* DR0-DR3 Debug Address Registers
* DR4-DR5 Reserved
* DR6 Debug Status Register
* DR7 Debug Control Register
* DR8-DR15 Reserved

Access the registers is available only from a kernel (ring 0) as there is
needed top protected access. For this reason there is need to use special
XEN functions to get and set the registers in the XEN3 kernels.

XEN specific functions as defined in NetBSD:
- HYPERVISOR_get_debugreg()
- HYPERVISOR_set_debugreg()

This code extends the existing rdr6() and ldr6() accessor for additional:
- rdr0() & ldr0()
- rdr1() & ldr1()
- rdr2() & ldr2()
- rdr3() & ldr3()
- rdr7() & ldr7()

Traditionally accessors for DR6 were passing vaddr_t argument, while it's
appropriate type for DR0-DR3, DR6-DR7 should be using u_long, however it's
not a big deal. The resulting functionality should be equivalent so stick
to this convention and use the vaddr_t type for all DR accessors.

There was already a function defined for rdr6() in XEN, but it had a nit on
AMD64 as it was casting HYPERVISOR_get_debugreg() to u_int (32-bit on
AMD64), truncating result. It still works for DR6, but for the sake of
simplicity always return full 64-bit value.

New accessors duplicate functionality of the dr0() function available on
i386 within the KSTACK_CHECK_DR0 option. dr0() is a specialized layer with
logic to set appropriate types of interrupts, now accessors are designed to
pass verbatim values from user-land (with simple sanity checks in the
kernel). At the moment there are no plans to make possible to coexist
KSTACK_CHECK_DR0 with debug registers for user applications (debuggers).

options KSTACK_CHECK_DR0
Detect kernel stack overflow using DR0 register. This option uses DR0
register exclusively so you can't use DR0 register for other purpose
(e.g., hardware breakpoint) if you turn this on.

The KSTACK_CHECK_DR0 functionality was designed for i386 and never ported
to amd64.

Code tested on i386 and amd64 with kernels: GENERIC, XEN3_DOMU, XEN3_DOM0.

Sponsored by <The NetBSD Foundation>
 1.13 06-Nov-2011  cherry branches: 1.13.10; 1.13.28; 1.13.32;
[merging from cherry-xenmp] Make the xen MMU op queue locking api private. Implement per-cpu queues.
 1.12 13-Aug-2011  cherry branches: 1.12.2;
Add locking around ops to the hypervisor MMU "queue".
 1.11 24-Jul-2010  jym branches: 1.11.6;
Welcome PAE inside i386 current.

This patch is inspired by work previously done by Jeremy Morse, ported by me
to -current, merged with the work previously done for port-xen, together with
additionals fixes and improvements.

PAE option is disabled by default in GENERIC (but will be enabled in ALL in
the next few days).

In quick, PAE switches the CPU to a mode where physical addresses become
36 bits (64 GiB). Virtual address space remains at 32 bits (4 GiB). To cope
with the increased size of the physical address, they are manipulated as
64 bits variables by kernel and MMU.

When supported by the CPU, it also allows the use of the NX/XD bit that
provides no-execution right enforcement on a per physical page basis.

Notes:

- reworked locore.S

- introduce cpu_load_pmap(), used to switch pmap for the curcpu. Due to the
different handling of pmap mappings with PAE vs !PAE, Xen vs native, details
are hidden within this function. This helps calling it from assembly,
as some features, like BIOS calls, switch to pmap_kernel before mapping
trampoline code in low memory.

- some changes in bioscall and kvm86_call, to reflect the above.

- the L3 is "pinned" per-CPU, and is only manipulated by a
reduced set of functions within pmap. To track the L3, I added two
elements to struct cpu_info, namely ci_l3_pdirpa (PA of the L3), and
ci_l3_pdir (the L3 VA). Rest of the code considers that it runs "just
like" a normal i386, except that the L2 is 4 pages long (PTP_LEVELS is
still 2).

- similar to the ci_pae_l3_pdir{,pa} variables, amd64's xen_current_user_pgd
becomes an element of cpu_info (slowly paving the way for MP world).

- bootinfo_source struct declaration is modified, to cope with paddr_t size
change with PAE (it is not correct to assume that bs_addr is a paddr_t when
compiled with PAE - it should remain 32 bits). bs_addrs is now a
void * array (in bootloader's code under i386/stand/, the bs_addrs
is a physaddr_t, which is an unsigned long).

- fixes in multiboot code (same reason as bootinfo): paddr_t size
change. I used Elf32_* types, use RELOC() where necessary, and move the
memcpy() functions out of the if/else if (I do not expect sym and str tables
to overlap with ELF).

- 64 bits atomic functions for pmap

- all pmap_pdirpa access are now done through the pmap_pdirpa macro. It
hides the L3/L2 stuff from PAE, as well as the pm_pdirpa change in
struct pmap (it now becomes a PDP_SIZE array, with or without PAE).

- manipulation of recursive mappings ( PDIR_SLOT_{,A}PTEs ) is done via
loops on PDP_SIZE.

See also http://mail-index.netbsd.org/port-i386/2010/07/17/msg002062.html

No objection raised on port-i386@ and port-xen@R for about a week.

XXX kvm(3) will be fixed in another patch to properly handle both PAE and !PAE
kernel dumps (VA => PA macros are slightly different, and need proper 64 bits
PA support in kvm_i386).

XXX Mixing PAE and !PAE modules may lead to unwanted/unexpected results. This
cannot be solved easily, and needs lots of thinking before being declared
safe (paddr_t/bus_addr_t size handling, PD/PT macros abstractions).
 1.10 12-Feb-2010  jym branches: 1.10.2;
Starting with Xen 3 API, MMU_EXTENDED_COMMAND (tlb flush, cache flush, page
pinning/unpinning, set_ldt, invlpg) operations cannot be queued in the
xpq_queue[] any more, as they use their own specific hypercall, mmuext_op().

Their associated xpq_queue_*() functions already call xpq_flush_queue()
before issuing the mmuext_op() hypercall, which makes these xpq_flush_queue()
calls not necessary.

Rapidly discussed with bouyer@ in private mail. XEN3_DOM0/XEN3PAE_DOM0 tested
through a build.sh release, amd64 was only compile tested. No regression
expected.
 1.9 23-Oct-2009  snj branches: 1.9.2;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.8 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.7 11-May-2008  ad branches: 1.7.12;
Don't reload LDTR unless a new value, which only happens for USER_LDT.
 1.6 30-Apr-2008  cegger branches: 1.6.2;
AMD's APM Volume 2 says 'All control registers are 64bit in long mode'.
Fix the CR0 prototype to match this (the asm implementation is correct though).
OK ad
 1.5 21-Apr-2008  cegger branches: 1.5.2;
Access Xen's vcpu info structure per-CPU.
Tested on i386 and amd64 (both dom0 and domU) by me.
Xen2 tested (both dom0 and domU) by bouyer.
OK bouyer
 1.4 17-Feb-2008  bouyer branches: 1.4.6; 1.4.8;
Add missing __KERNEL_RCSID()
 1.3 11-Jan-2008  bouyer 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.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.8; 1.2.12; 1.2.16;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file xenfunc.c was initially added on branch bouyer-xenamd64.
 1.1.4.2 18-Feb-2008  mjf Sync with HEAD.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.2 21-Oct-2007  bouyer Factorise some Xen pmap code in x86_xpmap.c.
More xpmap_{ptom,mtop} -> xpmap_{ptom,mtop}_masked

The xenamd64 kernel is now good enough to complete a sysinst install from
xennet to xbd.
 1.1.2.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.2.16.3 23-Mar-2008  matt sync with HEAD
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file xenfunc.c was added on branch matt-armv6 on 2008-01-09 01:50:17 +0000
 1.2.12.1 09-Jan-2008  bouyer Merge xen bits to i386/i386/gdt.c. Convert remaining uses of PTE_* macros to
pmap_pte_* macros/inlines.
Fix think-o in pmap.c for native i386.
 1.2.8.4 27-Feb-2008  yamt sync with head.
 1.2.8.3 21-Jan-2008  yamt sync with head
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file xenfunc.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:19 +0000
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file xenfunc.c was added on branch vmlocking on 2007-12-03 19:04:45 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file xenfunc.c was added on branch jmcneill-pm on 2007-11-27 19:36:24 +0000
 1.4.8.1 18-May-2008  yamt sync with head.
 1.4.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.5.2.4 11-Aug-2010  yamt sync with head.
 1.5.2.3 11-Mar-2010  yamt sync with head
 1.5.2.2 19-Aug-2009  yamt sync with head.
 1.5.2.1 16-May-2008  yamt sync with head.
 1.6.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.7.12.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.7.12.2 24-Oct-2010  jym Sync with HEAD
 1.7.12.1 01-Nov-2009  jym Sync with HEAD.
 1.9.2.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.9.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.10.2.1 05-Mar-2011  rmind sync with head
 1.11.6.3 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.11.6.2 31-Jul-2011  cherry grow MP support for i386. boots to single user
 1.11.6.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.12.2.1 10-Nov-2011  yamt sync with head
 1.13.32.2 20-Mar-2017  pgoyette Sync with HEAD
 1.13.32.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.13.28.3 28-Aug-2017  skrll Sync with HEAD
 1.13.28.2 05-Feb-2017  skrll Sync with HEAD
 1.13.28.1 05-Dec-2016  skrll Sync with HEAD
 1.13.10.1 03-Dec-2017  jdolecek update from HEAD
 1.15.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.17.2.6 18-Jan-2019  pgoyette Synch with HEAD
 1.17.2.5 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.17.2.4 20-Oct-2018  pgoyette Sync with head
 1.17.2.3 30-Sep-2018  pgoyette Ssync with HEAD
 1.17.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.17.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.18.2.1 10-Jun-2019  christos Sync with HEAD
 1.26.8.2 12-Apr-2020  bouyer kpreempt_disable() only for x86_64 (which calls pmap_changeprot_local)).
On i386 curcpu() is not valid yet and we don't need preemption disabled.
 1.26.8.1 11-Apr-2020  bouyer Move softint and preemtion-related functions out of x86/x86/intr.c to
its own file, x86/x86/x86_softintr.c
Add x86/x86/x86_softintr.c for native and XenPV
Make sure XenPV also check ci_ioending, which is used for softints.
Switch XenPV to fast softints and allow kernel preemption.
kpreempt_disable() before calling pmap_changeprot_local()
run xen_wallclock_time() and xen_global_systime_ns() at splshed() to
avoid being interrupted.

XXX amd64 lock stubs are racy for XPENDING
 1.24 24-Sep-2024  andvar smal->small in comment.
 1.23 06-May-2020  bouyer branches: 1.23.26;
Attempts to make MP-safe:
xpmap_ptom_unmap() doens't need to be called at splvm; we own the pa.
Use atomic ops to change pmap_pa_end
Make sure the ptom/mtop are up to date before giving the pages back to the
pool.
 1.22 06-May-2020  bouyer No need to run hypercalls at IPL_VM.
XXX still not MP-safe
 1.21 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.20 09-Apr-2020  jdolecek update to __XEN_INTERFACE_VERSION__ 0x0003020a aka Xen 3.2.10

this brings grant memory v2 support:
- status separated from flags - revoking access needs just memory barrier,
no need for expensive cmpxchg16 any more
- sub-page hypervisor copy-only grants, to be used by xennet(4)
- 64-bit frame, i.e. support for DomU RAM >16TB

the grant table is now always allocated on boot to maximum size, it's now
never grown in runtime; switch back to regular kmem_alloc()/kmem_free()

code now requires v2 support, no compatibility for grant version 1 retained -
Xen v2 support predates all currently supported Xen versions

also interface for baloon changed slightly, code updated
 1.19 24-Jun-2018  jdolecek branches: 1.19.2; 1.19.12;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.18 23-Dec-2016  cherry branches: 1.18.14;
balloon(4) can now use uvm_hotplug(9)

Do this.
 1.17 07-Jul-2016  msaitoh branches: 1.17.2;
KNF. Remove extra spaces. No functional change.
 1.16 30-Jun-2012  jym branches: 1.16.2; 1.16.16;
Use setter to set xenguest_handles.
 1.15 30-Jun-2012  jym Extend the xpmap API, as described in [1]. This change is mechanical and
avoids exposing the MD phys_to_machine/machine_to_phys tables directly.
Added:

- xpmap_ptom handles PFN (pseudo physical) to MFN (machine frame number)
translations, and is under control of the domain.
- xpmap_mtop is its counterpart (MFN to PFN), and is under control of
hypervisor.

xpmap_ptom_map() map a pseudo-phys address to a machine address
xpmap_ptom_unmap() unmap a pseudo-phys address (invalidation)
xpmap_ptom_isvalid() check for pseudo-phys address validity

The parameters are physical/machine addresses, like bus_dma/bus_space(9).
As x86 MFNs are tracked by u_long (Xen's choice) while machine addresses
can be 64 bits entities (PAE), use ptoa() to avoid truncation when bit
shifting by PAGE_SHIFT.

I kept the same namespace (xpmap_) to avoid code churn.

[1] http://mail-index.netbsd.org/port-xen/2009/05/09/msg004951.html

XXX will document ptoa/atop/trunc_page separately.
 1.14 27-Jun-2012  jym Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.13 05-Jan-2012  jym Comment fix.
 1.12 26-Dec-2011  jym Properly protect the min/target variables from balloon_sc, not just target.

Use their reference directly instead of going through their opaque
sysctl_data storage. It makes the locking a bit more obvious.
 1.11 20-Sep-2011  jym branches: 1.11.2; 1.11.6;
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.10 29-Jul-2011  jym Move xen.balloon to machdep in the sysctl(7) tree. It does not really
belong to either kern or hw.

Rename machdep.xen_timepush_ticks to xen.timepush_ticks, so it can live
under the same tree as the balloon node, machdep.xen.

ok bouyer@.
 1.9 29-Apr-2011  jym Silence xenbus_read_target() in ENOENT case (== entry is missing from
Xenstore). The error case does not bring much here; assume that the value
is 0.

Print the error code when writing the ``target'' value fails.
 1.8 18-Apr-2011  rmind balloon_xenbus_attach: use KM_SLEEP for allocation.

Note: please do not use KM_NOSLEEP.
 1.7 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.6 12-Nov-2010  uebayasi branches: 1.6.2; 1.6.6;
Pull in uvm/uvm.h where UVM's page level interface is used.
 1.5 11-Aug-2010  pgoyette branches: 1.5.2; 1.5.4; 1.5.6;
Keep condvar wmesg within 8-char limit
 1.4 10-Jul-2010  cherry Provide and accept memory reservation information from the hypervisor
in KiloBytes. This is valid both for the xenbus interface, as well as
the dom0 only sysctl interface.

Thanks cegger@ for testing.
 1.3 08-Jul-2010  cherry Provide the sysctl(7) interface to the balloon driver only to dom0
 1.2 08-Jul-2010  cherry make read-only, and return sane values in balloon related sysctl(7)
nodes when 'options XEN_BALLOON' is disabled.
 1.1 06-Jul-2010  cherry The Xen balloon driver enables growing and shrinking
PV domains on the fly, by collaborating with UVM and the hypervisor
 1.5.6.6 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.5.6.5 07-May-2011  jym Implement pmf(9) suspend/resume routines for balloon(4). Trivial, heh.
 1.5.6.4 02-May-2011  jym Sync with head.
 1.5.6.3 10-Jan-2011  jym Sync with HEAD
 1.5.6.2 24-Oct-2010  jym Sync with HEAD
 1.5.6.1 11-Aug-2010  jym file balloon.c was added on branch jym-xensuspend on 2010-10-24 22:48:22 +0000
 1.5.4.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.5.4.1 11-Aug-2010  uebayasi file balloon.c was added on branch uebayasi-xip on 2010-08-17 06:45:36 +0000
 1.5.2.3 09-Oct-2010  yamt sync with head
 1.5.2.2 11-Aug-2010  yamt sync with head.
 1.5.2.1 11-Aug-2010  yamt file balloon.c was added on branch yamt-nfs-mp on 2010-08-11 22:53:00 +0000
 1.6.6.4 31-May-2011  rmind sync with head
 1.6.6.3 21-Apr-2011  rmind sync with head
 1.6.6.2 05-Mar-2011  rmind sync with head
 1.6.6.1 12-Nov-2010  rmind file balloon.c was added on branch rmind-uvmplock on 2011-03-05 20:52:34 +0000
 1.6.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.11.6.1 18-Feb-2012  mrg merge to -current.
 1.11.2.2 30-Oct-2012  yamt sync with head
 1.11.2.1 17-Apr-2012  yamt sync with head
 1.16.16.2 05-Feb-2017  skrll Sync with HEAD
 1.16.16.1 09-Jul-2016  skrll Sync with HEAD
 1.16.2.1 03-Dec-2017  jdolecek update from HEAD
 1.17.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.18.14.1 25-Jun-2018  pgoyette Sync with HEAD
 1.19.12.1 20-Apr-2020  bouyer Sync with HEAD
 1.19.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.23.26.1 02-Aug-2025  perseant Sync with HEAD
 1.82 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.81 13-Apr-2020  bouyer By default, events are bound to CPU 0 (exept for IPIs and VTIMERs which
are bound to a different CPU at creation time).
Recent MI changes caused the scheduler to choose a different CPU when
probing and attaching xennet devices (I guess it's the xenbus thread which
runs on a different CPU). This cause the callback to be called on a different
CPU than the one expected by the kernel, and the event is ignored.
It is handled when the clock causes the callback to be called on the right
CPU, which is why xennet still run, but slowly.

Change event_set_handler() to do a EVTCHNOP_bind_vcpu if requested to,
and make sure we don't do it for IPIs and VIRQs (for theses, the op fails).
 1.80 16-Oct-2019  christos branches: 1.80.6;
Add and use __FPTRCAST, requested by uwe@
 1.79 16-Oct-2019  christos add void * casts for the clock interrupt handlers.
 1.78 09-Mar-2019  kre branches: 1.78.4;

error in xen_resumeclocks() is __diagused.
Avoid compile warning (-->error) when ! options DIAGNOSTIC.
 1.77 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.76 25-Dec-2018  cherry Excise XEN specific code out of x86/x86/intr.c into xen/x86/xen_intr.c

While at it, separate the source function tracking so that the interrupt
paths are truly independant.

Use weak symbol exporting to provision for future PVHVM co-existence
of both files, but with independant paths. Introduce assembler code
such that in a unified scenario, native interrupts get first priority
in spllower(), followed by XEN event callbacks. IPL management and
semantics are unchanged - native handlers and xen callbacks are
expected to maintain their ipl related semantics.

In summary, after this commit, native and XEN now have completely
unrelated interrupt handling mechanisms, including
intr_establish_xname() and assembler stubs and intr handler
management.

Happy Christmas!
 1.75 24-Dec-2018  cherry 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.74 18-Nov-2018  cherry On Xen, copy just the bits we need from the trapframe for hardclock(9)
and statclock(9).

Current, the macros that use the trapframe are:
CLKF_USERMODE()
CLKF_PC()
CLKF_INTR()

Of these, CLKF_INTR() already ignores the frame and uses the ci_idepth
variable to do its job.

Convert the two remaining ones to do this, but only for XEN.
 1.73 18-Nov-2018  cherry Save the interrupt trap/clockframe to a per-cpu copy.

We can use this copy to pass on the trapframe to hardclock(9) from
within the xen timer handler. This delinks the current dependency
between MD code and the handler, which is specially prototyped to take
the clockframe unlike any other handler.

This change has performance implications, as each interrupt entry will
copy the entire trapframe over to the per-cpu cached copy. This can be
mitigated by selectively copying just the parts of the clockframe that
are used by hardclock() et. al.

Tested on amd64 XEN domU
 1.72 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.71 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.70 30-Jun-2018  riastradh Rearm the Xen timer on resume.

This just moves the timer-arming logic from xen_initclocks into
xen_resumeclocks so that it runs on resume too.

I hypothesize that this is necessary for Xen to resume. Otherwise,
how could the one-shot timer possibly by rearmed? On the other hand,
it is conceivable that something automatically rearms it.

This also reorders the initialization so that we establish a timer
interrupt handler first, and _then_ arm the timer. If the order
matters, it is hard to imagine that the other way is correct:
conceivably, the interrupt could arrive before we've established the
handler, and then there's nothing to rearm it.

Whether this is _sufficient_ for Xen to resume, I don't know.
Symptoms recently reported in
<https://mail-index.netbsd.org/port-xen/2018/06/15/msg009207.html>
look different from how I would expect this to manifest, which is as
a system wedged because there's no no hardclock activity.

ok cherry
 1.69 30-Jun-2018  riastradh Just use struct cpu_info members for the Xen clock state.

Silly to use percpu(9) for some things and struct cpu_info for
others.
 1.68 29-Jun-2018  riastradh Rewrite Xen timecounter and hardclock timer.

With this change, the Xen timecounter should now be globally
monotonic, as every timecounter is supposed to be. Should also fix a
litany of races in the timecounter logic.

Proposed last year; see mailing list for further details:
https://mail-index.netbsd.org/port-xen/2017/10/31/msg009112.html

ok cherry
 1.67 24-Jun-2018  jdolecek branches: 1.67.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.66 11-May-2018  cherry Fixes port-xen/53267
re-educate xen_clock_handler() how to use the interrupt stackframe.
The current regs value passed in is *ci, and thus invalid.

Reported and tested by kre@. See PR 53267 for more details.
 1.65 06-Nov-2017  cherry branches: 1.65.2;
Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.64 12-Jun-2016  jnemeth - add machdep.xen.version sysctl to easily get hypervisor version
- move machdep.xen_timepush_ticks to machdep.xen.timepush_ticks to
consolidate all Xen related sysctls under machdep.xen
 1.63 21-Sep-2014  bouyer branches: 1.63.2;
Make Xen kernels compile without DIAGNOSTIC
 1.62 17-Feb-2012  bouyer branches: 1.62.2; 1.62.14;
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
 1.61 12-Feb-2012  jym branches: 1.61.2;
Xen clock management routines keep track of CPU (following MP merge).
Reflect this change in the suspend/resume routines so they can cope with
domU CPU suspend, instead of setting their cpu_info pointer to NULL.

Avoid copy/pasting by using the resume routines during attachement.

ok releng@.

No regression observed, and allows domU to suspend successfully again.
Restore is a different beast as PD/PT flags are marked "invalid" by Xen-4
hypervisor, and blocks resuming. Looking into it.
 1.60 09-Jan-2012  cherry Remove redundant use of curcpu().
 1.59 09-Jan-2012  cherry Remove obsolete comment.
 1.58 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.57 18-Nov-2011  cherry branches: 1.57.4;
[merging from cherry-xenmp]
- Make clock MP aware.
- Bring in fixes that bouyer@ brought in via:
cvs rdiff -u -r1.54.6.4 -r1.54.6.5 src/sys/arch/xen/xen/clock.c

Thanks to riz@ for testing on dom0
 1.56 20-Sep-2011  jym branches: 1.56.2;
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.55 29-Jul-2011  jym Move xen.balloon to machdep in the sysctl(7) tree. It does not really
belong to either kern or hw.

Rename machdep.xen_timepush_ticks to xen.timepush_ticks, so it can live
under the same tree as the balloon node, machdep.xen.

ok bouyer@.
 1.54 28-Mar-2010  snj branches: 1.54.6;
Spell "enough" properly.
 1.53 12-Dec-2009  tsutsui branches: 1.53.2; 1.53.4;
Remove `volatile' qualifier from argument types of
struct timeval passed to todr_gettime(9) and todr_settime(9).
We no longer have an ancient and volatile struct timeval `time'
global since we have switched to MI timercounter(9) on all port.

XXX1: some of these RTC drivers still assume 32bit time_t
XXX2: some of these should be rewritten to use todr_[gs]ettime_ymdhms()
XXX3: todr(9) man page doesn't mention todr_[gs]ettime_ymdhms()
 1.52 23-Oct-2009  snj Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.51 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.50 16-Jun-2009  bouyer Split mc146818-related functions from clock.c into rtc.c.
Call rtc_set_ymdhms() from xen/xen/clock.c:xen_rtc_set() for xen3 dom0
kernels as the Xen3 hypervisor doesn't write the new date/time to the CMOS
by itself.
Now a XEN3_DOM0 kernel properly updates the CMOS time.
 1.49 16-Jan-2009  jym branches: 1.49.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.48 13-Nov-2008  cegger Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.47 21-Oct-2008  cegger branches: 1.47.2; 1.47.4; 1.47.8;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.46 21-Apr-2008  cegger branches: 1.46.2; 1.46.8;
Access Xen's vcpu info structure per-CPU.
Tested on i386 and amd64 (both dom0 and domU) by me.
Xen2 tested (both dom0 and domU) by bouyer.
OK bouyer
 1.45 14-Apr-2008  cegger branches: 1.45.2;
- use POSIX integer types
- ansify functions
 1.44 17-Jan-2008  lukem branches: 1.44.6;
Remove unnecessary references to config_time.h.
 1.43 08-Jan-2008  joerg Switch Xen to generic TODR. Tested by Manuel Bouyer.
 1.42 22-Nov-2007  bouyer branches: 1.42.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.41 26-Oct-2007  joerg branches: 1.41.2;
Match delay/DELAY on x86 with delay(9). It takes an unsigned int as
argument. Use this and replace the inline assembly (mul + div using the
64bit intermediate result) with normal 32bit multiplication and
division. The compiler can turn the division into a multiplication and
shift, making it even cheaper then the original assembly. For extreme
long delays, just use 64bit arithmetic.
 1.40 17-Oct-2007  garbled 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.39 10-Jul-2007  he branches: 1.39.8; 1.39.10; 1.39.14;
Protect the initialization of xen_timepush_co with #ifdef DOM0OPS,
like its declaration and other uses.
 1.38 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.37 02-Jul-2007  jld Correct regression in xen3 wall-time reading accidentally committed in r1.35.
(Discussed on port-xen.)
 1.36 25-Jun-2007  bouyer Fix race condition in cpu_idle() on ci_want_resched. From YAMAMOTO Takashi.
 1.35 12-May-2007  jld Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.34 11-Nov-2006  jld branches: 1.34.2; 1.34.4; 1.34.8; 1.34.10; 1.34.16; 1.34.18;
Under Xen2, timestamp updates should happen every 10ms, and a timer
event should be raised every 10ms if we're runnable. Unfortunately,
there seems to be an intermittent bug in the hypervisor such that,
for about 1<<32 ns (~4.3s) after it manifests, every running domain
continues to run but not get its timer events and new timestamps (nor
is it preempted in favor of other domains on that CPU). This can cause
strange behavior from our timekeeping; for example, hardclock() is never
called during this interval.

So here's a workaround: if timestamp is allegedly up to date but is more
than 40ms old (this is semi-arbitrary), the domain sets its timer to a
time in the past, which causes it to become immediately pending, and also
results in the publication of a new timestamp.
 1.33 05-Nov-2006  jld If the timestamp is/becomes stale when computing the current system_time,
obviously shadow_system_time will need to be reread as well.
 1.32 03-Nov-2006  jld Ensure that the timestamp we use in get_tsc_offset_ns remains valid
until after we do the rdtsc; since Xen3 dynamically adjusts its idea of
the clock frequency, this is important. (Linux and FreeBSD both do this.)
 1.31 03-Nov-2006  jld "static volatile", not "volatile static".
 1.30 11-Sep-2006  jld branches: 1.30.2;
Convert TSC offset to nanoseconds correctly under Xen3. The hypervisor
appears to dynamically adjust its estimate of the CPU clock speed, and
scaling the TSC as intended is necessary to avoid time anomalies.
 1.29 10-Sep-2006  jld Re-arm the Xen interval timer in the timer event handler. This fixes
the case of HZ!=100, and might be beneficial otherwise.

The set_timer_op in idle_block remains, to protect against a system_time
miscalculation's causing us to block without the timer set; so, while
there, spl-protect the processed_system_time read and set_timer_op.
 1.28 16-Aug-2006  yamt branches: 1.28.2;
don't define XEN_CLOCK_DEBUG automatically for DEBUG.
it isn't safe to do printf in microtime().
 1.27 11-Jul-2006  yamt branches: 1.27.2;
inittodr: if xen3, add processed_system_time (system_time) to
shadow_tv (wc_sec/nsec) as linux guest does.
 1.26 04-Jul-2006  jld Fix accidentally unsigned comparison, which was causing the timer event
handler to run ~forever in the face of slightly unexpected input from
Xen (system_time apparently going backwards). Pointed out by kardel@.
 1.25 28-May-2006  bouyer branches: 1.25.2;
Revert previous. HYPERVISOR_set_timer_op() works fine for domU, and using
HYPERVISOR_yield() cause a domain to busy-wait for some work to do (so
using HYPERVISOR_yield doen't fix the problem for dom0, and makes things much
worse for domU).
 1.24 27-May-2006  bouyer HYPERVISOR_set_timer_op() doesn't seem to work right on a NetBSD Xen3 dom0,
the dom0 is getting a continous stream of clock interrupts. As we want an
interrupt every hz anyway, just use HYPERVISOR_yield() instead.
 1.23 05-May-2006  jld branches: 1.23.2;
Have our dom0 periodically set Xen's time-of-day clock (much as Linux does).

That time is used by ~all domUs when they boot (or resume), and Linux by
default will pin the local clock to it; so, it's useful to correct its
drift as best we can. A sysctl (machdep.xen_timepush_ticks) has been
added to allow changing the interval or disabling this feature.
 1.22 28-Apr-2006  jld What corresponds to the last run of hardclock(9), and thus the current
value of time(9), is processed_system_time and not shadow_system_time.
 1.21 18-Apr-2006  jld Raise time limit on sanity-check warning printf to 10s (from 10 ticks),
to avoid false postives on Xen 3, which doesn't update the shared time
info as often.
 1.20 04-Apr-2006  jld Change diagnostic assertions to DEBUG-conditionalized printfs.
(Alternately, XEN_CLOCK_DEBUG may be defined to enable just the
checks in this module.)
 1.19 17-Mar-2006  jld Add some missing memory barriers I noticed.
Discussed on port-xen.
 1.18 07-Mar-2006  jld branches: 1.18.2;
Improve timekeeping under Xen. Specifically, replace use of
cc_microtime with a MD function that isn't affected by erratic "clock
interrupts" and instead takes more advantage of time information
provided by the hypervisor. Fixes, most importantly, a case where the
clock as seen by userland would sometimes bounce back and forth by up to
1<<31 us (~35 min).

Approved by bouyer@; explained in more detail in
http://mail-index.netbsd.org/port-xen/2006/02/28/0002.html
 1.17 03-Feb-2006  jld branches: 1.17.2; 1.17.4; 1.17.6;
Use 64-bit intermediate in get_tsc_offset_ns to avoid overflow.
Discussed (3 weeks ago) on port-xen; okayed by yamt@, bouyer@.
 1.16 15-Jan-2006  bouyer branches: 1.16.2;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.15 11-Dec-2005  christos branches: 1.15.2;
merge ktrace-lwp.
 1.14 11-Aug-2005  cube Change all archs that did:

#define clockframe somethingelse

to:

struct clockframe {
struct somethingelse cf_se;
};

and change access macros accordingly.

That means that, at least for that very issue, things will not go
ka-boomy if you don't have the actual definition of struct clockframe
before including systm.h.
 1.13 25-Jun-2005  bouyer branches: 1.13.2;
Use the time provided by the hypervisor instead of our global time variable
for xen_microtime(). This match more closely what is done on a real i386
(where we read the RTC), and seems to fix gettimeofday() sometime going
backward by several seconds for me.
 1.12 27-May-2005  bouyer Reset the microtime state in inittodr(), so that the next clock tick will
initialise the microtime state again. it was initialised at the first
clock tick before time of day was set. Should fix port-xen/29846.
Also, try harder to call microset() once a second; the clock interrupt handler
can be called more often than HZ in some case.
 1.11 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.10 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.9 10-Mar-2005  bouyer branches: 1.9.2;
Report which IRQ is used.
 1.8 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.7 23-Sep-2004  tls branches: 1.7.4; 1.7.6; 1.7.8;
Changes from Christian Limpach to improve timekeeping on NetBSD/xen by
actually adjusting the time correctly (calling hardclock as needed, not
just blindly every time Xen schedules us) based on Xen's idea of the
time in the shared page.

Xen source repo change info:
ChangeSet
2004/09/22 13:47:22+01:00 cl349@freefall.cl.cam.ac.uk
Fix time.

netbsd-2.0-xen-sparse/sys/arch/xen/xen/clock.c
2004/09/22 13:47:21+01:00 cl349@freefall.cl.cam.ac.uk +28 -3
Don't call hardclock on spurious timer interrupt and call hardclock
for missed interrupts.

netbsd-2.0-xen-sparse/sys/arch/xen/conf/XEN
2004/09/22 13:47:21+01:00 cl349@freefall.cl.cam.ac.uk +0 -1
Don't need custom HZ value any longer.

: ----------------------------------------------------------------------
 1.6 16-Jul-2004  tls branches: 1.6.2;
Set the system clock (via Xen) in resettodr(), if domain 0.

This has a couple of beneficial effects:

1) The TOD time will be preserved across boots, as one would expect.

2) Newly-started VMs will get the correct time according to domain 0.
Previously, since we never set the time back to Xen, each VM would
get the uncorrected system clock time, never even seeing any
changes made since the current startup.

I don't understand how XenoLinux slaves the other domain's clocks to
the Xen "wall clock" time that we're setting here, so I haven't even
tried. But now we can run ntpd in each domain without a huge offset
at boot caused by never updating the wall clock time at all...
 1.5 24-Apr-2004  cl Let event_set_handler set the void * argument to event handlers.
 1.4 17-Apr-2004  cl use nanoseconds since boot counter for delay routine
 1.3 17-Apr-2004  cl - microtime support
- don't report cpu speed in startrtclock()
 1.2 10-Apr-2004  cl get the time from Xen
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.3 23-Sep-2004  jmc Pullup rev 1.7 (requested by tls in ticket #871)

Changes to improve timekeeping on NetBSD/xen by actually adjusting the time
correctly (calling hardclock as needed, not just blindly every time Xen
schedules us) based on Xens idea of the time in the shared page.
 1.1.2.2 17-Jul-2004  he Pull up revision 1.6 (requested by tls in ticket #671):
Set the system clock (via Xen) in resettodr(), if we are
running in domain 0, to improve timekeeping.
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.5 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.6.2.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.6.2.6 01-Apr-2005  skrll Sync with HEAD.
 1.6.2.5 24-Sep-2004  skrll Sync with HEAD.
 1.6.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.6.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.6.2.2 03-Aug-2004  skrll Sync with HEAD
 1.6.2.1 16-Jul-2004  skrll file clock.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.7.8.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.7.6.1 29-Apr-2005  kent sync with -current
 1.7.4.2 18-Jan-2005  bouyer - use the right test for dom0
- shadow_system_time is updated every xen's clock tick, which makes it
unsuitable for low delay(). For delays less than 0.5s, use cpu_counter()
instead to get accurate delays.
 1.7.4.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.9.2.10 16-Sep-2006  ghen Pull up following revision(s) (requested by jld in ticket #1515):
sys/arch/xen/xen/clock.c: revision 1.27
inittodr: if xen3, add processed_system_time (system_time) to
shadow_tv (wc_sec/nsec) as linux guest does.
 1.9.2.9 08-Sep-2006  ghen Pull up following revision(s) (requested by jld in ticket #1506):
sys/arch/xen/xen/clock.c: revision 1.26
Fix accidentally unsigned comparison, which was causing the timer event
handler to run ~forever in the face of slightly unexpected input from
Xen (system_time apparently going backwards). Pointed out by kardel@.
 1.9.2.8 08-Sep-2006  ghen Pull up following revision(s) (requested by jld in ticket #1505):
sys/arch/xen/xen/clock.c: revision 1.28
don't define XEN_CLOCK_DEBUG automatically for DEBUG.
it isn't safe to do printf in microtime().
 1.9.2.7 15-Jun-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1374):
sys/arch/xen/xen/clock.c: revision 1.19
sys/arch/xen/xen/if_xennet.c: revision 1.45
sys/arch/xen/xen/ctrl_if.c: revision 1.13
Add some missing memory barriers I noticed.
Discussed on port-xen.
 1.9.2.6 15-Jun-2006  tron Apply patch (requested by bouyer in ticket #1372):
- Implement a xen-specific cc_microtime, which properly handles the
Xen clock implementation. The MI cc_microtime could cause the clock
to jump forward by up to 35mn.
- Have dom0 periodically set the Xen's time of day clock. This makes
sure domUs start with the right time.
 1.9.2.5 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.9.2.4 29-Jun-2005  tron branches: 1.9.2.4.2;
Pull up revision 1.13 (requested by bouyer in ticket #510):
Use the time provided by the hypervisor instead of our global time variable
for xen_microtime(). This match more closely what is done on a real i386
(where we read the RTC), and seems to fix gettimeofday() sometime going
backward by several seconds for me.
 1.9.2.3 28-May-2005  tron Pull up revision 1.12 (requested by bouyer in ticket #354):
Reset the microtime state in inittodr(), so that the next clock tick will
initialise the microtime state again. it was initialised at the first
clock tick before time of day was set. Should fix port-xen/29846.
Also, try harder to call microset() once a second; the clock interrupt handler
can be called more often than HZ in some case.
 1.9.2.2 28-Apr-2005  tron Pull up revision 1.11 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.9.2.1 28-Apr-2005  tron Pull up revision 1.10 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.9.2.4.2.1 08-Sep-2006  ghen Pull up following revision(s) (requested by jld in ticket #1506):
sys/arch/xen/xen/clock.c: revision 1.26 via patch
Fix accidentally unsigned comparison, which was causing the timer event
handler to run ~forever in the face of slightly unexpected input from
Xen (system_time apparently going backwards). Pointed out by kardel@.
 1.13.2.6 21-Jan-2008  yamt sync with head
 1.13.2.5 07-Dec-2007  yamt sync with head
 1.13.2.4 27-Oct-2007  yamt sync with head.
 1.13.2.3 03-Sep-2007  yamt sync with head.
 1.13.2.2 30-Dec-2006  yamt sync with head.
 1.13.2.1 21-Jun-2006  yamt sync with head.
 1.15.2.2 18-Feb-2006  yamt sync with head.
 1.15.2.1 01-Feb-2006  yamt sync with head.
 1.16.2.1 09-Sep-2006  rpaulo sync with head
 1.17.6.2 11-May-2006  elad sync with head
 1.17.6.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.17.4.10 14-Sep-2006  yamt sync with head.
 1.17.4.9 03-Sep-2006  yamt sync with head.
 1.17.4.8 11-Aug-2006  yamt sync with head
 1.17.4.7 11-Jul-2006  yamt revert the previous because it was on a wrong branch.
 1.17.4.6 11-Jul-2006  yamt inittodr: if xen3, add processed_system_time (system_time) to
shadow_tv (wc_sec/nsec) as linux guest does.
 1.17.4.5 26-Jun-2006  yamt sync with head.
 1.17.4.4 24-May-2006  yamt sync with head.
 1.17.4.3 11-Apr-2006  yamt sync with head
 1.17.4.2 01-Apr-2006  yamt sync with head.
 1.17.4.1 13-Mar-2006  yamt sync with head.
 1.17.2.2 01-Jun-2006  kardel Sync with head.
 1.17.2.1 22-Apr-2006  simonb Sync with head.
 1.18.2.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.18.2.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.23.2.1 19-Jun-2006  chap Sync with head.
 1.25.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.27.2.1 10-Sep-2006  riz Pull up following revision(s) (requested by jld in ticket #143):
sys/arch/xen/xen/clock.c: revision 1.28
don't define XEN_CLOCK_DEBUG automatically for DEBUG.
it isn't safe to do printf in microtime().
 1.28.2.1 18-Nov-2006  ad Sync with head.
 1.30.2.1 10-Dec-2006  yamt sync with head.
 1.34.18.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.34.16.3 03-Oct-2007  garbled Sync with HEAD
 1.34.16.2 26-Jun-2007  garbled Sync with HEAD.
 1.34.16.1 22-May-2007  matt Update to HEAD.
 1.34.10.1 11-Jul-2007  mjf Sync with head.
 1.34.8.3 03-Dec-2007  ad Sync with HEAD.
 1.34.8.2 15-Jul-2007  ad Sync with head.
 1.34.8.1 27-May-2007  ad Sync with head.
 1.34.4.1 17-May-2007  yamt sync with head.
 1.34.2.2 16-Jul-2007  liamjfoy Pull up following revision(s) (requested by jld in ticket #764):
sys/arch/xen/xen/clock.c: revision 1.37
Correct regression in xen3 wall-time reading accidentally committed in r1.35.
(Discussed on port-xen.)
 1.34.2.1 10-Jun-2007  bouyer Pull up following revision(s) (requested by jld in ticket #711):
sys/arch/xen/xen/clock.c: revision 1.35 via patch
sys/arch/xen/include/types.h: revision 1.5 via patch
sys/arch/xen/i386/machdep.c: revision 1.38 via patch
sys/arch/xen/conf/files.xen: revision 1.60 via patch
sys/arch/xen/include/cpu.h: revision 1.19 via patch
Convert the xen port to use timecounters; ok'ed by bouyer@.
 1.39.14.2 13-Nov-2007  bouyer Sync with HEAD
 1.39.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.39.10.3 23-Mar-2008  matt sync with HEAD
 1.39.10.2 09-Jan-2008  matt sync with HEAD
 1.39.10.1 06-Nov-2007  matt sync with HEAD
 1.39.8.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.39.8.1 28-Oct-2007  joerg Sync with HEAD.
 1.41.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.41.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.42.6.2 19-Jan-2008  bouyer Sync with HEAD
 1.42.6.1 10-Jan-2008  bouyer Sync with HEAD
 1.44.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.44.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.45.2.1 18-May-2008  yamt sync with head.
 1.46.8.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.46.2.5 11-Aug-2010  yamt sync with head.
 1.46.2.4 11-Mar-2010  yamt sync with head
 1.46.2.3 19-Aug-2009  yamt sync with head.
 1.46.2.2 20-Jun-2009  yamt sync with head
 1.46.2.1 04-May-2009  yamt sync with head.
 1.47.8.1 21-Apr-2010  matt sync to netbsd-5
 1.47.4.1 19-Jun-2009  snj Pull up following revision(s) (requested by bouyer in ticket #816):
sys/arch/amd64/conf/files.amd64: revision 1.68
sys/arch/i386/conf/files.i386: revision 1.350
sys/arch/x86/include/rtc.h: revision 1.1
sys/arch/x86/isa/clock.c: revision 1.33
sys/arch/x86/isa/rtc.c: revision 1.1
sys/arch/xen/conf/files.xen: revision 1.100
sys/arch/xen/xen/clock.c: revision 1.50 via patch
Split mc146818-related functions from clock.c into rtc.c.
Call rtc_set_ymdhms() from xen/xen/clock.c:xen_rtc_set() for xen3 dom0
kernels as the Xen3 hypervisor doesn't write the new date/time to the CMOS
by itself.
Now a XEN3_DOM0 kernel properly updates the CMOS time.
 1.47.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.49.2.7 28-Aug-2011  jym Put some assertions to check values of the VIRQ <> event channels mappings.

Fix the VIRQ_TIMER per-cpu translations, so that save/restore does not
choke on event channel being "-1" anymore (ends badly 99,9% of the time
when used as an index...)

Some KNF and white space fixes.
 1.49.2.6 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.49.2.5 24-Oct-2010  jym Sync with HEAD
 1.49.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.49.2.3 01-Nov-2009  jym Sync with HEAD.
 1.49.2.2 23-Jul-2009  jym Sync with HEAD.
 1.49.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.53.4.1 30-May-2010  rmind sync with head
 1.53.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.54.6.7 22-Oct-2011  bouyer More improvements:
- Precise what tmutex protects
- springle some KASSERT(mutex_owned(&tmutex))
- the system time uses the CPU timecounter to get some precision;
don't mix local CPU timecounter with VCPU0's time, it won't work well.
Always use curcpu()'s time.
 1.54.6.6 22-Oct-2011  bouyer Really use HYPERVISOR_block()
 1.54.6.5 22-Oct-2011  bouyer Various clock fixes:
- xen_get_timecount() is CPU specific, so get the time from this
virtual CPU and not VCPU0. Also keep xen_clock_bias per-cpu.
- The periodic timer looks buggy, we stop receiving events after a (short)
while. Linux is using the one-shoot timer so use it too (and disable
the periodic timer), but rearm it from the event handler, not idle_block()
as the later may not be called if the CPU is busy, resulting it clock
event loss.
- use HYPERVISOR_block() in idle_block() again, now that interrupts are
working properly.
 1.54.6.4 18-Sep-2011  cherry Register the clock handler at the appropriate ipl (IPL_CLOCK). Cleanup the idle_block()
 1.54.6.3 23-Aug-2011  cherry MP-fy clock. per-cpu hardclock(). Use the periodic timer, instead of the one-shot reload.

idle_loop() does not block the domain anymore. Instead, it yields to the hypervisor(). We can thus remove the one-shot "wakeup" timer.
 1.54.6.2 25-Jul-2011  cherry Do not register the same callback more than once.
 1.54.6.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.56.2.1 17-Apr-2012  yamt sync with head
 1.57.4.1 18-Feb-2012  mrg merge to -current.
 1.61.2.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #28):
sys/arch/xen/include/hypervisor.h: revision 1.38
sys/arch/xen/xen/hypervisor.c: revision 1.61
sys/arch/xen/xen/clock.c: revision 1.62
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
 1.62.14.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.62.2.1 03-Dec-2017  jdolecek update from HEAD
 1.63.2.1 09-Jul-2016  skrll Sync with HEAD
 1.65.2.5 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.65.2.4 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.65.2.3 28-Jul-2018  pgoyette Sync with HEAD
 1.65.2.2 25-Jun-2018  pgoyette Sync with HEAD
 1.65.2.1 21-May-2018  pgoyette Sync with HEAD
 1.67.2.3 21-Apr-2020  martin Sync with HEAD
 1.67.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.67.2.1 10-Jun-2019  christos Sync with HEAD
 1.78.4.2 13-Oct-2024  martin Pull up following revision(s) (requested by riastradh in ticket #1904):

sys/arch/xen/xen/xen_clock.c: revision 1.19 (patch)
(applied to ..../clock.c)

xen_rtc_set: Zero-initialize op before passing to hypervisor.

PR port-xen/58362
 1.78.4.1 27-Jul-2023  martin Pull up following revision(s) (requested by bouyer in ticket #1668):

sys/arch/xen/xen/xen_clock.c: revision 1.10
sys/arch/xen/xen/xen_clock.c: revision 1.12
(applied to sys/arch/xen/xen/clock.c)

Unmask event after VCPUOP_stop_periodic_timer and
initializing ci->ci_xen_hardclock_systime_ns, to avoid a possible race with
xen_timer_handler()

Unmask event after arming the one-shot timer in clock initialisation,
to avoid a possible race with xen_timer_handler() updating
ci_xen_hardclock_systime_ns while we're reading it.

Pointed out by Taylor R Campbell
 1.80.6.3 16-Apr-2020  bouyer Rename xen/xen/clock.c to xen_clock.c, so that it can be compiled
with x86/isa/clock.c
call events_default_setup() from hypervisor_match to that event
arrays are properly initialised.
Use xen_delay() and xen's timecounter for PVHVM.
 1.80.6.2 12-Apr-2020  bouyer Get rid of xen-specific ci_x* interrupt handling:
- use the general SIR mechanism, reserving 3 more slots for IPL_VM, IPL_SCHED
and IPL_HIGH
- remove specific handling from C sources, or change to ipending
- convert IPL number to SIR number in various places
- Remove XUNMASK/XPENDING in assembly or change to IUNMASK/IPENDING
- remove Xen-specific ci_xsources, ci_xmask, ci_xunmask, ci_xpending from
struct cpu_info
- for now remove a KASSERT that there are no pending interrupts in
idle_block(). We can get there with some software interrupts pending
in autoconf XXX needs to be looked at.
 1.80.6.1 11-Apr-2020  bouyer Move softint and preemtion-related functions out of x86/x86/intr.c to
its own file, x86/x86/x86_softintr.c
Add x86/x86/x86_softintr.c for native and XenPV
Make sure XenPV also check ci_ioending, which is used for softints.
Switch XenPV to fast softints and allow kernel preemption.
kpreempt_disable() before calling pmap_changeprot_local()
run xen_wallclock_time() and xen_global_systime_ns() at splshed() to
avoid being interrupted.

XXX amd64 lock stubs are racy for XPENDING
 1.3 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.2 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up file removal (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.22 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.21 16-Jan-2009  jym branches: 1.21.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.20 18-Dec-2008  cegger remove unused malloc.h
 1.19 21-Oct-2008  cegger branches: 1.19.2;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.18 11-Jan-2008  bouyer branches: 1.18.6; 1.18.10; 1.18.16;
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.17 22-Nov-2007  bouyer branches: 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 04-Mar-2007  christos branches: 1.14.2; 1.14.4; 1.14.10;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.13 17-Mar-2006  jld branches: 1.13.14;
Add some missing memory barriers I noticed.
Discussed on port-xen.
 1.12 15-Jan-2006  bouyer branches: 1.12.2; 1.12.4; 1.12.6; 1.12.8; 1.12.10;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.11 11-Dec-2005  christos branches: 1.11.2;
merge ktrace-lwp.
 1.10 01-Jun-2005  yamt branches: 1.10.2;
appease -Wcast-qual.
 1.9 31-May-2005  yamt remove local prototypes of printk.
 1.8 20-Apr-2005  bouyer Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.

Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.7 17-Apr-2005  bouyer Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.6 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.5 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4 26-Mar-2005  bouyer branches: 1.4.2;
Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.3 17-Mar-2005  bouyer print the IRQ used by the domain controller.
 1.2 09-Mar-2005  bouyer branches: 1.2.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 13-Dec-2004  bouyer branches: 1.1.2; 1.1.4; 1.1.6;
file ctrl_if.c was initially added on branch bouyer-xen2.
 1.1.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.4.1 29-Apr-2005  kent sync with -current
 1.1.2.3 09-Mar-2005  bouyer Add CVS Id.
 1.1.2.2 18-Jan-2005  bouyer - Make compile for dom0ops
- Register ctrl_if_interrupt at IPL_HIGH + 2 so that ddb gets input in some
cases
XXX needs to be revised, we have a race condition in xencons !
 1.1.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.2.2.10 15-Jun-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1374):
sys/arch/xen/xen/clock.c: revision 1.19
sys/arch/xen/xen/if_xennet.c: revision 1.45
sys/arch/xen/xen/ctrl_if.c: revision 1.13
Add some missing memory barriers I noticed.
Discussed on port-xen.
 1.2.2.9 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.2.8 18-Jun-2005  tron Pull up revision 1.9 (requested by yamt in ticket #470):
remove local prototypes of printk.
 1.2.2.7 18-Jun-2005  tron Pull up revision 1.10 (requested by yamt in ticket #466):
appease -Wcast-qual.
 1.2.2.6 28-Apr-2005  tron Pull up revision 1.8 (requested by bouyer in ticket #193):
Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.
Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.2.2.5 28-Apr-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.2.2.4 28-Apr-2005  tron Pull up revision 1.5 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2.2.3 25-Apr-2005  tron Pull up revision 1.7 (requested by bouyer in ticket #187):
Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.2.2.2 30-Mar-2005  tron Pull up revision 1.4 (requested by bouyer in ticket #72):
Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.2.2.1 20-Mar-2005  tron Pull up revision 1.3 (requested by bouyer in ticket #31):
print the IRQ used by the domain controller.
 1.4.2.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.4.2.2 01-Apr-2005  skrll Sync with HEAD.
 1.4.2.1 26-Mar-2005  skrll file ctrl_if.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.10.2.4 21-Jan-2008  yamt sync with head
 1.10.2.3 07-Dec-2007  yamt sync with head
 1.10.2.2 03-Sep-2007  yamt sync with head.
 1.10.2.1 21-Jun-2006  yamt sync with head.
 1.11.2.1 01-Feb-2006  yamt sync with head.
 1.12.10.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.12.8.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.12.6.1 01-Apr-2006  yamt sync with head.
 1.12.4.1 22-Apr-2006  simonb Sync with head.
 1.12.2.1 09-Sep-2006  rpaulo sync with head
 1.13.14.1 12-Mar-2007  rmind Sync with HEAD.
 1.14.10.1 03-Oct-2007  garbled Sync with HEAD
 1.14.4.1 11-Jul-2007  mjf Sync with head.
 1.14.2.2 03-Dec-2007  ad Sync with HEAD.
 1.14.2.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.3 23-Mar-2008  matt sync with HEAD
 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 18-Feb-2008  mjf Sync with HEAD.
 1.16.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.17.6.1 08-Jan-2008  bouyer Make XEN kernels build again.
 1.18.16.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.18.10.2 19-Aug-2009  yamt sync with head.
 1.18.10.1 04-May-2009  yamt sync with head.
 1.18.6.1 17-Jan-2009  mjf Sync with HEAD.
 1.19.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.21.2.1 01-Nov-2009  jym Sync with HEAD.
 1.6 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.5 07-May-2004  cl branches: 1.5.2; 1.5.6; 1.5.8; 1.5.10;
Reboot machine when hypervisor signals "die" event.
 1.4 24-Apr-2004  cl Let event_set_handler set the void * argument to event handlers.
 1.3 17-Apr-2004  cl - fix event dispatching for event 0
- use struct trapframe instead of struct pt_regs
 1.2 11-Apr-2004  cl catch up with arch/x86/x86/intr.c
1.15/kochi
use designated initializer for struct pic initializers.
just for readability.

update the xenev_pic initializer as well
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.5 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.5.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.5.8.1 29-Apr-2005  kent sync with -current
 1.5.6.1 18-Jan-2005  bouyer Remove unused files.
 1.5.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.5.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.5.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.5.2.2 03-Aug-2004  skrll Sync with HEAD
 1.5.2.1 07-May-2004  skrll file events.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.100 07-Sep-2022  knakahara NetBSD/x86: Raise the number of interrupt sources per CPU from 32 to 56.

There has been no objection for three years.
https://mail-index.netbsd.org/port-amd64/2019/09/22/msg003012.html
Implemented by nonaka@n.o, updated by me.
 1.99 25-May-2022  bouyer PVH and HVM guests can easily have more than XEN_LEGACY_MAX_VCPUS (32) cpus.
Support up to HVM_MAX_VCPUS (256). This requires resizing a few arrays in
evtchn.c, and using
VCPUOP_register_vcpu_info for vcpuid >= XEN_LEGACY_MAX_VCPUS
Tested with 96 vCPUs.
 1.98 24-May-2022  bouyer Some devices (e.g. ixg in MSI-X mode) don't to have their handlers called
when no interrupt are pending. So add an extra ih_pending field
to struct intrhand, which is incremeted when the handler is not called because
of IPL level and reset to 0 when called. Check this in Xen's resume
assembly to call only handlers that are really pending.
 1.97 19-May-2022  bouyer Restore de EOI mechanism for pirq, using the newer hypervisor interface.
It is needed.
Hopefully fixes kern/56291, kern/56793, kern/55667
 1.96 15-Nov-2020  bouyer Don't restrict debug event to XENPV, it's also usefull for PVH/PVHVM
 1.95 13-May-2020  jdolecek branches: 1.95.2;
don't reinitialize mutexes/cv on resume

part of PR port-xen/55207
 1.94 07-May-2020  bouyer Change event_set_handler() to take the target CPU parameter. If ci is NULL,
event_set_handler() will choose the CPU and bind the event.
If ci is not NULL the caller is responsible for binding the event.
Use a IPI xcall to register the handlers if needed.
pull in a hack from x86 to force pirq handlers to be mpsafe if registered at
a level != IPL_VM. This is for the com at isa interrupt handler, which
registers at IPL_HIGH and has to way to tell it's mpsafe (taking
KERNEL_LOCK at IPL_HIGH causes deadlocks on MP systems).
 1.93 06-May-2020  bouyer remove the per-channel spin lock and instead make sure that events
add/remove will only be done on the CPU the handler is bound to, with
interrupts disabled.
Should be similar to the native x86 interrupts add/remove.
 1.92 04-May-2020  jdolecek add support for using MSI for XenPV Dom0

use PHYSDEVOP_map_pirq to get the pirq/gsi for MSI/MSI-X, switch also INTx
to use it instead of PHYSDEVOP_alloc_irq_vector

MSI confirmed working with single-vector MSI for wm(4), ahcisata(4), bge(4)

XXX added some provision for MSI-X, but it doesn't actually work (no interrupts
delivered), needs some further investigation; disable MSI-X for XENPV
via flag in x86/pci/pci_machdep.c
 1.91 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.90 21-Apr-2020  jdolecek convert to newer HYPERVISOR_physdev_op() interface, now command and the
arg are separate arguments - this is needed for newer physdev_op commands

remove code for PHYSDEVOP_IRQ_UNMASK_NOTIFY, it is obsolete since
interface version 0x00030202 and is unsupported by newer versions of Xen

confirmed working on amd64 Dom0, i386 compile-tested only
 1.89 13-Apr-2020  bouyer By default, events are bound to CPU 0 (exept for IPIs and VTIMERs which
are bound to a different CPU at creation time).
Recent MI changes caused the scheduler to choose a different CPU when
probing and attaching xennet devices (I guess it's the xenbus thread which
runs on a different CPU). This cause the callback to be called on a different
CPU than the one expected by the kernel, and the event is ignored.
It is handled when the clock causes the callback to be called on the right
CPU, which is why xennet still run, but slowly.

Change event_set_handler() to do a EVTCHNOP_bind_vcpu if requested to,
and make sure we don't do it for IPIs and VIRQs (for theses, the op fails).
 1.88 06-Apr-2020  jdolecek branches: 1.88.2;
add known_mpsafe parameter also to pirq_establish(), and pass the parameter
to underlying event_set_handler()
 1.87 06-Apr-2020  jdolecek remove restriction on interrupt level for MP-safe interrupt handlers
 1.86 09-May-2019  bouyer sti/cli are not allowed on Xen, we have to clear/set a bit in the
shared page. Revert x86_disable_intr/x86_enable_intr to plain function
calls on XENPV.
While there, clean up unused functions and macros, and change cli()/sti()
macros to x86_disable_intr/x86_enable_intr.
Makes Xen domU boot again
(http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/)
 1.85 13-Feb-2019  cherry Further restrict the scope of XENPV to relevant parts.
 1.84 13-Feb-2019  cherry In preparation for debut-ing PVHVM mode:

- Make the struct intrstub uniform across native and XEN.
- Introduce vector callback entrypoints for PVHVM mode.
 1.83 25-Dec-2018  cherry Excise XEN specific code out of x86/x86/intr.c into xen/x86/xen_intr.c

While at it, separate the source function tracking so that the interrupt
paths are truly independant.

Use weak symbol exporting to provision for future PVHVM co-existence
of both files, but with independant paths. Introduce assembler code
such that in a unified scenario, native interrupts get first priority
in spllower(), followed by XEN event callbacks. IPL management and
semantics are unchanged - native handlers and xen callbacks are
expected to maintain their ipl related semantics.

In summary, after this commit, native and XEN now have completely
unrelated interrupt handling mechanisms, including
intr_establish_xname() and assembler stubs and intr handler
management.

Happy Christmas!
 1.82 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.81 23-Sep-2018  cherry Encapsulate pre-processing of registered (pirq,evtchn) pair in
preparation for API reorg.
 1.80 24-Jun-2018  jdolecek branches: 1.80.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.79 13-Dec-2017  bouyer branches: 1.79.2;
Fixes for physical interrupts on Xen:
- do not cast int * to intr_handle_t *, they're not the same size
- legacy_irq is not always -1 for ioapic interrupts, test pic_type instead
- change irq2port[] to hold (port + 1) so that 0 is an invalid value
- add KASSERTs to make sure vect, port or irq values extracted from arrays are
valid (or that they are invalid before write)
- for the !ioapic case, we still need to do PHYSDEVOP_ASSIGN_VECTOR and
bind_pirq_to_evtch().

now XEN3_DOM0 boots again
 1.78 11-Nov-2017  riastradh Back out previous KASSERTMSG for now.

The immediate problem appears to have been elsewhere, in the wrong
interrupt level passed by the Xen-specific intr_establish to
event_set_handler.

We should maybe restore these assertions later, but at least one was
violated before cherry's interrupt rototill anyway.
 1.77 11-Nov-2017  riastradh Add kasserts to help diagnose xen interrupt level bug.

https://mail-index.netbsd.org/tech-kern/2017/11/09/msg022571.html
 1.76 11-Nov-2017  riastradh #if DIAGNOSTIC panic ---> KASSERTMSG
 1.75 10-Nov-2017  riastradh Fix theoretical race in xen_evtchn_trymask.

This now matches ioapic_trymask:

- Caller masks CPU interrupts while the PIC's interrupts are unmasked.
- Trymask masks the PIC's interrupts, and tests whether any were
pending that we missed because the CPU interrupts are masked.

==> If there were any pending, trymask unmasks PIC's interrupts and
returns false.

==> If not, interrupts are masked on the PIC and CPU and there were
none pending and trymask returns true.

No functional change right now because cpu_intr_redistribute is
currently disabled on Xen.

ok cherry
 1.74 04-Nov-2017  cherry Add a PIC_XEN abstraction to evtchn.c

This allows us to get XEN interrupt code closer to unification to x86/intr.c
 1.73 16-Jul-2017  cherry branches: 1.73.2;
Unify the xen and native x86/ interrupt setup functions and
spl traversal data structures.

This is towards PVHVM.
 1.72 16-Jul-2017  cherry Add a glue function to inspect event channel<->legacy IRQ mappings.
This is towards getting xen to use more x86/ "native" code.
 1.71 14-Mar-2015  bouyer Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.70 03-Dec-2013  bouyer branches: 1.70.4; 1.70.6;
Remove the "evtchn_do_event: handler %p didn't lower ipl %d %d\n" printf.
With help from Robert Elz we've finally figured out what's going on, and
it actually isn't a bug in the handler, but related to spin mutexes.
When a spin mutex is released, the IPL isn't lowered back if the
curcpu is holding other spin mutexes. This is because mutexes may not
be released in order (and, in this case, the CPU in interrupted while
it holds a spin mutex at IPL < IPL_SCHED).
Also remove the test and resetting the IPL, it will be reset anyway
inside the loop, or at the end of the loop.
 1.69 13-Jan-2013  bouyer branches: 1.69.2;
Re-apply
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039125.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039126.html
they're not involved in i386 domU hang shown by ATF.
 1.68 12-Jan-2013  bouyer Revert these commits from november 2012:
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039125.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039126.html
http://mail-index.netbsd.org/source-changes/2012/11/25/msg039142.html

they cause a i386PAE domU to hang while running ATF tests, as shown in
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/

(we should pay more attention to test results, myself first).
 1.67 12-Jan-2013  bouyer Back out this commit:
http://mail-index.netbsd.org/source-changes/2012/12/28/msg039950.html
which cause a panic when running tests on amd64, as shown on:
http://www-soc.lip6.fr/~bouyer/NetBSD-tests/xen/HEAD/
(i386 hangs for unrelated reasons).
 1.66 28-Dec-2012  cherry Simplify the xen event handler callback by:
- moving the interrupt handler callback traversal into a separate
function.
- using evt_iterate_bits() to scan through the pending bitfield
- removing cross-cpu pending actions - events recieved on the wrong
vcpu are re-routed via hypervisor_send_event().
- simplifying nested while() loops by encapsulating them in
equivalent functions.

Many thanks for multiple reviews by bouyer@ and jym@
 1.65 25-Nov-2012  cherry Do not escalate the local spl while taking the interrupt handler list
lock. Re-entrance is protected against within the events handler
framework by toggling interrupts/events, so we don't need (and cannot
use) the spl framework in that path. The other consumers (interrupt
registration/removal code) are not spl sensitive wrt this lock.
 1.64 25-Nov-2012  cherry Make hypervisor_set_ipending() and its consumers cpu unaware. This syncs syntax with semantics
 1.63 25-Nov-2012  cherry hypervisor_set_ipending() should not set the pending flag across cpus. The reason for this is that the pending flag update is not atomic, or protected by a lock. Since its current (mis)use in evtchn_do_event() across cpus is to notify the remote cpu of an interrupt, we use hypervisor_send_event() instead, to trigger an event on the remote cpu, which in turn invokes evtchn_do_event() on that cpu and DTRT on it.

On the local cpu, we protect hypervisor_set_ipending() from re-entry via cli(). Remove a redundant and unprotected call to hypervisor_set_ipending().

Thanks to jym@ for spotting this discrepancy while reviewing a related patch.

See: http://mail-index.netbsd.org/tech-kern/2012/11/12/msg014374.html
 1.62 12-Feb-2012  jym branches: 1.62.2; 1.62.6; 1.62.8; 1.62.10;
Xen MP merge introduced MP safety around ipl handlers. When removing an
event handler, check handler's function and arguments against the real
ones, not the ones from wrappers.

This fixes a bug where !mpsafe events could not be removed from the handler
chain, thereby blocking suspension of a domU.

ok releng@.
 1.61 08-Dec-2011  cherry kmem_free() the appropriate size.

Thanks cegger@
 1.60 07-Dec-2011  cegger replace malloc() with kmem_zalloc() per request from cherry.
While here fix error path.

XXX: The size allocated and freed for interrupt handlers looks
inconsistent to me.
 1.59 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.58 07-Dec-2011  cegger build fix: add back <sys/malloc.h>. malloc(9) is still in use.
 1.57 07-Dec-2011  cherry Move to kmem_zalloc() instead of malloc().
 1.56 19-Nov-2011  cherry branches: 1.56.4;
[merging from cherry-xenmp] bring in bouyer@'s changes via:
http://mail-index.netbsd.org/source-changes/2011/10/22/msg028271.html
From the Log:
Log Message:
Various interrupt fixes, mainly:
keep a per-cpu mask of enabled events, and use it to get pending events.
A cpu-specific event (all of them at this time) should not be ever masked
by another CPU, because it may prevent the target CPU from seeing it
(the clock events all fires at once for example).
 1.55 21-Sep-2011  cegger branches: 1.55.2;
Initialize mutex before use. Lets me boot a dom0 kernel again
without a lockdebug panic.
 1.54 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.53 28-Aug-2011  jym VIRQ_TIMER virqs are allocated and tracked in a array
(virq_timer_to_evtch, indexed by cpuid) different from the
VIRQ <> event channel one (virq_to_evtch, indexed by event channel ID).

This is fine: fix a "harmless" bug that resulted in the event
channel of VIRQ_TIMER getting lost during bind as it was not stored
in the proper array.

"Harmless" because it is not critical for -current, however in the Xen
save/restore branch this completely cripples restore. Xen clock gets
suspended, but never comes back (fetched channel ID being invalid). Oops.

Add a small comment so we can better see the "get => allocate? => set"
chain of actions when binding/unbinding event channels.
 1.52 28-Aug-2011  jym KNF, white spaces and comment typo fixes.
 1.51 13-Aug-2011  cherry Use spin mutices correctly.
- Prune redundant splxx()/splx() pairs.
- Do not "leak" a mutex_spin_enter() via conditional return.

Thanks rmind@
 1.50 11-Aug-2011  cherry Make event/interrupt handling MP aware
 1.49 10-Aug-2011  cherry refactor the bitstring/mask operations to be behind an API. Make pending interrupt marking cpu aware.
 1.48 02-Jul-2011  jym Remove all return error checks for event_set_handler(...). It either
succeeds or end in panic.
 1.47 20-Dec-2010  matt branches: 1.47.6;
Move counting of faults, traps, intrs, soft[intr]s, syscalls, and nswtch
from uvmexp to per-cpu cpu_data and move them to 64bits. Remove unneeded
includes of <uvm/uvm_extern.h> and/or <uvm/uvm.h>.
 1.46 23-Oct-2009  snj branches: 1.46.4;
Drop 3rd and 4th clauses. Approved by cl@ and Keir Fraser (copyright
holders).
 1.45 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.44 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.43 27-Mar-2009  dyoung Explicitly #include <sys/device.h> and <sys/evcnt.h>. These files only
got the definitions they needed by chance, before.
 1.42 17-Dec-2008  cegger branches: 1.42.2;
kill MALLOC and FREE macros.
 1.41 13-Nov-2008  bouyer Remove a debug printf(). It will call splx(), which may panic in spllowwer()
on KASSERT(psl == 0);
 1.40 05-Nov-2008  bouyer evtchn_do_event(): in our C implementation of spllower(), if a handler fails
to restore the spl (shouldn't happen, but ...), we could end up with a
higther pending ipl set and never cleared because iplbit already
handled this level. while (iplmask != 0) {} would then never be true,
and we'd end up triggering KASSERT(iplbit != 0).
Now print the faultly handler and reset the IPL, and start the whole
pending IPL handling again if needed.
 1.39 24-Oct-2008  jym branches: 1.39.2; 1.39.4;
- 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.38 24-May-2008  bouyer branches: 1.38.4;
Remork the C implementation of spllower() in evtchn_do_event(), so that
callbacks are always called in decreasing IPL order. Although it's not
strictly a bug, it makes the code easier to read, and avoids processing
the whole IPL range several times.
 1.37 21-Apr-2008  cegger branches: 1.37.2; 1.37.4;
Access Xen's vcpu info structure per-CPU.
Tested on i386 and amd64 (both dom0 and domU) by me.
Xen2 tested (both dom0 and domU) by bouyer.
OK bouyer
 1.36 19-Apr-2008  cegger Use interrupt biglock wrapper as in x86/x86/intr.c
This change is based on http://mail-index.netbsd.org/port-amd64/2004/02/22/0000.html
OK bouyer
 1.35 14-Apr-2008  cegger branches: 1.35.2;
- use POSIX integer types
- ansify functions
 1.34 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.33 13-Mar-2008  bouyer Implement a C version of splx() in evtchn_do_event(). using plain splx()
here will reenable interrupts as a side effect, and we don't want it here.

It could cause some event handlers to run twice (which should be harmless),
and trap() to be called on the wrong LWP in doreti_checkast (which can
probably cause some damage).
 1.32 19-Feb-2008  bouyer branches: 1.32.2; 1.32.6;
The event bitmasks provided by the hypervisor are unsigned long (so 64bits
on amd64). Make sure to use the right type to store and manipulate them.
This fixes amd64, where basically any event channel > 31 was not working
(and you get there after starting/stopping a domU a few times). Things
would occasionally unwedge though the spllower() callbacks.
 1.31 19-Feb-2008  bouyer Fix xenevt to not call softint_schedule() above IPL_HIGH:
Register a ipl callback for IPL_HIGH.
if the current ipl level is too high, just record the event in a bitmap,
and record IPL_HIGH as pending. The callback will process the pending events.
 1.30 11-Jan-2008  bouyer 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.29 05-Jan-2008  bouyer Wrap test for debug_port in __predict_false()
 1.28 05-Jan-2008  bouyer Make sure debug_port isn't used before initialized by setting it to -1.
 1.27 13-Dec-2007  bouyer Remove obsolete code and comment.
 1.26 12-Dec-2007  bouyer cleanup the debug event handler to not use the IPL system at all. Fix
debug event storm on XEN2.
 1.25 03-Dec-2007  ad branches: 1.25.2; 1.25.4; 1.25.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.24 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.23 17-Oct-2007  garbled branches: 1.23.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.22 26-Sep-2007  ad branches: 1.22.2;
x86 changes for pcc and LKMs.

- Replace most inline assembly with proper functions. As a side effect
this reduces the size of amd64 GENERIC by about 120kB, and i386 by a
smaller amount. Nearly all of the inlines did something slow, or something
that does not need to be fast.
- Make curcpu() and curlwp functions proper, unless __GNUC__ && _KERNEL.
In that case make them inlines. Makes curlwp LKM and preemption safe.
- Make bus_space and bus_dma more LKM friendly.
- Share a few more files between the ports.
- Other minor changes.
 1.21 23-Sep-2007  bouyer branches: 1.21.2;
Ajust for Xen 3.1.0 public headers. From Christoph Egger in private mail.
 1.20 08-Dec-2006  yamt branches: 1.20.6; 1.20.14; 1.20.22; 1.20.24;
- pass intrframe by-pointer, not by-value.
- make i386 and xen use per-cpu interrupt stack.

xen part is reviewed by Manuel Bouyer.
 1.19 28-Sep-2006  bouyer Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.18 15-Jan-2006  bouyer branches: 1.18.18; 1.18.20;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.17 11-Dec-2005  christos branches: 1.17.2;
merge ktrace-lwp.
 1.16 19-Aug-2005  bouyer Make PCI devices usable for non-domain0 kernels. Based on patches sent by
Ceri Storey to port-xen, with some additionnal changes by me:
- include bus_dma.c, bus_space.c and pci_machdep.c if pci is defined
instead of dom0ops
- Make various initialisations, and probe/attach pci busses based on NPCI
instead of DOM0OPS
- in conf/files.xen, move xen-specific devices before non-xen specific devices
so that the xen-specific match function is called first, to avoid false
attachement from too liberal match function in non-xen code.
 1.15 15-Jul-2005  yamt evtchn_do_event: enable interrupts while calling interrupt handlers.
 1.14 28-Apr-2005  yamt branches: 1.14.2;
rename do_event to evtchan_do_event.
the former is too generic name and it actually hides a bug in xennetback.
 1.13 20-Apr-2005  bouyer Event handling optimisations:
- sort the ih_evt_handler list by IPL, higher first. Otherwise some handlers
would have been delayed, event if they could run at the current IPL.
- As ih_evt_handler is sorted, remove IPLs that have been processed for
an event when calling hypervisor_set_ipending()
- In hypervisor_set_ipending(), enter the event in ipl_evt_mask only
for the lowest IPL. As deffered IPLs are processed high to low,
this ensure that hypervisor_enable_event() will be called only when all
callbacks have been called for an event. We don't need the evtch_maskcount[]
counters any more.

Thanks to YAMAMOTO Takashi for ideas and feedback.
 1.12 20-Apr-2005  xtraeme Fix typo (vitual -> virtual)
 1.11 19-Apr-2005  bouyer Avoid a race between do_hypervisor_event() and stipending() that could
cause an event to be both handled and marked as pending, or being
marked as pending twice (triggering the diagnostic check
evtch_maskcount[port] == 0 in hypervisor_set_ipending):
mask and clear event by word of 32bit in do_hypervisor_event() or stipending(),
instead of by indiviual bits in do_event() or xenevt_event().
In addition this is marginally more efficient.
 1.10 18-Apr-2005  bouyer Remove unused code.
 1.9 18-Apr-2005  yamt don't unmask an event channel until all interrupts for it are served.
fix problems with shared (physical) interrupts.
 1.8 18-Apr-2005  yamt make this compilable with IRQ_DEBUG defined.
 1.7 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.6 16-Apr-2005  bouyer Remove functions that just call another one.
Inline a function used at only one place.
 1.5 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4 17-Mar-2005  bouyer branches: 1.4.2;
Protect various IRQ and event allocation/deallocations with splhigh().
Print the IRQ used for debug and misdirect events.
 1.3 11-Mar-2005  bouyer branches: 1.3.2;
increment couters at the right place, so that deffered interrupts are
accounted too.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 13-Dec-2004  bouyer branches: 1.1.2; 1.1.4; 1.1.6;
file evtchn.c was initially added on branch bouyer-xen2.
 1.1.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.4.1 29-Apr-2005  kent sync with -current
 1.1.2.6 09-Mar-2005  bouyer Store the interrupt's name in struct intrsource.is_evname, and use this for
the event counters. Now 'systat vm' displays usefull informations.
 1.1.2.5 08-Mar-2005  bouyer Add infrastructure to unregsiter event callbacks.
 1.1.2.4 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.1.2.3 21-Jan-2005  bouyer - Add a centralised handler for physical interrupts, which will call the
real handler. This is not necessery but is usefull for debug.
- make irq_needs_unmask_notify[] per-IRQ, instead of per physical IRQ
- use a static physdev_op_t for PHYSDEVOP_IRQ_UNMASK_NOTIFY (it never changes,
so no need to allocate a new one each time)
- call pirq_notify() from hypervisor_enable_irq().
 1.1.2.2 18-Jan-2005  bouyer Implement physical IRQ mapping.
 1.1.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.3.2.14 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.3.2.13 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #694):
sys/arch/xen/xen/hypervisor.c: revision 1.15
sys/arch/xen/xen/evtchn.c: revision 1.16
sys/arch/xen/conf/files.xen: revision 1.27
sys/arch/xen/i386/machdep.c: revision 1.18
Make PCI devices usable for non-domain0 kernels. Based on patches sent by
Ceri Storey to port-xen, with some additionnal changes by me:
- include bus_dma.c, bus_space.c and pci_machdep.c if pci is defined
instead of dom0ops
- Make various initialisations, and probe/attach pci busses based on NPCI
instead of DOM0OPS
- in conf/files.xen, move xen-specific devices before non-xen specific devices
so that the xen-specific match function is called first, to avoid false
attachement from too liberal match function in non-xen code.
 1.3.2.12 18-Aug-2005  tron Pull up revision 1.15 (requested by yamt in ticket #676):
evtchn_do_event: enable interrupts while calling interrupt handlers.
 1.3.2.11 01-May-2005  tron Pull up revision 1.14 (requested by yamt in ticket #238):
rename do_event to evtchan_do_event.
the former is too generic name and it actually hides a bug in xennetback.
 1.3.2.10 28-Apr-2005  tron Pull up revision 1.12 (requested by bouyer in ticket #194):
Fix typo (vitual -> virtual)
 1.3.2.9 28-Apr-2005  tron Pull up revision 1.13 (requested by bouyer in ticket #192):
Event handling optimisations:
- sort the ih_evt_handler list by IPL, higher first. Otherwise some handlers
would have been delayed, event if they could run at the current IPL.
- As ih_evt_handler is sorted, remove IPLs that have been processed for
an event when calling hypervisor_set_ipending()
- In hypervisor_set_ipending(), enter the event in ipl_evt_mask only
for the lowest IPL. As deffered IPLs are processed high to low,
this ensure that hypervisor_enable_event() will be called only when all
callbacks have been called for an event. We don't need the evtch_maskcount[]
counters any more.
Thanks to YAMAMOTO Takashi for ideas and feedback.
 1.3.2.8 28-Apr-2005  tron Pull up revision 1.11 (requested by bouyer in ticket #192):
Avoid a race between do_hypervisor_event() and stipending() that could
cause an event to be both handled and marked as pending, or being
marked as pending twice (triggering the diagnostic check
evtch_maskcount[port] == 0 in hypervisor_set_ipending):
mask and clear event by word of 32bit in do_hypervisor_event() or stipending(),
instead of by indiviual bits in do_event() or xenevt_event().
In addition this is marginally more efficient.
 1.3.2.7 28-Apr-2005  tron Pull up revision 1.10 (requested by bouyer in ticket #192):
Remove unused code.
 1.3.2.6 28-Apr-2005  tron Pull up revision 1.9 (requested by bouyer in ticket #192):
don't unmask an event channel until all interrupts for it are served.
fix problems with shared (physical) interrupts.
 1.3.2.5 28-Apr-2005  tron Pull up revision 1.8 (requested by bouyer in ticket #192):
make this compilable with IRQ_DEBUG defined.
 1.3.2.4 28-Apr-2005  tron Pull up revision 1.7 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.3.2.3 28-Apr-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #192):
Remove functions that just call another one.
Inline a function used at only one place.
 1.3.2.2 28-Apr-2005  tron Pull up revision 1.5 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.3.2.1 20-Mar-2005  tron Pull up revision 1.4 (requested by bouyer in ticket #32):
Protect various IRQ and event allocation/deallocations with splhigh().
Print the IRQ used for debug and misdirect events.
 1.4.2.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.4.2.2 01-Apr-2005  skrll Sync with HEAD.
 1.4.2.1 17-Mar-2005  skrll file evtchn.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.14.2.7 17-Mar-2008  yamt sync with head.
 1.14.2.6 27-Feb-2008  yamt sync with head.
 1.14.2.5 21-Jan-2008  yamt sync with head
 1.14.2.4 07-Dec-2007  yamt sync with head
 1.14.2.3 27-Oct-2007  yamt sync with head.
 1.14.2.2 30-Dec-2006  yamt sync with head.
 1.14.2.1 21-Jun-2006  yamt sync with head.
 1.17.2.1 01-Feb-2006  yamt sync with head.
 1.18.20.2 10-Dec-2006  yamt sync with head.
 1.18.20.1 22-Oct-2006  yamt sync with head
 1.18.18.2 12-Jan-2007  ad Sync with head.
 1.18.18.1 18-Nov-2006  ad Sync with head.
 1.20.24.3 23-Mar-2008  matt sync with HEAD
 1.20.24.2 09-Jan-2008  matt sync with HEAD
 1.20.24.1 06-Nov-2007  matt sync with HEAD
 1.20.22.3 09-Dec-2007  jmcneill Sync with HEAD.
 1.20.22.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.20.22.1 02-Oct-2007  joerg Sync with HEAD.
 1.20.14.1 03-Oct-2007  garbled Sync with HEAD
 1.20.6.2 03-Dec-2007  ad Sync with HEAD.
 1.20.6.1 09-Oct-2007  ad Sync with head.
 1.21.2.1 06-Oct-2007  yamt sync with head.
 1.22.2.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.23.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.23.2.2 27-Dec-2007  mjf Sync with HEAD.
 1.23.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.25.6.3 08-Jan-2008  bouyer Make XEN kernels build again.
 1.25.6.2 08-Jan-2008  bouyer Sync with HEAD
 1.25.6.1 13-Dec-2007  bouyer cleanup the way debug event is handled: make it bypass the IPL system
completely, it's called by shortcuts in the normal path because we want it to
be always called, even if the IPL is high.
Fix debug even recursion on XEN2
 1.25.4.1 13-Dec-2007  yamt sync with head.
 1.25.2.1 26-Dec-2007  ad Sync with head.
 1.32.6.3 17-Jan-2009  mjf Sync with HEAD.
 1.32.6.2 02-Jun-2008  mjf Sync with HEAD.
 1.32.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.32.2.1 24-Mar-2008  keiichi sync with head.
 1.35.2.2 04-Jun-2008  yamt sync with head
 1.35.2.1 18-May-2008  yamt sync with head.
 1.37.4.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.37.2.3 11-Mar-2010  yamt sync with head
 1.37.2.2 19-Aug-2009  yamt sync with head.
 1.37.2.1 04-May-2009  yamt sync with head.
 1.38.4.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.39.4.3 11-Dec-2013  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1890):
sys/arch/xen/xen/evtchn.c: revision 1.70

Remove the "evtchn_do_event: handler %p didn't lower ipl %d %d\n" printf
as analysis shows it actually isn't a bug in the handler, but related to
spin mutexes. Fixes port-xen/46313
 1.39.4.2 14-Nov-2008  snj branches: 1.39.4.2.6; 1.39.4.2.10;
Pull up following revision(s) (requested by bouyer in ticket #55):
sys/arch/xen/xen/evtchn.c: revision 1.41
Remove a debug printf(). It will call splx(), which may panic in spllowwer()
on KASSERT(psl == 0);
 1.39.4.1 11-Nov-2008  snj Pull up following revision(s) (requested by bouyer in ticket #39):
sys/arch/xen/xen/evtchn.c: revision 1.40
evtchn_do_event(): in our C implementation of spllower(), if a handler fails
to restore the spl (shouldn't happen, but ...), we could end up with a
higther pending ipl set and never cleared because iplbit already
handled this level. while (iplmask != 0) {} would then never be true,
and we'd end up triggering KASSERT(iplbit != 0).
Now print the faultly handler and reset the IPL, and start the whole
pending IPL handling again if needed.
 1.39.4.2.10.1 13-Dec-2013  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1890):
sys/arch/xen/xen/evtchn.c: revision 1.70

Remove the "evtchn_do_event: handler %p didn't lower ipl %d %d\n" printf
as analysis shows it actually isn't a bug in the handler, but related to
spin mutexes. Fixes port-xen/46313
 1.39.4.2.6.1 13-Dec-2013  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1890):
sys/arch/xen/xen/evtchn.c: revision 1.70

Remove the "evtchn_do_event: handler %p didn't lower ipl %d %d\n" printf
as analysis shows it actually isn't a bug in the handler, but related to
spin mutexes. Fixes port-xen/46313
 1.39.2.2 28-Apr-2009  skrll Sync with HEAD.
 1.39.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.42.2.9 28-Aug-2011  jym Put some assertions to check values of the VIRQ <> event channels mappings.

Fix the VIRQ_TIMER per-cpu translations, so that save/restore does not
choke on event channel being "-1" anymore (ends badly 99,9% of the time
when used as an index...)

Some KNF and white space fixes.
 1.42.2.8 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.42.2.7 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.42.2.6 10-Jan-2011  jym Sync with HEAD
 1.42.2.5 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.42.2.4 01-Nov-2009  jym Sync with HEAD.
 1.42.2.3 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.42.2.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.42.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.46.4.1 05-Mar-2011  rmind sync with head
 1.47.6.6 22-Oct-2011  bouyer Various interrupt fixes, mainly:
keep a per-cpu mask of enabled events, and use it to get pending events.
A cpu-specific event (all of them at this time) should not be ever masked
by another CPU, because it may prevent the target CPU from seeing it
(the clock events all fires at once for example).
 1.47.6.5 18-Sep-2011  cherry Use an IPI to re-route events to the cpu where the handler has been registered
 1.47.6.4 17-Aug-2011  cherry Pullup relevant changes from -current
 1.47.6.3 04-Aug-2011  cherry Send an ipi at IPL_HIGH to remote cpu, in order to get it to run spllower()
 1.47.6.2 04-Aug-2011  cherry first cut at per-cpu event handling
 1.47.6.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.55.2.4 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.55.2.3 23-Jan-2013  yamt sync with head
 1.55.2.2 16-Jan-2013  yamt sync with (a bit old) head
 1.55.2.1 17-Apr-2012  yamt sync with head
 1.56.4.1 18-Feb-2012  mrg merge to -current.
 1.62.10.1 17-Dec-2013  riz Pull up following revision(s) (requested by bouyer in ticket #995):
sys/arch/xen/xen/evtchn.c: revision 1.70
Remove the "evtchn_do_event: handler %p didn't lower ipl %d %d\n" printf.
With help from Robert Elz we've finally figured out what's going on, and
it actually isn't a bug in the handler, but related to spin mutexes.
When a spin mutex is released, the IPL isn't lowered back if the
curcpu is holding other spin mutexes. This is because mutexes may not
be released in order (and, in this case, the CPU in interrupted while
it holds a spin mutex at IPL < IPL_SCHED).
Also remove the test and resetting the IPL, it will be reset anyway
inside the loop, or at the end of the loop.
 1.62.8.1 17-Dec-2013  riz Pull up following revision(s) (requested by bouyer in ticket #995):
sys/arch/xen/xen/evtchn.c: revision 1.70
Remove the "evtchn_do_event: handler %p didn't lower ipl %d %d\n" printf.
With help from Robert Elz we've finally figured out what's going on, and
it actually isn't a bug in the handler, but related to spin mutexes.
When a spin mutex is released, the IPL isn't lowered back if the
curcpu is holding other spin mutexes. This is because mutexes may not
be released in order (and, in this case, the CPU in interrupted while
it holds a spin mutex at IPL < IPL_SCHED).
Also remove the test and resetting the IPL, it will be reset anyway
inside the loop, or at the end of the loop.
 1.62.6.3 03-Dec-2017  jdolecek update from HEAD
 1.62.6.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.62.6.1 25-Feb-2013  tls resync with head
 1.62.2.2 14-Apr-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1278):
sys/arch/xen/include/evtchn.h: revision 1.23
sys/arch/xen/xen/evtchn.c: revision 1.71
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.17
Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.62.2.1 17-Dec-2013  riz Pull up following revision(s) (requested by bouyer in ticket #995):
sys/arch/xen/xen/evtchn.c: revision 1.70
Remove the "evtchn_do_event: handler %p didn't lower ipl %d %d\n" printf.
With help from Robert Elz we've finally figured out what's going on, and
it actually isn't a bug in the handler, but related to spin mutexes.
When a spin mutex is released, the IPL isn't lowered back if the
curcpu is holding other spin mutexes. This is because mutexes may not
be released in order (and, in this case, the CPU in interrupted while
it holds a spin mutex at IPL < IPL_SCHED).
Also remove the test and resetting the IPL, it will be reset anyway
inside the loop, or at the end of the loop.
 1.69.2.1 18-May-2014  rmind sync with head
 1.70.6.2 28-Aug-2017  skrll Sync with HEAD
 1.70.6.1 06-Apr-2015  skrll Sync with HEAD
 1.70.4.1 18-Mar-2015  snj Pull up following revision(s) (requested by bouyer in ticket #618):
sys/arch/xen/include/evtchn.h: revision 1.23
sys/arch/xen/xen/evtchn.c: revision 1.71
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.17
Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.73.2.2 16-Jul-2017  cherry 2302677
 1.73.2.1 16-Jul-2017  cherry file evtchn.c was added on branch perseant-stdc-iso10646 on 2017-07-16 14:02:49 +0000
 1.79.2.4 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.79.2.3 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.79.2.2 30-Sep-2018  pgoyette Ssync with HEAD
 1.79.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.80.2.4 21-Apr-2020  martin Sync with HEAD
 1.80.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.80.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.80.2.1 10-Jun-2019  christos Sync with HEAD
 1.88.2.12 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.88.2.11 20-Apr-2020  bouyer Misc fixes after merge
 1.88.2.10 20-Apr-2020  bouyer Sync with HEAD
 1.88.2.9 19-Apr-2020  bouyer Move xen_ipi.c to XENPV only.
Make sure we don't need to send events to remote CPUs (outside of IPIs)
 1.88.2.8 19-Apr-2020  bouyer Add per-PIC callbacks for interrupt_get_devname(), interrupt_get_assigned()
and interrupt_get_count(). Implement Xen-specific callbacks for
PIC_XEN and use the x86 one for others.
In event_set_handler(), call intr_allocate_io_intrsource() so that
events appears in interrupt list (intrctl list).
 1.88.2.7 19-Apr-2020  bouyer Add a struct pic * member to struct intrhand.
This will be used for interrupt_get_count()
For Xen remplace pic_type with a pointer to the pic, and add a pointer
to intrhand, in struct pintrhand
Make event_set_handler return the pointer to struct intrhand.
Don't allocate a fake intrhand in xen_intr_establish_xname(), use the
one returned by event_set_handler().
 1.88.2.6 18-Apr-2020  bouyer If we get an event for another CPU just ignore it. The hypervisor will
callback on the other CPU anyway.
 1.88.2.5 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.88.2.4 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.88.2.3 12-Apr-2020  bouyer Get rid of xen-specific ci_x* interrupt handling:
- use the general SIR mechanism, reserving 3 more slots for IPL_VM, IPL_SCHED
and IPL_HIGH
- remove specific handling from C sources, or change to ipending
- convert IPL number to SIR number in various places
- Remove XUNMASK/XPENDING in assembly or change to IUNMASK/IPENDING
- remove Xen-specific ci_xsources, ci_xmask, ci_xunmask, ci_xpending from
struct cpu_info
- for now remove a KASSERT that there are no pending interrupts in
idle_block(). We can get there with some software interrupts pending
in autoconf XXX needs to be looked at.
 1.88.2.2 12-Apr-2020  bouyer Now that we return from hypervisor_callback via Xdoreti, no need to emulate it
in evtchn_do_event() any more.
 1.88.2.1 10-Apr-2020  bouyer Remove extra assignement
 1.95.2.1 14-Dec-2020  thorpej Sync w/ HEAD.
 1.2 17-Oct-2023  bouyer branches: 1.2.2;
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.
Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.1 17-Oct-2023  bouyer Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.
Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen
when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.
x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console
xen/x86/consinit.c: support genfb as possible console
xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.
xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.
 1.2.2.2 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #428):

sys/arch/xen/xen/xen_machdep.c: revision 1.28
sys/arch/x86/pci/pci_machdep.c: revision 1.97
sys/arch/xen/xen/genfb_xen.c: revision 1.1
sys/arch/xen/xen/genfb_xen.c: revision 1.2
sys/arch/xen/include/hypervisor.h: revision 1.59
sys/arch/i386/conf/XEN3PAE_DOM0: revision 1.41 (patch)
sys/arch/x86/x86/genfb_machdep.c: revision 1.22
sys/arch/xen/x86/consinit.c: revision 1.18
sys/arch/xen/x86/autoconf.c: revision 1.26
sys/external/mit/xen-include-public/dist/xen/include/public/platform.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.188
sys/arch/x86/x86/consinit.c: revision 1.37
sys/arch/xen/conf/files.xen: revision 1.189
sys/arch/x86/x86/consinit.c: revision 1.38
sys/external/mit/xen-include-public/dist/xen/include/public/xen.h: revision 1.2
sys/arch/x86/include/genfb_machdep.h: revision 1.7
sys/arch/xen/x86/pvh_consinit.c: revision 1.5
sys/arch/xen/x86/pvh_consinit.c: revision 1.6
sys/arch/amd64/conf/XEN3_DOM0: revision 1.201

Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.

Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.

Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen

when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.

x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console

xen/x86/consinit.c: support genfb as possible console

xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.

xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.

Make sure to always fall back to xen_early_console, even for dom0

Enable genfb in DOM0 kernels

Add ext_lfb_base to dom0_vga_console_info, from recent Xen. We know if it's
present or not by checking dom0.info_size

Add XENPF_get_dom0_console, which gets a dom0_vga_console_info stucture
from the hypervisor. To be used by PVH dom0 kernels.

XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.

Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.2.2.1 17-Oct-2023  martin file genfb_xen.c was added on branch netbsd-10 on 2023-10-18 16:53:03 +0000
 1.101 19-Oct-2025  thorpej Use {,e}isabus_attach().
 1.100 03-Sep-2025  bouyer Our PVH bootstrap code assumed that the hvm_start_info structure provided
by Xen is just after the end of the symbol case. With Xen 4.20 it's not
always the case, so:
- get the symbol table size from the first byte of the symbol table area
provided by Xen. As we don't know if there is a symbol table or not,
do a minimal sanity check on the size.
- if the hvm_start_info structure is not in the page after kernel_end or
esym, copy it there (this was already done in the genpvh case). While
there, if we copy we can easily compute the size and not assume it all fits
in one page.
With this, a NetBSD PVH dom0 can boot on Xen 4.20
 1.99 30-Apr-2025  imil branches: 1.99.2;
Introduce pvh_boot boolean to identify the real hypervisor when booting in PVH
mode.

As of now, sys/arch/x86/x86/identcpu.c / identify_hypervisor() returns in the
case of vm_guest being VM_GUEST_GENPVH, yet this VM type is not an actual
hypervisor but an information recorded in locore.S to drive boot method.
We need to investigate what type of hypervisor is really running the VM in
order to apply specifics, so instead of relying on vm_guest_is_pvh() which only
checks for VM_GUEST_XENPVH || VM_GUEST_GENPVH, pvh_boot informs on the boot
method while allowing to identify the real hypervisor.

Idea ok'd by bouyer@, tested on Xen domU, Xen dom0 with GENERIC PVH and
qemu GENERIC PVH boot.
 1.98 06-Dec-2024  bouyer Introduce vm_guest_is_pvh() and use it in place of
(vm_guest == VM_GUEST_XENPVH || vm_guest == VM_GUEST_GENPVH)
 1.97 02-Dec-2024  bouyer Add support for non-Xen PVH guests to amd64. Patch from
Emile 'iMil' Heitor in PR kern/57813, with some cosmetic tweaks by me.
Tested on bare metal, Xen PV and Xen PVH by me.
 1.96 23-Jun-2022  bouyer branches: 1.96.4; 1.96.10;
Don't override isadma defflag for Xen, now that isadma is built for Xen PV too.
While there, match amd64 mainbus and call isa_dmainit() before attaching acpi.

Fixes a panic at boot on some hardware with a floppy disk controller.
 1.95 25-May-2022  bouyer Move <xen/include/public/vcpu.h> so it's always inclued. Fixes Xen PV builds.
 1.94 25-May-2022  bouyer PVH and HVM guests can easily have more than XEN_LEGACY_MAX_VCPUS (32) cpus.
Support up to HVM_MAX_VCPUS (256). This requires resizing a few arrays in
evtchn.c, and using
VCPUOP_register_vcpu_info for vcpuid >= XEN_LEGACY_MAX_VCPUS
Tested with 96 vCPUs.
 1.93 05-Dec-2021  msaitoh s/funtion/function/ in comment.
 1.92 07-Aug-2021  thorpej Merge thorpej-cfargs2.
 1.91 24-Apr-2021  thorpej branches: 1.91.8;
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.90 30-Nov-2020  bouyer branches: 1.90.2;
Work in progress on dom0 PVH support. kernel boots and xl info works,
but we can't start a domU yet.
 1.89 15-Sep-2020  bouyer branches: 1.89.2;
Fix side effect of previous commit: skip xencons attachement only for
PVHVM guests. Should fix issue with console on PV dom0 reported by
kardel@ in private mail
 1.88 09-Sep-2020  bouyer Workarounds for amazon's Xen 4.2 version, still present on some instances:
- It doens't provides an xencons, so allow HVM_PARAM_CONSOLE_PFN to fail
- the xen clock is not fully functionnal, so don't try to use it if
HVM_PARAM_CONSOLE_PFN failed.
- it doesn't have XEN_HVM_CPUID_VCPU_ID_PRESENT so fall back to ci_acpiid
as ci_vcpuid in this case.

It also doesn't support the per-CPU event callback; so fix the fallback
to global vector (don't try to register more per-cpu vector)
Should fix port-amd64/55543
 1.87 14-Jul-2020  yamaguchi Introduce per-cpu IDTs

This is realized by following modifications:
- Add IDT pages and its allocation maps for each cpu in "struct cpu_info"
- Load per-cpu IDTs at cpu_init_idt(struct cpu_info*)
- Copy the IDT entries for cpu0 to other CPUs at attach
- These are, for example, exceptions, db, system calls, etc.

And, added a kernel option named PCPU_IDT to enable the feature.
 1.86 26-May-2020  bouyer Add need-flags for kernfs.
Compile Xen kernfs support only if kernfs is compiled in the kernel.
Should fix MODULAR build.
 1.85 21-May-2020  ad - Recalibrate the APIC timer using the TSC, once the TSC has in turn been
recalibrated using the HPET. This gets the clock interrupt firing more
closely to HZ.

- Undo change with recent Xen merge and go back to starting the clocks in
initclocks() on the boot CPU, and in cpu_hatch() on secondary CPUs.

- On reflection don't use HPET delay any more, it works very well but means
going over the bus. It's enough to use HPET to calibrate the TSC and
APIC.

Tested on amd64 native, xen and xen PVH.
 1.84 09-May-2020  bouyer Also set x86_delay to xen_delay, unbreaks PVH
 1.83 08-May-2020  bouyer strlcpy() the command line only if it's present.
 1.82 07-May-2020  bouyer Cast physical address to uintptr_t, fix PAE build.
Pointed out by John D. Baker
 1.81 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.80 03-May-2020  bouyer For PVH and HVM, copy hvm_start_info.flags to xen_start_info.flags
 1.79 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.78 28-Apr-2020  bouyer Add xbd to the list of valid disks.
Remove hardcoded root on xbd0 for Xen PVHVM, now that the x86 findroot()
knowns about xbd disks.
 1.77 28-Apr-2020  bouyer Remove debug printfs
 1.76 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.75 19-Apr-2020  jdolecek change interface for xen_shm_map() so that caller always supplies the VA,
it now fails only if the Xen hypercall fails, in which case the failure
is final

change xbdback to pre-allocate KVA on xbdback attach (and free on detach),
so it has always KVA to map the request pages

remove no longer needed KVA allocation failure handling
 1.74 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.73 07-Apr-2020  jdolecek branches: 1.73.2;
switch KERNFS_ALLOCENTRY() to use kmem_zalloc() instead of malloc()
 1.72 14-Feb-2019  cherry Welcome XENPVHVM mode.

It is UP only, has xbd(4) and xennet(4) as PV drivers.

The console is com0 at isa and the native portion is very
rudimentary AT architecture, so is probably suboptimal to
run without PV support.
 1.71 13-Feb-2019  cherry Further restrict the scope of XENPV to relevant parts.
 1.70 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.69 11-Jul-2016  msaitoh branches: 1.69.18;
KNF. No functional change.
 1.68 21-Jun-2016  nonaka Pass bus_dma(9) tag to allow for porting sdhc(4) at acpi.
 1.67 12-Jun-2016  jnemeth - add machdep.xen.version sysctl to easily get hypervisor version
- move machdep.xen_timepush_ticks to machdep.xen.timepush_ticks to
consolidate all Xen related sysctls under machdep.xen
 1.66 29-May-2016  bouyer Switch to elf notes for amd64 instead of the old key=value list to describe the
guest requirements and support.
Add infrastructure to query the hypervisor about features support.
For verbose boot, print the features suppoted by the hypervisor for this
guest.
 1.65 01-Feb-2014  bouyer branches: 1.65.4; 1.65.6; 1.65.10;
Revert previous: calling fpuinit() leads to a panic, as a domU is not
allowed to manipulate cr0 directly. Xen doesn't need this, the fpu is
handled by the hypervisor.
 1.64 01-Feb-2014  dsl Add a direct call to fpuinit().
I'm not sure this is architecturally the best place, but I think it
is where npxattach() used to get called.
Might fix FP support in domu
 1.63 26-Jan-2014  dsl Remove support for 'external' floating point units and the MS-DOS
compatible method of handling floating point exceptions.
Make kernel support for teh fpu non-optional (486SX should still work).
Only 386 cpus support external fpu, and i386 support was removed years ago.
This means that the npx code no longer uses port 0xf0 or interupt 13.
All the "npx at isa" lines go from the configs, arch/i386/isa/npx.c
is now mandatory for all i386 kernels.
I've renamed npxinit() to fpuinit() and npxinit_cpu() to fpuinit_cpu()
to match the very similar amd64 functions.
The fpu of the boot cpu is now initialised by a direct call from
cpu_configure(), this enables FP emulation for a 486SX.
(for amd64 the cr0 values are set in locore.S and similar).
This fixes a long-standing bug in linux_setregs() - which did not
save the fpu regsiters if they were active.
I've test booted a single cpu i386 kernel (using anita).
amd64 builds - none of teh changes should affect it.
The i386 XEN kernels build, but I'm not sure where they set cr0, and
it might have got lost!
 1.62 06-Apr-2012  riz branches: 1.62.2; 1.62.4;
Retrive and print the hypervisor "extra" (teeny) version.
From Manuel Bouyer.
 1.61 17-Feb-2012  bouyer - make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
 1.60 09-Dec-2011  cherry branches: 1.60.2;
Move events/interrupt subsystem initialisation to before when ipis are used
 1.59 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.58 22-Sep-2011  jym branches: 1.58.2; 1.58.6;
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.57 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.56 13-Aug-2011  cherry MP probing and startup code
 1.55 17-May-2011  dyoung branches: 1.55.2;
PCI_FLAGS_IO_ENABLED and PCI_FLAGS_MEM_ENABLED changed their functional
role in NetBSD (drivers are no longer supposed to write these to
pa_flags) without changing name. Correct that.

Rename PCI_FLAGS_IO_ENABLED to PCI_FLAGS_IO_OKAY and
PCI_FLAGS_MEM_ENABLED to PCI_FLAGS_MEM_OKAY, thus making their names
consistent with the other PCI flags and poisoning 3rd-party driver
sources that use the flags in the old bad way.

This patch produces no binary changes in this set of PCI kernels when
they are compiled w/o 'options DIAGNOSTIC' and w/ -V MKREPRO=yes:

algor P4032 P5064 P6032
alpha GENERIC
amd64 GENERIC XEN3_DOM0
arc GENERIC
atari HADES MILAN-PCIIDE
bebox GENERIC
cats GENERIC
cobalt GENERIC
evbarm-el ADI_BRH ARMADILLO9 CP3100 GEMINI GEMINI_MASTER GEMINI_SLAVE
evbarm-el GUMSTIX HDL_G IMX31LITE INTEGRATOR IQ31244 IQ80310 IQ80321
evbarm-el IXDP425 IXM1200 KUROBOX_PRO
evbarm-el LUBBOCK MARVELL_NAS NAPPI NSLU2 SHEEVAPLUG SMDK2800 TEAMASA_NPWR
evbarm-el TEAMASA_NPWR_FC TS7200 TWINTAIL ZAO425
evbmips-el AP30 DBAU1500 DBAU1550 MALTA MERAKI MTX-1 OMSAL400 RB153 WGT624V3
evbmips64-el XLSATX
evbppc EV64260 MPC8536DS MPC8548CDS OPENBLOCKS200 OPENBLOCKS266
evbppc OPENBLOCKS266_OPT P2020RDB PMPPC RB800 WALNUT
hp700 GENERIC
i386 ALL XEN3_DOM0 XEN3_DOMU
ibmnws GENERIC
iyonix GENERIC
landisk GENERIC
macppc GENERIC
mvmeppc GENERIC
netwinder GENERIC
ofppc GENERIC
prep GENERIC
sandpoint GENERIC
sbmips-el GENERIC
sgimips GENERIC32_IP2x GENERIC32_IP3x
sparc GENERIC_SUN4U KRUPS
sparc64 GENERIC
 1.54 17-Apr-2011  jym Remove remnants from the past when Xen 2 was still alive.
 1.53 07-Aug-2010  cegger branches: 1.53.2;
acpi_madt.h is gone
 1.52 28-Apr-2010  dyoung On x86, change the bus_space_tag_t to a pointer to a struct
bus_space_tag. For now, bus_space_tag's only member is
bst_type, the type of space, which is either X86_BUS_SPACE_IO
or X86_BUS_SPACE_MEM. In the future, new bus_space_tag members
will refer to override-functions installed by a new function,
bus_space_tag_create(9).

Add pointers to constant struct bus_space_tag, x86_bus_space_io and
x86_bus_space_mem. Use them to replace most uses of X86_BUS_SPACE_IO
and X86_BUS_SPACE_MEM.

Add an x86-specific bus_space_is_equal(9) implementation that compares
the two tags' bst_type.
 1.51 02-Mar-2010  dyoung branches: 1.51.2;
Re-zero the attach args before setting them up for a new call to
config_found_ia(). In this way we avoid passing an uninitialized
pcibus_attach_args.pba_pc that will cause problems, later.
 1.50 23-Oct-2009  snj branches: 1.50.2;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.49 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.48 18-Aug-2009  jmcneill Switch to ACPICA 20090730, and update for API changes.
 1.47 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.46 18-Apr-2009  mlelstv mpacpi_scan_pci and mpbios_scan_pci were unified into mp_pci_scan.
See sys/arch/x86/x86/mp.c:1.9.
 1.45 10-Mar-2009  bouyer Make sure xenkernfs_init() is called for XEN2 too.
 1.44 13-Feb-2009  bouyer Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.43 18-Jan-2009  bouyer branches: 1.43.2;
The Xen PCI_BUS_FIXUP/PCI_ADDR_FIXUP has rotted, catch up with x86 changes
in this area. Patch provided by FUKAUMI Naoki in PR#40356.
 1.42 24-Oct-2008  jym branches: 1.42.2; 1.42.4;
- 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.41 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.40 21-Oct-2008  cegger introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.39 11-Oct-2008  bouyer Support 64bit DMA on PCI busses.
 1.38 16-Sep-2008  bouyer Implement the arch-dependent p2m frame lists list. This adds support for
'xm dump-core' for NetBSD domUs.
From Jean-Yves Migeon (jean-yves dot migeon at espci dot fr)
 1.37 27-May-2008  explorer branches: 1.37.4;
print out the xen hypervisor's version number when attaching
 1.36 16-Apr-2008  cegger branches: 1.36.2; 1.36.4; 1.36.6;
device_t / softc split
reviewed, tested and approved by bouyer
 1.35 14-Apr-2008  cegger - use POSIX integer types
- ansify functions
 1.34 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.33 11-Jan-2008  bouyer branches: 1.33.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.32 22-Nov-2007  bouyer branches: 1.32.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.31 06-Mar-2007  yamt branches: 1.31.2; 1.31.18; 1.31.20; 1.31.24; 1.31.26;
fix vcpu after recent cpubus changes.
 1.30 05-Mar-2007  dogcow The recent cpu_attach changes broke the xen build; while this compiles,
I'm far from positive it's the correct fix.
 1.29 29-Jan-2007  hubertf branches: 1.29.2;
Remove more duplicate headers.
Patch by Slava Semushin <slava.semushin@gmail.com>

Again, this was tested by comparing obj files from a pristine and a patched
source tree against an i386/ALL kernel, and also for src/sbin/fsck_ffs,
src/sbin/fsdb and src/usr.sbin/makefs. Only changes in assert() line numbers
were detected in 'objdump -d' output.
 1.28 15-Oct-2006  yamt include machine/mpconfig.h so that these files can be compiled
with ACPI but without MPBIOS.
 1.27 28-Sep-2006  bouyer Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.26 11-Aug-2006  yamt branches: 1.26.2; 1.26.4;
"xm shutdown" support for xen3. ok'ed by Manuel Bouyer.
 1.25 07-Jul-2006  yamt branches: 1.25.2;
make this compilable without kernfs in the case of !DOM0OPS && XEN3.
 1.24 09-Apr-2006  bouyer branches: 1.24.4;
Move xenkernfs_init() back in hypervisor.c so that /kern/xen/privcmd can
also register properly.
 1.23 09-Apr-2006  bouyer Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.22 19-Mar-2006  bouyer More xbd->xbd_hypervisor changes; non-Xen3 kernels build again.
 1.21 06-Mar-2006  bouyer branches: 1.21.2; 1.21.4;
Fix build issues that didn't show up in my private tree.
 1.20 06-Mar-2006  bouyer pull in the grant table and Xenbus interfaces, and attach xenbus at hypervisor.
 1.19 15-Jan-2006  bouyer branches: 1.19.2; 1.19.4; 1.19.6;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.18 11-Dec-2005  christos branches: 1.18.2;
merge ktrace-lwp.
 1.17 20-Sep-2005  bouyer inline 2 trivial functions that are called often (according to profiling
data).
 1.16 21-Aug-2005  bouyer Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.15 19-Aug-2005  bouyer Make PCI devices usable for non-domain0 kernels. Based on patches sent by
Ceri Storey to port-xen, with some additionnal changes by me:
- include bus_dma.c, bus_space.c and pci_machdep.c if pci is defined
instead of dom0ops
- Make various initialisations, and probe/attach pci busses based on NPCI
instead of DOM0OPS
- in conf/files.xen, move xen-specific devices before non-xen specific devices
so that the xen-specific match function is called first, to avoid false
attachement from too liberal match function in non-xen code.
 1.14 18-Apr-2005  bouyer branches: 1.14.2;
Add sysmon_power attribute to hypervisor, and an handler for CMSG_SHUTDOWN
messages.
Now a NetBSD guest will reboot or shutdown on 'xm shutdown' commands in
domain0.
 1.13 17-Apr-2005  bouyer Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.12 11-Mar-2005  bouyer branches: 1.12.2;
Properly define NISA and NPCI; cleanup use of NISA and NPCI.
 1.11 11-Mar-2005  bouyer Wrap some ISA stuff in DOM0OPS, so that DOMU kernels build again.
 1.10 10-Mar-2005  bouyer Clean up ISA attachement, and do not try to attach it at hypervisor if
it has already been attached at pcib.
 1.9 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.8 10-Dec-2004  christos branches: 1.8.2; 1.8.4; 1.8.6;
dkvar.h now needs bufq.h, and now xen compiles again.
 1.7 07-May-2004  cl branches: 1.7.2; 1.7.4;
Reboot machine when hypervisor signals "die" event.
 1.6 07-May-2004  cl Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.5 25-Apr-2004  cl Don't attach vga and keyboard if the domain is not privileged.
Also cleanup print command if attachment fails.
 1.4 24-Apr-2004  cl Add keyboard support and wscons config options.
 1.3 24-Apr-2004  cl Add vga display support.
 1.2 24-Apr-2004  cl Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.1 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.
 1.7.4.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.7.4.6 01-Apr-2005  skrll Sync with HEAD.
 1.7.4.5 18-Dec-2004  skrll Sync with HEAD.
 1.7.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.7.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.7.4.2 03-Aug-2004  skrll Sync with HEAD
 1.7.4.1 07-May-2004  skrll file hypervisor.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.7.2.2 22-May-2004  he Pull up revisions 1.1-1.7 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.7.2.1 07-May-2004  he file hypervisor.c was added on branch netbsd-2-0 on 2004-05-22 15:58:54 +0000
 1.8.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.8.4.1 29-Apr-2005  kent sync with -current
 1.8.2.6 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.8.2.5 16-Feb-2005  bouyer Glue xen_shm and the block and network backends to the system.
 1.8.2.4 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.8.2.3 18-Jan-2005  bouyer snapshot of work in progress on physical devices support:
- support pci at hypervisor. Attach one PCI bus for each bus returned by
PHYSDEVOP_PCI_PROBE_ROOT_BUSES (it looks like xen hides ppb bridges from
the guest OS).
- implement pci_conf_read()/pci_conf_write() using the appropriate
PHYSDEVOP_PCI_* calls.
- call PHYSDEVOP_PCI_INITIALISE_DEVICE from pci_intr_map() to make xen
do interrupts routing if needed, and map the hardware interrupt to
a xen interrupt.
- add pci and associated devices to files.xen

This is enouth to have IDE controllers work in PIO mode.
TODO: bus_dma support (currently bus_dma won't translate pseudo-physical
addresses to machine addresses), ISA support, test memory-mapped
I/O registers.
 1.8.2.2 17-Dec-2004  bouyer Merge rev 1.8
 1.8.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.12.2.7 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.12.2.6 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.12.2.5 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1085):
sys/arch/xen/xen/hypervisor.c: revision 1.17
sys/arch/xen/i386/hypervisor_machdep.c: revision 1.13
sys/arch/xen/include/hypervisor.h: revision 1.15
inline 2 trivial functions that are called often (according to profiling
data).
 1.12.2.4 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1083):
sys/arch/xen/xen/hypervisor.c: revision 1.16
sys/arch/xen/xen/if_xennet.c: revision 1.31
sys/arch/xen/conf/files.xen: revision 1.29
sys/arch/xen/xen/xbd.c: revision 1.22
Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.12.2.3 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #694):
sys/arch/xen/xen/hypervisor.c: revision 1.15
sys/arch/xen/xen/evtchn.c: revision 1.16
sys/arch/xen/conf/files.xen: revision 1.27
sys/arch/xen/i386/machdep.c: revision 1.18
Make PCI devices usable for non-domain0 kernels. Based on patches sent by
Ceri Storey to port-xen, with some additionnal changes by me:
- include bus_dma.c, bus_space.c and pci_machdep.c if pci is defined
instead of dom0ops
- Make various initialisations, and probe/attach pci busses based on NPCI
instead of DOM0OPS
- in conf/files.xen, move xen-specific devices before non-xen specific devices
so that the xen-specific match function is called first, to avoid false
attachement from too liberal match function in non-xen code.
 1.12.2.2 25-Apr-2005  tron Pull up revision 1.14 (requested by bouyer in ticket #190):
Add sysmon_power attribute to hypervisor, and an handler for CMSG_SHUTDOWN
messages.
Now a NetBSD guest will reboot or shutdown on 'xm shutdown' commands in
domain0.
 1.12.2.1 25-Apr-2005  tron Pull up revision 1.13 (requested by bouyer in ticket #187):
Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.14.2.6 21-Jan-2008  yamt sync with head
 1.14.2.5 07-Dec-2007  yamt sync with head
 1.14.2.4 03-Sep-2007  yamt sync with head.
 1.14.2.3 26-Feb-2007  yamt sync with head.
 1.14.2.2 30-Dec-2006  yamt sync with head.
 1.14.2.1 21-Jun-2006  yamt sync with head.
 1.18.2.1 01-Feb-2006  yamt sync with head.
 1.19.6.4 11-Aug-2006  yamt sync with head
 1.19.6.3 11-Apr-2006  yamt sync with head
 1.19.6.2 01-Apr-2006  yamt sync with head.
 1.19.6.1 13-Mar-2006  yamt sync with head.
 1.19.4.1 22-Apr-2006  simonb Sync with head.
 1.19.2.1 09-Sep-2006  rpaulo sync with head
 1.21.4.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.21.4.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.21.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.24.4.1 13-Jul-2006  gdamore Merge from HEAD.
 1.25.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.26.4.1 22-Oct-2006  yamt sync with head
 1.26.2.2 01-Feb-2007  ad Sync with head.
 1.26.2.1 18-Nov-2006  ad Sync with head.
 1.29.2.1 12-Mar-2007  rmind Sync with HEAD.
 1.31.26.2 18-Feb-2008  mjf Sync with HEAD.
 1.31.26.1 08-Dec-2007  mjf Sync with HEAD.
 1.31.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.31.20.2 23-Mar-2008  matt sync with HEAD
 1.31.20.1 09-Jan-2008  matt sync with HEAD
 1.31.18.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.31.2.1 03-Dec-2007  ad Sync with HEAD.
 1.32.6.1 05-Jan-2008  bouyer Remove files that just include the x86 counterpart.
 1.33.6.3 17-Jan-2009  mjf Sync with HEAD.
 1.33.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.33.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.36.6.2 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.36.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.36.4.4 11-Aug-2010  yamt sync with head.
 1.36.4.3 11-Mar-2010  yamt sync with head
 1.36.4.2 19-Aug-2009  yamt sync with head.
 1.36.4.1 04-May-2009  yamt sync with head.
 1.36.2.1 04-Jun-2008  yamt sync with head
 1.37.4.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.37.4.1 19-Oct-2008  haad Sync with HEAD.
 1.42.4.7 08-May-2012  sborrill Fix XEN2 build after last pullup
 1.42.4.6 08-May-2012  sborrill Pull up the following revisions(s) (requested by riz in ticket #1748):
sys/arch/xen/xen/hypervisor.c: revision 1.62 via patch

Retrieve and print the hypervisor "extra" (teeny) version.
 1.42.4.5 26-Sep-2011  sborrill Patch to fix build on Xen 2 (fallout from pullup #1672)
 1.42.4.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.42.4.3 04-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/xen/hypervisor.c: revision 1.45
Make sure xenkernfs_init() is called for XEN2 too.
 1.42.4.2 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.42.4.1 22-Jan-2009  snj branches: 1.42.4.1.4;
Pull up following revision(s) (requested by bouyer in ticket #286):
sys/arch/xen/conf/files.xen: revision 1.92
sys/arch/xen/x86/mainbus.c: revision 1.7 via patch
sys/arch/xen/xen/hypervisor.c: revision 1.43
The Xen PCI_BUS_FIXUP/PCI_ADDR_FIXUP has rotted, catch up with x86 changes
in this area. Patch provided by FUKAUMI Naoki in PR#40356.
 1.42.4.1.4.1 21-Apr-2010  matt sync to netbsd-5
 1.42.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.42.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.42.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.43.2.8 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.43.2.7 07-May-2011  jym KNF.
 1.43.2.6 02-May-2011  jym Sync with head.
 1.43.2.5 24-Oct-2010  jym Sync with HEAD
 1.43.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.43.2.3 01-Nov-2009  jym Sync with HEAD.
 1.43.2.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.43.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.50.2.2 17-Aug-2010  uebayasi Sync with HEAD.
 1.50.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.51.2.4 31-May-2011  rmind sync with head
 1.51.2.3 21-Apr-2011  rmind sync with head
 1.51.2.2 05-Mar-2011  rmind sync with head
 1.51.2.1 30-May-2010  rmind sync with head
 1.53.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.55.2.2 26-Jun-2011  cherry Unbreak uniprocessor build
 1.55.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.58.6.2 29-Apr-2012  mrg sync to latest -current.
 1.58.6.1 18-Feb-2012  mrg merge to -current.
 1.58.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.58.2.1 17-Apr-2012  yamt sync with head
 1.60.2.2 18-Apr-2012  snj Pull up following revision(s) (requested by riz in ticket #184):
sys/arch/xen/xen/hypervisor.c: revision 1.62
Retrive and print the hypervisor "extra" (teeny) version.
From Manuel Bouyer.
 1.60.2.1 22-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #28):
sys/arch/xen/include/hypervisor.h: revision 1.38
sys/arch/xen/xen/hypervisor.c: revision 1.61
sys/arch/xen/xen/clock.c: revision 1.62
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
- make xen_version globally available, with macros to access major and
minor xen version.
- In xen_initclocks(), do a VCPUOP_stop_periodic_timer only for Xen 3.1
and later
Should fix PR port-xen/45961
 1.62.4.1 18-May-2014  rmind sync with head
 1.62.2.2 03-Dec-2017  jdolecek update from HEAD
 1.62.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.65.10.1 13-Mar-2017  skrll Sync with netbsd-7-1-RELEASE
 1.65.6.2 05-Oct-2016  skrll Sync with HEAD
 1.65.6.1 09-Jul-2016  skrll Sync with HEAD
 1.65.4.1 07-Mar-2017  snj Pull up following revision(s) (requested by jnmeth in ticket #1328):
sys/arch/xen/xen/hypervisor.c: revision 1.67
add machdep.xen.version sysctl to easily get hypervisor version
 1.69.18.3 21-Apr-2020  martin Sync with HEAD
 1.69.18.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.69.18.1 10-Jun-2019  christos Sync with HEAD
 1.73.2.9 22-Apr-2020  bouyer Don't try to attach vcpu on !XENPV
remove debug printf and other misc cosmetic changes
 1.73.2.8 20-Apr-2020  bouyer Sync with HEAD
 1.73.2.7 18-Apr-2020  bouyer If possible, register a per-cpu callback via HVMOP_set_evtchn_upcall_vector.
From FreeBSD. This requires acking the interrupt in hypervisor_pvhvm_callback.

Don't try to use x86_cpu_idle_xen() for PVHVM, it cause the domU to hang.
FreeBSD doesn't seem to use it either.
 1.73.2.6 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.73.2.5 16-Apr-2020  bouyer switch vm_guest to VM_GUEST_XENPVHVM only after all tests succeeded.
 1.73.2.4 16-Apr-2020  bouyer Rename xen/xen/clock.c to xen_clock.c, so that it can be compiled
with x86/isa/clock.c
call events_default_setup() from hypervisor_match to that event
arrays are properly initialised.
Use xen_delay() and xen's timecounter for PVHVM.
 1.73.2.3 16-Apr-2020  bouyer fail silently if hypervisor is not found.
 1.73.2.2 11-Apr-2020  bouyer Use some PIO magic to disable qemu emulated disks and network device
when PVHVM is configured. From FreeBSD.
 1.73.2.1 08-Apr-2020  bouyer Remove VM_GUEST_XEN and define only Xen subtypes:
VM_GUEST_XENPV
VM_GUEST_XENPVH
VM_GUEST_XENHVM
VM_GUEST_XENPVHVM

Set vm_guest in the start routine, if it is hypervisor-specific (e.g Xen PV).
If vm_guest was not set early and we detect Xen in identify_hypervisor(),
assume it is VM_GUEST_XENHVM. Refine to VM_GUEST_PVXENHVM in
hypervisor_match().
 1.89.2.1 14-Dec-2020  thorpej Sync w/ HEAD.
 1.90.2.1 02-Apr-2021  thorpej config_found_ia() -> config_found() w/ CFARG_IATTR.
 1.91.8.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.96.10.1 02-Aug-2025  perseant Sync with HEAD
 1.96.4.2 01-Oct-2025  martin Pull up following revision(s) (requested by bouyer in ticket #1168):

sys/arch/i386/i386/locore.S: revision 1.202
sys/arch/i386/i386/locore.S: revision 1.203
sys/arch/i386/i386/locore.S: revision 1.204
sys/arch/amd64/amd64/locore.S: revision 1.231
sys/arch/amd64/amd64/locore.S: revision 1.232
sys/arch/amd64/amd64/locore.S: revision 1.233
sys/arch/xen/xen/hypervisor.c: revision 1.100
all via patch

Our PVH bootstrap code assumed that the hvm_start_info structure provided
by Xen is just after the end of the symbol case. With Xen 4.20 it's not
always the case, so:
- get the symbol table size from the first byte of the symbol table area
provided by Xen. As we don't know if there is a symbol table or not,
do a minimal sanity check on the size.
- if the hvm_start_info structure is not in the page after kernel_end or
esym, copy it there (this was already done in the genpvh case). While
there, if we copy we can easily compute the size and not assume it all fits
in one page.
With this, a NetBSD PVH dom0 can boot on Xen 4.20

Fix various typos, mainly in comments.

Fix some issues with symbol table detection on Xen PVH:
- the stack grows down so the last pushed value is at 0(%esp), not -4(%esp).
Pointed out by Joachim Kuebart.
- 0x3fffffff is 1GB-1, not 1MB-1. Test the symtab size against 16MB
(amd64 generic symbol table is just above 1MB these days)
- I got confused by cmp's arguments order between intel and gas syntax,
so the tests did the opposite of intended and the symtab was always
considered valid.
While there use unsigned conditions.
Should fix booting in PVH mode with netbsd-INSTALL (which is stripped)
 1.96.4.1 29-Mar-2025  martin Pull up following revision(s) (requested by imil in ticket #1074):

sys/arch/x86/x86/x86_machdep.c: revision 1.155
sys/arch/x86/include/cpu.h: revision 1.137
sys/arch/x86/x86/x86_machdep.c: revision 1.156
sys/arch/x86/include/cpu.h: revision 1.138
sys/arch/x86/x86/consinit.c: revision 1.40
sys/arch/x86/acpi/acpi_machdep.c: revision 1.37
sys/arch/x86/acpi/acpi_machdep.c: revision 1.38
sys/arch/amd64/amd64/machdep.c: revision 1.370
sys/arch/xen/xen/hypervisor.c: revision 1.97
sys/arch/xen/xen/hypervisor.c: revision 1.98
sys/arch/amd64/amd64/genassym.cf: revision 1.98
sys/arch/x86/x86/x86_autoconf.c: revision 1.88
sys/arch/x86/x86/x86_autoconf.c: revision 1.89
sys/arch/amd64/amd64/locore.S: revision 1.226
sys/arch/amd64/amd64/locore.S: revision 1.227
sys/arch/x86/x86/identcpu.c: revision 1.131

Add support for non-Xen PVH guests to amd64. Patch from
Emile 'iMil' Heitor in PR kern/57813, with some cosmetic tweaks by me.
Tested on bare metal, Xen PV and Xen PVH by me.

Get one more change from PR kern/57813, needed for non-Xen PVH.

Introduce vm_guest_is_pvh() and use it in place of
(vm_guest == VM_GUEST_XENPVH || vm_guest == VM_GUEST_GENPVH)
 1.99.2.1 01-Oct-2025  martin Pull up following revision(s) (requested by bouyer in ticket #43):

sys/arch/i386/i386/locore.S: revision 1.202
sys/arch/i386/i386/locore.S: revision 1.203
sys/arch/i386/i386/locore.S: revision 1.204
sys/arch/amd64/amd64/locore.S: revision 1.231
sys/arch/amd64/amd64/locore.S: revision 1.232
sys/arch/amd64/amd64/locore.S: revision 1.233
sys/arch/xen/xen/hypervisor.c: revision 1.100

Our PVH bootstrap code assumed that the hvm_start_info structure provided
by Xen is just after the end of the symbol case. With Xen 4.20 it's not
always the case, so:
- get the symbol table size from the first byte of the symbol table area
provided by Xen. As we don't know if there is a symbol table or not,
do a minimal sanity check on the size.
- if the hvm_start_info structure is not in the page after kernel_end or
esym, copy it there (this was already done in the genpvh case). While
there, if we copy we can easily compute the size and not assume it all fits
in one page.

With this, a NetBSD PVH dom0 can boot on Xen 4.20

Fix some issues with symbol table detection on Xen PVH:
- the stack grows down so the last pushed value is at 0(%esp), not -4(%esp).
Pointed out by Joachim Kuebart.
- 0x3fffffff is 1GB-1, not 1MB-1. Test the symtab size against 16MB
(amd64 generic symbol table is just above 1MB these days)
- I got confused by cmp's arguments order between intel and gas syntax,
so the tests did the opposite of intended and the symtab was always
considered valid.
While there use unsigned conditions.

Should fix booting in PVH mode with netbsd-INSTALL (which is stripped)

Fix various typos, mainly in comments.
 1.62 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.61 16-Jan-2009  jym branches: 1.61.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.60 27-Oct-2008  cegger branches: 1.60.2; 1.60.8;
change nfs boot behaviour to automatically try next boot method if boot information are incomplete to succeed.
That way, it is possible combine static and dhcp boot:
For example, to boot diskless you can specify the nfs-server and the rootpath statically. All other information will be taken via dhcp.

Patch has been presented on port-xen, tech-kern and tech-net:
http://mail-index.netbsd.org/port-xen/2008/10/24/msg004488.html
http://mail-index.netbsd.org/tech-kern/2008/10/24/msg003255.html
http://mail-index.netbsd.org/tech-net/2008/10/24/msg000864.html

No comments, no objections.
 1.59 21-Oct-2008  cegger branches: 1.59.2;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.58 16-Apr-2008  cegger branches: 1.58.4; 1.58.10;
Use device_xname(). From Manuel Bouyer.
 1.57 16-Apr-2008  cegger device_t / softc split
reviewed, tested and approved by bouyer
 1.56 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.55 03-Dec-2007  ad branches: 1.55.14;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.54 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.53 07-Nov-2007  ad Merge from vmlocking:

- pool_cache changes.
- Debugger/procfs locking fixes.
- Other minor changes.
 1.52 17-Oct-2007  garbled branches: 1.52.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.51 26-Aug-2007  dyoung branches: 1.51.2; 1.51.6;
Constify.
 1.50 08-Jul-2007  bouyer branches: 1.50.4; 1.50.8;
Add a bootstatic callback for dom0, which allows to take NFS boot informations
from the command line (nfsroot=, ip=) in the same way domUs did.
While there convert a for (;;) loop to TAILQ_FOREACH().
From Christoph Egger in private mail.

Make all bootstatic callbacks return the new NFS_BOOTSTATIC_NOSTATIC flag
when no nfs boot information is found on command line.
 1.49 04-Mar-2007  christos branches: 1.49.2; 1.49.4; 1.49.10;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.48 27-May-2006  bouyer branches: 1.48.12;
The receive buffer, once mapped back in the domU, is read/write and not
shared so there;s no problems marking the mbuf M_EXT_RW.
 1.47 05-May-2006  jld branches: 1.47.2;
Use TAILQ_FOREACH instead of duplicating the code.
No change to compiled object file.
 1.46 29-Mar-2006  thorpej Use device_is_a() rathern than testing the dv_cfattach pointer.
 1.45 17-Mar-2006  jld Add some missing memory barriers I noticed.
Discussed on port-xen.
 1.44 16-Mar-2006  bouyer m_copyback() doesn't work the way I expexted it: it doesn't update the
mbuf fields, and it seems it doesn't allocate mbuf clusters for large
packets. So revert to using MCLGET() if needed and memcpy to get the
data in the mbuf.
Should fix panic in m_makewritable() reported by Mike M. Volokhov
and Andreas Wrede on port-xen, patch tested by Andreas Wrede.
 1.43 06-Mar-2006  bouyer branches: 1.43.2; 1.43.4;
Fix build issues that didn't show up in my private tree.
 1.42 01-Feb-2006  bouyer branches: 1.42.2; 1.42.4;
Check the destination ethernet address when not in promiscous mode.
Fix problem where packets would be duplicated, possibly looping, when
a domU is doing IP routing.
Problem reported and fix tested by Mike M. Volokhov on port-xen

While there, add some __predict_false/true in conditionnals where
appropriate, remove a always-true test, and fix handling of
mbuf shortage.
 1.41 26-Jan-2006  bouyer branches: 1.41.2;
Fif off-by-one error: the last byte of a packet is at offset len - 1,
not len.
Should fix KASSERT panic reported by Mike M. Volokhov on port-xen.
 1.40 08-Jan-2006  bouyer Move if_xennet private functions and structures from if_xennetvar.h
to if_xennet.c
 1.39 15-Dec-2005  yamt branches: 1.39.2;
xennet_softstart: turn "xennet: no mbuf cluster" printf to DPRINTF.
 1.38 11-Dec-2005  christos merge ktrace-lwp.
 1.37 02-Oct-2005  bouyer Have xennet_start() schedule a software interrupt to defer processing of the
send queue. This give upper layer an opportunity to queue up all available
packets before starting to process them. This reduce the number of interrupt
generated on the backend, and the time spent doing hypercalls in a significant
way.
 1.36 12-Sep-2005  bouyer Check that m_copyback() didn't fail. Pointed out by YAMAMOTO Takashi.
 1.35 12-Sep-2005  yamt remove no longer used macros.
 1.34 12-Sep-2005  yamt xennet_start: don't allocate cluster mbufs unless needed.
 1.33 11-Sep-2005  bouyer Use MCLGET() instead of local list of buffers. Garbage-collect
struct xennet_txbuf usage.
 1.32 11-Sep-2005  bouyer Remplace MEXTMALLOC/memcpy with m_copyback.
 1.31 21-Aug-2005  bouyer Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.30 21-Aug-2005  yamt - count if_opackets correctly.
- reflect tx errors to if_oerrors.
 1.29 07-Aug-2005  yamt wrap some code with #if defined(NFS_BOOT_BOOTSTATIC).
 1.28 07-Jul-2005  yamt handle tx buffer exhaustion.
 1.27 06-Jun-2005  yamt branches: 1.27.2;
- xen_network_handler: fix transmit stall. (re)start transmit
after collecting tx buffers.
- some assertions.
 1.26 31-May-2005  yamt remove local prototypes of printk.
 1.25 26-May-2005  bouyer Always call the xpq_queue*() functions at splvm(), so that it's safe to call
them from interrupt context.
xpq_flush_queue() is called from IPL_NET in if_xennet.c, and
other xpq_queue* functions may be called from interrupt context via
pmap_kenter*(). Should fix port-xen/30153.
Thanks to Jason Thorpe and YAMAMOTO Takashi for enlightments on this issue.
 1.24 18-May-2005  bouyer Reorder the receive code to avoid a possible race with the sc_rx->event
stuff, and fix a possible infinite loop in case of ressource shortage.
 1.23 17-May-2005  christos Yes, it was a cool trick >20 years ago to use "0123456789abcdef"[a] to
implement, xtoa(), but I think defining the samestring 50 times is a bit
too much. Defined HEXDIGITS and hexdigits in subr_prf.c and use it...
 1.22 17-Apr-2005  bouyer Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.21 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.20 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.19 14-Apr-2005  yamt if_xennet: m_paddr is not always valid.
 1.18 14-Apr-2005  yamt if_xennet: handle MEXTREMOVE correctly. fix pool cache corruption.
 1.17 01-Apr-2005  yamt merge yamt-km branch.
- don't use managed mappings/backing objects for wired memory allocations.
save some resources like pv_entry. also fix (most of) PR/27030.
- simplify kernel memory management API.
- simplify pmap bootstrap of some ports.
- some related cleanups.
 1.16 26-Mar-2005  bouyer Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.15 18-Mar-2005  cube In xennet_ioctl, we need a declaration for sc when debugging macros are
active. Reported on current-users by Teemu Rinta-aho.
 1.14 17-Mar-2005  bouyer Convert to ether_ioctl(), and claim IFF_SIMPLEX | IFF_MULTICAST.
This allows IPv6 on NetBSD domU.
 1.13 11-Mar-2005  bouyer branches: 1.13.2;
Print the irq used.
 1.12 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.11 15-Sep-2004  tls branches: 1.11.4; 1.11.6; 1.11.8;
Actually mix samples into the kernel RNG pool. There's a buglet here: all
disk samples are reported as belonging to the first disk.
 1.10 14-May-2004  cl branches: 1.10.2;
- increase number of receive buffers
- handle case where last receive buffer is used
 1.9 26-Apr-2004  cl Rework the physical<->machine memory mapping: offset physical addresses
by 0x100000 (above the I/O Memory "hole") leaving all physical addresses
below unused, don't perform phys<->mach mapping for addresses below 0x100000
or beyond the real hardware's physical memory.

-> /dev/mem works now as expected and X works in domain0.
 1.8 24-Apr-2004  cl Let event_set_handler set the void * argument to event handlers.
 1.7 24-Apr-2004  cl Make kernel command line parsing support additional keywords without
having to change existing code which calls the parser.
 1.6 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.5 21-Apr-2004  cl - setup vfr rules when running as domain-0
- patch ethernet address if running as domain-0 so that arp can work

also include bpfilter.h to get NBPFILTER
 1.4 21-Apr-2004  cl don't crash when changing/adding ip addresses
 1.3 21-Apr-2004  cl cleanup debugging printf's
 1.2 11-Apr-2004  cl - remove pmap_kenter_ma prototype here
- xennet? is an interface not a driver (boot message)
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.2 16-Sep-2004  jmc Pullup rev 1.11 (requested by tls in ticket #846)

Actually mix samples into the kernel RNG pool.
 1.1.2.1 22-May-2004  he Pull up revisions 1.2-1.10 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.10.2.6 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.10.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.10.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.10.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.10.2.2 03-Aug-2004  skrll Sync with HEAD
 1.10.2.1 14-May-2004  skrll file if_xennet.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.11.8.3 19-Mar-2005  yamt (re-)convert arch/xen to the new API.
XXX except for xbdback.c and xennetback.c, because i'm not sure
what they're doing.
 1.11.8.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.11.8.1 13-Feb-2005  yamt - use new apis.
- simplify bootstrap and pvpage allocation.
- remove no longer needed .globl decls.
 1.11.6.1 29-Apr-2005  kent sync with -current
 1.11.4.4 16-Feb-2005  bouyer We may have no xennet interfaces at all, so wait only 1s for inteerfaces to
show up, not infinitly.
 1.11.4.3 18-Jan-2005  bouyer Remove debug printf now that xbd is fixed.
 1.11.4.2 17-Dec-2004  bouyer XXX Gross !!
Add a printf() in xennet_ctrlif_rx() to make the *xbd* driver work.
Without this, the kernel hang after printing xennet's MAC address,
and kbd is never probed. I suspect it hangs in the
while (!blkif_control_rsp_valid) {} loop, because xbd_response_handler() is
never called.
This needs to be investigated.
 1.11.4.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.13.2.26 05-Aug-2006  ghen Pull up following revision(s) (requested by bouyer in ticket #1452):
sys/arch/xen/xen/if_xennet.c: revision 1.48
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.10
The receive buffer, once mapped back in the domU, is read/write and not
shared so there;s no problems marking the mbuf M_EXT_RW.
 1.13.2.25 15-Jun-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1374):
sys/arch/xen/xen/clock.c: revision 1.19
sys/arch/xen/xen/if_xennet.c: revision 1.45
sys/arch/xen/xen/ctrl_if.c: revision 1.13
Add some missing memory barriers I noticed.
Discussed on port-xen.
 1.13.2.24 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.13.2.23 06-Apr-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1239):
sys/arch/xen/include/if_xennetvar.h: revision 1.9
sys/arch/xen/xen/if_xennet.c: revision 1.37
Have xennet_start() schedule a software interrupt to defer processing of the
send queue. This give upper layer an opportunity to queue up all available
packets before starting to process them. This reduce the number of interrupt
generated on the backend, and the time spent doing hypercalls in a significant
way.
 1.13.2.22 24-Mar-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1218):
sys/arch/xen/xen/if_xennet.c: revision 1.44
m_copyback() doesn't work the way I expexted it: it doesn't update the
mbuf fields, and it seems it doesn't allocate mbuf clusters for large
packets. So revert to using MCLGET() if needed and memcpy to get the
data in the mbuf.
Should fix panic in m_makewritable() reported by Mike M. Volokhov
and Andreas Wrede on port-xen, patch tested by Andreas Wrede.
 1.13.2.21 05-Feb-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1155):
sys/arch/xen/xen/if_xennet.c: revision 1.42 via patch
sys/arch/xen/xen/xennetback.c: revision 1.20 via patch
Check the destination ethernet address when not in promiscous mode.
Fix problem where packets would be duplicated, possibly looping, when
a domU is doing IP routing.
Problem reported and fix tested by Mike M. Volokhov on port-xen

While there, add some __predict_false/true in conditionnals where
appropriate, remove a always-true test, and fix handling of
mbuf shortage.
 1.13.2.20 01-Feb-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1154):
sys/arch/xen/xen/if_xennet.c: revision 1.41
Fif off-by-one error: the last byte of a packet is at offset len - 1,
not len.
Should fix KASSERT panic reported by Mike M. Volokhov on port-xen.
 1.13.2.19 22-Jan-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1105):
sys/arch/xen/xen/if_xennet.c: revision 1.33-1.35 via patch

- Use MCLGET() instead of local list of buffers.
- Garbage-collect struct xennet_txbuf usage.
- Don't allocate cluster mbufs unless needed.
- Remove no longer used macros.
 1.13.2.18 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1084):
sys/arch/xen/xen/if_xennet.c: revision 1.36
Check that m_copyback() didn't fail. Pointed out by YAMAMOTO Takashi.
 1.13.2.17 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1084):
sys/arch/xen/xen/if_xennet.c: revision 1.32
Remplace MEXTMALLOC/memcpy with m_copyback.
 1.13.2.16 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1083):
sys/arch/xen/xen/hypervisor.c: revision 1.16
sys/arch/xen/xen/if_xennet.c: revision 1.31
sys/arch/xen/conf/files.xen: revision 1.29
sys/arch/xen/xen/xbd.c: revision 1.22
Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.13.2.15 28-Aug-2005  tron branches: 1.13.2.15.2;
Pull up following revision(s) (requested by yamt in ticket #702):
sys/arch/xen/xen/if_xennet.c: revision 1.30
- count if_opackets correctly.
- reflect tx errors to if_oerrors.
 1.13.2.14 21-Aug-2005  tron Pull up revision 1.29 (requested by yamt in ticket #677):
wrap some code with #if defined(NFS_BOOT_BOOTSTATIC).
 1.13.2.13 11-Jul-2005  tron Pull up revision 1.28 (requested by yamt in ticket #574):
handle tx buffer exhaustion.
 1.13.2.12 18-Jun-2005  tron Pull up revision 1.26 (requested by yamt in ticket #470):
remove local prototypes of printk.
 1.13.2.11 18-Jun-2005  tron Pull up revision 1.27 (requested by yamt in ticket #467):
- xen_network_handler: fix transmit stall. (re)start transmit
after collecting tx buffers.
- some assertions.
 1.13.2.10 28-May-2005  tron Pull up revision 1.25 (requested by bouyer in ticket #355):
Always call the xpq_queue*() functions at splvm(), so that it's safe to call
them from interrupt context.
xpq_flush_queue() is called from IPL_NET in if_xennet.c, and
other xpq_queue* functions may be called from interrupt context via
pmap_kenter*(). Should fix port-xen/30153.
Thanks to Jason Thorpe and YAMAMOTO Takashi for enlightments on this issue.
 1.13.2.9 27-May-2005  riz Pull up revision 1.24 (requested by bouyer in ticket #326):
Reorder the receive code to avoid a possible race with the sc_rx->event
stuff, and fix a possible infinite loop in case of ressource shortage.
 1.13.2.8 28-Apr-2005  tron Pull up revision 1.21 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.13.2.7 28-Apr-2005  tron Pull up revision 1.20 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.13.2.6 25-Apr-2005  tron Pull up revision 1.22 (requested by bouyer in ticket #187):
Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.13.2.5 17-Apr-2005  tron Pull up revision 1.18 (requested by yamt in ticket #162):
if_xennet: handle MEXTREMOVE correctly. fix pool cache corruption.
 1.13.2.4 17-Apr-2005  tron Pull up revision 1.19 (requested by yamt in ticket #161):
if_xennet: m_paddr is not always valid.
 1.13.2.3 30-Mar-2005  tron Pull up revision 1.16 (requested by bouyer in ticket #72):
Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.13.2.2 22-Mar-2005  tron Pull up revision 1.15 (requested by bouyer in ticket #47):
In xennet_ioctl, we need a declaration for sc when debugging macros are
active. Reported on current-users by Teemu Rinta-aho.
 1.13.2.1 20-Mar-2005  tron Pull up revision 1.14 (requested by bouyer in ticket #33):
Convert to ether_ioctl(), and claim IFF_SIMPLEX | IFF_MULTICAST.
This allows IPv6 on NetBSD domU.
 1.13.2.15.2.2 05-Feb-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1155):
sys/arch/xen/xen/if_xennet.c: revision 1.42 via patch
sys/arch/xen/xen/xennetback.c: revision 1.20 via patch
Check the destination ethernet address when not in promiscous mode.
Fix problem where packets would be duplicated, possibly looping, when
a domU is doing IP routing.
Problem reported and fix tested by Mike M. Volokhov on port-xen

While there, add some __predict_false/true in conditionnals where
appropriate, remove a always-true test, and fix handling of
mbuf shortage.
 1.13.2.15.2.1 01-Feb-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1154):
sys/arch/xen/xen/if_xennet.c: revision 1.41 patch
Fif off-by-one error: the last byte of a packet is at offset len - 1,
not len.
Should fix KASSERT panic reported by Mike M. Volokhov on port-xen.
 1.27.2.7 27-Feb-2008  yamt revert the rest of lazy mapping code.
 1.27.2.6 27-Feb-2008  yamt drop lazy mapping of mbuf external storage for now, because:
- it's currently broken wrt asm code. (cpu_in_cksum)
- there are other approaches worth to consider. eg. sf_buf
 1.27.2.5 07-Dec-2007  yamt sync with head
 1.27.2.4 15-Nov-2007  yamt sync with head.
 1.27.2.3 03-Sep-2007  yamt sync with head.
 1.27.2.2 31-Dec-2006  yamt xennet_softstart: handle M_EXT_LAZY.
 1.27.2.1 21-Jun-2006  yamt sync with head.
 1.39.2.3 18-Feb-2006  yamt sync with head.
 1.39.2.2 01-Feb-2006  yamt sync with head.
 1.39.2.1 15-Jan-2006  yamt sync with head.
 1.41.2.1 09-Sep-2006  rpaulo sync with head
 1.42.4.4 26-Jun-2006  yamt sync with head.
 1.42.4.3 24-May-2006  yamt sync with head.
 1.42.4.2 01-Apr-2006  yamt sync with head.
 1.42.4.1 13-Mar-2006  yamt sync with head.
 1.42.2.2 01-Jun-2006  kardel Sync with head.
 1.42.2.1 22-Apr-2006  simonb Sync with head.
 1.43.4.3 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.43.4.2 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.43.4.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.43.2.2 11-May-2006  elad sync with head
 1.43.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.47.2.1 19-Jun-2006  chap Sync with head.
 1.48.12.1 12-Mar-2007  rmind Sync with HEAD.
 1.49.10.1 03-Oct-2007  garbled Sync with HEAD
 1.49.4.1 11-Jul-2007  mjf Sync with head.
 1.49.2.3 03-Dec-2007  ad Sync with HEAD.
 1.49.2.2 09-Oct-2007  ad Sync with head.
 1.49.2.1 15-Jul-2007  ad Sync with head.
 1.50.8.4 09-Dec-2007  jmcneill Sync with HEAD.
 1.50.8.3 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.50.8.2 11-Nov-2007  joerg Sync with HEAD.
 1.50.8.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.50.4.1 03-Sep-2007  skrll Sync with HEAD.
 1.51.6.2 13-Nov-2007  bouyer Sync with HEAD
 1.51.6.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.51.2.3 09-Jan-2008  matt sync with HEAD
 1.51.2.2 08-Nov-2007  matt sync with -HEAD
 1.51.2.1 06-Nov-2007  matt sync with HEAD
 1.52.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.52.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.55.14.2 17-Jan-2009  mjf Sync with HEAD.
 1.55.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.58.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.58.4.2 19-Aug-2009  yamt sync with head.
 1.58.4.1 04-May-2009  yamt sync with head.
 1.59.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.60.8.1 21-Apr-2010  matt sync to netbsd-5
 1.60.2.1 28-Mar-2010  snj Apply patch (requested by bad in ticket #1347):
Make it possible to use netbsd-5 domUs running on a Xen2 hypervisor.
This changes two panics to printfs.
 1.61.2.1 01-Nov-2009  jym Sync with HEAD.
 1.132 28-Aug-2025  mrg use the new XENNET_XNFRX_LOWAT not the undefined IF_XNFRX_LOWAT.

pretty sure this is right and the amd64 build has been broken for a bit.
 1.131 27-Aug-2025  buhrow Add the ability to control the minimum number of xnfrx buffers in the RX pool cache using the
sysctl(8) utility and the hw.xennet.xnfrx_lowat variable or the XENNET_XNFRX_LOWAT kernel
compile time option.
Also update the man page to document this change.

Testing shows a significant improvement in behavior when memory becomes tight on long-running
systems.
 1.130 09-Jan-2024  jdolecek disable TX checksum optimization, it's causing ARP lossage in some
configurations using Linux dom0

PR port-xen/57743 by Brian Marcotte, thanks for the patch
 1.129 25-Feb-2023  riastradh 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.

XXX pullup-8 (at least the xen_mb part; requires patch)
XXX pullup-9 (at least the xen_mb part; requires patch)
XXX pullup-10
 1.128 26-Aug-2020  riastradh branches: 1.128.20;
Nix trailing whitespace and mixed space/tab indentation.
 1.127 24-Jun-2020  jdolecek fix tyop in KASSERT() condition which made it an assignment rather than a check

the field indeed is supposed to be set to GRANT_INVALID_REF at this moment,
the grant must be already revoked by this time

pointed out by Taylor R Campbell
 1.126 14-May-2020  jdolecek fix resume for xennet, now the network continues working after resume

we can't read feature-rx-copy in resume, at that time the new backend
device is not filled yet; convert it just to feature flag read on interface
attach, can assume any backend would support rx-copy anyway

fix compile with XENNET_DEBUG

part of PR port-xen/55207
 1.125 14-May-2020  jdolecek rearrange so that suspend & resume doesn't cause panics, and interface
is more likely to work - particularly, don't try to xengnt_revoke_access()
after resume, move xen_intr_disestablish() call to resume, also
unmask the event channel on resume

XXX right now xennet device detaches immediately after resume, which is not
desirable and needs to be fixed

part of PR port-xen/55207
 1.124 05-May-2020  jdolecek due to the locking wild west for if_ioctl can't assert IFNET_LOCKED()
in xennet_ioctl(), so just do splnet() like other drivers do, and hope for best

fixes failed KASSERT() e.g. when starting rpcbind(), which ends
up calling this via sys_setsockopt()->sosetopt()->...->in6_addmulti()->
if_mcast_op(), this path doesn't currently take IFNET_LOCK()
 1.123 04-May-2020  jdolecek fix delet-o
 1.122 04-May-2020  jdolecek remove IPv4 csum offloading for xennet(4) - it's not complete, and even
if it was, it doesn't work with Linux Dom0 as it expects the IPv4 csum present
 1.121 01-May-2020  jdolecek make the csum blank/undefer counters per interface
 1.120 30-Apr-2020  jdolecek NETTXF_csum_blank and NETTXF_data_validated should be set only for first
fragment of the packet
 1.119 26-Apr-2020  jdolecek support feature-sg

practical offshot of this xennet(4) now supports jumbo frames, it is
also necessary for eventual TSO support

always defrag short Tx packets before passing to backend - even
with feature-sg it's way faster to process the packet if it's
passed as single fragment
 1.118 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.117 25-Apr-2020  jdolecek use m_defrag() instead of local code now that it returns single mbuf
 1.116 23-Apr-2020  jdolecek also let backend know that feature-ipv6-csum-offload is supported
 1.115 23-Apr-2020  jdolecek g/c unused macro
 1.114 12-Apr-2020  jdolecek move IFNET_UNLOCK() immediatelly after xennet_stop() call in detach,
doesn't need to be held for the disestablish
 1.113 12-Apr-2020  jdolecek fix locking against myself panic in xennet_stop() on 'ifconfig xennet0 down'

xennet_stop() is called with IFNET already held
 1.112 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.111 10-Apr-2020  jdolecek use ETHER_ADDR_LEN on one more place
 1.110 10-Apr-2020  jdolecek convert to bus_dma(9), remove now not necessary XENPVHVM redefines
 1.109 07-Apr-2020  jdolecek branches: 1.109.2;
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.108 06-Apr-2020  jdolecek adjust previous - set the Tx flag only when the feature supported, instead
of first setting it and then masking it when unsupported
 1.107 06-Apr-2020  jdolecek pass and use feature-ipv6-csum-offload for ipv6 csum support, matches
Linux Dom0/DomU
 1.106 06-Apr-2020  jdolecek convert to IFEF_MPSAFE, also enable interrupt handler without biglock

no performance difference observed compared to version before change,
for neither UP nor MP DomU
 1.105 06-Apr-2020  jdolecek make a pass on locking, replacing spl*() calls with mutexes:
- sc_tx_lock covers any access to tx list, tx ring, and writes to
if_flags and if_itimer
- sc_rx_lock covers any access to rx list, tx ring, and free tx counter

fix suspend and detach to work again - recent softintr changes made
xennet_tx_complete() not actually being called, because the call
in xennet_handler() was after IFF_RUNNING check; now it's called
directly rather than triggering softint, with updates locking it's safe

enable DVF_DETACH_SHUTDOWN for xennet(4), though the call only
triggers notification to backend to close, leaves actual detach to
xenwatch_thread
 1.104 06-Apr-2020  jdolecek #undef XENNET_DEBUG, accidentaly left enabled in previous commit
 1.103 06-Apr-2020  jdolecek remove some XENNET_DEBUG code which causes kernel faults
 1.102 06-Apr-2020  jdolecek remove some unnecessary spl*() calls, and fix the rxreq va/pa deallocation
in xennet_xenbus_detach()
 1.101 06-Apr-2020  jdolecek save some memory - no need to embed sc inside struct xennet_rxreq
 1.100 06-Apr-2020  jdolecek set rxreq_gntref to GRANT_INVALID_REF immediately after revoking
access, so it's clear where it comes from
 1.99 05-Apr-2020  jdolecek remove support for legacy rx-flip mode for xennet(4)/xvif(4), making
rx-copy (first shipped in NetBSD 6.0 in 2012) the only supported
mode

this is mostly to simplify maintenance and future development

rx-flip is not supported by Linux Dom0/DomU, and NetBSD Dom0/DomU
defaults to rx-copy for over 8 years now too, so there is little
need to keep the support for compatibility

besides compatibility there is no other reason to keep rx-flip -
page transfer is generally slower than copy due to necessary MMU/TLB
manipulation, especially on MP systems
 1.98 05-Apr-2020  jdolecek no need to pollute dmesg by xennet_watchdog(), turn the message into DPRINTFN()
 1.97 27-Mar-2020  jdolecek use standard deferred if_start framework instead of custom variant
 1.96 26-Mar-2020  jdolecek in xennet_tx_complete() replace the grant status condition with just KASSERT()

it's really a possible programming error rather than something actually
happening in wild - since rev. 1.57 in 2012 this condition leaked
locked mutex, and nobody noticed
 1.95 26-Mar-2020  jdolecek Stop using IFF_OACTIVE, just keep processing until we fill the Tx ring

xennet_tx_complete() is now called only in xennet_softstart() just
before processing Tx queue; xennet_handler() just schedules the
softint for xennet_softstart() iff the Tx ring list is completely empty

no performance changes observed

XXX the detach and suspend code expects xennet_handler() to collect
XXX the Tx responses, the tsleep() loop should still work since the tsleep()
XXX would allow the softint to trigger; untested though
 1.94 22-Mar-2020  jdolecek actually in data_validated case, there is no need to inspect the data for Rx,
simply set the supported csum offload flags to skip the software csum
verification
 1.93 22-Mar-2020  jdolecek add support for skipping IPv6 checksum validation aka offloading -
for xennet(4) both Rx and Tx, for xvif(4) only Tx for now
 1.92 19-Mar-2020  jdolecek add support for skipping IPv4 checksums
 1.91 18-Mar-2020  jdolecek use NET[RT]XF_data_validated flag to mark when Tx packet has valid
checksum; this is used to skip software checksum validation on
xennet Rx side when configured for Rx offloading

in Dom0 assume that checksum is valid when the Tx mbuf has no offload flags
- in that case either it's local packet where checksum has just been
computed in software, or forwarded external packet already
verified when received on Dom0

practical offshot of this is that DomU doesn't re-verify checksum of
packets forwarded from external hosts, e.g. via bridge(4)
 1.90 16-Mar-2020  jdolecek drop the disabled M_EXT_ROMAP mbuf code, convert xennet_checksum_fill()
to use in_undefer_cksum() instead of custom code to compute the checksum
for Rx packets, and set csum_data appropriately for eventual hw offloading

make it possible to skip the sw checksum computation by appropriate Rx
flag similarily as we do for Tx

XXX for now, the Rx flag is mostly for testing as it only works for
dom0<->domu, need some further network stack changes to arrange for
the checksum to be eventually computed when packets goes outside xen
 1.89 16-Mar-2020  jdolecek drop the disabled M_EXT_ROMAP mbuf code, and convert xennet_checksum_fill()
to use in_undefer_cksum() instead of custom code to compute the checksum
for incoming packets

make it possible to defer/skip the checksum computation by appropriate Rx
flag similarily as we do for Tx; with
 1.88 29-Jan-2020  thorpej Adopt <net/if_stats.h>.
 1.87 30-Oct-2019  maxv branches: 1.87.2;
Switch to new PTE bits.
 1.86 09-Mar-2019  maxv branches: 1.86.4;
Start replacing the x86 PTE bits.
 1.85 14-Feb-2019  cherry Welcome XENPVHVM mode.

It is UP only, has xbd(4) and xennet(4) as PV drivers.

The console is com0 at isa and the native portion is very
rudimentary AT architecture, so is probably suboptimal to
run without PV support.
 1.84 05-Feb-2019  msaitoh Remove very old IFF_NOTRAILERS flag.
 1.83 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.82 24-Dec-2018  cherry 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.81 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.80 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.79 03-Sep-2018  riastradh Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.78 10-Aug-2018  maxv Don't unconditionally call pmap_extract_ma, it is part of XENNET_DEBUG.
This call costs us.
 1.77 26-Jun-2018  msaitoh branches: 1.77.2;
Implement the BPF direction filter (BIOC[GS]DIRECTION). It provides backward
compatibility with BIOC[GS]SEESENT ioctl. The userland interface is the same
as FreeBSD.

This change also fixes a bug that the direction is misunderstand on some
environment by passing the direction to bpf_mtap*() instead of checking
m->m_pkthdr.rcvif.
 1.76 24-Jun-2018  jdolecek mark with XXXSMP all remaining spl*() and tsleep() calls
 1.75 22-Jun-2018  msaitoh It's not required to include net/bpfdesc.h. Remove it.
 1.74 25-Jan-2018  riastradh branches: 1.74.2;
Don't call xennet_softstart from hard interrupt; schedule softint.

Fixes panic in bpf_mtap which expects to be called only in softint.
 1.73 11-Nov-2017  riastradh Restore MP-safety annotations in intr_establish_xname.

In the old API, event_set_handler would assume MP-unsafe for IPL_VM
and MP-safe for all other levels (IPL_SCHED, IPL_HIGH). The recent
Xen interrupt rototill started passing known_mpsafe=true for _all_
interrupt handlers. Change it to known_mpsafe=false for the IPL_VM
(= IPL_CLOCK, IPL_NET) ones.
 1.72 06-Nov-2017  cherry Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.71 30-Aug-2017  maxv Make these pages non-executable, and style.
 1.70 04-Mar-2017  bouyer branches: 1.70.6;
Try to be smarted in the rx path: if the packet is small enough copy the
data to the mbuf's data area instead of allocating a new page from the pool.
While there fix a struct xennet_rxreq leak if xennet_checksum_fill()
fails with ENOMEM.
 1.69 15-Dec-2016  ozaki-r branches: 1.69.2;
Move bpf_mtap and if_ipackets++ on Rx of each driver to percpuq if_input

The benefits of the change are:
- We can reduce codes
- We can provide the same behavior between drivers
- Where/When if_ipackets is counted up
- Note that some drivers still update packet statistics in their own
way (periodical update)
- Moved bpf_mtap run in softint
- This makes it easy to MP-ify bpf

Proposed on tech-kern and tech-net
 1.68 10-Jun-2016  ozaki-r branches: 1.68.2;
Introduce m_set_rcvif and m_reset_rcvif

The API is used to set (or reset) a received interface of a mbuf.
They are counterpart of m_get_rcvif, which will come in another
commit, hide internal of rcvif operation, and reduce the diff of
the upcoming change.

No functional change.
 1.67 16-Feb-2016  bouyer In xennet_xenbus_detach(), remove the event handler early (just after
xennet_stop()) so that we don't get events while slepping (e.g.
in softint_disestablish()) when some structures have already been
freed.
Problem reported and patch tested by Rohan Desai.
 1.66 09-Feb-2016  ozaki-r Introduce softint-based if_input

This change intends to run the whole network stack in softint context
(or normal LWP), not hardware interrupt context. Note that the work is
still incomplete by this change; to that end, we also have to softint-ify
if_link_state_change (and bpf) which can still run in hardware interrupt.

This change softint-ifies at ifp->if_input that is called from
each device driver (and ieee80211_input) to ensure Layer 2 runs
in softint (e.g., ether_input and bridge_input). To this end,
we provide a framework (called percpuq) that utlizes softint(9)
and percpu ifqueues. With this patch, rxintr of most drivers just
queues received packets and schedules a softint, and the softint
dequeues packets and does rest packet processing.

To minimize changes to each driver, percpuq is allocated in struct
ifnet for now and that is initialized by default (in if_attach).
We probably have to move percpuq to softc of each driver, but it's
future work. At this point, only wm(4) has percpuq in its softc
as a reference implementation.

Additional information including performance numbers can be found
in the thread at tech-kern@ and tech-net@:
http://mail-index.netbsd.org/tech-kern/2016/01/14/msg019997.html

Acknowledgment: riastradh@ greatly helped this work.
Thank you very much!
 1.65 19-Nov-2015  christos Enable the VLAN mtu capability and check for the adjusted packet size
(Jean-Jacques.Puig at espci.fr).
Factor out the packet-size checking function for clarity.
 1.64 13-Apr-2015  riastradh MD rnd.h cleanups. Please let me know if I broke anything!
 1.63 10-Aug-2014  tls branches: 1.63.2; 1.63.4;
Merge tls-earlyentropy branch into HEAD.
 1.62 30-Jun-2012  jym branches: 1.62.2; 1.62.12;
Use setter to set xenguest_handles.
 1.61 30-Jun-2012  jym Extend the xpmap API, as described in [1]. This change is mechanical and
avoids exposing the MD phys_to_machine/machine_to_phys tables directly.
Added:

- xpmap_ptom handles PFN (pseudo physical) to MFN (machine frame number)
translations, and is under control of the domain.
- xpmap_mtop is its counterpart (MFN to PFN), and is under control of
hypervisor.

xpmap_ptom_map() map a pseudo-phys address to a machine address
xpmap_ptom_unmap() unmap a pseudo-phys address (invalidation)
xpmap_ptom_isvalid() check for pseudo-phys address validity

The parameters are physical/machine addresses, like bus_dma/bus_space(9).
As x86 MFNs are tracked by u_long (Xen's choice) while machine addresses
can be 64 bits entities (PAE), use ptoa() to avoid truncation when bit
shifting by PAGE_SHIFT.

I kept the same namespace (xpmap_) to avoid code churn.

[1] http://mail-index.netbsd.org/port-xen/2009/05/09/msg004951.html

XXX will document ptoa/atop/trunc_page separately.
 1.60 27-Jun-2012  jym Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.59 22-Feb-2012  bouyer Allocating a fixed, limited number of buffers for receive and sending them
to the network stack is a bad idea, because if all receive buffers sits
in socket queues, the interface receive stalls by lack of buffers.
Instead, get receive pages from a pool_cache(9). Copying to a mbuf cluser
would work too, but testings shows this has an important performance hit.
This also simplifies locking.

While there, notify the dom0 when we add some receive buffers (older
linux dom0 didn't care, but newer one do).

Problem reported and fix tested by Brian Marcotte on port-xen
 1.58 02-Feb-2012  tls branches: 1.58.2;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.57 04-Jan-2012  cherry Tighten up locking in the network driver.
This probably needs more attention since xennet_rx_mbuf_free() hooks
into the network layer.
The locking can also be made finer grained. Performance testing could
add some insights.
 1.56 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.55 04-Dec-2011  cherry [merging from cherry-xenmp]
Make MP aware: use mutex(9) instead of spl(9)
 1.54 19-Nov-2011  tls branches: 1.54.2;
First step of random number subsystem rework described in
<20111022023242.BA26F14A158@mail.netbsd.org>. This change includes
the following:

An initial cleanup and minor reorganization of the entropy pool
code in sys/dev/rnd.c and sys/dev/rndpool.c. Several bugs are
fixed. Some effort is made to accumulate entropy more quickly at
boot time.

A generic interface, "rndsink", is added, for stream generators to
request that they be re-keyed with good quality entropy from the pool
as soon as it is available.

The arc4random()/arc4randbytes() implementation in libkern is
adjusted to use the rndsink interface for rekeying, which helps
address the problem of low-quality keys at boot time.

An implementation of the FIPS 140-2 statistical tests for random
number generator quality is provided (libkern/rngtest.c). This
is based on Greg Rose's implementation from Qualcomm.

A new random stream generator, nist_ctr_drbg, is provided. It is
based on an implementation of the NIST SP800-90 CTR_DRBG by
Henric Jungheim. This generator users AES in a modified counter
mode to generate a backtracking-resistant random stream.

An abstraction layer, "cprng", is provided for in-kernel consumers
of randomness. The arc4random/arc4randbytes API is deprecated for
in-kernel use. It is replaced by "cprng_strong". The current
cprng_fast implementation wraps the existing arc4random
implementation. The current cprng_strong implementation wraps the
new CTR_DRBG implementation. Both interfaces are rekeyed from
the entropy pool automatically at intervals justifiable from best
current cryptographic practice.

In some quick tests, cprng_fast() is about the same speed as
the old arc4randbytes(), and cprng_strong() is about 20% faster
than rnd_extract_data(). Performance is expected to improve.

The AES code in src/crypto/rijndael is no longer an optional
kernel component, as it is required by cprng_strong, which is
not an optional kernel component.

The entropy pool output is subjected to the rngtest tests at
startup time; if it fails, the system will reboot. There is
approximately a 3/10000 chance of a false positive from these
tests. Entropy pool _input_ from hardware random numbers is
subjected to the rngtest tests at attach time, as well as the
FIPS continuous-output test, to detect bad or stuck hardware
RNGs; if any are detected, they are detached, but the system
continues to run.

A problem with rndctl(8) is fixed -- datastructures with
pointers in arrays are no longer passed to userspace (this
was not a security problem, but rather a major issue for
compat32). A new kernel will require a new rndctl.

The sysctl kern.arandom() and kern.urandom() nodes are hooked
up to the new generators, but the /dev/*random pseudodevices
are not, yet.

Manual pages for the new kernel interfaces are forthcoming.
 1.53 26-Sep-2011  jym branches: 1.53.2;
Fix a fallout with my xensuspend merge: talk_to_backend() returns a
boolean, so checking for "true" with "== 0" is... wrong.

Now xennet(4) should work as expected, and not stay in the InitWait state
(which blocks network communication with the backend).

Thanks to riz@ and sborrill@ for reporting breakage with -current
xennet(4) after my merge.
 1.52 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.51 30-May-2011  joerg branches: 1.51.2;
Use proper format string
 1.50 25-Apr-2011  jym Check status before proceeding further. Avoids spurious watch calls.
 1.49 25-Apr-2011  jym Separate xennet(4) backend initialization code ("resume") from the part
that talks with Xenstore to query backend's information. Resuming is now
performed just after xennet(4) attachment instead of waiting for backend
to announce its features in Xenstore and change it state.

This fixes the race observed by Urban Boquist when the domU boots with
root on NFS.

FWIW, the boot code (when root is NFS-backed) can innit() the xennet(4)
interface very early: it tried to access ifnet structures that were not
yet allocated.

Will ask for a pullup. Thanks to Urban for reporting the issue and
investigate it. Confirmed fixed. No regression observed by me for
dynamic attach/detach of xvif(4) and xennet(4) interfaces.

See also http://mail-index.netbsd.org/port-xen/2011/04/18/msg006647.html
 1.48 30-Mar-2011  jym Do not clobber autoconf messages (and variables, for error reporting)
in xennet(4) handler. Keep if_xname though.

Reported by dyoung@, thanks.
 1.47 30-Mar-2011  jym printf("xennet: ...") => aprint_error_ifnet()
 1.46 11-Jan-2011  jym branches: 1.46.2;
Introduce "vifname" keys for Xen domains. Its value is the interface
name for the vif, e.g. xvif(4) for dom0, and xennet(4) for domU.

ok bouyer@.

See http://mail-index.netbsd.org/port-xen/2011/01/11/msg006405.html
 1.45 17-Oct-2010  jym Implement feature-rx-copy support in xennet (domU network frontend).

Instead of flipping pages back and forth between dom0 and domU for the
network RX queue, feature-rx-copy tells frontend to use content copy
instead.

This is the only mode supported by the dom0 Linux pv_ops backend. NetBSD
domU and dom0 can still fall back to flipping, when needed.

Copy is supposed to be faster than flipping, as it does not require
MMU manipulation and TLB shootdowns.

Based on patch provided by Hideki ONO. Thanks!

See also http://mail-index.netbsd.org/port-xen/2010/09/24/msg006265.html
and http://mail-index.netbsd.org/port-xen/2010/10/16/msg006312.html

ok bouyer@.

XXX will ask for a pull-up after 5.1 is branched.
 1.44 16-Oct-2010  jym size_t: %d => %zd
 1.43 16-Oct-2010  jym Handle error case (avoid changing to XenbusStateConnected when resume
failed)
 1.42 16-Oct-2010  jym Features (like feature-rx-copy) are set during XenbusStateInitWait in
backend. So delay xennet_xenbus_resume() up to notification of
backend state change.

This avoids a race that happens during dynamic attach/detach of network
interfaces with xm(1), where frontend queries xenstore for features not
yet reported by backend. This does not happen during normal domU boot,
as the backend has enough time to fill in these entries before frontend
asks for them.

Issue was reported by sborrill@: detaching xennet interfaces with RX copy
mode enabled turns them back during attach to RX flip mode due to the race.

feature-rx-copy support is part of another patch.
 1.41 05-Apr-2010  joerg Push the bpf_ops usage back into bpf.h. Push the common ifp->if_bpf
check into the inline functions as well the fourth argument for
bpf_attach.
 1.40 19-Jan-2010  pooka branches: 1.40.2; 1.40.4;
Redefine bpf linkage through an always present op vector, i.e.
#if NBPFILTER is no longer required in the client. This change
doesn't yet add support for loading bpf as a module, since drivers
can register before bpf is attached. However, callers of bpf can
now be modularized.

Dynamically loadable bpf could probably be done fairly easily with
coordination from the stub driver and the real driver by registering
attachments in the stub before the real driver is loaded and doing
a handoff. ... and I'm not going to ponder the depths of unload
here.

Tested with i386/MONOLITHIC, modified MONOLITHIC without bpf and rump.
 1.39 23-Oct-2009  snj Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.38 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.37 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.36 02-May-2009  bouyer Patch from Brian Marcotte on port-xen@:
write a "feature-rx-notify" to the xenstore, which is used by recent
linux dom0 kernels. This reduce packet loss when using a NetBSD domU
on such linux dom0.
This entry is ignored by NetBSD and older linux domUs.
 1.35 29-Apr-2009  jym Do not forget to detach rnd(4) device when removing a xennet device.

Ok by bouyer@ in private mail.
 1.34 03-Mar-2009  bouyer Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.33 08-Feb-2009  jym branches: 1.33.2;
- add comments
- wrap long lines for easier reading
- make some debug messages a bit more relevant
 1.32 16-Jan-2009  jym 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.31 15-Jan-2009  jym Remove unneeded x86_lfence(). Xen RING macros use memory fences internally, and
hypercalls are also memory fences.

Ok by bouyer@.
 1.30 13-Nov-2008  cegger Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.29 30-Oct-2008  cegger branches: 1.29.2;
twiddle with headers: include what is really needed
 1.28 27-Oct-2008  cegger change nfs boot behaviour to automatically try next boot method if boot information are incomplete to succeed.
That way, it is possible combine static and dhcp boot:
For example, to boot diskless you can specify the nfs-server and the rootpath statically. All other information will be taken via dhcp.

Patch has been presented on port-xen, tech-kern and tech-net:
http://mail-index.netbsd.org/port-xen/2008/10/24/msg004488.html
http://mail-index.netbsd.org/tech-kern/2008/10/24/msg003255.html
http://mail-index.netbsd.org/tech-net/2008/10/24/msg000864.html

No comments, no objections.
 1.27 25-Oct-2008  jym branches: 1.27.2;
- add and fix some comments in xennet and xbd code
- add BEST_SUSPENDED macro, the xennet equivalent of BLKIF_STATE_SUSPENDED
for xbd (indicate that backend is in suspended state)
- use GNTST_okay to check the grant table setup status returned by hypercall,
rather than 0

No functional changes.
 1.26 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.25 16-Apr-2008  cegger branches: 1.25.4; 1.25.10;
device_t / softc split
reviewed, tested and approved by bouyer
 1.24 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.23 23-Jan-2008  bouyer branches: 1.23.6;
Merge the bouyer-xeni386 branch. This brings in PAE support to NetBSD xeni386
(domU only). PAE support is enabled by 'options PAE', see the new XEN3PAE_DOMU
and INSTALL_XEN3PAE_DOMU kernel config files.

See the comments in arch/i386/include/{pte.h,pmap.h} to see how it works.
In short, we still handle it as a 2-level MMU, with the second level page
directory being 4 pages in size. pmap switching is done by switching the
L2 pages in the L3 entries, instead of loading %cr3. This is almost required
by Xen, which handle the last L2 page (the one mapping 0xc0000000 - 0xffffffff)
in a very special way. But this approach should also work for native PAE
support if ever supported (in fact, the pmap should almost suport native
PAE, what's missing is bootstrap code in locore.S).
 1.22 03-Dec-2007  ad branches: 1.22.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.21 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.20 07-Nov-2007  ad Merge from vmlocking:

- pool_cache changes.
- Debugger/procfs locking fixes.
- Other minor changes.
 1.19 17-Oct-2007  garbled branches: 1.19.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.18 26-Aug-2007  dyoung branches: 1.18.2; 1.18.6;
Constify.
 1.17 22-Jul-2007  he branches: 1.17.4;
Explicitly include <sys/proc.h> for tsleep() declaration.
 1.16 08-Jul-2007  bouyer branches: 1.16.2; 1.16.4;
Add a bootstatic callback for dom0, which allows to take NFS boot informations
from the command line (nfsroot=, ip=) in the same way domUs did.
While there convert a for (;;) loop to TAILQ_FOREACH().
From Christoph Egger in private mail.

Make all bootstatic callbacks return the new NFS_BOOTSTATIC_NOSTATIC flag
when no nfs boot information is found on command line.
 1.15 04-Mar-2007  christos branches: 1.15.2; 1.15.4; 1.15.10;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.14 03-Nov-2006  jld branches: 1.14.2; 1.14.4;
Recycle old buffers into new rx requests when their number reaches half of
those not tied up in active mbufs, rather than half of the total number.

(Or, to more literally translate the C, when there are at least as
many buffers waiting to be reused as outstanding rx requests.)

This prevents us from dropping off the network for a while when more
than 128 of the 256 buffers are in use by mbufs, which might not be
freed for an arbitrarily long time; the remaining buffers would stack up
on the free list but not be reclaimed until enough mbufs were eventually
freed, leaving the interface unable to receive packets until then.
 1.13 12-Jul-2006  yamt branches: 1.13.4; 1.13.6; 1.13.8;
implement NETRXF_csum_blank/NETTXF_csum_blank tx checksum "offloading".
 1.12 12-Jul-2006  yamt implement a simple NETTXF_csum_blank/NETRXF_csum_blank workaround
so that we can talk with linux guests at least.
just fill checksum field of received packets if the flag is set.
maybe should be revisited later.
 1.11 02-Jul-2006  bouyer m_copyback() doesn't adjust m->m_len if the data to copy is smaller than
the mbuf's size. Set it to a proper value before calling m_copyback().
This should fix panics in m_makewritable() reported by several peoples.
While there, add MCLAIM() calls where appropriate, a few more #ifdef DEBUG
checks, and various minor fixes.
 1.10 27-May-2006  bouyer branches: 1.10.2; 1.10.4;
The receive buffer, once mapped back in the domU, is read/write and not
shared so there;s no problems marking the mbuf M_EXT_RW.
 1.9 23-May-2006  bouyer 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.8 14-May-2006  bouyer branches: 1.8.2;
Add a missing break in a switch clause, causing the frontend to go to Closed
state when it should not.
 1.7 02-Apr-2006  bouyer branches: 1.7.2; 1.7.4;
Properly update output counters.
 1.6 02-Apr-2006  bouyer Add proper detach support for Xen3 xennet.
 1.5 02-Apr-2006  bouyer Ops, better include bpfilter.h if we're going to use NBPFILTER > 0.
Should make bpf work on Xen3 domU, problem reported by Stephane Bortzmeyer
on port-xen.
 1.4 19-Mar-2006  bouyer Add code to dump the xenstore's node for this device under XENNET_DEBUG.
 1.3 06-Mar-2006  bouyer branches: 1.3.2; 1.3.4; 1.3.6;
Adapt for recent changes in UVM.
 1.2 06-Mar-2006  bouyer Fix build issues that didn't show up in my private tree.
 1.1 06-Mar-2006  bouyer Add a Xen Virtual Network device driver for Xenbus.
 1.3.6.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.3.6.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.3.4.7 11-Aug-2006  yamt sync with head
 1.3.4.6 26-Jun-2006  yamt sync with head.
 1.3.4.5 24-May-2006  yamt sync with head.
 1.3.4.4 11-Apr-2006  yamt sync with head
 1.3.4.3 01-Apr-2006  yamt sync with head.
 1.3.4.2 13-Mar-2006  yamt sync with head.
 1.3.4.1 06-Mar-2006  yamt file if_xennet_xenbus.c was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.3.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.7.4.3 01-Jun-2006  kardel Sync with head.
 1.7.4.2 22-Apr-2006  simonb Sync with head.
 1.7.4.1 02-Apr-2006  simonb file if_xennet_xenbus.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.7.2.8 13-May-2009  bouyer Pull up following revision(s) (requested by jym in ticket #2013):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.35
Do not forget to detach rnd(4) device when removing a xennet device.
Ok by bouyer@ in private mail.
 1.7.2.7 20-Mar-2009  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1996):
sys/arch/xen/xen/if_xennet_xenbus.c 1.34
src/sys/arch/xen/xen/xbd_xenbus.c 1.39

Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.7.2.6 11-Nov-2006  bouyer Pull up following revision(s) (requested by jld in ticket #1581):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.14
Recycle old buffers into new rx requests when their number reaches half of
those not tied up in active mbufs, rather than half of the total number.
(Or, to more literally translate the C, when there are at least as
many buffers waiting to be reused as outstanding rx requests.)
This prevents us from dropping off the network for a while when more
than 128 of the 256 buffers are in use by mbufs, which might not be
freed for an arbitrarily long time; the remaining buffers would stack up
on the free list but not be reclaimed until enough mbufs were eventually
freed, leaving the interface unable to receive packets until then.
 1.7.2.5 05-Aug-2006  ghen branches: 1.7.2.5.2;
Pull up following revision(s) (requested by bouyer in ticket #1454):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.11
m_copyback() doesn't adjust m->m_len if the data to copy is smaller than
the mbuf's size. Set it to a proper value before calling m_copyback().
This should fix panics in m_makewritable() reported by several peoples.
While there, add MCLAIM() calls where appropriate, a few more #ifdef DEBUG
checks, and various minor fixes.
 1.7.2.4 05-Aug-2006  ghen Pull up following revision(s) (requested by bouyer in ticket #1452):
sys/arch/xen/xen/if_xennet.c: revision 1.48
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.10
The receive buffer, once mapped back in the domU, is read/write and not
shared so there;s no problems marking the mbuf M_EXT_RW.
 1.7.2.3 05-Aug-2006  ghen Pull up following revision(s) (requested by bouyer in ticket #1451):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.8
Add a missing break in a switch clause, causing the frontend to go to Closed
state when it should not.
 1.7.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.7.2.1 02-Apr-2006  tron file if_xennet_xenbus.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.7.2.5.2.1 11-Nov-2006  bouyer Pull up following revision(s) (requested by jld in ticket #1581):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.14
Recycle old buffers into new rx requests when their number reaches half of
those not tied up in active mbufs, rather than half of the total number.
(Or, to more literally translate the C, when there are at least as
many buffers waiting to be reused as outstanding rx requests.)
This prevents us from dropping off the network for a while when more
than 128 of the 256 buffers are in use by mbufs, which might not be
freed for an arbitrarily long time; the remaining buffers would stack up
on the free list but not be reclaimed until enough mbufs were eventually
freed, leaving the interface unable to receive packets until then.
 1.8.2.1 19-Jun-2006  chap Sync with head.
 1.10.4.10 27-Feb-2008  yamt revert the rest of lazy mapping code.
 1.10.4.9 27-Feb-2008  yamt drop lazy mapping of mbuf external storage for now, because:
- it's currently broken wrt asm code. (cpu_in_cksum)
- there are other approaches worth to consider. eg. sf_buf
 1.10.4.8 04-Feb-2008  yamt sync with head.
 1.10.4.7 07-Dec-2007  yamt sync with head
 1.10.4.6 15-Nov-2007  yamt sync with head.
 1.10.4.5 03-Sep-2007  yamt sync with head.
 1.10.4.4 31-Dec-2006  yamt xennet_softstart: handle M_EXT_LAZY.
 1.10.4.3 30-Dec-2006  yamt sync with head.
 1.10.4.2 21-Jun-2006  yamt sync with head.
 1.10.4.1 27-May-2006  yamt file if_xennet_xenbus.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.10.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.13.8.1 10-Dec-2006  yamt sync with head.
 1.13.6.2 09-Sep-2006  rpaulo sync with head
 1.13.6.1 12-Jul-2006  rpaulo file if_xennet_xenbus.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.13.4.1 18-Nov-2006  ad Sync with head.
 1.14.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.14.2.2 13-May-2009  bouyer Pull up following revision(s) (requested by jym in ticket #1316):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.35
Do not forget to detach rnd(4) device when removing a xennet device.
Ok by bouyer@ in private mail.
 1.14.2.1 20-Mar-2009  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1282):
sys/arch/xen/xen/if_xennet_xenbus.c 1.34
src/sys/arch/xen/xen/xbd_xenbus.c 1.39

Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.15.10.1 03-Oct-2007  garbled Sync with HEAD
 1.15.4.1 11-Jul-2007  mjf Sync with head.
 1.15.2.4 03-Dec-2007  ad Sync with HEAD.
 1.15.2.3 09-Oct-2007  ad Sync with head.
 1.15.2.2 20-Aug-2007  ad Sync with HEAD.
 1.15.2.1 15-Jul-2007  ad Sync with head.
 1.16.4.2 03-Sep-2007  skrll Sync with HEAD.
 1.16.4.1 15-Aug-2007  skrll Sync with HEAD.
 1.16.2.1 07-Aug-2007  matt Sync with HEAD.
 1.17.4.4 09-Dec-2007  jmcneill Sync with HEAD.
 1.17.4.3 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.17.4.2 11-Nov-2007  joerg Sync with HEAD.
 1.17.4.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.18.6.2 13-Nov-2007  bouyer Sync with HEAD
 1.18.6.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.18.2.4 23-Mar-2008  matt sync with HEAD
 1.18.2.3 09-Jan-2008  matt sync with HEAD
 1.18.2.2 08-Nov-2007  matt sync with -HEAD
 1.18.2.1 06-Nov-2007  matt sync with HEAD
 1.19.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.19.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.19.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.22.6.1 13-Jan-2008  bouyer Work in progress on xeni386 PAE support:
Make xeni386 build with a 64bit paddr_t. For this vaddr_t vs paddr_t vs
pointers usages had to be clarified.
If 'options PAE' is present in a Xen3 kernel, switch paddr_t, pd_entry_t
and pt_entry_t to 64bits, and add the PAE entry in the __xen_guest ELF section.
 1.23.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.23.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.25.10.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.25.4.4 11-Aug-2010  yamt sync with head.
 1.25.4.3 11-Mar-2010  yamt sync with head
 1.25.4.2 19-Aug-2009  yamt sync with head.
 1.25.4.1 04-May-2009  yamt sync with head.
 1.27.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.27.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.27.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.29.2.7 24-Feb-2012  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1730):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.59 via patch

Fix receive stall on the domU side when buffers stay a long time in the
network stack or socket buffers.
 1.29.2.6 19-May-2011  bouyer Pull up following revision(s) (requested by jym in ticket #1609):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.50
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.43
Check that xvif(4) is not already connected before proceeding in the
XenbusStateConnected mode. Under rare occasions, the xenbus watcher
can fire multiple times, overwriting the I/O ring memory mappings with
invalid values. This will lead sooner or later to dom0 panic().
Will ask for pullup. FWIW, xbdback(4) is not affected.
Check status before proceeding further. Avoids spurious watch calls.
 1.29.2.5 19-May-2011  bouyer Pull up following revision(s) (requested by jym in ticket #1608):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.49
Separate xennet(4) backend initialization code ("resume") from the part
that talks with Xenstore to query backend's information. Resuming is now
performed just after xennet(4) attachment instead of waiting for backend
to announce its features in Xenstore and change it state.
This fixes the race observed by Urban Boquist when the domU boots with
root on NFS.
FWIW, the boot code (when root is NFS-backed) can innit() the xennet(4)
interface very early: it tried to access ifnet structures that were not
yet allocated.
Will ask for a pullup. Thanks to Urban for reporting the issue and
investigate it. Confirmed fixed. No regression observed by me for
dynamic attach/detach of xvif(4) and xennet(4) interfaces.
See also http://mail-index.netbsd.org/port-xen/2011/04/18/msg006647.html
 1.29.2.4 21-Nov-2010  riz Pull up following revision(s) (requested by jym in ticket #1473):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.42
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.43
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.45
Features (like feature-rx-copy) are set during XenbusStateInitWait in
backend. So delay xennet_xenbus_resume() up to notification of
backend state change.
This avoids a race that happens during dynamic attach/detach of network
interfaces with xm(1), where frontend queries xenstore for features not
yet reported by backend. This does not happen during normal domU boot,
as the backend has enough time to fill in these entries before frontend
asks for them.
Issue was reported by sborrill@: detaching xennet interfaces with RX copy
mode enabled turns them back during attach to RX flip mode due to the race.
feature-rx-copy support is part of another patch.
Handle error case (avoid changing to XenbusStateConnected when resume
failed)
Implement feature-rx-copy support in xennet (domU network frontend).
Instead of flipping pages back and forth between dom0 and domU for the
network RX queue, feature-rx-copy tells frontend to use content copy
instead.
This is the only mode supported by the dom0 Linux pv_ops backend. NetBSD
domU and dom0 can still fall back to flipping, when needed.
Copy is supposed to be faster than flipping, as it does not require
MMU manipulation and TLB shootdowns.
Based on patch provided by Hideki ONO. Thanks!
See also http://mail-index.netbsd.org/port-xen/2010/09/24/msg006265.html
and http://mail-index.netbsd.org/port-xen/2010/10/16/msg006312.html
ok bouyer@.
XXX will ask for a pull-up after 5.1 is branched.
 1.29.2.3 28-Sep-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1027):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.36
Patch from Brian Marcotte on port-xen@:
write a "feature-rx-notify" to the xenstore, which is used by recent
linux dom0 kernels. This reduce packet loss when using a NetBSD domU
on such linux dom0.
This entry is ignored by NetBSD and older linux domUs.
 1.29.2.2 13-May-2009  snj Pull up following revision(s) (requested by jym in ticket #754):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.35
Do not forget to detach rnd(4) device when removing a xennet device.
Ok by bouyer@ in private mail.
 1.29.2.1 08-Mar-2009  snj branches: 1.29.2.1.4;
Pull up following revision(s) (requested by bouyer in ticket #545):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.34
sys/arch/xen/xen/xbd_xenbus.c: revision 1.39
Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.29.2.1.4.1 21-Apr-2010  matt sync to netbsd-5
 1.33.2.13 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.33.2.12 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.33.2.11 26-May-2011  jym Split allocation and initialization of ring I/O for xbd(4) and xennet(4):
- allocation belongs to _attach()
- init to _resume(), so that it can be used by suspend/resume code too.
 1.33.2.10 07-May-2011  jym When it successfully resume a device, return true instead of 0 aka false.
 1.33.2.9 02-May-2011  jym Sync with head.
 1.33.2.8 30-Mar-2011  jym Sync with my commits in HEAD.
 1.33.2.7 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.33.2.6 24-Oct-2010  jym Sync with HEAD
 1.33.2.5 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.33.2.4 01-Nov-2009  jym Sync with HEAD.
 1.33.2.3 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.33.2.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.33.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.40.4.4 31-May-2011  rmind sync with head
 1.40.4.3 21-Apr-2011  rmind sync with head
 1.40.4.2 05-Mar-2011  rmind sync with head
 1.40.4.1 30-May-2010  rmind sync with head
 1.40.2.2 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.40.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.46.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.51.2.2 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.51.2.1 21-Aug-2011  cherry add locking around mmu flush xpq ops
 1.53.2.2 30-Oct-2012  yamt sync with head
 1.53.2.1 17-Apr-2012  yamt sync with head
 1.54.2.2 24-Feb-2012  mrg sync to -current.
 1.54.2.1 18-Feb-2012  mrg merge to -current.
 1.58.2.1 23-Feb-2012  riz Pull up following revision(s) (requested by bouyer in ticket #33):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.59
Allocating a fixed, limited number of buffers for receive and sending them
to the network stack is a bad idea, because if all receive buffers sits
in socket queues, the interface receive stalls by lack of buffers.
Instead, get receive pages from a pool_cache(9). Copying to a mbuf cluser
would work too, but testings shows this has an important performance hit.
This also simplifies locking.
While there, notify the dom0 when we add some receive buffers (older
linux dom0 didn't care, but newer one do).
Problem reported and fix tested by Brian Marcotte on port-xen
 1.62.12.1 07-Apr-2014  tls Be a little more clear and consistent about harvesting entropy from devices:

1) deprecate RND_FLAG_NO_ESTIMATE

2) define RND_FLAG_COLLECT_TIME, RND_FLAG_COLLECT_VALUE

3) define RND_FLAG_ESTIMATE_TIME, RND_FLAG_ESTIMATE_VALUE

4) define RND_FLAG_DEFAULT: RND_FLAG_COLLECT_TIME|
RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_TIME

5) Make entropy harvesting from environmental sensors a little more generic
and remove it from individual sensor drivers.

6) Remove individual open-coded delta-estimators for values from a few
places in the tree (uvm, environmental drivers).

7) 0 -> RND_FLAG_DEFAULT, actually gather entropy from various drivers
that had stubbed out code, other minor cleanups.
 1.62.2.2 03-Dec-2017  jdolecek update from HEAD
 1.62.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.63.4.6 28-Aug-2017  skrll Sync with HEAD
 1.63.4.5 05-Feb-2017  skrll Sync with HEAD
 1.63.4.4 09-Jul-2016  skrll Sync with HEAD
 1.63.4.3 19-Mar-2016  skrll Sync with HEAD
 1.63.4.2 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.63.4.1 06-Jun-2015  skrll Sync with HEAD
 1.63.2.3 24-Sep-2017  snj Pull up following revision(s) (requested by manu in ticket #1409):
sys/arch/xen/xen/if_xennet_xenbus.c: 1.65
sys/arch/xen/xen/xennetback_xenbus.c: 1.53, 1.56 via patch
sys/net/if_bridge.c: 1.105
sys/net/if_ether.h: 1.65
sys/net/if_ethersubr.c: 1.215, 1.235
sys/net/if_vlan.c: 1.76, 1.77, 1.83, 1.88, 1.94
Protect vlan_unconfig with a mutex
It is not thread-safe but is likely to be executed in concurrent.
See PR 49264 for more detail.
--
Tweak vlan_unconfig
No functional change.
--
Add handling of VLAN packets in if_bridge where the parent interface supports
them (Jean-Jacques.Puig%espci.fr@localhost). Factor out the vlan_mtu enabling and
disabling code.
--
Enable the VLAN mtu capability and check for the adjusted packet size
(Jean-Jacques.Puig at espci.fr).
Factor out the packet-size checking function for clarity.
--
Don't increment the reference count only when it was 0...
From Jean-Jacques.Puig
--
Account for the CRC len (Jean-Jacques.Puig)
--
Fix a bug that the parent interface's callback wasn't called when the vlan
interface is configured. A callback function uses VLAN_ATTACHED() function
which check ec->ec_nvlans, the value should be incremented before calling the
callback. This bug was added in if_vlan.c rev. 1.83 (2015/11/19).
 1.63.2.2 26-Mar-2017  snj Pull up following revision(s) (requested by bouyer in ticket #1379):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.70 via patch
Try to be smarted in the rx path: if the packet is small enough copy the
data to the mbuf's data area instead of allocating a new page from the pool.
While there fix a struct xennet_rxreq leak if xennet_checksum_fill()
fails with ENOMEM.
 1.63.2.1 06-Mar-2016  martin Pull up following revision(s) (requested by bouyer in ticket #1125):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.67
In xennet_xenbus_detach(), remove the event handler early (just after
xennet_stop()) so that we don't get events while slepping (e.g.
in softint_disestablish()) when some structures have already been
freed.
Problem reported and patch tested by Rohan Desai.
 1.68.2.2 20-Mar-2017  pgoyette Sync with HEAD
 1.68.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.69.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.70.6.3 31-Jul-2023  martin Apply patch, requested by riastradh in ticket #1863, pulling up
the essential part of

sys/arch/xen/xen/if_xennet_xenbus.c 1.129

xennet(4): Add missing membar.
 1.70.6.2 21-Jan-2019  martin Pull up following revision(s) (requested by bouyer in ticket #1166):

sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.74

Don't call xennet_softstart from hard interrupt; schedule softint.

Fixes panic in bpf_mtap which expects to be called only in softint.
 1.70.6.1 26-Jul-2018  snj Pull up following revision(s) (requested by msaitoh in ticket #938):
sys/arch/acorn32/podulebus/if_ie.c: revision 1.41
sys/arch/amiga/dev/if_es.c: revision 1.58
sys/arch/amiga/dev/if_qn.c: revision 1.45
sys/arch/arm/at91/at91emac.c: revision 1.20
sys/arch/arm/ep93xx/epe.c: revision 1.37
sys/arch/emips/ebus/if_le_ebus.c: revision 1.14
sys/arch/emips/ebus/if_le_ebus.c: revision 1.15
sys/arch/mac68k/dev/if_mc.c: revision 1.46
sys/arch/macppc/dev/am79c950.c: revision 1.39
sys/arch/newsmips/apbus/if_sn.c: revision 1.40
sys/arch/next68k/dev/mb8795.c: revision 1.59
sys/arch/playstation2/dev/if_smap.c: revision 1.25
sys/arch/playstation2/dev/if_smap.c: revision 1.26
sys/arch/sun2/dev/if_ec.c: revision 1.28
sys/arch/sun3/dev/if_ie.c: revision 1.63
sys/arch/x68k/dev/if_ne_intio.c: revision 1.19
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.75
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.63
sys/dev/bi/if_ni.c: revision 1.45
sys/dev/cadence/if_cemac.c: revision 1.12
sys/dev/ic/am7990.c: revision 1.78
sys/dev/ic/am79900.c: revision 1.27
sys/dev/ic/an.c: revision 1.67
sys/dev/ic/cs89x0.c: revision 1.40
sys/dev/ic/dm9000.c: revision 1.13
sys/dev/ic/dm9000.c: revision 1.14
sys/dev/ic/dp8390.c: revision 1.88
sys/dev/ic/elink3.c: revision 1.141
sys/dev/ic/elinkxl.c: revision 1.122
sys/dev/ic/hme.c: revision 1.98
sys/dev/ic/i82586.c: revision 1.77
sys/dev/ic/lance.c: revision 1.53
sys/dev/ic/mb86950.c: revision 1.27
sys/dev/ic/mb86960.c: revision 1.86
sys/dev/ic/mtd803.c: revision 1.34
sys/dev/ic/pdq_ifsubr.c: revision 1.59
sys/dev/ic/rrunner.c: revision 1.86
sys/dev/ic/seeq8005.c: revision 1.58
sys/dev/ic/sgec.c: revision 1.47
sys/dev/ic/smc90cx6.c: revision 1.72
sys/dev/ic/smc91cxx.c: revision 1.96
sys/dev/ic/tropic.c: revision 1.49
sys/dev/ic/wi.c: revision 1.245
sys/dev/isa/if_eg.c: revision 1.93
sys/dev/isa/if_el.c: revision 1.95
sys/dev/isa/if_iy.c: revision 1.101
sys/dev/ofw/ofnet.c: revision 1.58
sys/dev/pci/if_alc.c: revision 1.27
sys/dev/pci/if_de.c: revision 1.152
sys/dev/pci/if_fpa.c: revision 1.61
sys/dev/pci/if_jme.c: revision 1.34
sys/dev/pci/if_tl.c: revision 1.108
sys/dev/pci/if_vte.c: revision 1.19
sys/dev/pci/ixgbe/ixgbe.h: revision 1.50
sys/dev/pcmcia/if_cnw.c: revision 1.62
sys/dev/pcmcia/if_malo_pcmcia.c: revision 1.17
sys/dev/pcmcia/if_ray.c: revision 1.89
sys/dev/pcmcia/if_xi.c: revision 1.81
sys/dev/pcmcia/mhzc.c: revision 1.51
sys/dev/pcmcia/xirc.c: revision 1.34
sys/dev/qbus/if_de.c: revision 1.33
sys/dev/qbus/if_qe.c: revision 1.78
sys/dev/qbus/if_qt.c: revision 1.22
sys/dev/sbus/be.c: revision 1.87
sys/dev/sbus/qe.c: revision 1.68
sys/dev/scsipi/if_se.c: revision 1.96
sys/dev/usb/if_atu.c: revision 1.59
sys/net/if_l2tp.c: revision 1.28 via patch
sys/net/if_ppp.c: revision 1.160
It's not required to include net/bpfdesc.h. Remove it.
--
Simplify like other drivers. NULL check of ifp->if_bpf is done in
bpf_mtap(), so it's not required to do it here.
--
Remove duplicated inclusion of net/bpf.h.
--
Remove duplicated inclusion of net/bpf.h.
--
Simplify bpf_mtap() call. No functional change.
 1.74.2.5 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.74.2.4 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.74.2.3 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.74.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.74.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.77.2.4 21-Apr-2020  martin Sync with HEAD
 1.77.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.77.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.77.2.1 10-Jun-2019  christos Sync with HEAD
 1.86.4.1 31-Jul-2023  martin Apply patch, requested by riastradh in ticket #1680, pulling up
the essential part of

sys/arch/xen/xen/if_xennet_xenbus.c 1.129

xennet(4): Add missing membar.
 1.87.2.1 29-Feb-2020  ad Sync with head.
 1.109.2.3 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.109.2.2 20-Apr-2020  bouyer Sync with HEAD
 1.109.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.128.20.2 14-Jan-2024  martin Pull up following revision(s) (requested by jdolecek in ticket #543):

sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.130

disable TX checksum optimization, it's causing ARP lossage in some
configurations using Linux dom0

PR port-xen/57743 by Brian Marcotte, thanks for the patch
 1.128.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.28 16-Jul-2017  cherry branches: 1.28.2;
Unify the xen and native x86/ interrupt setup functions and
spl traversal data structures.

This is towards PVHVM.
 1.27 16-Jul-2017  cherry Remove the xen specific interrupt type for the x86 intr_handle_t
For this to work, we use the evtchn.c:get_pirq_to_evtchn() glue
function to make things easier.
 1.26 01-Sep-2011  christos branches: 1.26.12; 1.26.30;
Add bus_dma overrides. From dyoung
 1.25 27-Aug-2011  christos use c99 struct initializers
 1.24 01-Jul-2011  dyoung #include <sys/bus.h> instead of <machine/bus.h>.
 1.23 06-Feb-2010  bouyer Enlarge temporary buffer, so that event counter's name is not truncated
for ioapic interrupts.
 1.22 19-Aug-2009  dyoung isa_detach_hook() needs two arguments, the first an isa_chipset_tag_t.
 1.21 18-Aug-2009  dyoung These are stragglers from my last commit ("Let us safely detach
the ISA bus and devices attaching to the ISA bus"). Define
isa_detach_hook() in MD ISA implementations. Define isa_dmadestroy().
 1.20 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.19 01-May-2009  cegger struct device * -> device_t
 1.18 16-Mar-2009  cegger ansify function definitions
 1.17 14-Mar-2009  dsl ANSIfy another 1261 function definitions.
The only ones left in sys are beyond by sed script!
(or in sys/dist or sys/external)
Mostly they have function pointer parameters.
 1.16 14-Mar-2009  dsl Change about 4500 of the K&R function definitions to ANSI ones.
There are still about 1600 left, but they have ',' or /* ... */
in the actual variable definitions - which my awk script doesn't handle.
There are also many that need () -> (void).
(The script does handle misordered arguments.)
 1.15 14-Mar-2009  dsl Remove all the __P() from sys (excluding sys/dist)
Diff checked with grep and MK1 eyeball.
i386 and amd64 GENERIC and sys still build.
 1.14 10-Mar-2009  bouyer When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.13 18-Dec-2008  cegger branches: 1.13.2;
remove unused malloc.h
 1.12 03-Jul-2008  drochner branches: 1.12.4; 1.12.6; 1.12.12;
split device/softc for ioapic
 1.11 03-Jul-2008  drochner Remove "struct device" from "struct pic", where it was only real
for ioapics and faked up for others. Add it to "struct ioapic_softc"
for now, until device/softc get split.
This required all typecasts between "struct pic" and "struct ioapic_softc"
to be replaced, I hope I got them all.
functionally tested on i386, compile-tested on xen, untested on amd64
 1.10 28-Apr-2008  martin branches: 1.10.2; 1.10.4;
Remove clause 3 and 4 from TNF licenses
 1.9 18-Nov-2007  bouyer branches: 1.9.14; 1.9.16; 1.9.18;
Properly setup the IO APIC for ISA and pciide compat interrupts too.
Makes ACPI kernels works again with pciide controllers in compat mode.
 1.8 22-Feb-2007  bouyer branches: 1.8.4; 1.8.20; 1.8.22; 1.8.26; 1.8.28;
Catch up with x86: add _bus_dmatag_subregion() and _bus_dmatag_destroy()
to x86_bus_dma_tag
 1.7 22-Feb-2007  dogcow add _tag_needs_free members to x86_bus_dma_tag structs
 1.6 28-Sep-2006  bouyer branches: 1.6.2; 1.6.4; 1.6.6;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.5 11-Dec-2005  christos branches: 1.5.20; 1.5.22;
merge ktrace-lwp.
 1.4 16-Apr-2005  bouyer branches: 1.4.2; 1.4.4;
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.3 16-Apr-2005  yamt tweak x86 bus_dma code so that it can be used by xen port.

- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4; 1.2.6;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 08-Mar-2005  bouyer branches: 1.1.2;
file isa_machdep.c was initially added on branch bouyer-xen2.
 1.1.2.1 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.2.6.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.6.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.6.1 09-Mar-2005  skrll file isa_machdep.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.2.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.4.1 09-Mar-2005  yamt file isa_machdep.c was added on branch yamt-km on 2005-03-19 08:33:26 +0000
 1.2.2.2 28-Apr-2005  tron Pull up revision 1.4 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2.2.1 21-Apr-2005  tron Pull up revision 1.3 (requested by yamt in ticket #175):
tweak x86 bus_dma code so that it can be used by xen port.
- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.4.4.3 07-Dec-2007  yamt sync with head
 1.4.4.2 26-Feb-2007  yamt sync with head.
 1.4.4.1 30-Dec-2006  yamt sync with head.
 1.4.2.2 29-Apr-2005  kent sync with -current
 1.4.2.1 16-Apr-2005  kent file isa_machdep.c was added on branch kent-audio2 on 2005-04-29 11:28:30 +0000
 1.5.22.1 22-Oct-2006  yamt sync with head
 1.5.20.1 18-Nov-2006  ad Sync with head.
 1.6.6.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.6.4.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.6.2.1 01-Feb-2008  riz Pull up following revision(s) (requested by bouyer in ticket #1001):
sys/arch/xen/xen/isa_machdep.c: revision 1.9
sys/arch/xen/xen/pciide_machdep.c: revision 1.7
Properly setup the IO APIC for ISA and pciide compat interrupts too.
Makes ACPI kernels works again with pciide controllers in compat mode.
 1.8.28.1 19-Nov-2007  mjf Sync with HEAD.
 1.8.26.1 18-Nov-2007  bouyer Sync with HEAD
 1.8.22.1 09-Jan-2008  matt sync with HEAD
 1.8.20.1 21-Nov-2007  joerg Sync with HEAD.
 1.8.4.1 03-Dec-2007  ad Sync with HEAD.
 1.9.18.4 11-Mar-2010  yamt sync with head
 1.9.18.3 19-Aug-2009  yamt sync with head.
 1.9.18.2 04-May-2009  yamt sync with head.
 1.9.18.1 16-May-2008  yamt sync with head.
 1.9.16.1 18-May-2008  yamt sync with head.
 1.9.14.3 17-Jan-2009  mjf Sync with HEAD.
 1.9.14.2 28-Sep-2008  mjf Sync with HEAD.
 1.9.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.10.4.1 03-Jul-2008  simonb Sync with head.
 1.10.2.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.12.12.1 21-Apr-2010  matt sync to netbsd-5
 1.12.6.2 22-Feb-2010  snj Pull up following revision(s) (requested by bouyer in ticket #1299):
sys/arch/xen/xen/isa_machdep.c: revision 1.23
sys/arch/xen/xen/pciide_machdep.c: revision 1.17
Enlarge temporary buffer, so that event counter's name is not truncated
for ioapic interrupts.
 1.12.6.1 04-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/x86/intr.c: revision 1.22
sys/arch/xen/xen/isa_machdep.c: revision 1.14
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.9
sys/arch/xen/xen/pciide_machdep.c: revision 1.12
When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.12.4.2 28-Apr-2009  skrll Sync with HEAD.
 1.12.4.1 19-Jan-2009  skrll Sync with HEAD.
 1.13.2.4 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.13.2.3 24-Oct-2010  jym Sync with HEAD
 1.13.2.2 01-Nov-2009  jym Sync with HEAD.
 1.13.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.26.30.1 28-Aug-2017  skrll Sync with HEAD
 1.26.12.1 03-Dec-2017  jdolecek update from HEAD
 1.28.2.2 16-Jul-2017  cherry 2302677
 1.28.2.1 16-Jul-2017  cherry file isa_machdep.c was added on branch perseant-stdc-iso10646 on 2017-07-16 14:02:49 +0000
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 07-May-2004  cl branches: 1.1.2; 1.1.4; 1.1.8; 1.1.10; 1.1.12;
Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.1.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.10.1 29-Apr-2005  kent sync with -current
 1.1.8.1 12-Feb-2005  bouyer Remove /kern/xen/machmem, which was providing a view of the real physical
(machine, in xen's sense) memory. This is not needed any more.
 1.1.4.6 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.5 27-Oct-2004  skrll Fix various comments that describe the argument structures
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 07-May-2004  skrll file machmem.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 07-May-2004  he file machmem.c was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.22 04-Nov-2017  cherry Remove bitrotted xen specific versions of pci, pciide machdep related code.

Use the common x86/ code instead.
 1.21 04-Nov-2017  cherry Retire xen/x86/intr.c and use the new xen specific glue in x86/x86/intr.c

The purpose of this change is to expose the x86/include/intr.h API
to drivers. Specifically the following functions:

void *intr_establish_xname(...);
void *intr_establish(...);
void intr_disestablish(...);

while maintaining the old API from xen/include/evtchn.h, specifically
the following functions:

int event_set_handler(...);
int event_remove_handler(...);

This is so that if things break, we can keep using the old API until
everything stabilises. This is a stepping stone towards getting the
actual XEN event callback path rework code in place - which can be
done opaquely behind the intr.h API - NetBSD/XEN specific drivers that
have been ported to the intr.h API should then work without
significant further modifications.
 1.20 28-Jul-2017  cherry APIC related macros are mixed with IOAPIC related ones.
This needs separation.

Until then, band-aid it.

This fixes pci-passthrough build failures on XEN3_DOMU pointed out by
msaitoh@
 1.19 16-Jul-2017  cherry branches: 1.19.2;
Unify the xen and native x86/ interrupt setup functions and
spl traversal data structures.

This is towards PVHVM.
 1.18 16-Jul-2017  cherry Remove the xen specific interrupt type for the x86 intr_handle_t
For this to work, we use the evtchn.c:get_pirq_to_evtchn() glue
function to make things easier.
 1.17 14-Mar-2015  bouyer Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.16 29-Mar-2014  christos branches: 1.16.4; 1.16.6;
make pci_intr_string and eisa_intr_string take a buffer and a length
instead of relying in local static storage.
 1.15 01-Jul-2011  dyoung branches: 1.15.2; 1.15.8; 1.15.12; 1.15.16;
#include <sys/bus.h> instead of <machine/bus.h>.
 1.14 04-Apr-2011  dyoung Neither pci_dma64_available(), pci_probe_device(), pci_mapreg_map(9),
pci_find_rom(), pci_intr_map(9), pci_enumerate_bus(), nor the match
predicate passed to pciide_compat_intr_establish() should ever modify
their pci_attach_args argument, so make their pci_attach_args arguments
const and deal with the fallout throughout the kernel.

For the most part, these changes add a 'const' where there was no
'const' before, however, some drivers and MD code used to modify
pci_attach_args. Now those drivers either copy their pci_attach_args
and modify the copy, or refrain from modifying pci_attach_args:

Xen: according to Manuel Bouyer, writing to pci_attach_args in
pci_intr_map() was a leftover from Xen 2. Probably a bug. I
stopped writing it. I have not tested this change.

siside(4): sis_hostbr_match() needlessly wrote to pci_attach_args.
Probably a bug. I use a temporary variable. I have not tested this
change.

slide(4): sl82c105_chip_map() overwrote the caller's pci_attach_args.
Probably a bug. Use a local pci_attach_args. I have not tested
this change.

viaide(4): via_sata_chip_map() and via_sata_chip_map_new() overwrote the
caller's pci_attach_args. Probably a bug. Make a local copy of the
caller's pci_attach_args and modify the copy. I have not tested
this change.

While I'm here, make pci_mapreg_submap() static.

With these changes in place, I have tested the compilation of these
kernels:

alpha GENERIC
amd64 GENERIC XEN3_DOM0
arc GENERIC
atari HADES MILAN-PCIIDE
bebox GENERIC
cats GENERIC
cobalt GENERIC
evbarm-eb NSLU2
evbarm-el ADI_BRH ARMADILLO9 CP3100 GEMINI GEMINI_MASTER GEMINI_SLAVE GUMSTIX
HDL_G IMX31LITE INTEGRATOR IQ31244 IQ80310 IQ80321 IXDP425 IXM1200
KUROBOX_PRO LUBBOCK MARVELL_NAS NAPPI SHEEVAPLUG SMDK2800 TEAMASA_NPWR
TEAMASA_NPWR_FC TS7200 TWINTAIL ZAO425
evbmips-el AP30 DBAU1500 DBAU1550 MALTA MERAKI MTX-1 OMSAL400 RB153 WGT624V3
evbmips64-el XLSATX
evbppc EV64260 MPC8536DS MPC8548CDS OPENBLOCKS200 OPENBLOCKS266
OPENBLOCKS266_OPT P2020RDB PMPPC RB800 WALNUT
hp700 GENERIC
i386 ALL XEN3_DOM0 XEN3_DOMU
ibmnws GENERIC
macppc GENERIC
mvmeppc GENERIC
netwinder GENERIC
ofppc GENERIC
prep GENERIC
sandpoint GENERIC
sgimips GENERIC32_IP2x
sparc GENERIC_SUN4U KRUPS
sparc64 GENERIC

As of Sun Apr 3 15:26:26 CDT 2011, I could not compile these kernels
with or without my patches in place:

### evbmips-el GDIUM

nbmake: nbmake: don't know how to make /home/dyoung/pristine-nbsd/src/sys/arch/mips/mips/softintr.c. Stop

### evbarm-el MPCSA_GENERIC
src/sys/arch/evbarm/conf/MPCSA_GENERIC:318: ds1672rtc*: unknown device `ds1672rtc'

### ia64 GENERIC

/tmp/genassym.28085/assym.c: In function 'f111':
/tmp/genassym.28085/assym.c:67: error: invalid application of 'sizeof' to incomplete type 'struct pcb'
/tmp/genassym.28085/assym.c:76: error: dereferencing pointer to incomplete type

### sgimips GENERIC32_IP3x

crmfb.o: In function `crmfb_attach':
crmfb.c:(.text+0x2304): undefined reference to `ddc_read_edid'
crmfb.c:(.text+0x2304): relocation truncated to fit: R_MIPS_26 against `ddc_read_edid'
crmfb.c:(.text+0x234c): undefined reference to `edid_parse'
crmfb.c:(.text+0x234c): relocation truncated to fit: R_MIPS_26 against `edid_parse'
crmfb.c:(.text+0x2354): undefined reference to `edid_print'
crmfb.c:(.text+0x2354): relocation truncated to fit: R_MIPS_26 against `edid_print'
 1.13 19-Oct-2009  bouyer branches: 1.13.4; 1.13.6;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.12 18-Aug-2009  jmcneill Switch to ACPICA 20090730, and update for API changes.
 1.11 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.10 13-Mar-2009  bouyer Keep the BIOS-configured interrupt number if intr_find_mpmapping() doesn't
return a APIC_INT_VIA_APIC pirq.
Problem debugged and patch tested by jym@
 1.9 10-Mar-2009  bouyer When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.8 13-Feb-2009  bouyer Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.7 03-Jul-2008  drochner branches: 1.7.4; 1.7.6; 1.7.10; 1.7.14;
split device/softc for ioapic
 1.6 03-Jul-2008  drochner Remove "struct device" from "struct pic", where it was only real
for ioapics and faked up for others. Add it to "struct ioapic_softc"
for now, until device/softc get split.
This required all typecasts between "struct pic" and "struct ioapic_softc"
to be replaced, I hope I got them all.
functionally tested on i386, compile-tested on xen, untested on amd64
 1.5 30-May-2008  ad branches: 1.5.2;
pci_intr_setattr(), allows PCI interrupts to be marked MPSAFE on x86, and
other platforms if the code is added.

pci_intr_map(...)
pci_intr_setattr(pc, ih, PCI_INTR_MPSAFE, 1);
pci_intr_establish(...)
 1.4 17-Feb-2008  bouyer branches: 1.4.6; 1.4.8; 1.4.10; 1.4.12;
Add missing __KERNEL_RCSID()
 1.3 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.2 28-Sep-2006  bouyer branches: 1.2.2; 1.2.8; 1.2.18; 1.2.26; 1.2.28; 1.2.32; 1.2.34;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.1 09-Apr-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.6; 1.1.10; 1.1.14; 1.1.18; 1.1.20; 1.1.22;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.1.22.1 22-Oct-2006  yamt sync with head
 1.1.20.2 09-Sep-2006  rpaulo sync with head
 1.1.20.1 09-Apr-2006  rpaulo file pci_intr_machdep.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.1.18.1 18-Nov-2006  ad Sync with head.
 1.1.14.5 27-Feb-2008  yamt sync with head.
 1.1.14.4 07-Dec-2007  yamt sync with head
 1.1.14.3 30-Dec-2006  yamt sync with head.
 1.1.14.2 21-Jun-2006  yamt sync with head.
 1.1.14.1 09-Apr-2006  yamt file pci_intr_machdep.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.1.10.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.1.10.1 09-Apr-2006  tron file pci_intr_machdep.c was added on branch peter-altq on 2006-05-24 15:48:26 +0000
 1.1.6.2 22-Apr-2006  simonb Sync with head.
 1.1.6.1 09-Apr-2006  simonb file pci_intr_machdep.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.1.4.2 19-Apr-2006  elad sync with head - hopefully this will work
 1.1.4.1 09-Apr-2006  elad file pci_intr_machdep.c was added on branch elad-kernelauth on 2006-04-19 02:34:03 +0000
 1.1.2.2 11-Apr-2006  yamt sync with head
 1.1.2.1 09-Apr-2006  yamt file pci_intr_machdep.c was added on branch yamt-pdpolicy on 2006-04-11 11:53:48 +0000
 1.2.34.2 18-Feb-2008  mjf Sync with HEAD.
 1.2.34.1 08-Dec-2007  mjf Sync with HEAD.
 1.2.32.1 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.2.28.2 23-Mar-2008  matt sync with HEAD
 1.2.28.1 09-Jan-2008  matt sync with HEAD
 1.2.26.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.18.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.2.8.1 03-Dec-2007  ad Sync with HEAD.
 1.2.2.1 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.4.12.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.4.12.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.4.10.3 11-Mar-2010  yamt sync with head
 1.4.10.2 19-Aug-2009  yamt sync with head.
 1.4.10.1 04-May-2009  yamt sync with head.
 1.4.8.1 04-Jun-2008  yamt sync with head
 1.4.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.4.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.5.2.1 03-Jul-2008  simonb Sync with head.
 1.7.14.1 21-Apr-2010  matt sync to netbsd-5
 1.7.10.4 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.7.10.3 02-May-2011  jym Sync with head.
 1.7.10.2 01-Nov-2009  jym Sync with HEAD.
 1.7.10.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.7.6.3 04-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.10
Keep the BIOS-configured interrupt number if intr_find_mpmapping() doesn't
return a APIC_INT_VIA_APIC pirq.
Problem debugged and patch tested by jym@
 1.7.6.2 04-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/x86/intr.c: revision 1.22
sys/arch/xen/xen/isa_machdep.c: revision 1.14
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.9
sys/arch/xen/xen/pciide_machdep.c: revision 1.12
When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.7.6.1 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.7.4.2 28-Apr-2009  skrll Sync with HEAD.
 1.7.4.1 03-Mar-2009  skrll Sync with HEAD.
 1.13.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.13.4.1 21-Apr-2011  rmind sync with head
 1.15.16.1 18-May-2014  rmind sync with head
 1.15.12.2 03-Dec-2017  jdolecek update from HEAD
 1.15.12.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.15.8.1 14-Apr-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1278):
sys/arch/xen/include/evtchn.h: revision 1.23
sys/arch/xen/xen/evtchn.c: revision 1.71
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.17
Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.15.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.16.6.2 28-Aug-2017  skrll Sync with HEAD
 1.16.6.1 06-Apr-2015  skrll Sync with HEAD
 1.16.4.1 18-Mar-2015  snj Pull up following revision(s) (requested by bouyer in ticket #618):
sys/arch/xen/include/evtchn.h: revision 1.23
sys/arch/xen/xen/evtchn.c: revision 1.71
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.17
Properly implemement pci_intr_disestablish(9), so that interrupt
handlers stop being called when the device has been detached.
Should fix PR port-xen/47720 (which turns out to not be related to raidframe).
While there fix possible races in event_remove_handler() and pirq_establish().
 1.19.2.2 16-Jul-2017  cherry 2302677
 1.19.2.1 16-Jul-2017  cherry file pci_intr_machdep.c was added on branch perseant-stdc-iso10646 on 2017-07-16 14:02:49 +0000
 1.15 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.14 01-May-2009  cegger struct device * -> device_t
 1.13 14-Apr-2008  cegger branches: 1.13.4; 1.13.18;
- use POSIX integer types
- ansify functions
 1.12 17-Feb-2008  bouyer branches: 1.12.6;
Add missing __KERNEL_RCSID()
 1.11 22-Feb-2007  bouyer branches: 1.11.22; 1.11.28;
Catch up with x86: add _bus_dmatag_subregion() and _bus_dmatag_destroy()
to x86_bus_dma_tag
 1.10 22-Feb-2007  dogcow add _tag_needs_free members to x86_bus_dma_tag structs
 1.9 15-May-2006  dogcow branches: 1.9.12; 1.9.14; 1.9.16;
gcc4: initalize uninitialized data (one valid, one not quite as valid)
 1.8 09-Apr-2006  bouyer Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.7 15-Jan-2006  bouyer branches: 1.7.2; 1.7.4; 1.7.6; 1.7.8; 1.7.10;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.6 11-Dec-2005  christos branches: 1.6.2;
merge ktrace-lwp.
 1.5 19-Aug-2005  bouyer Provide a PCI_MACHDEP_ENUMERATE_BUS to the MI pci system. We can't use
the generic pci_enumerate_bus() in Xen because the hypervisor may
hide the function 0, but give access to other functions of the same device
and pci_enumerate_bus() will stop if function 0 isn't available.
 1.4 16-Apr-2005  bouyer branches: 1.4.2; 1.4.4;
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.3 16-Apr-2005  yamt tweak x86 bus_dma code so that it can be used by xen port.

- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 18-Jan-2005  bouyer branches: 1.1.2; 1.1.4;
file pci_machdep.c was initially added on branch bouyer-xen2.
 1.1.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.2.2 21-Jan-2005  bouyer Remove debug printf's
 1.1.2.1 18-Jan-2005  bouyer snapshot of work in progress on physical devices support:
- support pci at hypervisor. Attach one PCI bus for each bus returned by
PHYSDEVOP_PCI_PROBE_ROOT_BUSES (it looks like xen hides ppb bridges from
the guest OS).
- implement pci_conf_read()/pci_conf_write() using the appropriate
PHYSDEVOP_PCI_* calls.
- call PHYSDEVOP_PCI_INITIALISE_DEVICE from pci_intr_map() to make xen
do interrupts routing if needed, and map the hardware interrupt to
a xen interrupt.
- add pci and associated devices to files.xen

This is enouth to have IDE controllers work in PIO mode.
TODO: bus_dma support (currently bus_dma won't translate pseudo-physical
addresses to machine addresses), ISA support, test memory-mapped
I/O registers.
 1.2.4.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.4.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.1 09-Mar-2005  skrll file pci_machdep.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.2.2.3 25-Aug-2005  tron Pull up following revision(s) (requested by bouyer in ticket #694):
sys/arch/xen/xen/pci_machdep.c: revision 1.5
sys/arch/xen/include/pci_machdep.h: revision 1.4
Provide a PCI_MACHDEP_ENUMERATE_BUS to the MI pci system. We can't use
the generic pci_enumerate_bus() in Xen because the hypervisor may
hide the function 0, but give access to other functions of the same device
and pci_enumerate_bus() will stop if function 0 isn't available.
 1.2.2.2 28-Apr-2005  tron Pull up revision 1.4 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2.2.1 21-Apr-2005  tron Pull up revision 1.3 (requested by yamt in ticket #175):
tweak x86 bus_dma code so that it can be used by xen port.
- distinguish paddr_t and bus_addr_t.
for xen, use bus_addr_t in the sense of machine address.
- move _X86_BUS_DMA_PRIVATE part of bus.h into bus_private.h.
- remove special handling of xen_shm. we can always grab
machine address from pte.
 1.4.4.3 27-Feb-2008  yamt sync with head.
 1.4.4.2 26-Feb-2007  yamt sync with head.
 1.4.4.1 21-Jun-2006  yamt sync with head.
 1.4.2.2 29-Apr-2005  kent sync with -current
 1.4.2.1 16-Apr-2005  kent file pci_machdep.c was added on branch kent-audio2 on 2005-04-29 11:28:30 +0000
 1.6.2.1 01-Feb-2006  yamt sync with head.
 1.7.10.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.7.8.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.7.6.2 24-May-2006  yamt sync with head.
 1.7.6.1 11-Apr-2006  yamt sync with head
 1.7.4.2 01-Jun-2006  kardel Sync with head.
 1.7.4.1 22-Apr-2006  simonb Sync with head.
 1.7.2.1 09-Sep-2006  rpaulo sync with head
 1.9.16.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.9.14.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.9.12.1 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.11.28.1 18-Feb-2008  mjf Sync with HEAD.
 1.11.22.1 23-Mar-2008  matt sync with HEAD
 1.12.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.13.18.2 01-Nov-2009  jym Sync with HEAD.
 1.13.18.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.13.4.2 19-Aug-2009  yamt sync with head.
 1.13.4.1 04-May-2009  yamt sync with head.
 1.7 03-Aug-2008  joerg Move some MD declarations from x86/pci/files.pci to x86/conf/files.x86,
so that Xen can use the former.

Drop Xen's pcib.c in favor of the x86 code and thereby unbreak ichlpcib.
 1.6 28-Apr-2008  martin branches: 1.6.2; 1.6.6;
Remove clause 3 and 4 from TNF licenses
 1.5 16-Apr-2008  cegger branches: 1.5.2; 1.5.4;
device_t / softc split
reviewed, tested and approved by bouyer
 1.4 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.3 11-Dec-2005  christos branches: 1.3.74;
merge ktrace-lwp.
 1.2 09-Mar-2005  bouyer branches: 1.2.4; 1.2.6; 1.2.8;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 08-Mar-2005  bouyer branches: 1.1.2;
file pcib.c was initially added on branch bouyer-xen2.
 1.1.2.1 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.2.8.2 29-Apr-2005  kent sync with -current
 1.2.8.1 09-Mar-2005  kent file pcib.c was added on branch kent-audio2 on 2005-04-29 11:28:30 +0000
 1.2.6.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.6.1 09-Mar-2005  skrll file pcib.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.2.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.4.1 09-Mar-2005  yamt file pcib.c was added on branch yamt-km on 2005-03-19 08:33:26 +0000
 1.3.74.2 28-Sep-2008  mjf Sync with HEAD.
 1.3.74.1 02-Jun-2008  mjf Sync with HEAD.
 1.5.4.2 04-May-2009  yamt sync with head.
 1.5.4.1 16-May-2008  yamt sync with head.
 1.5.2.1 18-May-2008  yamt sync with head.
 1.6.6.1 19-Oct-2008  haad Sync with HEAD.
 1.6.2.1 07-Sep-2008  wrstuden Delete a bunch of things that are now gone in head.
 1.23 01-Sep-2022  bouyer in backend drivers, use xen_shm_(un)map for the rings instead of inline,
mostly duplicate code.
 1.22 01-Sep-2022  bouyer Add PVH support for backend drivers grant operation.
Now a domU in a PVH dom0 boots multiuser.
 1.21 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.20 07-Apr-2020  jdolecek switch KERNFS_ALLOCENTRY() to use kmem_zalloc() instead of malloc()
 1.19 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.18 24-Dec-2018  cherry 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.17 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.16 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.15 08-Oct-2018  cherry Convert the last remaining users of event_set_handler() to
intr_establish_xname(,&xen_pic,...)
 1.14 24-Jun-2018  jdolecek branches: 1.14.2;
similar treatment as xennetback_xenbus.c:
- protect instance list with mutex
- mark more local variables static
- mark with XXXSMP what looks suspicious
- in pciback.c use kmem_zalloc() et.al to allocate the device structures
 1.13 24-Jun-2018  jdolecek 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.12 16-Jul-2017  cherry branches: 1.12.2; 1.12.4;
Remove the xen specific interrupt type for the x86 intr_handle_t
For this to work, we use the evtchn.c:get_pirq_to_evtchn() glue
function to make things easier.
 1.11 07-Jul-2016  msaitoh KNF. Remove extra spaces. No functional change.
 1.10 06-Jan-2016  bouyer Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.9 29-Mar-2014  christos branches: 1.9.4; 1.9.6; 1.9.8;
make pci_intr_string and eisa_intr_string take a buffer and a length
instead of relying in local static storage.
 1.8 27-Mar-2014  christos correct/add protection against snprintf overflow.
 1.7 02-Feb-2012  tls branches: 1.7.2; 1.7.6; 1.7.8; 1.7.10; 1.7.16;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.6 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.5 19-Oct-2009  bouyer branches: 1.5.6; 1.5.14; 1.5.18;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.4 06-Mar-2009  bouyer branches: 1.4.2; 1.4.4; 1.4.6;
Move a message to debug
 1.3 06-Mar-2009  bouyer Unbind the event channel after mapping the interrupt. Otherwise domain0
will share the interrupt with the domU and xen will wait for domain0 to
ack the interrupt too. Now devices that don't share an interrupt with
a device in domain0 works too.
Make sure the same PCI bus isn't published multiple times.
 1.2 05-Mar-2009  bouyer - xentools also use pci vendor/product id and subsystem id; export them
via the kernfs file
- EVTCHNOP_bind_pirq wants the legacy IRQ number; so always set the
legacy IRQ number in the PCI_INTERRUPT_REG.
 1.1 13-Feb-2009  bouyer branches: 1.1.2;
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.1.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.1.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.1.2.1 13-Feb-2009  skrll file pciback.c was added on branch nick-hppapmap on 2009-03-03 18:29:49 +0000
 1.4.6.5 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/xen/pciback.c: revision 1.4
Move a message to debug
 1.4.6.4 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/xen/pciback.c: revision 1.3
Unbind the event channel after mapping the interrupt. Otherwise domain0
will share the interrupt with the domU and xen will wait for domain0 to
ack the interrupt too. Now devices that don't share an interrupt with
a device in domain0 works too.
Make sure the same PCI bus isn't published multiple times.
 1.4.6.3 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/xen/pciback.c: revision 1.2
- xentools also use pci vendor/product id and subsystem id; export them
via the kernfs file
- EVTCHNOP_bind_pirq wants the legacy IRQ number; so always set the
legacy IRQ number in the PCI_INTERRUPT_REG.
 1.4.6.2 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.4.6.1 06-Mar-2009  snj file pciback.c was added on branch netbsd-5 on 2009-10-03 23:54:05 +0000
 1.4.4.3 01-Nov-2009  jym Sync with HEAD.
 1.4.4.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.4.4.1 06-Mar-2009  jym file pciback.c was added on branch jym-xensuspend on 2009-05-13 17:18:50 +0000
 1.4.2.3 11-Mar-2010  yamt sync with head
 1.4.2.2 04-May-2009  yamt sync with head.
 1.4.2.1 06-Mar-2009  yamt file pciback.c was added on branch yamt-nfs-mp on 2009-05-04 08:12:14 +0000
 1.5.18.1 18-Feb-2012  mrg merge to -current.
 1.5.14.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.5.14.1 17-Apr-2012  yamt sync with head
 1.5.6.2 21-Apr-2010  matt sync to netbsd-5
 1.5.6.1 19-Oct-2009  matt file pciback.c was added on branch matt-nb5-mips64 on 2010-04-21 00:33:45 +0000
 1.7.16.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.7.10.1 18-May-2014  rmind sync with head
 1.7.8.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.7.6.2 03-Dec-2017  jdolecek update from HEAD
 1.7.6.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.7.2.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.9.8.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1071):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.9.6.3 28-Aug-2017  skrll Sync with HEAD
 1.9.6.2 09-Jul-2016  skrll Sync with HEAD
 1.9.6.1 19-Mar-2016  skrll Sync with HEAD
 1.9.4.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1071):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.12.4.4 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.12.4.3 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.12.4.2 20-Oct-2018  pgoyette Sync with head
 1.12.4.1 25-Jun-2018  pgoyette Sync with HEAD
 1.12.2.2 16-Jul-2017  cherry 2739767
 1.12.2.1 16-Jul-2017  cherry file pciback.c was added on branch perseant-stdc-iso10646 on 2017-07-16 06:14:25 +0000
 1.14.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.14.2.1 10-Jun-2019  christos Sync with HEAD
 1.21 04-Nov-2017  cherry Remove bitrotted xen specific versions of pci, pciide machdep related code.

Use the common x86/ code instead.
 1.20 04-Nov-2017  cherry Retire xen/x86/intr.c and use the new xen specific glue in x86/x86/intr.c

The purpose of this change is to expose the x86/include/intr.h API
to drivers. Specifically the following functions:

void *intr_establish_xname(...);
void *intr_establish(...);
void intr_disestablish(...);

while maintaining the old API from xen/include/evtchn.h, specifically
the following functions:

int event_set_handler(...);
int event_remove_handler(...);

This is so that if things break, we can keep using the old API until
everything stabilises. This is a stepping stone towards getting the
actual XEN event callback path rework code in place - which can be
done opaquely behind the intr.h API - NetBSD/XEN specific drivers that
have been ported to the intr.h API should then work without
significant further modifications.
 1.19 16-Jul-2017  cherry branches: 1.19.2;
Remove the xen specific interrupt type for the x86 intr_handle_t
For this to work, we use the evtchn.c:get_pirq_to_evtchn() glue
function to make things easier.
 1.18 04-Apr-2011  dyoung branches: 1.18.14; 1.18.32;
Neither pci_dma64_available(), pci_probe_device(), pci_mapreg_map(9),
pci_find_rom(), pci_intr_map(9), pci_enumerate_bus(), nor the match
predicate passed to pciide_compat_intr_establish() should ever modify
their pci_attach_args argument, so make their pci_attach_args arguments
const and deal with the fallout throughout the kernel.

For the most part, these changes add a 'const' where there was no
'const' before, however, some drivers and MD code used to modify
pci_attach_args. Now those drivers either copy their pci_attach_args
and modify the copy, or refrain from modifying pci_attach_args:

Xen: according to Manuel Bouyer, writing to pci_attach_args in
pci_intr_map() was a leftover from Xen 2. Probably a bug. I
stopped writing it. I have not tested this change.

siside(4): sis_hostbr_match() needlessly wrote to pci_attach_args.
Probably a bug. I use a temporary variable. I have not tested this
change.

slide(4): sl82c105_chip_map() overwrote the caller's pci_attach_args.
Probably a bug. Use a local pci_attach_args. I have not tested
this change.

viaide(4): via_sata_chip_map() and via_sata_chip_map_new() overwrote the
caller's pci_attach_args. Probably a bug. Make a local copy of the
caller's pci_attach_args and modify the copy. I have not tested
this change.

While I'm here, make pci_mapreg_submap() static.

With these changes in place, I have tested the compilation of these
kernels:

alpha GENERIC
amd64 GENERIC XEN3_DOM0
arc GENERIC
atari HADES MILAN-PCIIDE
bebox GENERIC
cats GENERIC
cobalt GENERIC
evbarm-eb NSLU2
evbarm-el ADI_BRH ARMADILLO9 CP3100 GEMINI GEMINI_MASTER GEMINI_SLAVE GUMSTIX
HDL_G IMX31LITE INTEGRATOR IQ31244 IQ80310 IQ80321 IXDP425 IXM1200
KUROBOX_PRO LUBBOCK MARVELL_NAS NAPPI SHEEVAPLUG SMDK2800 TEAMASA_NPWR
TEAMASA_NPWR_FC TS7200 TWINTAIL ZAO425
evbmips-el AP30 DBAU1500 DBAU1550 MALTA MERAKI MTX-1 OMSAL400 RB153 WGT624V3
evbmips64-el XLSATX
evbppc EV64260 MPC8536DS MPC8548CDS OPENBLOCKS200 OPENBLOCKS266
OPENBLOCKS266_OPT P2020RDB PMPPC RB800 WALNUT
hp700 GENERIC
i386 ALL XEN3_DOM0 XEN3_DOMU
ibmnws GENERIC
macppc GENERIC
mvmeppc GENERIC
netwinder GENERIC
ofppc GENERIC
prep GENERIC
sandpoint GENERIC
sgimips GENERIC32_IP2x
sparc GENERIC_SUN4U KRUPS
sparc64 GENERIC

As of Sun Apr 3 15:26:26 CDT 2011, I could not compile these kernels
with or without my patches in place:

### evbmips-el GDIUM

nbmake: nbmake: don't know how to make /home/dyoung/pristine-nbsd/src/sys/arch/mips/mips/softintr.c. Stop

### evbarm-el MPCSA_GENERIC
src/sys/arch/evbarm/conf/MPCSA_GENERIC:318: ds1672rtc*: unknown device `ds1672rtc'

### ia64 GENERIC

/tmp/genassym.28085/assym.c: In function 'f111':
/tmp/genassym.28085/assym.c:67: error: invalid application of 'sizeof' to incomplete type 'struct pcb'
/tmp/genassym.28085/assym.c:76: error: dereferencing pointer to incomplete type

### sgimips GENERIC32_IP3x

crmfb.o: In function `crmfb_attach':
crmfb.c:(.text+0x2304): undefined reference to `ddc_read_edid'
crmfb.c:(.text+0x2304): relocation truncated to fit: R_MIPS_26 against `ddc_read_edid'
crmfb.c:(.text+0x234c): undefined reference to `edid_parse'
crmfb.c:(.text+0x234c): relocation truncated to fit: R_MIPS_26 against `edid_parse'
crmfb.c:(.text+0x2354): undefined reference to `edid_print'
crmfb.c:(.text+0x2354): relocation truncated to fit: R_MIPS_26 against `edid_print'
 1.17 06-Feb-2010  bouyer branches: 1.17.4; 1.17.6;
Enlarge temporary buffer, so that event counter's name is not truncated
for ioapic interrupts.
 1.16 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.15 01-May-2009  cegger struct device * -> device_t
 1.14 16-Mar-2009  cegger ansify function definitions
 1.13 14-Mar-2009  dsl Remove all the __P() from sys (excluding sys/dist)
Diff checked with grep and MK1 eyeball.
i386 and amd64 GENERIC and sys still build.
 1.12 10-Mar-2009  bouyer When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.11 03-Jul-2008  drochner branches: 1.11.4; 1.11.6; 1.11.10; 1.11.14;
split device/softc for ioapic
 1.10 03-Jul-2008  drochner Remove "struct device" from "struct pic", where it was only real
for ioapics and faked up for others. Add it to "struct ioapic_softc"
for now, until device/softc get split.
This required all typecasts between "struct pic" and "struct ioapic_softc"
to be replaced, I hope I got them all.
functionally tested on i386, compile-tested on xen, untested on amd64
 1.9 06-Apr-2008  cegger branches: 1.9.4; 1.9.6; 1.9.8;
use aprint_*_dev and device_xname
 1.8 22-Nov-2007  bouyer branches: 1.8.14;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.7 18-Nov-2007  bouyer Properly setup the IO APIC for ISA and pciide compat interrupts too.
Makes ACPI kernels works again with pciide controllers in compat mode.
 1.6 28-Sep-2006  bouyer branches: 1.6.2; 1.6.8; 1.6.18; 1.6.26; 1.6.28; 1.6.32; 1.6.34;
Add Xen3 support for ACPI and/or MPBIOS + IOAPIC. To help with this, physical
CPUs are now configured on mainbus only in dom0, and only to know about
their APIC id. virtual CPUs are attached to hypervisor as:
vcpu* at hypervisor?
and this is what's used as curcpu(). The kernel config files needs to be
updated for this, see XEN3_DOM0 or XEN3_DOMU for examples.
XEN3_DOM0 now has acpi, MPBIOS and ioapic by default.
Note that a Xen dom0 kernel doens't have access to the lapic.
 1.5 15-Jan-2006  bouyer branches: 1.5.18; 1.5.20;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.4 11-Dec-2005  christos branches: 1.4.2;
merge ktrace-lwp.
 1.3 16-Apr-2005  bouyer branches: 1.3.2; 1.3.4;
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 18-Jan-2005  bouyer branches: 1.1.2; 1.1.4;
file pciide_machdep.c was initially added on branch bouyer-xen2.
 1.1.4.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.2.1 18-Jan-2005  bouyer snapshot of work in progress on physical devices support:
- support pci at hypervisor. Attach one PCI bus for each bus returned by
PHYSDEVOP_PCI_PROBE_ROOT_BUSES (it looks like xen hides ppb bridges from
the guest OS).
- implement pci_conf_read()/pci_conf_write() using the appropriate
PHYSDEVOP_PCI_* calls.
- call PHYSDEVOP_PCI_INITIALISE_DEVICE from pci_intr_map() to make xen
do interrupts routing if needed, and map the hardware interrupt to
a xen interrupt.
- add pci and associated devices to files.xen

This is enouth to have IDE controllers work in PIO mode.
TODO: bus_dma support (currently bus_dma won't translate pseudo-physical
addresses to machine addresses), ISA support, test memory-mapped
I/O registers.
 1.2.4.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.4.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.4.1 09-Mar-2005  skrll file pciide_machdep.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.2.2.1 28-Apr-2005  tron Pull up revision 1.3 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.3.4.3 07-Dec-2007  yamt sync with head
 1.3.4.2 30-Dec-2006  yamt sync with head.
 1.3.4.1 21-Jun-2006  yamt sync with head.
 1.3.2.2 29-Apr-2005  kent sync with -current
 1.3.2.1 16-Apr-2005  kent file pciide_machdep.c was added on branch kent-audio2 on 2005-04-29 11:28:30 +0000
 1.4.2.1 01-Feb-2006  yamt sync with head.
 1.5.20.1 22-Oct-2006  yamt sync with head
 1.5.18.1 18-Nov-2006  ad Sync with head.
 1.6.34.2 08-Dec-2007  mjf Sync with HEAD.
 1.6.34.1 19-Nov-2007  mjf Sync with HEAD.
 1.6.32.2 18-Nov-2007  bouyer Sync with HEAD
 1.6.32.1 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.6.28.1 09-Jan-2008  matt sync with HEAD
 1.6.26.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.6.26.1 21-Nov-2007  joerg Sync with HEAD.
 1.6.18.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.6.8.1 03-Dec-2007  ad Sync with HEAD.
 1.6.2.1 01-Feb-2008  riz Pull up following revision(s) (requested by bouyer in ticket #1001):
sys/arch/xen/xen/isa_machdep.c: revision 1.9
sys/arch/xen/xen/pciide_machdep.c: revision 1.7
Properly setup the IO APIC for ISA and pciide compat interrupts too.
Makes ACPI kernels works again with pciide controllers in compat mode.
 1.8.14.2 28-Sep-2008  mjf Sync with HEAD.
 1.8.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.9.8.1 03-Jul-2008  simonb Sync with head.
 1.9.6.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.9.4.3 11-Mar-2010  yamt sync with head
 1.9.4.2 19-Aug-2009  yamt sync with head.
 1.9.4.1 04-May-2009  yamt sync with head.
 1.11.14.1 21-Apr-2010  matt sync to netbsd-5
 1.11.10.4 02-May-2011  jym Sync with head.
 1.11.10.3 24-Oct-2010  jym Sync with HEAD
 1.11.10.2 01-Nov-2009  jym Sync with HEAD.
 1.11.10.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.11.6.2 22-Feb-2010  snj Pull up following revision(s) (requested by bouyer in ticket #1299):
sys/arch/xen/xen/isa_machdep.c: revision 1.23
sys/arch/xen/xen/pciide_machdep.c: revision 1.17
Enlarge temporary buffer, so that event counter's name is not truncated
for ioapic interrupts.
 1.11.6.1 04-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/x86/intr.c: revision 1.22
sys/arch/xen/xen/isa_machdep.c: revision 1.14
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.9
sys/arch/xen/xen/pciide_machdep.c: revision 1.12
When ioapic is used, for ISA interrupts, reuse the legacy ISA interrupt
number instead of allocating a new one. Force allocating a new interrupt number
for PCI devices, as the number stored in the PCI interrupt register
may be wrong.
This should help using a pciide controller in compat mode or ISA devices
in a non-0 domain.
 1.11.4.1 28-Apr-2009  skrll Sync with HEAD.
 1.17.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.17.4.1 21-Apr-2011  rmind sync with head
 1.18.32.1 28-Aug-2017  skrll Sync with HEAD
 1.18.14.1 03-Dec-2017  jdolecek update from HEAD
 1.19.2.2 16-Jul-2017  cherry 2739767
 1.19.2.1 16-Jul-2017  cherry file pciide_machdep.c was added on branch perseant-stdc-iso10646 on 2017-07-16 06:14:25 +0000
 1.66 01-Sep-2022  bouyer Redo previous; kill the right printf()
 1.65 01-Sep-2022  bouyer Remove a noisy printf; HVM guests triggers it (a lot) and it seems to be
expected.
 1.64 01-Sep-2022  bouyer Adjust privcmd_notify() so it has a chance to work in a PVH environnement too.
 1.63 31-Aug-2022  bouyer If we check XENPV, make sure to #include "opt_xen.h"
 1.62 31-Aug-2022  bouyer Work in progress on dom0 PVH support: ioctl support for tools.
Basically, in PVH mode (where XENFEAT_auto_translated_physmap is enabled),
the hypervisor will not map foreing ressources in our virtual address
space for us. Instead, we have to pass it an address in our physical
address space (but not mapped to some RAM) where the ressource will show up
and then enter this PA in pour page table.

For this, introduce xenmem_* which manage the PA space. In PVH mode this
is just allocated from the iomem_ex extent.

With this, I can start a PV domU, and the guest's kernel boots (and
the console works). It hangs because the backend driver can't map the
frontend ressources (yet).

Note that, per https://xenbits.xen.org/docs/unstable/support-matrix.html,
dom0 PVH support is still considered experimental by Xen.
 1.61 10-Dec-2021  andvar s/occured/occurred/ in comments, log messages and man pages.
 1.60 30-Nov-2020  bouyer Work in progress on dom0 PVH support. kernel boots and xl info works,
but we can't start a domU yet.
 1.59 26-May-2020  bouyer branches: 1.59.2;
Add need-flags for kernfs.
Compile Xen kernfs support only if kernfs is compiled in the kernel.
Should fix MODULAR build.
 1.58 26-May-2020  bouyer Implement new ioctl, needed by Xen 4.13:
IOCTL_PRIVCMD_MMAPBATCH_V2
IOCTL_PRIVCMD_MMAP_RESOURCE
IOCTL_GNTDEV_MMAP_GRANT_REF
IOCTL_GNTDEV_ALLOC_GRANT_REF
 1.57 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.56 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.55 19-Apr-2020  jdolecek make privpgops const
 1.54 07-Apr-2020  jdolecek branches: 1.54.2;
switch KERNFS_ALLOCENTRY() to use kmem_zalloc() instead of malloc()
 1.53 23-Feb-2020  ad UVM locking changes, proposed on tech-kern:

- Change the lock on uvm_object, vm_amap and vm_anon to be a RW lock.
- Break v_interlock and vmobjlock apart. v_interlock remains a mutex.
- Do partial PV list locking in the x86 pmap. Others to follow later.
 1.52 22-Feb-2020  chs do not wait for memory in pgo_fault methods, just return ENOMEM
and let the uvm_fault code wait if it is appropriate.
 1.51 22-Jun-2017  chs branches: 1.51.6; 1.51.10; 1.51.12;
use UVM_FLAG_UNMAP instead of unmapping separately.
 1.50 01-Jun-2017  chs remove checks for failure after memory allocation calls that cannot fail:

kmem_alloc() with KM_SLEEP
kmem_zalloc() with KM_SLEEP
percpu_alloc()
pserialize_create()
psref_class_create()

all of these paths include an assertion that the allocation has not failed,
so callers should not assert that again.
 1.49 17-Oct-2014  christos branches: 1.49.2;
now that privcmd_map_obj cleans up correctly, don't double free...
 1.48 03-Oct-2014  christos correct error paths; still need to verify that the "didn't give us back..."
case is correct.
 1.47 21-Sep-2014  christos fix leak.
 1.46 21-Sep-2014  christos fix leak.
 1.45 06-Nov-2013  mrg branches: 1.45.4;
- move variables inside their #ifdef use
- remove unused and set-but-unused variables
- use __USE() in a particularly ugly case

with these, and a couple of other changes, amd64 gcc 4.8.1 world
is able to complete build.sh release.
 1.44 27-Jun-2012  jym branches: 1.44.2; 1.44.4;
Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.43 15-Jun-2011  rmind branches: 1.43.2;
- privpgop_fault: call pmap_update() before uvmfault_unlockall().
- privcmd_ioctl, xengnt_more_entries: add missing pmap_update().
 1.42 12-Jun-2011  rmind Welcome to 5.99.53! Merge rmind-uvmplock branch:

- Reorganize locking in UVM and provide extra serialisation for pmap(9).
New lock order: [vmpage-owner-lock] -> pmap-lock.

- Simplify locking in some pmap(9) modules by removing P->V locking.

- Use lock object on vmobjlock (and thus vnode_t::v_interlock) to share
the locks amongst UVM objects where necessary (tmpfs, layerfs, unionfs).

- Rewrite and optimise x86 TLB shootdown code, make it simpler and cleaner.
Add TLBSTATS option for x86 to collect statistics about TLB shootdowns.

- Unify /dev/mem et al in MI code and provide required locking (removes
kernel-lock on some ports). Also, avoid cache-aliasing issues.

Thanks to Andrew Doran and Joerg Sonnenberger, as their initial patches
formed the core changes of this branch.
 1.41 06-Feb-2010  uebayasi branches: 1.41.4; 1.41.10;
Typo in a wchan string.
 1.40 23-Jan-2010  bouyer Use paddr_t to store physical address and cast mfn values to paddr_t
before shift. Fix overflow issue preventing xend from working as
reported by Mark Davies on port-xen@
 1.39 23-Oct-2009  snj Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.38 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.37 16-Mar-2009  cegger ansify function definitions
 1.36 04-Mar-2009  jym Do not continue silently on a page fault for an INVALID_PAGE, and instead
report an EFAULT like Xen does.

This avoids bad situations where a domain calls privpgop_fault() in a loop
by trying to map an invalid MFN.

See also http://mail-index.netbsd.org/port-xen/2009/03/03/msg004803.html
 1.35 04-Dec-2008  jym branches: 1.35.4;
Clean up code. No functional changes intended.

Reviewed by bouyer@.
 1.34 15-Nov-2008  ad Remove trivial compat ifdefs.
 1.33 21-Oct-2008  cegger branches: 1.33.2; 1.33.4;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.32 18-Oct-2008  bouyer Don't use a pointer to vm_map_entry after releasing the vm_map_lock().
Pointed out by yamt@.
 1.31 17-Oct-2008  jym Add checks to avoid possible calls to kmem_alloc() with 0 values.

Approved by bouyer.
 1.30 20-Sep-2008  bouyer Fix IOCTL_PRIVCMD_MMAPBATCH: don't blindly map requested pages read/write
but use the map's protection bits, as the hypervisor may refuse read/write
mappings for some entries. Now suspend/resume of domUs should work
from a NetBSD dom0, provided that the domU's kernel supports it.

From Jean-Yves Migeon.
 1.29 24-Aug-2008  bouyer privcmd_xen2bsd_errno(): 0 is also a valid error code.
 1.28 19-Aug-2008  cegger When shutting down or rebooting a domu, a phantom domain was left with:

> xm list
Domain-Unnamed 1 467 1 ---s-- 46.0

The root cause is a discrepancy in the error *value* codes:
BSD uses the AT&T Unix Version 6 error codes, while Xen uses
Unix System V error codes (or actually what Linux/i386 has taken over from it).

After shutting down (or rebooting) a domU, the guest container gets destroyed.
This implies freeing resources used by the guest (RAM, internal management structures, etc.).

The destroy process is an asynchronous process in order to not block the Dom0 (and other DomUs).
The destroy process works this way:

The XEN_DOMCTL_destroydomain is invoked from the xentools (python, libxc code).
XEN_DOMCTL_destroydomain hypercall calls domain_kill().
domain_kill() calls domain_relinquish_resources().
domain_relinquish_resources() calls relinquish_memory().
relinquish_memory() calls hypercall_preempt_check().

hypercall_preempt_check() makes all this asynchronous.
It fails, if there's an other hypercall pending.
In that case relinquish_memory() returns EAGAIN, which means, just retry to continue the destroy process.

EAGAIN is passed through the return path back into the python code
(= userspace). The python code checks for EAGAIN and *should*
retry, but it didn't.

In Unix System V / Linux, EAGAIN has the error code value 11.
In BSD, EAGAIN has the error code value 35 and EDEADLK has the error code value 11.

This means, Xen returning EAGAIN means for the python code EDEADLK.
This lead to the confusing error message 'domain destroy failed due to Resource Deadlock avoided'.

We finally convert the error code from the Xen hypercall to BSD before passing it upstream.
 1.27 18-Aug-2008  cegger IOCTL_PRIVCMD_MMAPBATCH used guest physical address 0x0 to mark a page as invalid.
Since we have to treat 0x0 as a valid page, this got broken in rev. 1.26.
Introduce INVALID_PAGE as magic value and restore the check.
This unbreaks IOCTL_PRIVCMD_MMAPBATCH while allowing to launch HVM guests.
 1.26 16-Aug-2008  cegger In Xen 3.2 (c/s 15985) the semantic of the HVM domain builder changed:

hvm: Avoid need for ugly setcpucontext() in HVM domain builder by
pre-setting the vcpu0 to runnable inside Xen, and have the builder
insert a JMP instruction to reach the hvmloader entry point from
address 0x0.

So we have to treat guest physical address 0x0 like every one
or we end in a page fault loop when launching a HVM guest, otherwise.

XXX Keep this for Xen2 as this change hasn't been tested there.
 1.25 17-Feb-2008  bouyer branches: 1.25.6; 1.25.10; 1.25.12; 1.25.16;
Fix IOCTL_PRIVCMD_HYPERCALL issue which shows up with xen-3.1.3:
some hypercalls results are returned though the error path (depending on
sign, it's an error code or a result), and some results are interpreted in
a special way by the NetBSD kernel (e.g. -1).
Add a 'retval' member to the privcmd_hypercall_t argument, which holds
the hypercall result if it completed without error. The error code
is returned via the usual error path.
Handle the old IOCTL_PRIVCMD_HYPERCALL under COMPAT_40.

While there, make the double-inclusion protection #define match the
convention in xenio3.h and xenio.h.
 1.24 08-Jan-2008  bouyer uobj->vmobjlock is a mutex these days.
 1.23 02-Dec-2007  bouyer branches: 1.23.6;
IOCTL_PRIVCMD_MMAP*: instead of brutally enterring the new entries in
the process page table, properly uvm_unmap1() the VA range and enter a new
uvm_map backed by an object. There is a know race between uvm_unmap1() and
uvm_map(), where another thread could get our VA range; in practice
I think none of the softwares using this interface are multithreaded (or at
last they are single-threaded when using it).
 1.22 01-Dec-2007  bouyer Use __define__ instead of define, pointed out by christos.
 1.21 27-Nov-2007  pooka a_l -> curlwp
 1.20 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.19 17-Oct-2007  garbled branches: 1.19.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.18 22-Jul-2007  ad branches: 1.18.4; 1.18.6; 1.18.10;
+#include <sys/proc.h>
 1.17 12-Jun-2007  bouyer branches: 1.17.2; 1.17.4;
IOCTL_PRIVCMD_MMAP: properly map entries from 0 to (mcmd->num - 1), instead
of mapping mcmd->num times entry 0. xentools < 3.1 probably didn't use this
with more than one entry at a time ...
 1.16 27-Apr-2007  bouyer branches: 1.16.2;
IOCTL_PRIVCMD_MMAPBATCH: mimic linux behavior and return no error
even if a pmap_enter_ma() (the underlying hypercall, really) returns one.
The fact that the mapping failed is notified to the called by oring
0xF0000000 to the mfn. This makes Xen 3.0.4 HVM work.
 1.15 17-Oct-2006  bouyer branches: 1.15.2; 1.15.4; 1.15.8; 1.15.10;
XEN_NO_HYPERCALLPAGE is redundant with XEN_COMPAT_030001; make
XEN_COMPAT_030001 disable the hypercall page too and remove
XEN_NO_HYPERCALLPAGE
 1.14 24-Sep-2006  bouyer The Xen folks says everyone should use hypercall call page now, make it so.
Old hypercall call method still still available with
options XEN_NO_HYPERCALLPAGE
but this is disabled by default (xen-3.0.2-2 supports hypercall call page
just fine).
While there add a VIRT_BASE= string in __xen_guest section; from
Bastian Blank on port-xen@.
 1.13 14-May-2006  elad branches: 1.13.8; 1.13.10;
integrate kauth.
 1.12 23-Jan-2006  yamt branches: 1.12.2; 1.12.4; 1.12.6; 1.12.8; 1.12.10;
merge xen pmap_enter, pmap_enter_ma, pmap_remap_pages.
ok'ed by Manuel Bouyer.
 1.11 15-Jan-2006  bouyer Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.10 14-Jan-2006  yamt privcmd_ioctl: wrap IOCTL_PRIVCMD_INITDOMAIN_EVTCHN_OLD in #ifdef COMPAT_30.
 1.9 24-Dec-2005  perry branches: 1.9.2;
__asm__ -> __asm
__const__ -> const
__inline__ -> inline
__volatile__ -> volatile
 1.8 12-Dec-2005  christos change a_p -> a_l->l_proc [untested]
 1.7 11-Dec-2005  christos merge ktrace-lwp.
 1.6 10-Sep-2005  bouyer Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.5 10-Sep-2005  bouyer Apply patch from Jed Davis, posted to port-xen:
Implement IOCTL_PRIVCMD_MMAPBATCH, as a loop calling pmap_remap_pages().
XXX this should do the MMU operations in batch.
 1.4 01-Jun-2005  yamt branches: 1.4.2;
privcmd_ioctl: remove unnecessary variable shadowing.
 1.3 24-May-2005  yamt privcmd_ioctl: don't abuse errno for IOCTL_PRIVCMD_INITDOMAIN_EVTCHN.
renumber the ioctl and keep the old one for compatibility.
PR/30027.
 1.2 09-Mar-2005  bouyer branches: 1.2.2;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 07-May-2004  cl branches: 1.1.2; 1.1.4; 1.1.8; 1.1.10; 1.1.12;
Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.1.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.10.1 29-Apr-2005  kent sync with -current
 1.1.8.2 12-Feb-2005  bouyer Implement pmap_remap_pages(), which remplace one or more pages in a
mapping with different pages, possibly from a foreing domain.
Use this to implement IOCTL_PRIVCMD_MMAP.
 1.1.8.1 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.1.4.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.4.6 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.5 27-Oct-2004  skrll Fix various comments that describe the argument structures
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 07-May-2004  skrll file privcmd.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 07-May-2004  he file privcmd.c was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.2.2.4 20-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1104):
sys/arch/xen/xen/privcmd.c: revision 1.6
sys/arch/xen/i386/pmap.c: revision 1.14
sys/arch/xen/include/pmap.h: revision 1.5
sys/arch/xen/xen/xennetback.c: revision 1.12 via patch
sys/arch/xen/xen/xbdback.c: revision 1.15
Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.2.2.3 14-Sep-2005  tron Pull up following revision(s) (requested by bouyer in ticket #776):
sys/arch/xen/xen/privcmd.c: revision 1.5
Apply patch from Jed Davis, posted to port-xen:
Implement IOCTL_PRIVCMD_MMAPBATCH, as a loop calling pmap_remap_pages().
XXX this should do the MMU operations in batch.
 1.2.2.2 18-Jun-2005  tron Pull up revision 1.4 (requested by yamt in ticket #465):
privcmd_ioctl: remove unnecessary variable shadowing.
 1.2.2.1 28-May-2005  tron Pull up revision 1.3 (requested by yamt in ticket #351):
privcmd_ioctl: don't abuse errno for IOCTL_PRIVCMD_INITDOMAIN_EVTCHN.
renumber the ioctl and keep the old one for compatibility.
PR/30027.
 1.4.2.6 27-Feb-2008  yamt sync with head.
 1.4.2.5 21-Jan-2008  yamt sync with head
 1.4.2.4 07-Dec-2007  yamt sync with head
 1.4.2.3 03-Sep-2007  yamt sync with head.
 1.4.2.2 30-Dec-2006  yamt sync with head.
 1.4.2.1 21-Jun-2006  yamt sync with head.
 1.9.2.2 01-Feb-2006  yamt sync with head.
 1.9.2.1 15-Jan-2006  yamt sync with head.
 1.12.10.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.12.8.1 08-Mar-2006  elad Adapt to kernel authorization KPI.

I expect *some* lossage here...
 1.12.6.1 24-May-2006  yamt sync with head.
 1.12.4.1 01-Jun-2006  kardel Sync with head.
 1.12.2.1 09-Sep-2006  rpaulo sync with head
 1.13.10.1 22-Oct-2006  yamt sync with head
 1.13.8.1 18-Nov-2006  ad Sync with head.
 1.15.10.1 11-Jul-2007  mjf Sync with head.
 1.15.8.4 03-Dec-2007  ad Sync with HEAD.
 1.15.8.3 20-Aug-2007  ad Sync with HEAD.
 1.15.8.2 15-Jul-2007  ad Sync with head.
 1.15.8.1 27-May-2007  ad Sync with head.
 1.15.4.1 07-May-2007  yamt sync with head.
 1.15.2.3 23-Mar-2008  jdc Pull up revisions: (requested by bouyer in ticket #1083)
src/sys/arch/xen/include/xenio.h 1.6
src/sys/arch/xen/include/xenio3.h 1.2
src/sys/arch/xen/xen/privcmd.c 1.25

Fix IOCTL_PRIVCMD_HYPERCALL issue which shows up with xen-3.1.3:
some hypercalls results are returned though the error path (depending on
sign, it's an error code or a result), and some results are interpreted in
a special way by the NetBSD kernel (e.g. -1).
Add a 'retval' member to the privcmd_hypercall_t argument, which holds
the hypercall result if it completed without error. The error code
is returned via the usual error path.
Handle the old IOCTL_PRIVCMD_HYPERCALL under COMPAT_40.

While there, make the double-inclusion protection #define match the
convention in xenio3.h and xenio.h.
 1.15.2.2 13-Jun-2007  liamjfoy Pull up following revision(s) (requested by bouyer in ticket #720):
sys/arch/xen/xen/privcmd.c: revision 1.17
IOCTL_PRIVCMD_MMAP: properly map entries from 0 to (mcmd->num - 1), instead
of mapping mcmd->num times entry 0. xentools < 3.1 probably didn't use this
with more than one entry at a time ...
 1.15.2.1 03-May-2007  snj branches: 1.15.2.1.2;
Pull up following revision(s) (requested by bouyer in ticket #599):
sys/arch/xen/xen/privcmd.c: revision 1.16
IOCTL_PRIVCMD_MMAPBATCH: mimic linux behavior and return no error
even if a pmap_enter_ma() (the underlying hypercall, really) returns one.
The fact that the mapping failed is notified to the called by oring
0xF0000000 to the mfn. This makes Xen 3.0.4 HVM work.
 1.15.2.1.2.2 03-Jun-2008  skrll Sync with netbsd-4.
 1.15.2.1.2.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.16.2.2 03-Oct-2007  garbled Sync with HEAD
 1.16.2.1 26-Jun-2007  garbled Sync with HEAD.
 1.17.4.1 15-Aug-2007  skrll Sync with HEAD.
 1.17.2.1 07-Aug-2007  matt Sync with HEAD.
 1.18.10.3 17-Nov-2007  bouyer Of course a_data is already in kernel space.
 1.18.10.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.18.10.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.18.6.3 23-Mar-2008  matt sync with HEAD
 1.18.6.2 09-Jan-2008  matt sync with HEAD
 1.18.6.1 06-Nov-2007  matt sync with HEAD
 1.18.4.2 03-Dec-2007  joerg Sync with HEAD.
 1.18.4.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.19.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.19.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.23.6.1 08-Jan-2008  bouyer Sync with HEAD
 1.25.16.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.25.16.1 19-Oct-2008  haad Sync with HEAD.
 1.25.12.2 10-Oct-2008  skrll Sync with HEAD.
 1.25.12.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.25.10.3 11-Mar-2010  yamt sync with head
 1.25.10.2 19-Aug-2009  yamt sync with head.
 1.25.10.1 04-May-2009  yamt sync with head.
 1.25.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.25.6.1 28-Sep-2008  mjf Sync with HEAD.
 1.33.4.2 30-Jan-2010  snj Pull up following revision(s) (requested by bouyer in ticket #1271):
sys/arch/xen/xen/privcmd.c: revision 1.40
Use paddr_t to store physical address and cast mfn values to paddr_t
before shift. Fix overflow issue preventing xend from working as
reported by Mark Davies on port-xen@
 1.33.4.1 08-Mar-2009  snj branches: 1.33.4.1.4;
Pull up following revision(s) (requested by jym in ticket #547):
sys/arch/xen/xen/privcmd.c: revision 1.36
Do not continue silently on a page fault for an INVALID_PAGE, and instead
report an EFAULT like Xen does.
This avoids bad situations where a domain calls privpgop_fault() in a loop
by trying to map an invalid MFN.
See also http://mail-index.netbsd.org/port-xen/2009/03/03/msg004803.html
 1.33.4.1.4.1 21-Apr-2010  matt sync to netbsd-5
 1.33.2.2 28-Apr-2009  skrll Sync with HEAD.
 1.33.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.35.4.4 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.35.4.3 24-Oct-2010  jym Sync with HEAD
 1.35.4.2 01-Nov-2009  jym Sync with HEAD.
 1.35.4.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.41.10.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.41.4.3 30-May-2011  rmind privpgop_fault: simplify and sync with changes in the branch.
 1.41.4.2 19-May-2011  rmind Implement sharing of vnode_t::v_interlock amongst vnodes:
- Lock is shared amongst UVM objects using uvm_obj_setlock() or getnewvnode().
- Adjust vnode cache to handle unsharing, add VI_LOCKSHARE flag for that.
- Use sharing in tmpfs and layerfs for underlying object.
- Simplify locking in ubc_fault().
- Sprinkle some asserts.

Discussed with ad@.
 1.41.4.1 16-Mar-2010  rmind Change struct uvm_object::vmobjlock to be dynamically allocated with
mutex_obj_alloc(). It allows us to share the locks among UVM objects.
 1.43.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.43.2.1 30-Oct-2012  yamt sync with head
 1.44.4.1 18-May-2014  rmind sync with head
 1.44.2.2 03-Dec-2017  jdolecek update from HEAD
 1.44.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.45.4.1 10-Nov-2014  snj Pull up following revision(s) (requested by maxv in ticket #195):
sys/arch/arm/iomd/iomd_irqhandler.c: revision 1.21
sys/arch/arm/ofw/ofw_irqhandler.c: revision 1.21
sys/arch/atari/atari/intr.c: revision 1.24-1.25
sys/arch/ews4800mips/sbd/fb_sbdio.c: revision 1.14
sys/arch/hpcmips/tx/tx39icu.c: revision 1.34
sys/arch/shark/isa/isa_irqhandler.c: revision 1.27
sys/arch/sparc/sparc/machdep.c: revision 1.327
sys/arch/sparc64/dev/psycho.c: revision 1.119
sys/arch/sparc64/dev/schizo.c: revision 1.32
sys/arch/sparc64/sparc64/machdep.c: revision 1.279
sys/arch/sun68k/sun68k/bus.c: revision 1.22
sys/arch/x86/x86/ipmi.c: revision 1.58
sys/arch/xen/xen/privcmd.c: revision 1.46-1.49
Fix several memory leaks.
 1.49.2.1 28-Aug-2017  skrll Sync with HEAD
 1.51.12.1 29-Feb-2020  ad Sync with head.
 1.51.10.1 31-May-2020  martin Pull up following revision(s) (requested by bouyer in ticket #935):

sys/arch/xen/x86/x86_xpmap.c: revision 1.89
sys/arch/x86/include/pmap.h: revision 1.121
sys/arch/xen/xen/privcmd.c: revision 1.58
sys/external/mit/xen-include-public/dist/xen/include/public/memory.h: revision 1.2
sys/arch/xen/include/xenpmap.h: revision 1.44
sys/arch/xen/include/xenio.h: revision 1.12
sys/arch/x86/x86/pmap.c: revision 1.394
(all via patch)

Ajust pmap_enter_ma() for upcoming new Xen privcmd ioctl:
pass flags to xpq_update_foreign()

Introduce a pmap MD flag: PMAP_MD_XEN_NOTR, which cause xpq_update_foreign()
to use the MMU_PT_UPDATE_NO_TRANSLATE flag.
make xpq_update_foreign() return the raw Xen error. This will cause
pmap_enter_ma() to return a negative error number in this case, but the
only user of this code path is privcmd.c and it can deal with it.

Add pmap_enter_gnt()m which maps a set of Xen grant entries at the
specified va in the specified pmap. Use the hooks implemented for EPT to
keep track of mapped grand entries in the pmap, and unmap them
when pmap_remove() is called. This requires pmap_remove() to be split
into a pmap_remove_locked(), to be called from pmap_remove_gnt().

Implement new ioctl, needed by Xen 4.13:
IOCTL_PRIVCMD_MMAPBATCH_V2
IOCTL_PRIVCMD_MMAP_RESOURCE
IOCTL_GNTDEV_MMAP_GRANT_REF
IOCTL_GNTDEV_ALLOC_GRANT_REF

Always enable declarations needed by privcmd.c
 1.51.6.3 21-Apr-2020  martin Sync with HEAD
 1.51.6.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.51.6.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.54.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.59.2.1 14-Dec-2020  thorpej Sync w/ HEAD.
 1.9 13-May-2020  jdolecek fix how shutdown is acknowledged - do it just by writing empty value
into the key; this is necessary for suspend, for it removal fails with
EACCESS

now 'xl save' proceeds almost instantly without timing out, and no longer
complains about the domain not acknowleding suspend

part of port-xen/55207
 1.8 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.7 20-Sep-2011  jym branches: 1.7.54;
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.6 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.5 24-Oct-2008  jym branches: 1.5.8;
- 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.4 22-Nov-2007  bouyer branches: 1.4.14; 1.4.18; 1.4.24;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.3 09-Oct-2006  dsl branches: 1.3.4; 1.3.10; 1.3.28; 1.3.30; 1.3.34; 1.3.36;
Another piece of code that was clearly committed before being compiled.
 1.2 08-Oct-2006  bouyer Alias 'halt' to 'poweroff' for now ...
 1.1 11-Aug-2006  yamt branches: 1.1.2; 1.1.4; 1.1.6; 1.1.8; 1.1.10; 1.1.12;
"xm shutdown" support for xen3. ok'ed by Manuel Bouyer.
 1.1.12.1 22-Oct-2006  yamt sync with head
 1.1.10.2 09-Sep-2006  rpaulo sync with head
 1.1.10.1 11-Aug-2006  rpaulo file shutdown_xenbus.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.1.8.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.1.6.2 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.1.6.1 11-Aug-2006  ghen file shutdown_xenbus.c was added on branch netbsd-3 on 2006-09-08 10:27:36 +0000
 1.1.4.1 18-Nov-2006  ad Sync with head.
 1.1.2.2 11-Aug-2006  yamt sync with head
 1.1.2.1 11-Aug-2006  yamt file shutdown_xenbus.c was added on branch yamt-pdpolicy on 2006-08-11 15:43:16 +0000
 1.3.36.1 08-Dec-2007  mjf Sync with HEAD.
 1.3.34.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.3.30.1 09-Jan-2008  matt sync with HEAD
 1.3.28.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.3.10.1 03-Dec-2007  ad Sync with HEAD.
 1.3.4.3 07-Dec-2007  yamt sync with head
 1.3.4.2 30-Dec-2006  yamt sync with head.
 1.3.4.1 09-Oct-2006  yamt file shutdown_xenbus.c was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.4.24.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.4.18.2 11-Mar-2010  yamt sync with head
 1.4.18.1 04-May-2009  yamt sync with head.
 1.4.14.1 17-Jan-2009  mjf Sync with HEAD.
 1.5.8.4 27-Aug-2011  jym Rename the functions for suspend to reflect that Xen does not hijack
the ACPI "sleepstate" sysctl(7) node anymore.

Add a boolean value to mark that the save/suspend operation has been
notified by dom0, so as to avoid possible errors where admin would like
to schedule the domain for sleep without dom0 being prepared for that. Fail
with EAGAIN in this case.

Sprinkle some KNF.
 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.7.54.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.2 12-May-2005  bouyer Remove obsolete file missed from the bouyer-xen2 merge.
Pointed out by YAMAMOTO Takashi.
 1.1 07-May-2004  cl branches: 1.1.2; 1.1.4; 1.1.8; 1.1.16;
Add support for domain0 operations:
- access to all physical memory
- access to hypervisor traps from userland
- setup/config Xen's network routing/firewall rules
 1.1.16.1 16-May-2005  riz Pull up file removal (requested by bouyer in ticket #299):
Remove obsolete file missed from the bouyer-xen2 merge.
Pointed out by YAMAMOTO Takashi.
 1.1.8.2 12-Feb-2005  bouyer Remove vfr.c, which was providing /kern/xen/vfr.c. Since xen 2.0 most
of the code was commented out, and this /kern entry is useless.
 1.1.8.1 18-Jan-2005  bouyer Make it compile. Probably not functionnal yet.
 1.1.4.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 07-May-2004  skrll file vfr.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 07-May-2004  he file vfr.c was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.3 17-Apr-2005  bouyer Remove
xenkbc at hypervisor
vga at hypervisor
they have been obsoleted by PCI and ISA support, and don't compile any more.
 1.2 25-Apr-2004  cl branches: 1.2.2; 1.2.4; 1.2.8; 1.2.10; 1.2.16;
Don't attach vga and keyboard if the domain is not privileged.
Also cleanup print command if attachment fails.
 1.1 24-Apr-2004  cl Add vga display support.
 1.2.16.1 25-Apr-2005  tron Pull up file removal (requested by bouyer in ticket #186):
Remove
xenkbc at hypervisor
vga at hypervisor
they have been obsoleted by PCI and ISA support, and don't compile any more.
 1.2.10.1 29-Apr-2005  kent sync with -current
 1.2.8.1 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.2.4.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.2.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.2.4.2 03-Aug-2004  skrll Sync with HEAD
 1.2.4.1 25-Apr-2004  skrll file vga_xen.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.2.2.2 22-May-2004  he Pull up revisions 1.1-1.2 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.2.2.1 25-Apr-2004  he file vga_xen.c was added on branch netbsd-2-0 on 2004-05-22 15:57:25 +0000
 1.53 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.52 21-Jan-2009  bouyer branches: 1.52.2;
major() is 32bits again, fix printf() format. Pointed out by Paul Goyette.
 1.51 16-Jan-2009  jym 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.50 13-Jan-2009  yamt g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.49 13-Jan-2009  taca Fix compile error: one more sys/arch/xen/xen/xbd.c and xbdback.c.
 1.48 12-Jan-2009  christos fix dev_t debugging printf lossage.
 1.47 17-Dec-2008  cegger make this compile
 1.46 17-Dec-2008  cegger kill MALLOC and FREE macros.
 1.45 03-May-2008  plunky branches: 1.45.8;
after the "struct disk" is finished with, it should be
destroyed with disk_destroy(9) to stave off LOCKDEBUG
panics.
 1.44 16-Apr-2008  cegger branches: 1.44.2; 1.44.4;
device_t / softc split
reviewed, tested and approved by bouyer
 1.43 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.42 05-Jan-2008  ad branches: 1.42.6;
Fix includes.
 1.41 22-Nov-2007  bouyer branches: 1.41.6;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.40 17-Oct-2007  garbled branches: 1.40.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.39 08-Oct-2007  ad branches: 1.39.2;
Merge disk init changes from the vmlocking branch. These seperate init /
destroy of 'struct disk' from attach / detach.
 1.38 22-Jul-2007  ad branches: 1.38.4; 1.38.6; 1.38.8;
+#include <sys/proc.h>
 1.37 04-Mar-2007  christos branches: 1.37.2; 1.37.10; 1.37.12; 1.37.14;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.36 05-May-2006  jld branches: 1.36.14;
Suspend/resume support for xbd -- keep enough info on live I/O to replay.

The rest of suspend/resume isn't there yet, but the parts that touch
regular usage have been tested. Discussed on port-xen on 2006-04-25;
approved by bouyer@.
 1.35 22-Apr-2006  jld Don't leak an xbdreq when probing disks.
 1.34 20-Apr-2006  blymn Prefix iostat structure elements with io_
 1.33 15-Apr-2006  matt Use M_ZERO instead of an explicit memset.
 1.32 15-Apr-2006  dogcow Make XENU compile again. (I'm not sure if this is the 'correct'
way to it build correctly; there might be some different method to
the new iostats collection framework.)
 1.31 29-Mar-2006  thorpej Use device_is_a() rathern than testing the dv_cfattach pointer.
 1.30 28-Mar-2006  thorpej Use device_unit().
 1.29 19-Mar-2006  bouyer More xbd->xbd_hypervisor changes; non-Xen3 kernels build again.
 1.28 19-Mar-2006  bouyer Add a block device frontend driver for Xen-3. xbd at hypervisor now
attaches with xbd_hypervisor, adjust CFATTACH_DECL().
 1.27 26-Dec-2005  yamt branches: 1.27.4; 1.27.6; 1.27.8; 1.27.10; 1.27.12;
- add ioctls to set/get disk bufq strategy.
- implement them for some drivers.
 1.26 13-Dec-2005  yamt fix a compilation problem after ktrace-lwp merge.
 1.25 11-Dec-2005  christos merge ktrace-lwp.
 1.24 18-Oct-2005  yamt dksubr: do b_blkno -> b_rawblkno translation earlier so that bufq can uses it.
 1.23 15-Oct-2005  yamt - change the way to specify a bufq strategy. (by string rather than by number)
- rather than embedding bufq_state in driver softc,
have a pointer to the former.
- move bufq related functions from kern/subr_disk.c to kern/subr_bufq.c.
- rename method to strategy for consistency.
- move some definitions which don't need to be exposed to the rest of kernel
from sys/bufq.h to sys/bufq_impl.h.
(is it better to move it to kern/ or somewhere?)
- fix some obvious breakage in dev/qbus/ts.c. (not tested)
 1.22 21-Aug-2005  bouyer Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.21 20-Aug-2005  yamt add wedge support to xbd and cgd.
 1.20 17-Apr-2005  bouyer branches: 1.20.2;
Remove unused code.
Add support for dynamic attach/detach. Should fix port-xen/29856 by Juan RP.
 1.19 17-Apr-2005  bouyer Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.18 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.17 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.16 01-Apr-2005  yamt merge yamt-km branch.
- don't use managed mappings/backing objects for wired memory allocations.
save some resources like pv_entry. also fix (most of) PR/27030.
- simplify kernel memory management API.
- simplify pmap bootstrap of some ports.
- some related cleanups.
 1.15 26-Mar-2005  bouyer Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.14 11-Mar-2005  bouyer branches: 1.14.2;
Print the irq used.
 1.13 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.12 10-Dec-2004  jmc branches: 1.12.2; 1.12.4; 1.12.6;
Fix some improper casts now that b_count is an int
 1.11 28-Oct-2004  yamt move buffer queue related stuffs from buf.h to their own header, bufq.h.
 1.10 15-Sep-2004  tls Actually mix samples into the kernel RNG pool. There's a buglet here: all
disk samples are reported as belonging to the first disk.
 1.9 10-May-2004  cl branches: 1.9.2; 1.9.4;
Avoid overrun problems with the indexes of the slots in the
communication rings.
 1.8 07-May-2004  cl add support for hot-add/-remove of block devices
 1.7 26-Apr-2004  cl Rework the physical<->machine memory mapping: offset physical addresses
by 0x100000 (above the I/O Memory "hole") leaving all physical addresses
below unused, don't perform phys<->mach mapping for addresses below 0x100000
or beyond the real hardware's physical memory.

-> /dev/mem works now as expected and X works in domain0.
 1.6 24-Apr-2004  cl Add sysctl nodes for Xen disk cookies used by domain creation tools.
 1.5 24-Apr-2004  cl Allow the block device driver to impersonate wd/sd/cd devices. This allows
for most system seamless migration from a NetBSD/i386 setup to a NetBSD/xen
setup without changing /etc/fstab or creating additional device nodes.
 1.4 24-Apr-2004  cl Let event_set_handler set the void * argument to event handlers.
 1.3 24-Apr-2004  cl Add ``hypervisor at mainbus'' and attach all devices provided by the
hypervisor to it instead of mainbus.

rename arch/xen/i386/hypervisor.c -> arch/xen/i386/hypervisor_machdep.c
 1.2 24-Apr-2004  cl Catch up with recent src/sys/dev/dksubr.c change (1.11 -> 1.12) which
prevents us from processing multiple bufs during 1 *start call. We now
rely on dk_start calling us repeatedly as long as there are bufs on the
queue.
 1.1 17-Apr-2004  cl add block device driver
 1.9.4.9 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.9.4.8 01-Apr-2005  skrll Sync with HEAD.
 1.9.4.7 04-Feb-2005  skrll Adapt to branch.
 1.9.4.6 18-Dec-2004  skrll Sync with HEAD.
 1.9.4.5 02-Nov-2004  skrll Sync with HEAD.
 1.9.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.9.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.9.4.2 03-Aug-2004  skrll Sync with HEAD
 1.9.4.1 10-May-2004  skrll file xbd.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.9.2.3 16-Sep-2004  jmc Pullup rev 1.10 (requested by tls in ticket #846)

Actually mix samples into the kernel RNG pool.
 1.9.2.2 22-May-2004  he Pull up revisions 1.1-1.9 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.9.2.1 10-May-2004  he file xbd.c was added on branch netbsd-2-0 on 2004-05-22 15:59:11 +0000
 1.12.6.3 19-Mar-2005  yamt (re-)convert arch/xen to the new API.
XXX except for xbdback.c and xennetback.c, because i'm not sure
what they're doing.
 1.12.6.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.12.6.1 13-Feb-2005  yamt - use new apis.
- simplify bootstrap and pvpage allocation.
- remove no longer needed .globl decls.
 1.12.4.1 29-Apr-2005  kent sync with -current
 1.12.2.4 19-Jan-2005  bouyer {wd,sd,cd} -> {wd,sd,cd}_xen.
 1.12.2.3 18-Jan-2005  bouyer We can't wait for an interrupt in xbd_ctrlif_rx() because we're already in
interrupt context. Handle controller messages in a kernel thread.
 1.12.2.2 17-Dec-2004  bouyer Merge revs 1.11-1.12
 1.12.2.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.14.2.8 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.14.2.7 06-Apr-2006  tron Apply patch (requested by jld in ticket #1240):
Avoid panics under high system load. This fixes PR port-xen/33110.
 1.14.2.6 05-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1083):
sys/arch/xen/xen/hypervisor.c: revision 1.16
sys/arch/xen/xen/if_xennet.c: revision 1.31
sys/arch/xen/conf/files.xen: revision 1.29
sys/arch/xen/xen/xbd.c: revision 1.22
Define a xendevbus atttibute and add it to hypervisor. Use it for xen devices
which attach to hypervisor. This allows to use config_found_ia() instead of
config_found(), instead of relying on the order of which device are
written in ioconf.c.
From Quentin Garnier.
 1.14.2.5 28-Apr-2005  tron branches: 1.14.2.5.2;
Pull up revision 1.18 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.14.2.4 28-Apr-2005  tron Pull up revision 1.17 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.14.2.3 25-Apr-2005  tron Pull up revision 1.20 (requested by bouyer in ticket #188):
Remove unused code.
Add support for dynamic attach/detach. Should fix port-xen/29856 by Juan RP.
 1.14.2.2 25-Apr-2005  tron Pull up revision 1.19 (requested by bouyer in ticket #187):
Add a kernel thread to the control interface, to handle deferred callacks
(which may sleep). Fix port-xen/29851 by YAMAMOTO Takashi.
Use config_pending_incr()/config_pending_decr() in if_xennet instead of
busy-looping (which doesn't work any more).
Remove the kernel thread from xbd, which isn't needed any more.
 1.14.2.1 30-Mar-2005  tron Pull up revision 1.15 (requested by bouyer in ticket #72):
Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.14.2.5.2.1 06-Apr-2006  tron Apply patch (requested by jld in ticket #1240):
Avoid panics under high system load. This fixes PR port-xen/33110.
 1.20.2.5 21-Jan-2008  yamt sync with head
 1.20.2.4 07-Dec-2007  yamt sync with head
 1.20.2.3 27-Oct-2007  yamt sync with head.
 1.20.2.2 03-Sep-2007  yamt sync with head.
 1.20.2.1 21-Jun-2006  yamt sync with head.
 1.27.12.3 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.27.12.2 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.27.12.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.27.10.2 11-May-2006  elad sync with head
 1.27.10.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.27.8.2 24-May-2006  yamt sync with head.
 1.27.8.1 01-Apr-2006  yamt sync with head.
 1.27.6.2 01-Jun-2006  kardel Sync with head.
 1.27.6.1 22-Apr-2006  simonb Sync with head.
 1.27.4.1 09-Sep-2006  rpaulo sync with head
 1.36.14.1 12-Mar-2007  rmind Sync with HEAD.
 1.37.14.1 15-Aug-2007  skrll Sync with HEAD.
 1.37.12.1 07-Aug-2007  matt Sync with HEAD.
 1.37.10.2 16-Oct-2007  garbled Sync with HEAD
 1.37.10.1 03-Oct-2007  garbled Sync with HEAD
 1.37.2.3 03-Dec-2007  ad Sync with HEAD.
 1.37.2.2 20-Aug-2007  ad - Alter disk attach/detach to fix a panic when closing a vnd device.
- Sync with HEAD.
 1.37.2.1 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.38.8.1 14-Oct-2007  yamt sync with head.
 1.38.6.2 09-Jan-2008  matt sync with HEAD
 1.38.6.1 06-Nov-2007  matt sync with HEAD
 1.38.4.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.38.4.1 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.39.2.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.40.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.40.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.41.6.1 08-Jan-2008  bouyer Sync with HEAD
 1.42.6.2 17-Jan-2009  mjf Sync with HEAD.
 1.42.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.44.4.3 19-Aug-2009  yamt sync with head.
 1.44.4.2 04-May-2009  yamt sync with head.
 1.44.4.1 16-May-2008  yamt sync with head.
 1.44.2.1 18-May-2008  yamt sync with head.
 1.45.8.2 03-Mar-2009  skrll Sync with HEAD.
 1.45.8.1 19-Jan-2009  skrll Sync with HEAD.
 1.52.2.1 01-Nov-2009  jym Sync with HEAD.
 1.135 29-Jul-2025  andvar Fix few typos in comments.
 1.134 25-Jul-2023  bouyer branches: 1.134.6;
Propoerly handle 4k sector size backends:
- report the backend's sector size to upper layers, not DEV_BSIZE.
Adjust the number of sectors accordingly.
- Use sc_secsize instead of XEN_BSIZE where appropriate. The sectors numbers
in I/O requests are still in XEN_BSIZE units, but must be a multiple
of sc_secsize/XEN_BSIZE.
- As a consequence of previous, the buffer has to be aligned to sc_secsize,
aligned to XEN_BSIZE may not be enough. This means that we may have to
xbd_map_align() more buffer, including some without B_PHYS set.
- Add some more DPRINTF lines, related to I/O requests

Tested with a linux dom0.
thanks to Christian Kujau for providing access to his hardware for testing
and debugging.
 1.133 21-Jul-2023  bouyer Also use XEN_BSIZE when computing the number of bytes for format_bytes().
While there note in a comment that sc_sectors is in XEN_BSIZE units
 1.132 21-Jul-2023  bouyer The disk size reported in the xenstore is always in XEN_BSIZE units,
not sector-size. Should fix the issue reported by Christian Kujau
on netbsd-users and port-xen.
 1.131 25-Feb-2023  riastradh xbd(4): Nix trailing whitespace.
 1.130 25-Feb-2023  riastradh 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.
 1.129 13-Jul-2020  jdolecek branches: 1.129.20;
change xbd_unmap_align() to get the bp via argument rathen than
req_bp, to avoid using req_bp after it's been set to NULL by the
time xbd_unmap_align() is called in xbd_handler(); this fixes
regression caused by previous fix

also make sure xbd_unmap_align() works correctly even when using the
linked request pair for I/O (ie. for Dom0 not supporting indirect
segments), regardless of the order the paired requests finish
 1.128 29-Jun-2020  jdolecek when using two linked requests for I/O (i.e. when backend doesn't support
INDIRECT segments), make sure to clear req_bp for both of them

fixes a misfired assertion in BLKIF_OP_FLUSH_DISKCACHE - PR port-xen/55431
 1.127 13-May-2020  jdolecek move the xen_intr_disestablish() to resume - having it in suspend
seems to cause panic in later phases of suspend

don't try to revoke grants in resume, they are all gone

add some diagnostic code in suspend to make sure the request lists are ready
for resume

part of PR port-xen/55207
 1.126 12-May-2020  jdolecek move xen_intr_disestablish() call in xbd_xenbus_suspend() so it's executed
without holding the xbd mutex, to avoid LOCKDEBUG assertion on suspend

while here only disestablish the intr if it was established

part of PR port-xen/55207
 1.125 07-May-2020  maxv Localify.
 1.124 03-May-2020  jdolecek reduce buffer size for format_number() so that xbd(4) would show the size
in KB/GB/TB instead of bytes again; the '9' matches what xbd(4) used before,
and also e.g. wd(4)
 1.123 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.122 21-Apr-2020  jdolecek adjust the condition for max indirect segments to make it clear what
is really needed (enough to map MAXPHYS + 1 page); no functional difference
 1.121 19-Apr-2020  jdolecek fix calculation of remaining bytes in xbd_diskstart_submit() for second
part of exactly I/O over 32KB when the remaining size was less than 1 page,
loop just finished without filling the last segment

triggered by anita run by Manuel
 1.120 18-Apr-2020  jdolecek read feature-persistent value on connect, e.g. the Linux Dom0 writes
the value only together with the device info; no functional difference,
since we don't allow persistent mappings
 1.119 18-Apr-2020  jdolecek make compile with XBD_DEBUG
 1.118 17-Apr-2020  jdolecek constify xbddkdriver
 1.117 17-Apr-2020  jdolecek remove some old #if 0 code
 1.116 16-Apr-2020  jdolecek if backend supports it, use indirect segment for I/O requests over 32KB

there is some overhead for backend without persistent mappings
(backend needs to map the extra indirect segment page), but this
makes it possible to queue more I/O if using 64KB block size

tested on NetBSD/amd64 DomU against Linux/amd64 Dom0
 1.115 16-Apr-2020  jdolecek when checking for physio request check for B_PHYS in b_flags rather
than an internal field, so this works when the original buf is
wrapped by another one by e.g. dk(4)

fixes misfired assert when doing newfs on a wedge, reported by Manuel Bouyer
 1.114 15-Apr-2020  jdolecek make xbd(4) accept 64k (aka regular MAXPHYS) I/O, use two linked requests
for this, so that it does not require MAXPHYS override in kernel config
any more

this is useful even if we get indirect segment support one day,
for compatibility with Dom0's not supporting it, like currently NetBSD
 1.113 14-Apr-2020  jdolecek fix KASSERT() in xbd_map_align()
 1.112 14-Apr-2020  jdolecek rearrange slightly to do proper b_resid accounting, to prepare for partial
transfers
 1.111 14-Apr-2020  jdolecek use single pre-allocated buffer for unaligned I/O - it's rare and not
performance critical path, it's more important to ensure it will succeed
eventually; also return EAGAIN rather than ENOMEM, so the I/O will be
retried by dk_start() when previous I/O finishes

fix yet another leak on the xengnt_grant_access() fail path in
xbd_diskstart() - this time the unalign buffer
 1.110 14-Apr-2020  jdolecek add forgotten mutex_exit() in detach, which caused panic in uvm_unmap1()
due to held spin lock
 1.109 14-Apr-2020  jdolecek provide d_iosize hook to cap the xfer size used for dumps
 1.108 14-Apr-2020  jdolecek if grant fails also revoke the grants for previous segments, fixes
grant leak on grant error
 1.107 13-Apr-2020  jdolecek KASSERT() that requested I/O size is <= XBD_MAX_XFER - this can happen
e.g. with custom DomU kernel which doesn't have the value for MAXPHYS
reduced to 32k like the XEN3_DOMU config
 1.106 13-Apr-2020  jdolecek make xbd(4) D_MPSAFE
 1.105 12-Apr-2020  jdolecek convert to bus_dma(9)

simplify and fix error handling in xbd_handler(), expect backend
to not use the grant once request is finished, and avoid leaking
bounce buffer when the request using it happens to end with error

in xbd_diskstart() only do the RING_PUSH_REQUESTS_AND_CHECK_NOTIFY()
when actually the request was pushed successfully
 1.104 12-Apr-2020  jdolecek add KASSERT() for unaligned case to make sure it indeed happens only
for requests via physio
 1.103 11-Apr-2020  jdolecek revert previous - physio arranges for tranfer directly to user-provided
buffers, which are generally not DEV_BSIZE-aligned
 1.102 11-Apr-2020  jdolecek now that getebuf() et.al. always returns DEV_BSIZE-aligned buffers
and rest of kernel code doing disk I/O was also converted over to it,
it's finally safe to remove the code to handle misaligned buffers

leave just KASSERT() in place
 1.101 11-Apr-2020  jdolecek print 'backend features' instead of just 'features' for the detected backend
features, so that it's clear it's just capability rather than something
actually used by the frontend driver
 1.100 10-Apr-2020  jdolecek update comment for DIOCGCACHE (no DKCACHE_READ)
 1.99 10-Apr-2020  jdolecek allow DIOCSSTRATEGY, there is no particular reason why it should be
restricted for xbd(4)
 1.98 10-Apr-2020  jdolecek implement DIOCGCACHE - assume if CACHE_FLUSH is supported, write cache
is enabled

convert the sc_cache_flush flag to a feature bit field, and recognize
also barrier and persistent features; print the recognized features
on boot, and remove the warning on DIOCCACHESYNC
 1.97 07-Apr-2020  jdolecek branches: 1.97.2;
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.96 13-Mar-2020  jdolecek xbd backend not supporting cache flush is not an autoconfiguration error
 1.95 01-Mar-2020  riastradh Add a flag to dk_dump for virtual disk devices.

If a disk is backed by a physical medium other than itself, such as
cgd(4), then it passes DK_DUMP_RECURSIVE to disable the recursion
detection for dk_dump.

If, however, a device represents a physical medium on its own, such
as wd(4), then it passes 0 instead.

With this, I can now dump to dk on cgd on dk on wd.
 1.94 16-Oct-2019  jdolecek use aprint_normal_dev() for the capacity message, same as e.g. wd(4) and sd(4)
 1.93 16-Oct-2019  jdolecek don't try to read disklabel for attached xbd* disk from (effectively) always
xbd0a device, there is no need for it and it is just wrong

same problem as PR kern/41704 for cgd

XXX perhaps xbd should use native block size passed from dom0 rather then
XXX recomputing in DEV_BSIZE
 1.92 02-Feb-2019  cherry branches: 1.92.4;
Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.91 24-Dec-2018  cherry 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.90 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.89 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.88 26-Aug-2018  jdolecek add event counter for the unaligned map to get a handle on how often this
actually happens
 1.87 26-Aug-2018  bouyer Add back non-512-byte aligned buffers support (reverting 1.84).
Userland users of raw devices don't have to use sector-aligned buffers,
and e.g. disklabel doesn't.
anita tests on Xen should at last complete the install phase again.
 1.86 21-Aug-2018  jdolecek (only commit message for rev 1.85, no actual change)

shuffle code in xbd_handler() response loop so that req_bp is never
touched for non I/O responses; unknown commands are now just skipped, also
without touching neither xbdreq, nor req_bp

add some KASSERTS() for xbdreq->req_nr_segments and xbdreq->req_bp,
and reset req_bp after it's processed to ensure the buf can never
be processed twice

intended to simplify debugging in cases like PR port-xen/53506
 1.85 21-Aug-2018  jdolecek *** empty log message ***
 1.84 21-Aug-2018  jdolecek remove code handling buffers not aligned to 512-byte boundary, simply return
EINVAL straight away

the condition doesn't seem to be actually hit for normal boots and
tools like fsck, which is good thing since it allocated memory
separately for each such I/O
 1.83 21-Aug-2018  jdolecek avoid race condition between I/O submission in xbd_diskstart() and
interrupt handling in xbd_handler() - need to protect it with splbio()

fixes PR port-xen/53506 by Emmanuel Dreyfus, and likely also port-xen/53074
by Brad Spencer
 1.82 15-Aug-2018  jdolecek fix comment
 1.81 15-Aug-2018  jdolecek minor KNF
 1.80 15-Aug-2018  jdolecek mark XXXSMP also wakeup() calls
 1.79 24-Jun-2018  jdolecek branches: 1.79.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.78 11-Nov-2017  riastradh branches: 1.78.2;
Restore MP-safety annotations in intr_establish_xname.

In the old API, event_set_handler would assume MP-unsafe for IPL_VM
and MP-safe for all other levels (IPL_SCHED, IPL_HIGH). The recent
Xen interrupt rototill started passing known_mpsafe=true for _all_
interrupt handlers. Change it to known_mpsafe=false for the IPL_VM
(= IPL_CLOCK, IPL_NET) ones.
 1.77 06-Nov-2017  cherry Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.76 05-Mar-2017  mlelstv branches: 1.76.6;
Enhance disk metrics by calculating a weighted sum that is incremented
by the number of concurrent I/O requests. Also introduce a new disk_wait()
function to measure requests waiting in a bufq.
iostat -y now reports data about waiting and active requests.

So far only drivers using dksubr and dk, ccd, wd and xbd collect data about
waiting requests.
 1.75 25-Oct-2015  maxv branches: 1.75.2; 1.75.4;
Uninitialized variable. Found by Brainy.

ok pgoyette@
 1.74 28-Aug-2015  mlelstv move entropy gathering into dksubr.
 1.73 27-Aug-2015  mlelstv Make dksubr use a spin-mutex again, since some drivers still call dk_done
from hardware interrupt. Instead, release mutex while calling start routine.

The buffer peek/use/get sequence which can no longer be atomic. So consume
the buffer directly and on error privately save and retry the buffer later.
The dk_drain function is used to flush such a deferred buffer together with
the buffer queue.
Adjust drivers to use dk_drain.

Fix an error path where dk_done was called while the lock was already held.
 1.72 16-Aug-2015  mlelstv Two changes to the dksubr interface.

- d_diskstart callback now processes a single buffer only.
The new wrapper function dk_start processes the queue,
performs other buffer handling and also provides locking
for the the data structures.

- add d_discard callback to handle device specific function inside
the new dk_discard helper function.

Replace splbio with mutex to protect queue and disk structure.
Refactor common code in dk_strategy and dk_discard into dk_translate.

Adjust and simplify ld(4), cgd(4) and xbd(4) drivers accordingly.

ld(4) now becomes MP_SAFE.

Bump kernel version.
 1.71 02-May-2015  mlelstv Merge dk_intf and dkdriver interfaces.
Merge common disk driver functionality in ld.c with dksubr.c.
Adjust the two previous users of dk_intf (cgd and xbd) to
the changes.

bump kernel version to 7.99.14
 1.70 13-Apr-2015  riastradh MD rnd.h cleanups. Please let me know if I broke anything!
 1.69 02-Jan-2015  christos We have three sets of DTYPE_ constants in the kernel:
altq Drop Type
disklabel Disk Type
file Descriptor Type
(not to mention constants that contain the string DTYPE).
Let's make them two, by changing the disklabel one to be DisK TYPE since the
other disklabel constants seem to do that. Not many userland programs use
these constants (and the ones that they do are mostly in ifdefs). They will
be fixed shortly.
 1.68 02-Jan-2015  christos these call dk_ioctl, no need to call disk_ioctl anymore.
 1.67 31-Dec-2014  christos pass dev to disk_ioctl
 1.66 10-Aug-2014  tls branches: 1.66.4;
Merge tls-earlyentropy branch into HEAD.
 1.65 25-Jul-2014  dholland Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.64 25-Jul-2014  dholland Add d_discard to all struct bdevsw instances I could find.

I've set them all to nodiscard. Some of them (wd, dk, vnd, ld,
raidframe, maybe cgd) should be implemented for real.
 1.63 25-May-2014  bouyer As proposed in
https://mail-index.netbsd.org/tech-kern/2014/05/21/msg017098.html
remove dk_start() and dk_iodone() from dksubr.c and move the related code
to the underlying driver.
This increase complexity only marginally: the underlying drivers have
to do the while() loop themselves, but this can now be done properly with
bufq_peek()/bufq_get(), removing the buffer from the queue at the right time.
This handle both the recursion and reordering issues (the reordering
issue is described here:
https://mail-index.netbsd.org/tech-kern/2014/05/19/msg017089.html
the recursion isssue is PR #25240).

Difference with the patch posted to tech-kern@: KASSERT() that the
buffer we remove with bufq_get() is the same as the one we bufq_peek()'d
just before.
Hopefully this will allow more disk drivers to use dksubr.c
 1.62 16-Mar-2014  dholland branches: 1.62.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.61 06-Nov-2013  mrg - move variables inside their #ifdef use
- remove unused and set-but-unused variables
- use __USE() in a particularly ugly case

with these, and a couple of other changes, amd64 gcc 4.8.1 world
is able to complete build.sh release.
 1.60 13-Oct-2013  riz Catch up to recent changes in config_pending_{incr,decr}().
 1.59 29-May-2013  christos branches: 1.59.2;
get dir of dk_geom.
 1.58 29-May-2013  christos phase 1 of disk geometry cleanup:
- centralize the geometry -> plist code so that we don't have
n useless copies of it.
 1.57 25-May-2012  elric branches: 1.57.2;
Update xdb_xenbus.c to new usage of routines in dksubr.c.
 1.56 22-Feb-2012  jakllsch Only attempt to handle as much data as we can handle.
Per my testing, allows normal MAXPHYS on domU kernels.
 1.55 22-Feb-2012  jakllsch Define XBD_MAX_XFER instead of using (PAGE_SIZE * BLKIF_MAX_SEGMENTS_PER_REQUEST) directly.
 1.54 21-Feb-2012  jakllsch Add and use xbdminphys() to handle transfer segmentation/size limit.
Should allow us to use a normal MAXPHYS in domU kernels.
 1.53 02-Feb-2012  para branches: 1.53.2;
call uvm_km_kmem_alloc with right arguments

fixes: kern/45913
 1.52 02-Feb-2012  tls Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.51 27-Jan-2012  para extending vmem(9) to be able to allocated resources for it's own needs.
simplifying uvm_map handling (no special kernel entries anymore no relocking)
make malloc(9) a thin wrapper around kmem(9)
(with private interface for interrupt safety reasons)

releng@ acknowledged
 1.50 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.49 19-Nov-2011  tls branches: 1.49.2;
First step of random number subsystem rework described in
<20111022023242.BA26F14A158@mail.netbsd.org>. This change includes
the following:

An initial cleanup and minor reorganization of the entropy pool
code in sys/dev/rnd.c and sys/dev/rndpool.c. Several bugs are
fixed. Some effort is made to accumulate entropy more quickly at
boot time.

A generic interface, "rndsink", is added, for stream generators to
request that they be re-keyed with good quality entropy from the pool
as soon as it is available.

The arc4random()/arc4randbytes() implementation in libkern is
adjusted to use the rndsink interface for rekeying, which helps
address the problem of low-quality keys at boot time.

An implementation of the FIPS 140-2 statistical tests for random
number generator quality is provided (libkern/rngtest.c). This
is based on Greg Rose's implementation from Qualcomm.

A new random stream generator, nist_ctr_drbg, is provided. It is
based on an implementation of the NIST SP800-90 CTR_DRBG by
Henric Jungheim. This generator users AES in a modified counter
mode to generate a backtracking-resistant random stream.

An abstraction layer, "cprng", is provided for in-kernel consumers
of randomness. The arc4random/arc4randbytes API is deprecated for
in-kernel use. It is replaced by "cprng_strong". The current
cprng_fast implementation wraps the existing arc4random
implementation. The current cprng_strong implementation wraps the
new CTR_DRBG implementation. Both interfaces are rekeyed from
the entropy pool automatically at intervals justifiable from best
current cryptographic practice.

In some quick tests, cprng_fast() is about the same speed as
the old arc4randbytes(), and cprng_strong() is about 20% faster
than rnd_extract_data(). Performance is expected to improve.

The AES code in src/crypto/rijndael is no longer an optional
kernel component, as it is required by cprng_strong, which is
not an optional kernel component.

The entropy pool output is subjected to the rngtest tests at
startup time; if it fails, the system will reboot. There is
approximately a 3/10000 chance of a false positive from these
tests. Entropy pool _input_ from hardware random numbers is
subjected to the rngtest tests at attach time, as well as the
FIPS continuous-output test, to detect bad or stuck hardware
RNGs; if any are detected, they are detached, but the system
continues to run.

A problem with rndctl(8) is fixed -- datastructures with
pointers in arrays are no longer passed to userspace (this
was not a security problem, but rather a major issue for
compat32). A new kernel will require a new rndctl.

The sysctl kern.arandom() and kern.urandom() nodes are hooked
up to the new generators, but the /dev/*random pseudodevices
are not, yet.

Manual pages for the new kernel interfaces are forthcoming.
 1.48 20-Sep-2011  jym branches: 1.48.2;
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.47 25-Jul-2011  jym And... explain xbd(4).
 1.46 25-Jul-2011  jym KNF. No functional change.
 1.45 30-Mar-2011  jym Remove spurious spaces.
 1.44 19-Oct-2009  bouyer branches: 1.44.4; 1.44.6;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.43 23-Sep-2009  bouyer xbdback: implement and publish "feature-flush-cache".
xbd: if feature-flush-cache is present, use it for DIOCCACHESYNC.
If not present, make DIOCCACHESYNC return EOPNOTSUPP and warn on
first call.
Should improve WAPBL reliability of Xen guests on a NetBSD dom0.
Unfortunably not all linux guests seems to support this feature, and using
feature-write-barrier would require a B_BARRIER flag in the buffer.
 1.42 21-Sep-2009  bouyer Allow a xbd to be detached from the domU (e.g. at shutdown). Patch tested
and adjusted by David Young, discussed on port-xen@ early august.
 1.41 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.40 15-Jun-2009  bouyer Implement DIOCGDISKINFO for xbd disk driver.
 1.39 03-Mar-2009  bouyer Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.38 08-Feb-2009  jym branches: 1.38.2;
- add comments
- wrap long lines for easier reading
- make some debug messages a bit more relevant
 1.37 16-Jan-2009  jym 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.36 13-Jan-2009  yamt g/c BUFQ_FOO() macros and use bufq_foo() directly.
 1.35 18-Dec-2008  cegger remove unused malloc.h
 1.34 30-Oct-2008  cegger branches: 1.34.2;
twiddle with headers: include what is really needed
 1.33 25-Oct-2008  jym branches: 1.33.2;
- add and fix some comments in xennet and xbd code
- add BEST_SUSPENDED macro, the xennet equivalent of BLKIF_STATE_SUSPENDED
for xbd (indicate that backend is in suspended state)
- use GNTST_okay to check the grant table setup status returned by hypercall,
rather than 0

No functional changes.
 1.32 24-Oct-2008  jym - 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.31 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.30 05-Jul-2008  bouyer branches: 1.30.2;
xbd_map_align() can be called from interrupt context (though
xbd_handler()->dk_iodone()->dk_start()->xbd_start()), so don't try
to sleep in uvm_km_alloc().
 1.29 12-Jun-2008  cegger xbddump: remove duplicate check done by device_lookup_private
 1.28 12-Jun-2008  briggs Don't try to look up the softc manually in xbddump(). We already have it
from device_lookup_private().
 1.27 03-May-2008  plunky branches: 1.27.2; 1.27.4;
after the "struct disk" is finished with, it should be
destroyed with disk_destroy(9) to stave off LOCKDEBUG
panics.
 1.26 16-Apr-2008  cegger branches: 1.26.2; 1.26.4;
device_t / softc split
reviewed, tested and approved by bouyer
 1.25 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.24 13-Mar-2008  bouyer 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.23 22-Nov-2007  bouyer branches: 1.23.10; 1.23.14;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.22 17-Oct-2007  garbled branches: 1.22.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.21 08-Oct-2007  ad branches: 1.21.2;
Merge disk init changes from the vmlocking branch. These seperate init /
destroy of 'struct disk' from attach / detach.
 1.20 29-Jul-2007  ad branches: 1.20.4; 1.20.6; 1.20.8;
It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.19 22-Jul-2007  he Explicitly include <sys/proc.h> for tsleep() prototype.
 1.18 04-Mar-2007  christos branches: 1.18.2; 1.18.10; 1.18.12; 1.18.14;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.17 11-Jan-2007  bouyer branches: 1.17.2;
Fix still possible race condition where xbd_handler could still try to process
an event before the device is ready.
Reported, and tested by anzi at dnainternet net on port-xen.
 1.16 06-Jan-2007  bouyer Don't try to handle xbd interrupts if the device is not yet connected.
Should fix NULL dereference at boot when more than one xbd device is
configured, reported by Juraj Hercek on port-xen@
 1.15 01-Jan-2007  yamt xbd_handler: call dk_iodone after updating rsp_cons.
otherwise, xbdstart kicked via dk_iodone still sees RING_FULL condition
and just returns. it makes xbd stall when all requests are acked by
a single xbd_handler. PR/34005.
 1.14 07-Jul-2006  yamt branches: 1.14.4; 1.14.6; 1.14.10;
xbdsize: fix an null dereference.
 1.13 25-Jun-2006  bouyer Add a 'base' argument to xenbus_read_ul, so that we can read number is base
other than 10.
 1.12 23-May-2006  bouyer branches: 1.12.2; 1.12.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.11 14-May-2006  elad branches: 1.11.2;
integrate kauth.
 1.10 10-May-2006  bouyer Fix NULL pointer deref when a backend switches to closing without
have been connected.
Make sure we connect only once; avoid getting disk attached multiple times
when there are multiple block devices configured (the xenbus seems to call
the callbacks for all xbd when each of them comes up).
 1.9 20-Apr-2006  dogcow branches: 1.9.2;
fix breakage due to iostats header twiddling
 1.8 15-Apr-2006  dogcow branches: 1.8.2;
Make XENU compile again. (I'm not sure if this is the 'correct'
way to it build correctly; there might be some different method to
the new iostats collection framework.)
 1.7 02-Apr-2006  bouyer branches: 1.7.2; 1.7.4;
xbd_xenbus_detach(): wait for the backed to release grant reference to the
ring before trying to revoke it, otherwise the kernel may panic in
xengnt_revoke_access.
 1.6 29-Mar-2006  thorpej Use device_unit().
 1.5 26-Mar-2006  bouyer branches: 1.5.2;
Adapt to xenbus changes; add a proper autoconf detach callback and
handle state change to Closing. We can now dynamically attach/detach
block devices.
 1.4 25-Mar-2006  bouyer When printing the size of the xbd, also print the number of sectors.
in xbdstart(), check the sector number and transfer size against the
size of the xbd; if we issue an out of bound request the linux xbd backend
will fail to unmap the request, and xbd hangs up.
 1.3 25-Mar-2006  bouyer - Add a missing UVM_KMF_WIRED to uvm_km_alloc(), this would trigger an
assertion in uvm
- xbd_open(): check that the device exists before calling dk_open(). Fix
a null pointer dereference when trying to open a unconfigured xbd.
Should fix 2 issues reported by Stephane Bortzmeyer on port-xen.
 1.2 20-Mar-2006  bouyer Make it build with options DEBUG
 1.1 19-Mar-2006  bouyer Add a block device frontend driver for Xen-3. xbd at hypervisor now
attaches with xbd_hypervisor, adjust CFATTACH_DECL().
 1.5.2.4 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.5.2.3 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.5.2.2 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.5.2.1 26-Mar-2006  tron file xbd_xenbus.c was added on branch peter-altq on 2006-03-28 09:46:22 +0000
 1.7.4.6 15-Sep-2006  yamt fix a merge botch.
 1.7.4.5 11-Aug-2006  yamt sync with head
 1.7.4.4 26-Jun-2006  yamt sync with head.
 1.7.4.3 24-May-2006  yamt sync with head.
 1.7.4.2 11-Apr-2006  yamt sync files somehow mis-tagged by yamt-pdpolicy-base2.
 1.7.4.1 02-Apr-2006  yamt file xbd_xenbus.c was added on branch yamt-pdpolicy on 2006-04-11 12:20:51 +0000
 1.7.2.8 20-Mar-2009  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1996):
sys/arch/xen/xen/if_xennet_xenbus.c 1.34
src/sys/arch/xen/xen/xbd_xenbus.c 1.39

Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.7.2.7 22-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1653):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.15
xbd_handler: call dk_iodone after updating rsp_cons.
otherwise, xbdstart kicked via dk_iodone still sees RING_FULL condition
and just returns. it makes xbd stall when all requests are acked by
a single xbd_handler. PR/34005.
 1.7.2.6 15-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1632):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.17
Fix still possible race condition where xbd_handler could still try to process
an event before the device is ready.
Reported, and tested by anzi at dnainternet net on port-xen.
 1.7.2.5 08-Jan-2007  ghen Pull up following revision(s) (requested by bouyer in ticket #1624):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.16
Don't try to handle xbd interrupts if the device is not yet connected.
Should fix NULL dereference at boot when more than one xbd device is
configured, reported by Juraj Hercek on port-xen@
 1.7.2.4 30-Jul-2006  tron branches: 1.7.2.4.2;
Apply patch (requested by bouyer in ticket #1437):
Use uvm_km_kmemalloc1() instead of uvm_km_alloc() for xbd_align_map(), as
it seems that the later is not safe to use in interrupt context. Should
fix random panics in domU after using the character device with unaligned
buffers.
 1.7.2.3 13-May-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1319):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.10 via patch
Fix NULL pointer deref when a backend switches to closing without
have been connected.
Make sure we connect only once; avoid getting disk attached multiple times
when there are multiple block devices configured (the xenbus seems to call
the callbacks for all xbd when each of them comes up).
 1.7.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.7.2.1 02-Apr-2006  tron file xbd_xenbus.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.7.2.4.2.3 22-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1653):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.15
xbd_handler: call dk_iodone after updating rsp_cons.
otherwise, xbdstart kicked via dk_iodone still sees RING_FULL condition
and just returns. it makes xbd stall when all requests are acked by
a single xbd_handler. PR/34005.
 1.7.2.4.2.2 15-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1632):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.17
Fix still possible race condition where xbd_handler could still try to process
an event before the device is ready.
Reported, and tested by anzi at dnainternet net on port-xen.
 1.7.2.4.2.1 08-Jan-2007  ghen Pull up following revision(s) (requested by bouyer in ticket #1624):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.16
Don't try to handle xbd interrupts if the device is not yet connected.
Should fix NULL dereference at boot when more than one xbd device is
configured, reported by Juraj Hercek on port-xen@
 1.8.2.3 11-May-2006  elad sync with head
 1.8.2.2 19-Apr-2006  elad sync with head - hopefully this will work
 1.8.2.1 15-Apr-2006  elad file xbd_xenbus.c was added on branch elad-kernelauth on 2006-04-19 02:34:03 +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 20-Apr-2006  simonb file xbd_xenbus.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.11.2.1 19-Jun-2006  chap Sync with head.
 1.12.4.8 17-Mar-2008  yamt sync with head.
 1.12.4.7 07-Dec-2007  yamt sync with head
 1.12.4.6 27-Oct-2007  yamt sync with head.
 1.12.4.5 03-Sep-2007  yamt sync with head.
 1.12.4.4 26-Feb-2007  yamt sync with head.
 1.12.4.3 30-Dec-2006  yamt sync with head.
 1.12.4.2 21-Jun-2006  yamt sync with head.
 1.12.4.1 23-May-2006  yamt file xbd_xenbus.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.12.2.1 13-Jul-2006  gdamore Merge from HEAD.
 1.14.10.6 20-Mar-2009  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1282):
sys/arch/xen/xen/if_xennet_xenbus.c 1.34
src/sys/arch/xen/xen/xbd_xenbus.c 1.39

Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.14.10.5 31-Aug-2008  jdc Pull up revision 1.30 (requested by bouyer in ticket #1163).

xbd_map_align() can be called from interrupt context (though
xbd_handler()->dk_iodone()->dk_start()->xbd_start()), so don't try
to sleep in uvm_km_alloc().
 1.14.10.4 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.14.10.3 18-Jan-2007  tron branches: 1.14.10.3.2;
Pull up following revision(s) (requested by yamt in ticket #360):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.15
xbd_handler: call dk_iodone after updating rsp_cons.
otherwise, xbdstart kicked via dk_iodone still sees RING_FULL condition
and just returns. it makes xbd stall when all requests are acked by
a single xbd_handler. PR/34005.
 1.14.10.2 17-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #365):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.17
Fix still possible race condition where xbd_handler could still try to process
an event before the device is ready.
Reported, and tested by anzi at dnainternet net on port-xen.
 1.14.10.1 08-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #338):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.16
Don't try to handle xbd interrupts if the device is not yet connected.
Should fix NULL dereference at boot when more than one xbd device is
configured, reported by Juraj Hercek on port-xen@
 1.14.10.3.2.1 04-Sep-2008  skrll Sync with netbsd-4.
 1.14.6.2 09-Sep-2006  rpaulo sync with head
 1.14.6.1 07-Jul-2006  rpaulo file xbd_xenbus.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.14.4.1 12-Jan-2007  ad Sync with head.
 1.17.2.1 12-Mar-2007  rmind Sync with HEAD.
 1.18.14.1 15-Aug-2007  skrll Sync with HEAD.
 1.18.12.1 07-Aug-2007  matt Sync with HEAD.
 1.18.10.2 16-Oct-2007  garbled Sync with HEAD
 1.18.10.1 03-Oct-2007  garbled Sync with HEAD
 1.18.2.3 03-Dec-2007  ad Sync with HEAD.
 1.18.2.2 20-Aug-2007  ad - Alter disk attach/detach to fix a panic when closing a vnd device.
- Sync with HEAD.
 1.18.2.1 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.20.8.1 14-Oct-2007  yamt sync with head.
 1.20.6.3 23-Mar-2008  matt sync with HEAD
 1.20.6.2 09-Jan-2008  matt sync with HEAD
 1.20.6.1 06-Nov-2007  matt sync with HEAD
 1.20.4.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.20.4.1 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.21.2.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.22.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.23.14.5 17-Jan-2009  mjf Sync with HEAD.
 1.23.14.4 28-Sep-2008  mjf Sync with HEAD.
 1.23.14.3 29-Jun-2008  mjf Sync with HEAD.
 1.23.14.2 02-Jun-2008  mjf Sync with HEAD.
 1.23.14.1 03-Apr-2008  mjf Sync with HEAD.
 1.23.10.1 24-Mar-2008  keiichi sync with head.
 1.26.4.5 11-Mar-2010  yamt sync with head
 1.26.4.4 19-Aug-2009  yamt sync with head.
 1.26.4.3 20-Jun-2009  yamt sync with head
 1.26.4.2 04-May-2009  yamt sync with head.
 1.26.4.1 16-May-2008  yamt sync with head.
 1.26.2.2 17-Jun-2008  yamt sync with head.
 1.26.2.1 18-May-2008  yamt sync with head.
 1.27.4.2 18-Jul-2008  simonb Sync with head.
 1.27.4.1 18-Jun-2008  simonb Sync with head.
 1.27.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.27.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.30.2.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.33.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.33.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.33.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.34.2.3 28-Sep-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1026):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.43 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.25
xbdback: implement and publish "feature-flush-cache".
xbd: if feature-flush-cache is present, use it for DIOCCACHESYNC.
If not present, make DIOCCACHESYNC return EOPNOTSUPP and warn on
first call.
Should improve WAPBL reliability of Xen guests on a NetBSD dom0.
Unfortunably not all linux guests seems to support this feature, and using
feature-write-barrier would require a B_BARRIER flag in the buffer.
 1.34.2.2 28-Sep-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1025):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.40
Implement DIOCGDISKINFO for xbd disk driver.
 1.34.2.1 08-Mar-2009  snj branches: 1.34.2.1.4;
Pull up following revision(s) (requested by bouyer in ticket #545):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.34
sys/arch/xen/xen/xbd_xenbus.c: revision 1.39
Fix rnd(4) support for Xen3 xennet
Add rnd(4) support to Xen3 xbd
Should fix port-xen/40739.
 1.34.2.1.4.1 21-Apr-2010  matt sync to netbsd-5
 1.38.2.12 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.38.2.11 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.38.2.10 26-May-2011  jym Split allocation and initialization of ring I/O for xbd(4) and xennet(4):
- allocation belongs to _attach()
- init to _resume(), so that it can be used by suspend/resume code too.
 1.38.2.9 02-May-2011  jym Sync with head.
 1.38.2.8 30-Mar-2011  jym Sync with my commits in HEAD.
 1.38.2.7 24-Oct-2010  jym Sync with HEAD
 1.38.2.6 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.38.2.5 01-Nov-2009  jym Sync with HEAD.
 1.38.2.4 23-Jul-2009  jym Sync with HEAD.
 1.38.2.3 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.38.2.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.38.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.44.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.44.4.1 21-Apr-2011  rmind sync with head
 1.48.2.3 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.48.2.2 30-Oct-2012  yamt sync with head
 1.48.2.1 17-Apr-2012  yamt sync with head
 1.49.2.3 02-Jun-2012  mrg sync to latest -current.
 1.49.2.2 24-Feb-2012  mrg sync to -current.
 1.49.2.1 18-Feb-2012  mrg merge to -current.
 1.53.2.1 03-Jun-2014  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1075):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.63 via patch
sys/dev/cgd.c: revision 1.87 via patch
sys/dev/dksubr.c: revision 1.50 via patch
sys/dev/dkvar.h: revision 1.19 via patch

Avoid xbd(4) reordering requests, which, depending on the underlying
hardware, can badly affect write performances. This can give up to a 5x
performance gain in sequencial writes.
 1.57.2.3 03-Dec-2017  jdolecek update from HEAD
 1.57.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.57.2.1 23-Jun-2013  tls resync from head
 1.59.2.1 18-May-2014  rmind sync with head
 1.62.2.3 10-Aug-2014  tls Rebase.
 1.62.2.2 08-Apr-2014  tls Type fixo.
 1.62.2.1 07-Apr-2014  tls Be a little more clear and consistent about harvesting entropy from devices:

1) deprecate RND_FLAG_NO_ESTIMATE

2) define RND_FLAG_COLLECT_TIME, RND_FLAG_COLLECT_VALUE

3) define RND_FLAG_ESTIMATE_TIME, RND_FLAG_ESTIMATE_VALUE

4) define RND_FLAG_DEFAULT: RND_FLAG_COLLECT_TIME|
RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_TIME

5) Make entropy harvesting from environmental sensors a little more generic
and remove it from individual sensor drivers.

6) Remove individual open-coded delta-estimators for values from a few
places in the tree (uvm, environmental drivers).

7) 0 -> RND_FLAG_DEFAULT, actually gather entropy from various drivers
that had stubbed out code, other minor cleanups.
 1.66.4.5 28-Aug-2017  skrll Sync with HEAD
 1.66.4.4 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.66.4.3 22-Sep-2015  skrll Sync with HEAD
 1.66.4.2 06-Jun-2015  skrll Sync with HEAD
 1.66.4.1 06-Apr-2015  skrll Sync with HEAD
 1.75.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.75.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.76.6.1 25-Aug-2018  martin Pull up following revision(s) (requested by jdolecek in ticket #984):

sys/arch/xen/xen/xbd_xenbus.c: revision 1.83

avoid race condition between I/O submission in xbd_diskstart() and
interrupt handling in xbd_handler() - need to protect it with splbio()

fixes PR port-xen/53506 by Emmanuel Dreyfus, and likely also port-xen/53074
by Brad Spencer
 1.78.2.4 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.78.2.3 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.78.2.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.78.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.79.2.4 21-Apr-2020  martin Sync with HEAD
 1.79.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.79.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.79.2.1 10-Jun-2019  christos Sync with HEAD
 1.92.4.1 21-Mar-2020  martin Pull up following revision(s) (requested by riastradh in ticket #795):

sys/dev/dksubr.c: revision 1.112
sys/arch/xen/xen/xbd_xenbus.c: revision 1.95
sys/dev/scsipi/sd.c: revision 1.328
sys/dev/dkvar.h: revision 1.32
sys/dev/ld.c: revision 1.109
sys/dev/cgd.c: revision 1.120
sys/dev/raidframe/rf_netbsdkintf.c: revision 1.380
sys/dev/ata/wd.c: revision 1.458

Add a flag to dk_dump for virtual disk devices.

If a disk is backed by a physical medium other than itself, such as
cgd(4), then it passes DK_DUMP_RECURSIVE to disable the recursion
detection for dk_dump.

If, however, a device represents a physical medium on its own, such
as wd(4), then it passes 0 instead.

With this, I can now dump to dk on cgd on dk on wd.
 1.97.2.3 25-Apr-2020  bouyer Sync with bouyer-xenpvh-base2 (HEAD)
 1.97.2.2 20-Apr-2020  bouyer Sync with HEAD
 1.97.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.129.20.3 04-Sep-2023  martin Actually apply a cherry picked version of

sys/arch/xen/xen/xbd_xenbus.c rev 1.130

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.

Part of ticket #268 (and already noted in that CHANGES-10 entry, but
somehow lost during pullup)
 1.129.20.2 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.129.20.1 27-Jul-2023  martin Pull up following revision(s) (requested by bouyer in ticket #248):

sys/arch/xen/xen/xbd_xenbus.c: revision 1.132 (patch)
sys/arch/xen/xen/xbd_xenbus.c: revision 1.133 (patch)
sys/arch/xen/xen/xbd_xenbus.c: revision 1.134 (patch)

The disk size reported in the xenstore is always in XEN_BSIZE units,
not sector-size. Should fix the issue reported by Christian Kujau
on netbsd-users and port-xen.

Also use XEN_BSIZE when computing the number of bytes for format_bytes().
While there note in a comment that sc_sectors is in XEN_BSIZE units

Propoerly handle 4k sector size backends:
- report the backend's sector size to upper layers, not DEV_BSIZE.
Adjust the number of sectors accordingly.
- Use sc_secsize instead of XEN_BSIZE where appropriate. The sectors numbers
in I/O requests are still in XEN_BSIZE units, but must be a multiple
of sc_secsize/XEN_BSIZE.
- As a consequence of previous, the buffer has to be aligned to sc_secsize,
aligned to XEN_BSIZE may not be enough. This means that we may have to
xbd_map_align() more buffer, including some without B_PHYS set.
- Add some more DPRINTF lines, related to I/O requests

Tested with a linux dom0.

thanks to Christian Kujau for providing access to his hardware for testing
and debugging.
 1.134.6.1 02-Aug-2025  perseant Sync with HEAD
 1.40 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.39 21-Jan-2009  pgoyette branches: 1.39.2;
Update format strings now that DISKPART() and DISKNUMBER() are back to
32-bit.
 1.38 21-Jan-2009  bouyer minor() is 32bit again, and so is DISKNUMBER/DISKPART. Pointed out by
Paul Goyette, thanks !
 1.37 16-Jan-2009  jym 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.36 13-Jan-2009  taca Fix compile error: one more sys/arch/xen/xen/xbd.c and xbdback.c.
 1.35 17-Nov-2008  cegger build fix: include <sys/buf.h>
 1.34 21-Oct-2008  cegger branches: 1.34.2;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.33 24-Aug-2008  bouyer Defer xbdback_do_io() to a workqueue; which allow us to take the
xbd_io->xio_buf.b_vp->v_interlock mutex before v_numoutput++;
which should fix the "vwakeup: neg numoutput" panic.
 1.32 16-Apr-2008  cegger branches: 1.32.4; 1.32.6; 1.32.10;
device_t / softc split
reviewed, tested and approved by bouyer
 1.31 22-Mar-2008  ad vn_close no longer takes a 'struct lwp' argument.
 1.30 17-Feb-2008  bouyer branches: 1.30.6;
Add missing __KERNEL_RCSID()
 1.29 02-Jan-2008  ad Merge vmlocking2 to head.
 1.28 26-Nov-2007  pooka branches: 1.28.2; 1.28.6;
Remove the "struct lwp *" argument from all VFS and VOP interfaces.
The general trend is to remove it from all kernel interfaces and
this is a start. In case the calling lwp is desired, curlwp should
be used.

quick consensus on tech-kern
 1.27 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.26 17-Oct-2007  garbled branches: 1.26.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.25 29-Jul-2007  ad branches: 1.25.4; 1.25.6; 1.25.10;
It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.24 09-Jul-2007  ad branches: 1.24.2; 1.24.4;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.23 12-Mar-2007  ad branches: 1.23.2; 1.23.8;
Pass an ipl argument to pool_init/POOL_INIT to be used when initializing
the pool's lock.
 1.22 14-Nov-2006  bouyer branches: 1.22.2; 1.22.4; 1.22.8; 1.22.10;
vn_lock() the vnode we got from bdevvp(), otherwise if something else uses
this device in userland (e.g. qemu-dm when running a HVM guest), we'll reuse
the existing vnode and we'll panic in VOP_UNLOCK(). We don't have this issue
most of the time because when xbdback is the only user, we get a specfs
vnode for which locking operation are NOPs.

Thanks to Antti Kantee for spotting the missing vn_lock() in sources and
giving details about vnode locking.
 1.21 15-May-2006  yamt branches: 1.21.8; 1.21.10;
include kauth.h for NOCRED.
 1.20 27-Apr-2006  jld Put the "excessively fragmented I/O" warning printf under #ifdef DEBUG,
where it belonged in the first place.
 1.19 23-Jan-2006  yamt branches: 1.19.2; 1.19.4; 1.19.6; 1.19.8; 1.19.10;
merge xen pmap_enter, pmap_enter_ma, pmap_remap_pages.
ok'ed by Manuel Bouyer.
 1.18 24-Dec-2005  perry branches: 1.18.2;
Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
 1.17 11-Dec-2005  christos merge ktrace-lwp.
 1.16 20-Sep-2005  bouyer When disconnecting a device, we have to wait for pending I/O before releasing
the ressources (this can lead to a dom0 kernel crash when destroying a domain
with active I/O going). For this, add a refcount to struct xbdback_instance,
and defer CMSG_BLKIF_BE_DISCONNECT reply until refcount is back to 0 (which
means all queued buffers have completed).
Based on patch sent by Jed Davis on port-xen.
 1.15 10-Sep-2005  bouyer Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.14 19-Jul-2005  yamt fix a race by re-checking req_prod when exiting main loop.
 1.13 19-Jul-2005  yamt xbdback_co_main_loop: don't forget to set xbdi->cont in the case of error.
 1.12 17-Jul-2005  tls Improvements to xbdback (the domain 0 driver that provides "xbd" virtual
disks to other domains) from Jed Davis, <jld@panix.com>:

* Issue multiple requests when necessary rather than
assuming that arbitrary requests can be mapped into single
contiguous virtual address ranges.

* Don't assume that all data for a request is consecutive
in memory. With some client OSes, it's not.

The above two changes fix data corruption issues with Linux
clients with certain filesystem block sizes.

* Gracefully handle memory or pool allocation failures after
beginning to handle a request from the ring.

* Merge contiguous requests to avoid the "64K turns into 44K + 20K
and doubles the transactions per second at the disk" problem
caused by the 11-page limit caused by the structure of Xen
ring entries. This causes a very slight performance decrease
for sequential 64K I/O if the disk is not already saturated with
requests (about 1%) but halves the transactions per second we
hit the disk with -- or better. It even compensates for bizarre
Linux behaviour like breaking long requests up into 5.5K pieces.

* Probably some stuff I forgot to mention.

Disk throughput (though not latency) is now much, much closer to the
"raw hardware" case than it was before.
 1.11 18-Apr-2005  bouyer branches: 1.11.2; 1.11.4;
If VOP_IOCTL fails on the device we're trying to export, return an error
instead of dereferencing a NULL pointer later.
Problem reported by Martti Kuparinen in private email.
 1.10 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.9 16-Apr-2005  bouyer Remove functions that just call another one.
Inline a function used at only one place.
 1.8 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.7 01-Apr-2005  yamt branches: 1.7.2;
merge yamt-km branch.
- don't use managed mappings/backing objects for wired memory allocations.
save some resources like pv_entry. also fix (most of) PR/27030.
- simplify kernel memory management API.
- simplify pmap bootstrap of some ports.
- some related cleanups.
 1.6 31-Mar-2005  bouyer Use DEV_STRATEGY() instead of VOP_STRATEGY(). YAMAMOTO Takashi says it's not
safe to use VOP_STRATEGY() from interrupt context.
 1.5 26-Mar-2005  bouyer Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.4 15-Mar-2005  bouyer branches: 1.4.2; 1.4.4;
Add more DIAGNOSTIC checks
fix test of xbdback_map_shm() return value.
 1.3 10-Mar-2005  bouyer Use the same type as xen in the request descriptor.
Change the logic in the request processing loop so that we don't
think the ring is empty when is has BLKIF_RING_SIZE requests.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 16-Feb-2005  bouyer branches: 1.1.2;
file xbdback.c was initially added on branch bouyer-xen2.
 1.1.2.4 09-Mar-2005  bouyer Use a pool for request descriptors instead of a linked list.
As requests can complete out of order, don't keep a pointer to the request
in the ring, the descriptor can be overwritten by a reply. Copy the required
infos in our request descriptor instead.
 1.1.2.3 08-Mar-2005  bouyer Improve ressource shortage recovery: if xen_shm_map() fails, register a
callback wich will retry the I/O when xen_shm_unmap() is called.
 1.1.2.2 08-Mar-2005  bouyer When a virtual device is destroyed (e.g. because a domain is destroyed),
properly free ressources and unregister callbacks.
 1.1.2.1 16-Feb-2005  bouyer Work in progress on block device backend. It's enouth to have a vbd
attached in a guest OS, and to mount a filesystem.
TODO:
- deal with ressources shortages
- properly release ressources on domain shutdown
- load balancing
Also, this implementation doesn't deal with multiple physical device per
vbd, but it should be easy to add if we really want it.
 1.4.4.3 19-Mar-2005  yamt convert to the new API.
 1.4.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.4.4.1 15-Mar-2005  yamt file xbdback.c was added on branch yamt-km on 2005-03-19 08:33:26 +0000
 1.4.2.11 20-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1104):
sys/arch/xen/xen/privcmd.c: revision 1.6
sys/arch/xen/i386/pmap.c: revision 1.14
sys/arch/xen/include/pmap.h: revision 1.5
sys/arch/xen/xen/xennetback.c: revision 1.12 via patch
sys/arch/xen/xen/xbdback.c: revision 1.15
Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.4.2.10 11-Jan-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1106):
sys/arch/xen/xen/xbdback.c: revision 1.16 via patch
When disconnecting a device, we have to wait for pending I/O before releasing
the ressources (this can lead to a dom0 kernel crash when destroying a domain
with active I/O going). For this, add a refcount to struct xbdback_instance,
and defer CMSG_BLKIF_BE_DISCONNECT reply until refcount is back to 0 (which
means all queued buffers have completed).
Based on patch sent by Jed Davis on port-xen.
 1.4.2.9 21-Aug-2005  tron branches: 1.4.2.9.2;
Pull up revision 1.14 (requested by yamt in ticket #682):
fix a race by re-checking req_prod when exiting main loop.
 1.4.2.8 21-Aug-2005  tron Pull up revision 1.13 (requested by yamt in ticket #681):
xbdback_co_main_loop: don't forget to set xbdi->cont in the case of error.
 1.4.2.7 15-Aug-2005  tron Pull up revision 1.12 (requested by tls in ticket #656):
Improvements to xbdback (the domain 0 driver that provides "xbd" virtual
disks to other domains) from Jed Davis, <jld@panix.com>:
* Issue multiple requests when necessary rather than
assuming that arbitrary requests can be mapped into single
contiguous virtual address ranges.
* Don't assume that all data for a request is consecutive
in memory. With some client OSes, it's not.
The above two changes fix data corruption issues with Linux
clients with certain filesystem block sizes.
* Gracefully handle memory or pool allocation failures after
beginning to handle a request from the ring.
* Merge contiguous requests to avoid the "64K turns into 44K + 20K
and doubles the transactions per second at the disk" problem
caused by the 11-page limit caused by the structure of Xen
ring entries. This causes a very slight performance decrease
for sequential 64K I/O if the disk is not already saturated with
requests (about 1%) but halves the transactions per second we
hit the disk with -- or better. It even compensates for bizarre
Linux behaviour like breaking long requests up into 5.5K pieces.
* Probably some stuff I forgot to mention.
Disk throughput (though not latency) is now much, much closer to the
"raw hardware" case than it was before.
 1.4.2.6 28-Apr-2005  tron Pull up revision 1.10 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.4.2.5 28-Apr-2005  tron Pull up revision 1.9 (requested by bouyer in ticket #192):
Remove functions that just call another one.
Inline a function used at only one place.
 1.4.2.4 28-Apr-2005  tron Pull up revision 1.8 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4.2.3 25-Apr-2005  tron Pull up revision 1.11 (requested by bouyer in ticket #189):
If VOP_IOCTL fails on the device we're trying to export, return an error
instead of dereferencing a NULL pointer later.
Problem reported by Martti Kuparinen in private email.
 1.4.2.2 21-Apr-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #170):
Use DEV_STRATEGY() instead of VOP_STRATEGY(). YAMAMOTO Takashi says it's not
safe to use VOP_STRATEGY() from interrupt context.
 1.4.2.1 30-Mar-2005  tron Pull up revision 1.5 (requested by bouyer in ticket #72):
Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.4.2.9.2.1 11-Jan-2006  tron Pull up following revision(s) (requested by bouyer in ticket #1106):
sys/arch/xen/xen/xbdback.c: revision 1.16 via patch
When disconnecting a device, we have to wait for pending I/O before releasing
the ressources (this can lead to a dom0 kernel crash when destroying a domain
with active I/O going). For this, add a refcount to struct xbdback_instance,
and defer CMSG_BLKIF_BE_DISCONNECT reply until refcount is back to 0 (which
means all queued buffers have completed).
Based on patch sent by Jed Davis on port-xen.
 1.7.2.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.7.2.2 01-Apr-2005  skrll Sync with HEAD.
 1.7.2.1 01-Apr-2005  skrll file xbdback.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.11.4.7 24-Mar-2008  yamt sync with head.
 1.11.4.6 27-Feb-2008  yamt sync with head.
 1.11.4.5 21-Jan-2008  yamt sync with head
 1.11.4.4 07-Dec-2007  yamt sync with head
 1.11.4.3 03-Sep-2007  yamt sync with head.
 1.11.4.2 30-Dec-2006  yamt sync with head.
 1.11.4.1 21-Jun-2006  yamt sync with head.
 1.11.2.2 29-Apr-2005  kent sync with -current
 1.11.2.1 18-Apr-2005  kent file xbdback.c was added on branch kent-audio2 on 2005-04-29 11:28:30 +0000
 1.18.2.1 01-Feb-2006  yamt sync with head.
 1.19.10.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.19.8.1 11-May-2006  elad sync with head
 1.19.6.1 24-May-2006  yamt sync with head.
 1.19.4.1 01-Jun-2006  kardel Sync with head.
 1.19.2.1 09-Sep-2006  rpaulo sync with head
 1.21.10.1 10-Dec-2006  yamt sync with head.
 1.21.8.1 18-Nov-2006  ad Sync with head.
 1.22.10.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.22.8.4 03-Dec-2007  ad Sync with HEAD.
 1.22.8.3 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.22.8.2 15-Jul-2007  ad Sync with head.
 1.22.8.1 13-Mar-2007  ad Sync with head.
 1.22.4.1 24-Mar-2007  yamt sync with head.
 1.22.2.1 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.23.8.1 03-Oct-2007  garbled Sync with HEAD
 1.23.2.1 11-Jul-2007  mjf Sync with head.
 1.24.4.1 15-Aug-2007  skrll Sync with HEAD.
 1.24.2.1 07-Aug-2007  matt Sync with HEAD.
 1.25.10.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.25.6.3 23-Mar-2008  matt sync with HEAD
 1.25.6.2 09-Jan-2008  matt sync with HEAD
 1.25.6.1 06-Nov-2007  matt sync with HEAD
 1.25.4.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.26.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.26.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.28.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.28.2.1 01-Jan-2008  ad Make it build. XXX does not use getiobuf/putiobuf.
 1.30.6.4 17-Jan-2009  mjf Sync with HEAD.
 1.30.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.30.6.2 02-Jun-2008  mjf Sync with HEAD.
 1.30.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.32.10.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.32.10.1 19-Oct-2008  haad Sync with HEAD.
 1.32.6.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.32.4.2 19-Aug-2009  yamt sync with head.
 1.32.4.1 04-May-2009  yamt sync with head.
 1.34.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.34.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.39.2.1 01-Nov-2009  jym Sync with HEAD.
 1.107 20-Jun-2024  bouyer Fix various cosmetic issues pointed out by Roland Illig. NFC.
While there add 2024 to copyright.
 1.106 19-Jun-2024  martin Fix format string for size_t argument
 1.105 19-Jun-2024  bouyer Restore "sparse" segements support which was lost in rev 1.83, causing
VBD corruption with linux guests.
The segments in a single request are not always contigous in VA; this means
that the end of a segment is not always 7 and the start of the next one is not
always 0. When this happens this means that a contigous chunk of data from
disk has to be dispatched to various non-contigous VA, in chunks of VBD_BSIZE
bytes (or the other way round for writes).
Linux I/O subsystems seems to support this natively; to emulate this allocate
a MAXPHYS bounce buffer to do the I/O and then memcpy() the data from/to
the segments as requested. If the request is contigous do the I/O
directly to the mapped VA.

This means that we need to keep segments details until iodone(); so move
the blkif_request_segment array from xbdback_instance to xbdback_io. The
array is allocated separately to guarantee proper page alignement.

non-contigous segments seems rare so allocate one bounce buffer per
xbdback_instance, and stall the ring if the bounce buffer is already in use.
For this add back a mechanism to restart an I/O at a specific point
after thread sleep/wakeup.

While there guard some more printfs with ratecheck() and add more checks on
segments bounds.

Tested with a HVM scientific linux install from iso image; the install would
fail with a xfs corruption when installing grub.
 1.104 13-Jun-2024  bouyer Get req->handle and req->sector_number from the correct structure for the
current opertation. They happen to be at the same offset in all structures
so NFC, but better be correct, it may help for future changes.
While there KASSERT() that our indirect segments fits in a single page.
 1.103 25-Feb-2023  riastradh xbdback(4): Nix trailing whitespace.

No functional change intended.
 1.102 25-Feb-2023  riastradh 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.
 1.101 01-Sep-2022  bouyer branches: 1.101.4;
in backend drivers, use xen_shm_(un)map for the rings instead of inline,
mostly duplicate code.
 1.100 01-Sep-2022  bouyer Add PVH support for backend drivers grant operation.
Now a domU in a PVH dom0 boots multiuser.
 1.99 28-Jul-2021  jdolecek fix off-by-one check in another KASSERT() for bcount

still related to PR port-xen/56328
 1.98 28-Jul-2021  jdolecek fix intentional, but eventually faulty off-by-one for the maximum number
of segments for I/O - this was supposed to allow MAXPHYS-size I/O even
with page offset, but actually ended up letting through I/O up to
MAXPHYS+PAGE_SIZE

the KASSERT(bcount < MAXPHYS) is kept as-is, since at that place the number
of segments should already be validated, so it's kernel bug if the size
is still too big there

fixes PR port-xen/56328 by Greg Oster
 1.97 21-Feb-2021  jdolecek branches: 1.97.4;
reset xio_xv before calling xbdback_io_error() so it won't try to unmap
the non-initialized handles when xen_shm_map() fails

remove the ratechecked error printf for shm error - xbdback_io_error() prints
the error too

part of fixes for XSA-362
 1.96 07-May-2020  bouyer branches: 1.96.2;
This should be mpsafe, register the event handler as such.
 1.95 06-May-2020  bouyer Drop xbdi_lock() before calling intr_disestablish(), it needs to take
cpu_lock.
 1.94 06-May-2020  bouyer We can't take v_interlock with a spin lock held. Release xbdi_lock
before scheduling the I/O, and take again once queued.
It looks safe to drop the lock here.
 1.93 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.92 25-Apr-2020  jdolecek parse indirect_op using appropriate 32/64-bit access types, this fixes
the BLKIF_OP_INDIRECT handling for 32-bit DomU running against 64-bit Dom0,
problem reported and fix tested by Manuel

make sure to use the provided indirect_op when reporting back errors
 1.91 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.90 23-Apr-2020  jdolecek make xbdback actually MPSAFE and stop using KERNEL_LOCK()

remove no longer necessary atomics, the counters are now always
updated with held mutex
 1.89 23-Apr-2020  jdolecek allocate xbdback_io instances as part of struct xbdback_instance and
stop using pool_cache(9), to remove possibility for the allocation to sleep
 1.88 23-Apr-2020  jdolecek g/c no longer used xbdi_same_page and xbdi_lastfragio_time
 1.87 23-Apr-2020  jdolecek g/c no longer needed xbdi_io structure member, just pass it as continuation
parameter
 1.86 21-Apr-2020  jdolecek add support for indirect segments, which makes it possible to pass
up to MAXPHYS (implementation limit, interface allows more) using
single request

request using indirect segment requires 1 extra copy hypercall per
request, but saves 2 shared memory hypercalls (map_grant/unmap_grant),
so should be net performance boost due to less TLB flushing

this also effectively doubles disk queue size for xbd(4)
 1.85 20-Apr-2020  jdolecek xbdback_fragio_intvl is also no longer used
 1.84 20-Apr-2020  bouyer Fix build with DIAGNOSTIC
 1.83 20-Apr-2020  jdolecek remove I/O defragmentation logic, pass requests straight to the
underlying block device without trying to coalesce them

it seems rarely useful, and it makes the handling logic unnecessarily complex -
ultimately it's the DomU operating system responsibility to issue optimal I/O

might also help with the ZFS problem reported on port-xen, and will surely
simplify eventual indirect segment support
 1.82 20-Apr-2020  jdolecek do not retry when xen_shm_map() fails after having returned error for it,
it corrupts the ring; just go to next request

should fix the problem with ZFS causing failscade when req fails,
reported by Brian Buhrow on port-xen
 1.81 20-Apr-2020  msaitoh Fix typo to make compilable.
 1.80 19-Apr-2020  jdolecek SLIST_INIT() xbdi->xbdi_va_free just for clarity
 1.79 19-Apr-2020  jdolecek change interface for xen_shm_map() so that caller always supplies the VA,
it now fails only if the Xen hypercall fails, in which case the failure
is final

change xbdback to pre-allocate KVA on xbdback attach (and free on detach),
so it has always KVA to map the request pages

remove no longer needed KVA allocation failure handling
 1.78 13-Apr-2020  chs slightly change and fix the semantics of pool_set*wat(), pool_sethardlimit()
and pool_prime() (and their pool_cache_* counterparts):

- the pool_set*wat() APIs are supposed to specify thresholds for the count of
free items in the pool before pool pages are automatically allocated or freed
during pool_get() / pool_put(), whereas pool_sethardlimit() and pool_prime()
are supposed to specify minimum and maximum numbers of total items
in the pool (both free and allocated). these were somewhat conflated
in the existing code, so separate them as they were intended.

- change pool_prime() to take an absolute number of items to preallocate
rather than an increment over whatever was done before, and wait for
any memory allocations to succeed. since pool_prime() can no longer fail
after this, change its return value to void and adjust all callers.

- pool_setlowat() is documented as not immediately attempting to allocate
any memory, but it was changed some time ago to immediately try to allocate
up to the lowat level, so just fix the manpage to describe the current
behaviour.

- add a pool_cache_prime() to complete the API set.
 1.77 07-Apr-2020  jdolecek branches: 1.77.2;
convert the node watch code to use kmem_alloc() instead of malloc()
 1.76 07-Apr-2020  jdolecek revert the watch.node change, xenbus_watch_path() and xenbus_watch_path2()
need to use non-constant path
 1.75 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.74 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.73 17-Jan-2020  ad Acquire kernel_lock in the bp->b_iodone callback.
 1.72 07-Apr-2019  bouyer branches: 1.72.6;
Adjyst some debug printfs
 1.71 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.70 24-Dec-2018  cherry 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.69 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.68 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.67 24-Jun-2018  jdolecek branches: 1.67.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.66 24-Jun-2018  jdolecek similar treatment as xennetback_xenbus.c:
- protect instance list with mutex
- mark more local variables static
- mark with XXXSMP what looks suspicious
- in pciback.c use kmem_zalloc() et.al to allocate the device structures
 1.65 11-Nov-2017  riastradh branches: 1.65.2;
Restore MP-safety annotations in intr_establish_xname.

In the old API, event_set_handler would assume MP-unsafe for IPL_VM
and MP-safe for all other levels (IPL_SCHED, IPL_HIGH). The recent
Xen interrupt rototill started passing known_mpsafe=true for _all_
interrupt handlers. Change it to known_mpsafe=false for the IPL_VM
(= IPL_CLOCK, IPL_NET) ones.
 1.64 06-Nov-2017  cherry Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.63 26-Dec-2016  skrll branches: 1.63.6; 1.63.8;
Hold the interlock before cv_broadcast as per condvar(9)
 1.62 06-Jan-2016  bouyer branches: 1.62.2;
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.61 15-Nov-2015  bouyer Fix typo which caused the kenrel thread to be created with a 0 priority.
This would cause the thread to be almost never scheduled when a userland
process could use all CPU.
Should fix the problem reported by Torbj�rn Granlund on port-xen@
 1.60 21-Sep-2014  bouyer branches: 1.60.2;
Make Xen kernels compile without DIAGNOSTIC
 1.59 06-Nov-2013  mrg branches: 1.59.4;
- move variables inside their #ifdef use
- remove unused and set-but-unused variables
- use __USE() in a particularly ugly case

with these, and a couple of other changes, amd64 gcc 4.8.1 world
is able to complete build.sh release.
 1.58 20-Oct-2013  bouyer Add more XENPRINTF() to xbdback_connect()
in xbdback_backend_changed(), fix memory leak.
Do not free an uninitialized pointer in xbdback_connect(). Should fix
hypervisor or dom0 reboot when using windows PV drivers, as reported
by several users on port-xen.
 1.57 23-Jul-2012  jym branches: 1.57.2; 1.57.4;
Add more ratechecks to avoid console spam when the backend gets a
stream of errors.

Remove the unused xbdi_errps (error per second) variable. Errors should
rather be tracked in absolute, with a threshold that forces a time penalty
to the xbdback thread when frontend goes nuts.
 1.56 23-May-2012  cegger destroy mutex and cv first.
Fixes LOCKDEBUG crash when a guest shut down.
 1.55 07-Dec-2011  cegger branches: 1.55.2;
switch from xen3-public to xen-public.
 1.54 03-Dec-2011  bouyer branches: 1.54.2;
xbdback_disconnect() can be called twice, from XenbusStateClosing then from
xbdback_xenbus_destroy(). The second call will wait forever as the first
already caused the xbd thread to exit.
Have xbdback_disconnect() check if we're already disconnected and if so,
do nothing.
 1.53 24-Nov-2011  joerg Don't use variables as format string.
 1.52 24-Nov-2011  jym Deep rework of the xbdback(4) driver; it now uses a thread per instance
instead of continuations directly from shm callbacks or interrupt
handlers. The whole CPS design remains but is adapted to cope with
a thread model.

This patch allows scheduling away I/O requests of domains that behave
abnormally, or even destroy them if there is a need to (without thrashing
dom0 with lots of error messages at IPL_BIO).

I took this opportunity to make the driver MPSAFE, so multiple instances
can run concurrently. Moved from home-grown pool(9) queues to
pool_cache(9), and rework the callback mechanism so that it delegates
I/O processing to thread instead of handling it itself through the
continuation trampoline.

This one fixes the potential DoS many have seen in a dom0 when trying to
suspend a NetBSD domU with a corrupted I/O ring.

Benchmarks (build.sh release runs and bonnie++) do not show any
performance regression, the "new" driver is on-par with the "old" one.

ok bouyer@.
 1.51 14-Nov-2011  christos Use getdiskinfo() to print the name of the device; the previous code
constructed the wrong name if it was a wedge.
 1.50 14-Nov-2011  hannken Bring back sys/disklabel.h for DISKUNIT and DISKPART.
 1.49 13-Nov-2011  christos use getdiskinfo()
 1.48 25-Oct-2011  bouyer branches: 1.48.2;
Fix bogus KASSERT: if there is a xbdi_io, xbdi_pendingreqs must *NOT* be 0.
Not sure why it has stayed unoticed for so long ...
 1.47 24-Oct-2011  jym Move disconnection code to a separate function, similar to what is done
with xbdback_connect.
 1.46 24-Aug-2011  jym Protect xbdback(4) ring indexes from overflowing; leave the continuation
prematurely in case they do, to avoid looping "endlessly" (or at least
a very long time) at IPL_BIO while trying to handle requests.

This should not happen in a nominal scenario, but the ring can get
corrupted for whatever reason (memory errors, domU failures or
exploitation).
 1.45 07-Aug-2011  bouyer Guard against spurious xbdback_backend_changed() calls which would result
in the block device being opened twice. Fixes port-xen/45158,
although the underlying cause (multiple open of the same device not
properly handled any more) is not fixed.
 1.44 07-Aug-2011  bouyer Add a comment explaing why a flush workqueue is handled differently from
read/write workqueue requests.
 1.43 07-Aug-2011  bouyer Several fixes to the continuation engine:
- make sure to enter the continuation loop at splbio(), and add some
KASSERT() for this.
- When a flush operation is enqueued to the workqueue, make sure the
continuation loop can't be restarted by a previous workqueue
completion or an event. We can't restart it at this point because
the flush even is still recorded as the current I/O.
For this add a xbdback_co_cache_doflush_wait() which acts as a noop;
the workqueue callback will restart the loop once the flush is complete.
Should fix "kernel diagnostic assertion xbd_io->xio_mapped == 0" panics
reported by Jeff Rizzo on port-xen@.
 1.42 04-Aug-2011  bouyer Make sure to call xbdback_trampoline() at splbio()
 1.41 24-Jul-2011  jym Add more comments to xbdback(4) code. These make the continuations a bit
easier to follow (and understand). Helped tracking down a regression
between save/restore xbdback(4) states.

A few minor fixes, which are merely cosmetic:
- call graph is (somewhat) more readable
- rework the xbdback_do_io routine with a switch statement, so as to
trigger a panic() in case an invalid operation passed through the sanity
checks. panic might be overkill here, but I am sure to catch errrors in
case it happens.
 1.40 12-Jun-2011  rmind Welcome to 5.99.53! Merge rmind-uvmplock branch:

- Reorganize locking in UVM and provide extra serialisation for pmap(9).
New lock order: [vmpage-owner-lock] -> pmap-lock.

- Simplify locking in some pmap(9) modules by removing P->V locking.

- Use lock object on vmobjlock (and thus vnode_t::v_interlock) to share
the locks amongst UVM objects where necessary (tmpfs, layerfs, unionfs).

- Rewrite and optimise x86 TLB shootdown code, make it simpler and cleaner.
Add TLBSTATS option for x86 to collect statistics about TLB shootdowns.

- Unify /dev/mem et al in MI code and provide required locking (removes
kernel-lock on some ports). Also, avoid cache-aliasing issues.

Thanks to Andrew Doran and Joerg Sonnenberger, as their initial patches
formed the core changes of this branch.
 1.39 07-Jun-2011  jym Be more consistent for event handler naming with block backend: it is
xbdback(4) rather than xbd(4), and use i for identifier separation
(like xvif(4)).

The name is not used outside from event counters (vmstat -i), so
should be transparent to Xen block scripts.
 1.38 26-May-2011  jym branches: 1.38.2;
Reuse the pointer to the request operation, as set above.
 1.37 21-May-2011  jym In xbdback(4), move the code that copies segments after the bound checks
of the ``nr_segments'' variable.

In cases where we are running domUs with an architecture different from the
dom0 one (for example: 32 bits domUs on 64 bits dom0), copying segments
with an invalid nr_segments value will lead to the corruption of the
xbdback instance structure and quickly crash the dom0 backend.

Tested under 64 bits dom0 with 32 bits domUs. No regression observed.

ok bouyer@.

Will be pulled up to -4 and -5.
 1.36 15-May-2011  jym As noted by rmind@, use the _nv() to fetch the new value. A race is
possible between the decrement and the fetch of the ref counter value,
hence we might call the G/C routine twice. Not good.

Also remove the 'volatile' attribute, refcnt is only use by xbdi_put/_get
and should not be exposed anywhere else (except for initialization).
 1.35 15-May-2011  jym Use atomic_ops(3) for ref counting.
 1.34 29-Apr-2011  jym Move the connection code of xbdback(4) and xvif(4) backends in separate
functions. The frontend watch function is easier to read, and mixing
switch() with goto's error paths is rather error-prone.

While here, sprinkle some aprint_*.

Tested under amd64 dom0 with i386 PAE and amd64 domUs.
 1.33 05-Mar-2011  bouyer Test and set xbdi->xbdi_cont at splbio(). Otherwise we could overwrite
xbdi->xbdi_cont and process the same request twice.
 1.32 24-Jun-2010  hannken branches: 1.32.2;
Clean up vnode lock operations pass 2:

VOP_UNLOCK(vp, flags) -> VOP_UNLOCK(vp): Remove the unneeded flags argument.

Welcome to 5.99.32.

Discussed on tech-kern.
 1.31 24-Jan-2010  haad branches: 1.31.2; 1.31.4;
Finaly fix problems with using WEDGE like devices as xen backend devices.
 1.30 17-Jan-2010  haad Fix problem where xbdi->xbdi_size was set to 0 after succesfull DIOCGWEDGEINFO
call. Problem reported in private mail.
 1.29 15-Dec-2009  haad Fix problem with using wedge like devicesi(LVM Logical Volumes, ZFS Zvols)
as xkbd backend. This problem was reported by Hugo Silva on port-xen.

Now we call DIOCGWEDGEINFO for all partitions, when it is not implemented
we use DIOCGPART to get information about volume size.

Fix oked by jym@.
 1.28 25-Oct-2009  bouyer When we stall the continuation waiting for the queued I/O to complete
in xbdback_co_cache_flush2(), we don't advance the req pointer. In this
case call xbdi_put() to compensate for the xbdi_get() that will be done again
in xbdback_co_main_loop().
Fix stalled xbdback detach that would stall the whole xenbus thread,
preventing new domU from be created.
 1.27 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.26 14-Oct-2009  bouyer Don't pass a NULL arg to DIOCCACHESYNC, sd(4) dereferences it.
Pass a pointer to a int initialized to 1, so that a DIOCCACHESYNC will be
forced on sd(4).
Should fix dom0 panic reported by Sarton O'Brien.
 1.25 23-Sep-2009  bouyer xbdback: implement and publish "feature-flush-cache".
xbd: if feature-flush-cache is present, use it for DIOCCACHESYNC.
If not present, make DIOCCACHESYNC return EOPNOTSUPP and warn on
first call.
Should improve WAPBL reliability of Xen guests on a NetBSD dom0.
Unfortunably not all linux guests seems to support this feature, and using
feature-write-barrier would require a B_BARRIER flag in the buffer.
 1.24 21-Jan-2009  cegger branches: 1.24.2;
buildfix: re-adapt to major()/minor() returning a 32bit value.
 1.23 16-Jan-2009  jym 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.22 12-Jan-2009  cegger use PRI macro formats to printf type dev_t
 1.21 17-Nov-2008  cegger build fix: include <sys/buf.h>
 1.20 24-Oct-2008  jym branches: 1.20.2; 1.20.4; 1.20.6; 1.20.8;
- 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.19 16-Sep-2008  bouyer Fix typos and pasto, from Jean-Yves Migeon (jean-yves dot migeon at espci
dot fr). Luckily this pasto is fatal only in case of a 64bit domU on a
32bit dom0, which we don't support yet as dom0.
 1.18 24-Aug-2008  bouyer Defer xbdback_do_io() to a workqueue; which allow us to take the
xbd_io->xio_buf.b_vp->v_interlock mutex before v_numoutput++;
which should fix the "vwakeup: neg numoutput" panic.
 1.17 13-Jun-2008  cegger branches: 1.17.2;
correct spelling in error message
 1.16 22-Mar-2008  ad branches: 1.16.2; 1.16.4; 1.16.6; 1.16.8;
vn_close no longer takes a 'struct lwp' argument.
 1.15 13-Mar-2008  bouyer 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.14 17-Feb-2008  bouyer branches: 1.14.2; 1.14.6;
Add missing __KERNEL_RCSID()
 1.13 06-Jan-2008  bouyer Fix fallout from vmlocking2: in xbdback_co_io_loop(), return the right object.
 1.12 02-Jan-2008  ad Merge vmlocking2 to head.
 1.11 26-Nov-2007  pooka branches: 1.11.2; 1.11.6;
Remove the "struct lwp *" argument from all VFS and VOP interfaces.
The general trend is to remove it from all kernel interfaces and
this is a start. In case the calling lwp is desired, curlwp should
be used.

quick consensus on tech-kern
 1.10 25-Nov-2007  bouyer Ops, don't forget break in switch statements ...
 1.9 25-Nov-2007  bouyer Add support for the "protocol" entry in xenstore, which really specifies
the ABI used by the front-end (x86 32 or 64 bit). A linux 32bit PAE kernel
can now boot on a NetBSD 64bit dom0 and use the block devices.
 1.8 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.7 17-Oct-2007  garbled branches: 1.7.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.6 29-Jul-2007  ad branches: 1.6.4; 1.6.6; 1.6.10;
It's not a good idea for device drivers to modify b_flags, as they don't
need to understand the locking around that field. Instead of setting
B_ERROR, set b_error instead. b_error is 'owned' by whoever completes
the I/O request.
 1.5 09-Jul-2007  ad branches: 1.5.2; 1.5.4;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.4 12-Mar-2007  ad branches: 1.4.2; 1.4.8;
Pass an ipl argument to pool_init/POOL_INIT to be used when initializing
the pool's lock.
 1.3 14-Nov-2006  bouyer branches: 1.3.2; 1.3.4; 1.3.6; 1.3.10; 1.3.12;
vn_lock() the vnode we got from bdevvp(), otherwise if something else uses
this device in userland (e.g. qemu-dm when running a HVM guest), we'll reuse
the existing vnode and we'll panic in VOP_UNLOCK(). We don't have this issue
most of the time because when xbdback is the only user, we get a specfs
vnode for which locking operation are NOPs.

Thanks to Antti Kantee for spotting the missing vn_lock() in sources and
giving details about vnode locking.
 1.2 15-Oct-2006  bouyer Don't rely on the /dev entry in the xenstore to get the handle; extract
it from the xenstore path. /dev will have whatever is in the guest's config
file disk entry, and this may not be a number (in the case of HVM guests
it's a string starting with 'ioemu').
 1.1 02-Jul-2006  bouyer branches: 1.1.2; 1.1.6; 1.1.8; 1.1.10; 1.1.12;
block device backend driver for Xen3. Add
pseudo-device xbdback
in your config file to use.
 1.1.12.2 10-Dec-2006  yamt sync with head.
 1.1.12.1 22-Oct-2006  yamt sync with head
 1.1.10.2 09-Sep-2006  rpaulo sync with head
 1.1.10.1 02-Jul-2006  rpaulo file xbdback_xenbus.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.1.8.1 18-Nov-2006  ad Sync with head.
 1.1.6.2 11-Aug-2006  yamt sync with head
 1.1.6.1 02-Jul-2006  yamt file xbdback_xenbus.c was added on branch yamt-pdpolicy on 2006-08-11 15:43:16 +0000
 1.1.2.2 13-Jul-2006  gdamore Merge from HEAD.
 1.1.2.1 02-Jul-2006  gdamore file xbdback_xenbus.c was added on branch gdamore-uart on 2006-07-13 17:49:07 +0000
 1.3.12.2 04-Sep-2008  skrll Sync with netbsd-4.
 1.3.12.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.3.10.4 03-Dec-2007  ad Sync with HEAD.
 1.3.10.3 19-Aug-2007  ad - Back out the biodone() changes.
- Eliminate B_ERROR (from HEAD).
 1.3.10.2 15-Jul-2007  ad Sync with head.
 1.3.10.1 13-Mar-2007  ad Sync with head.
 1.3.6.1 24-Mar-2007  yamt sync with head.
 1.3.4.8 24-Mar-2008  yamt sync with head.
 1.3.4.7 17-Mar-2008  yamt sync with head.
 1.3.4.6 27-Feb-2008  yamt sync with head.
 1.3.4.5 21-Jan-2008  yamt sync with head
 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 30-Dec-2006  yamt sync with head.
 1.3.4.1 14-Nov-2006  yamt file xbdback_xenbus.c was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.3.2.2 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.3.2.1 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.4.8.1 03-Oct-2007  garbled Sync with HEAD
 1.4.2.1 11-Jul-2007  mjf Sync with head.
 1.5.4.1 15-Aug-2007  skrll Sync with HEAD.
 1.5.2.1 07-Aug-2007  matt Sync with HEAD.
 1.6.10.3 16-Nov-2007  bouyer Fix printf formap for i386
 1.6.10.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.6.10.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.6.3 23-Mar-2008  matt sync with HEAD
 1.6.6.2 09-Jan-2008  matt sync with HEAD
 1.6.6.1 06-Nov-2007  matt sync with HEAD
 1.6.4.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.7.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.7.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.11.6.2 08-Jan-2008  bouyer Sync with HEAD
 1.11.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.11.2.1 01-Jan-2008  ad Make it build. XXX does not use getiobuf/putiobuf.
 1.14.6.4 17-Jan-2009  mjf Sync with HEAD.
 1.14.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.14.6.2 29-Jun-2008  mjf Sync with HEAD.
 1.14.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.14.2.1 24-Mar-2008  keiichi sync with head.
 1.16.8.1 18-Jun-2008  simonb Sync with head.
 1.16.6.3 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.16.6.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.16.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.16.4.3 11-Aug-2010  yamt sync with head.
 1.16.4.2 11-Mar-2010  yamt sync with head
 1.16.4.1 04-May-2009  yamt sync with head.
 1.16.2.1 17-Jun-2008  yamt sync with head.
 1.17.2.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.17.2.1 19-Oct-2008  haad Sync with HEAD.
 1.20.8.1 21-Apr-2010  matt sync to netbsd-5
 1.20.6.1 02-Nov-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1682):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.45
Guard against spurious xbdback_backend_changed() calls which would result
in the block device being opened twice. Fixes port-xen/45158,
although the underlying cause (multiple open of the same device not
properly handled any more) is not fixed.
 1.20.4.8 02-Nov-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1685):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.48
Fix bogus KASSERT: if there is a xbdi_io, xbdi_pendingreqs must *NOT* be 0.
Not sure why it has stayed unoticed for so long ...
 1.20.4.7 02-Nov-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1682):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.45
Guard against spurious xbdback_backend_changed() calls which would result
in the block device being opened twice. Fixes port-xen/45158,
although the underlying cause (multiple open of the same device not
properly handled any more) is not fixed.
 1.20.4.6 12-Aug-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1654):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.42
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.43
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.44
Make sure to call xbdback_trampoline() at splbio()
Several fixes to the continuation engine:
- make sure to enter the continuation loop at splbio(), and add some
KASSERT() for this.
- When a flush operation is enqueued to the workqueue, make sure the
continuation loop can't be restarted by a previous workqueue
completion or an event. We can't restart it at this point because
the flush even is still recorded as the current I/O.
For this add a xbdback_co_cache_doflush_wait() which acts as a noop;
the workqueue callback will restart the loop once the flush is complete.
Should fix "kernel diagnostic assertion xbd_io->xio_mapped == 0" panics
reported by Jeff Rizzo on port-xen@.
Add a comment explaing why a flush workqueue is handled differently from
read/write workqueue requests.
 1.20.4.5 18-Jun-2011  bouyer Pull up following revision(s) (requested by jym in ticket #1630):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.37
In xbdback(4), move the code that copies segments after the bound checks
of the ``nr_segments'' variable.
In cases where we are running domUs with an architecture different from the
dom0 one (for example: 32 bits domUs on 64 bits dom0), copying segments
with an invalid nr_segments value will lead to the corruption of the
xbdback instance structure and quickly crash the dom0 backend.
Tested under 64 bits dom0 with 32 bits domUs. No regression observed.
ok bouyer@.
Will be pulled up to -4 and -5.
 1.20.4.4 07-Mar-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1573):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.33
Test and set xbdi->xbdi_cont at splbio(). Otherwise we could overwrite
xbdi->xbdi_cont and process the same request twice.
 1.20.4.3 31-Oct-2009  sborrill branches: 1.20.4.3.2;
Pull up the following revisions(s) (requested by bouyer in ticket #1117):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.28

Fix stalled xbdback detach that would stall the whole xenbus thread,
preventing new domU from be created.
 1.20.4.2 16-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1093):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.26
Don't pass a NULL arg to DIOCCACHESYNC, sd(4) dereferences it.
Pass a pointer to a int initialized to 1, so that a DIOCCACHESYNC will be
forced on sd(4).
Should fix dom0 panic reported by Sarton O'Brien.
 1.20.4.1 28-Sep-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1026):
sys/arch/xen/xen/xbd_xenbus.c: revision 1.43 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.25
xbdback: implement and publish "feature-flush-cache".
xbd: if feature-flush-cache is present, use it for DIOCCACHESYNC.
If not present, make DIOCCACHESYNC return EOPNOTSUPP and warn on
first call.
Should improve WAPBL reliability of Xen guests on a NetBSD dom0.
Unfortunably not all linux guests seems to support this feature, and using
feature-write-barrier would require a B_BARRIER flag in the buffer.
 1.20.4.3.2.3 02-Nov-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1682):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.45
Guard against spurious xbdback_backend_changed() calls which would result
in the block device being opened twice. Fixes port-xen/45158,
although the underlying cause (multiple open of the same device not
properly handled any more) is not fixed.
 1.20.4.3.2.2 18-Jun-2011  bouyer Pull up following revision(s) (requested by jym in ticket #1630):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.37
In xbdback(4), move the code that copies segments after the bound checks
of the ``nr_segments'' variable.
In cases where we are running domUs with an architecture different from the
dom0 one (for example: 32 bits domUs on 64 bits dom0), copying segments
with an invalid nr_segments value will lead to the corruption of the
xbdback instance structure and quickly crash the dom0 backend.
Tested under 64 bits dom0 with 32 bits domUs. No regression observed.
ok bouyer@.
Will be pulled up to -4 and -5.
 1.20.4.3.2.1 07-Mar-2011  riz Pull up following revision(s) (requested by bouyer in ticket #1573):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.33
Test and set xbdi->xbdi_cont at splbio(). Otherwise we could overwrite
xbdi->xbdi_cont and process the same request twice.
 1.20.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.20.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.24.2.6 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.24.2.5 26-May-2011  jym Pull-up some modifications from -current to my branch.
 1.24.2.4 02-May-2011  jym Sync with head.
 1.24.2.3 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.24.2.2 24-Oct-2010  jym Sync with HEAD
 1.24.2.1 01-Nov-2009  jym Sync with HEAD.
 1.31.4.5 12-Jun-2011  rmind sync with head
 1.31.4.4 31-May-2011  rmind sync with head
 1.31.4.3 06-Mar-2011  rmind sync with head (and fix few botches with this)
 1.31.4.2 03-Jul-2010  rmind sync with head
 1.31.4.1 16-Mar-2010  rmind Change struct uvm_object::vmobjlock to be dynamically allocated with
mutex_obj_alloc(). It allows us to share the locks among UVM objects.
 1.31.2.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.32.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.38.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.48.2.3 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.48.2.2 30-Oct-2012  yamt sync with head
 1.48.2.1 17-Apr-2012  yamt sync with head
 1.54.2.2 02-Jun-2012  mrg sync to latest -current.
 1.54.2.1 18-Feb-2012  mrg merge to -current.
 1.55.2.4 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.55.2.3 16-Nov-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1347):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.61
Fix typo which caused the kenrel thread to be created with a 0 priority.
This would cause the thread to be almost never scheduled when a userland
process could use all CPU.
Should fix the problem reported by Torbj?rn Granlund on port-xen@
 1.55.2.2 07-Nov-2013  snj Pull up following revision(s) (requested by bouyer in ticket #974):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.58
Add more XENPRINTF() to xbdback_connect()
in xbdback_backend_changed(), fix memory leak.
Do not free an uninitialized pointer in xbdback_connect(). Should fix
hypervisor or dom0 reboot when using windows PV drivers, as reported
by several users on port-xen.
 1.55.2.1 05-Jun-2012  jdc branches: 1.55.2.1.4; 1.55.2.1.6;
Pull up revision 1.56 (requested by cegger in ticket #282).

destroy mutex and cv first.
Fixes LOCKDEBUG crash when a guest shut down.
 1.55.2.1.6.3 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.55.2.1.6.2 16-Nov-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1347):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.61
Fix typo which caused the kenrel thread to be created with a 0 priority.
This would cause the thread to be almost never scheduled when a userland
process could use all CPU.
Should fix the problem reported by Torbj?rn Granlund on port-xen@
 1.55.2.1.6.1 07-Nov-2013  snj Pull up following revision(s) (requested by bouyer in ticket #974):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.58
Add more XENPRINTF() to xbdback_connect()
in xbdback_backend_changed(), fix memory leak.
Do not free an uninitialized pointer in xbdback_connect(). Should fix
hypervisor or dom0 reboot when using windows PV drivers, as reported
by several users on port-xen.
 1.55.2.1.4.3 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.55.2.1.4.2 16-Nov-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1347):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.61
Fix typo which caused the kenrel thread to be created with a 0 priority.
This would cause the thread to be almost never scheduled when a userland
process could use all CPU.
Should fix the problem reported by Torbj?rn Granlund on port-xen@
 1.55.2.1.4.1 07-Nov-2013  snj Pull up following revision(s) (requested by bouyer in ticket #974):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.58
Add more XENPRINTF() to xbdback_connect()
in xbdback_backend_changed(), fix memory leak.
Do not free an uninitialized pointer in xbdback_connect(). Should fix
hypervisor or dom0 reboot when using windows PV drivers, as reported
by several users on port-xen.
 1.57.4.1 18-May-2014  rmind sync with head
 1.57.2.2 03-Dec-2017  jdolecek update from HEAD
 1.57.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.59.4.3 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1071):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.59.4.2 16-Nov-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1040):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.61
Fix typo which caused the kenrel thread to be created with a 0 priority.
This would cause the thread to be almost never scheduled when a userland
process could use all CPU.
Should fix the problem reported by Torbj?rn Granlund on port-xen@
 1.59.4.1 22-Sep-2014  martin branches: 1.59.4.1.2;
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.59.4.1.2.2 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1071):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.59.4.1.2.1 16-Nov-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1040):
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.61
Fix typo which caused the kenrel thread to be created with a 0 priority.
This would cause the thread to be almost never scheduled when a userland
process could use all CPU.
Should fix the problem reported by Torbj?rn Granlund on port-xen@
 1.60.2.3 05-Feb-2017  skrll Sync with HEAD
 1.60.2.2 19-Mar-2016  skrll Sync with HEAD
 1.60.2.1 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.62.2.2 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.62.2.1 20-Jul-2016  pgoyette Adapt the machine/arch dependent code to the new {b,c}devsw reference
counting.

XXX Most of these will require testing by someone other than myself, as
I have a limited selection of hardware!
 1.63.8.1 23-Sep-2018  martin Apply patch, requested by manu in ticket #1034:

Avoid sleeping with a spin lock held (solved differently on HEAD).
 1.63.6.1 27-Apr-2017  pgoyette Restore all work from the former pgoyette-localcount branch (which is
now abandoned doe to cvs merge botch).

The branch now builds, and installs via anita. There are still some
problems (cgd is non-functional and all atf tests time-out) but they
will get resolved soon.
 1.65.2.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.65.2.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.65.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.67.2.4 21-Apr-2020  martin Sync with HEAD
 1.67.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.67.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.67.2.1 10-Jun-2019  christos Sync with HEAD
 1.72.6.1 17-Jan-2020  ad Sync with head.
 1.77.2.4 25-Apr-2020  bouyer sync with bouyer-xenpvh-base2 (HEAD)
 1.77.2.3 20-Apr-2020  bouyer Fix build with DIAGNOSTIC
 1.77.2.2 20-Apr-2020  bouyer Bring in 1.82 and 1.83 from HEAD
 1.77.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.96.2.1 03-Apr-2021  thorpej Sync with HEAD.
 1.97.4.1 01-Aug-2021  thorpej Sync with HEAD.
 1.101.4.2 22-Jun-2024  martin Pull up the following, requested by bouyer in ticket #726:

sys/arch/xen/xen/xbdback_xenbus.c upto 1.107

Restore "sparse" segements support which was lost in rev 1.83, causing
VBD corruption with linux guests.
The segments in a single request are not always contigous in VA; this means
that the end of a segment is not always 7 and the start of the next one is not
always 0. When this happens this means that a contigous chunk of data from
disk has to be dispatched to various non-contigous VA, in chunks of VBD_BSIZE
bytes (or the other way round for writes).
Linux I/O subsystems seems to support this natively; to emulate this allocate
a MAXPHYS bounce buffer to do the I/O and then memcpy() the data from/to
the segments as requested. If the request is contigous do the I/O
directly to the mapped VA.

This means that we need to keep segments details until iodone(); so move
the blkif_request_segment array from xbdback_instance to xbdback_io. The
array is allocated separately to guarantee proper page alignement.

non-contigous segments seems rare so allocate one bounce buffer per
xbdback_instance, and stall the ring if the bounce buffer is already in use.
For this add back a mechanism to restart an I/O at a specific point
after thread sleep/wakeup.

While there guard some more printfs with ratecheck() and add more checks on
segments bounds.

Tested with a HVM scientific linux install from iso image; the install would
fail with a xfs corruption when installing grub.

(Plus mostly cosmetic/minor changes.)
 1.101.4.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.6 24-May-2022  bouyer PR kern/56853: add a dummy acpi_md_vbios_reset variable so that we
can again enable vga at pci for Xen.
Note that this only works in BIOS mode, not UEFI mode.
 1.5 18-Aug-2009  jmcneill Switch to ACPICA 20090730, and update for API changes.
 1.4 17-Feb-2008  bouyer branches: 1.4.10; 1.4.24;
Add missing __KERNEL_RCSID()
 1.3 17-Dec-2007  joerg Revert, fixed differently.
 1.2 15-Dec-2007  jmcneill Provide a stub acpi_md_sleep_init on xen, pointed out by Paul Goyette
on current-users.
 1.1 09-Apr-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.6; 1.1.10; 1.1.14; 1.1.20; 1.1.24; 1.1.40; 1.1.50; 1.1.56; 1.1.60;
Add support for ACPI in xen-3 dom0 support. We can now boot a xen-3 dom0
kernel with a default xen comamnd line.
 1.1.60.1 26-Dec-2007  ad Sync with head.
 1.1.56.1 18-Feb-2008  mjf Sync with HEAD.
 1.1.50.1 23-Mar-2008  matt sync with HEAD
 1.1.40.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.1.24.1 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.1.20.2 09-Sep-2006  rpaulo sync with head
 1.1.20.1 09-Apr-2006  rpaulo file xen_acpi_machdep.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.1.14.3 27-Feb-2008  yamt sync with head.
 1.1.14.2 21-Jun-2006  yamt sync with head.
 1.1.14.1 09-Apr-2006  yamt file xen_acpi_machdep.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.1.10.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.1.10.1 09-Apr-2006  tron file xen_acpi_machdep.c was added on branch peter-altq on 2006-05-24 15:48:26 +0000
 1.1.6.2 22-Apr-2006  simonb Sync with head.
 1.1.6.1 09-Apr-2006  simonb file xen_acpi_machdep.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.1.4.2 19-Apr-2006  elad sync with head - hopefully this will work
 1.1.4.1 09-Apr-2006  elad file xen_acpi_machdep.c was added on branch elad-kernelauth on 2006-04-19 02:34:03 +0000
 1.1.2.2 11-Apr-2006  yamt sync with head
 1.1.2.1 09-Apr-2006  yamt file xen_acpi_machdep.c was added on branch yamt-pdpolicy on 2006-04-11 11:53:48 +0000
 1.4.24.2 01-Nov-2009  jym Sync with HEAD.
 1.4.24.1 18-Jun-2009  cegger implement ACPI S3 support for Dom0.
ok jym@
 1.4.10.1 19-Aug-2009  yamt sync with head.
 1.23 18-Aug-2025  andvar Fix various typos, mainly in comments:
s/invaid/invalid/
s/instad/instead/
s/wich/with/
s/tranform/transform/
s/tranmist/transmit/
s/tranceiver/transceiver/
s/Tranparent/Transparent/
s/tranlated/translated/
s/tranfer/transfer/
s/tranmissions/transmissions/
s/condtions/conditions/
s/Recient/Recent/
 1.22 22-May-2025  riastradh xen: Use ordered rdtsc() function, rather than inline rdtsc asm.

This will issue either lfence, mfence, or cpuid before the rdtsc
instruction, depending on the CPU identification, in order to prevent
reordering with respect to surrounding loads -- particularly those
used to query the host's systime parameters.

May help with:

PR port-xen/59437: Xen time stability
 1.21 03-Mar-2025  andvar s/nead/near/ in comment.
 1.20 01-Dec-2024  andvar s/timeocunter/timecounter/ in comment.
 1.19 23-Jun-2024  riastradh branches: 1.19.2;
xen_rtc_set: Zero-initialize op before passing to hypervisor.

PR port-xen/58362
 1.18 10-Sep-2023  bouyer If Xen system time went backward, instead of ignoring the event assume
one tick passed and reinitialise the local state.
If we need to call the hardclock timer several time to catch up missed events,
don't do more than one second in a single event; and schedule the next
event faster.

Hopefully fixes occasionnal
panic: cpu1: time has not advanced in 1501 heartbeats
or
panic: cpu0: softints stuck for 16 seconds
seen in daily Xen atf runs.
 1.17 01-Aug-2023  riastradh xen: Fix previous commit, forgot to amend it before committing.
 1.16 01-Aug-2023  riastradh xen: Report when hardclock jump exceeds timecounter(9) limit.
 1.15 28-Jul-2023  riastradh xen_clock(4): New hardclock dtrace probes.

sdt:xen:hardclock:tick(last, now)
fires on every hardclock tick

sdt:xen:hardclock:jump(last, now, nticks)
fires on every hardclock tick when (now - last) >= 2*NS_PER_TICK,
i.e., this call to the timer interrupt handler requires multiple
hardclock ticks
 1.14 28-Jul-2023  riastradh xen_clock(4): Compute NS_PER_TICK only once per call.

This involves a division by an unknown number, so let's cache it.

XXX Could do better by precomputing it in xen_resumeclocks, with some
effort to ensure concurrent calls don't stomp on each other.
 1.13 28-Jul-2023  riastradh xen_clock(4): Fix whitespace and sprinkle comments.

No functional change intended.
 1.12 17-Jul-2023  bouyer Unmask event after arming the one-shot timer in clock initialisation,
to avoid a possible race with xen_timer_handler() updating
ci_xen_hardclock_systime_ns while we're reading it.
Pointed out by Taylor R Campbell
 1.11 13-Jul-2023  riastradh xen: Record event when local view of timecounter is behind global.
 1.10 07-Jul-2023  bouyer Unmask event after VCPUOP_stop_periodic_timer and
initializing ci->ci_xen_hardclock_systime_ns, to avoid a possible race with
xen_timer_handler()
 1.9 01-May-2023  riastradh xen_clock(4): Sprinkle dtrace probes.
 1.8 30-Nov-2020  bouyer branches: 1.8.18;
Switch some debug/informational printf to device_printf(). Usefull
for multiprocessor environnement.
 1.7 22-May-2020  riastradh branches: 1.7.2;
Set frame = NULL in XENPV whether DIAGNOSTIC or not.

For Xen PV, the interrupt logic stores the relevant information in
the CPU's struct cpu_info, and CLKF_USERMODE and CLKF_PC don't use
the frame argument at all. (For Xen PVH, we behave like normal x86
and use the frame as passed.)

DIAGNOSTIC shouldn't change the data flows that affect non-DIAGNOSTIC
logic, so let's just unconditionally set frame = NULL for XENPV. If
this makes a difference, that's a bug -- and it's a bug that should
not be concealed by enabling DIAGNOSTIC.

ok bouyer
 1.6 21-May-2020  ad - Recalibrate the APIC timer using the TSC, once the TSC has in turn been
recalibrated using the HPET. This gets the clock interrupt firing more
closely to HZ.

- Undo change with recent Xen merge and go back to starting the clocks in
initclocks() on the boot CPU, and in cpu_hatch() on secondary CPUs.

- On reflection don't use HPET delay any more, it works very well but means
going over the bus. It's enough to use HPET to calibrate the TSC and
APIC.

Tested on amd64 native, xen and xen PVH.
 1.5 07-May-2020  bouyer Change event_set_handler() to take the target CPU parameter. If ci is NULL,
event_set_handler() will choose the CPU and bind the event.
If ci is not NULL the caller is responsible for binding the event.
Use a IPI xcall to register the handlers if needed.
pull in a hack from x86 to force pirq handlers to be mpsafe if registered at
a level != IPL_VM. This is for the com at isa interrupt handler, which
registers at IPL_HIGH and has to way to tell it's mpsafe (taking
KERNEL_LOCK at IPL_HIGH causes deadlocks on MP systems).
 1.4 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.3 26-Apr-2020  roy Guard xen_wallclock with XENPV.

Thanks to riastradh@
 1.2 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.1 16-Apr-2020  bouyer branches: 1.1.2;
file xen_clock.c was initially added on branch bouyer-xenpvh.
 1.1.2.4 19-Apr-2020  bouyer Add per-PIC callbacks for interrupt_get_devname(), interrupt_get_assigned()
and interrupt_get_count(). Implement Xen-specific callbacks for
PIC_XEN and use the x86 one for others.
In event_set_handler(), call intr_allocate_io_intrsource() so that
events appears in interrupt list (intrctl list).
 1.1.2.3 18-Apr-2020  bouyer Add PVHVM multiprocessor support:
We need the hypervisor to be set up before cpus attaches.
Move hypervisor setup to a new function xen_hvm_init(), called at the
beggining of mainbus_attach(). This function searches the cfdata[] array
to see if the hypervisor device is enabled (so you can disable PV
support with
disable hypervisor
from userconf).
For HVM, ci_cpuid doens't match the virtual CPU index needed by Xen.
Introduce ci_vcpuid to cpu_info. Introduce xen_hvm_init_cpu(), to be
called for each CPU in in its context, which initialize ci_vcpuid and
ci_vcpu, and setup the event callback.
Change Xen code to use ci_vcpuid.

Do not call lapic_calibrate_timer() for VM_GUEST_XENPVHVM, we will use
Xen timers.

Don't call lapic_initclocks() from cpu_hatch(); instead set
x86_cpu_initclock_func to lapic_initclocks() in lapic_calibrate_timer(),
and call *(x86_cpu_initclock_func)() from cpu_hatch().
Also call x86_cpu_initclock_func from cpu_attach() for the boot CPU.
As x86_cpu_initclock_func is called for all CPUs, x86_initclock_func can
be a NOP for lapic timer.

Reorganize Xen code for x86_initclock_func/x86_cpu_initclock_func.
Move x86_cpu_idle_xen() to hypervisor_machdep.c
 1.1.2.2 16-Apr-2020  bouyer MOve the #ifdef XENPV block to the end, so that XEN3_DOM0 builds again.
 1.1.2.1 16-Apr-2020  bouyer Rename xen/xen/clock.c to xen_clock.c, so that it can be compiled
with x86/isa/clock.c
call events_default_setup() from hypervisor_match to that event
arrays are properly initialised.
Use xen_delay() and xen's timecounter for PVHVM.
 1.7.2.1 14-Dec-2020  thorpej Sync w/ HEAD.
 1.8.18.3 03-Oct-2025  martin Pull up following revision(s) (requested by riastradh in ticket #1170):

sys/arch/xen/xen/xen_clock.c: revision 1.22

xen: Use ordered rdtsc() function, rather than inline rdtsc asm.

This will issue either lfence, mfence, or cpuid before the rdtsc
instruction, depending on the CPU identification, in order to prevent
reordering with respect to surrounding loads -- particularly those
used to query the host's systime parameters.

May help with:
PR port-xen/59437: Xen time stability
 1.8.18.2 13-Oct-2024  martin Pull up following revision(s) (requested by riastradh in ticket #961):

sys/arch/xen/xen/xen_clock.c: revision 1.19

xen_rtc_set: Zero-initialize op before passing to hypervisor.

PR port-xen/58362
 1.8.18.1 19-Jul-2023  martin Pull up following revision(s) (requested by bouyer in ticket #238):

sys/arch/xen/xen/xen_clock.c: revision 1.10
sys/arch/xen/xen/xen_clock.c: revision 1.12

Unmask event after VCPUOP_stop_periodic_timer and
initializing ci->ci_xen_hardclock_systime_ns, to avoid a possible race with
xen_timer_handler()

Unmask event after arming the one-shot timer in clock initialisation,
to avoid a possible race with xen_timer_handler() updating
ci_xen_hardclock_systime_ns while we're reading it.

Pointed out by Taylor R Campbell
 1.19.2.1 02-Aug-2025  perseant Sync with HEAD
 1.11 26-Jul-2018  maxv Merge the content of xen_debug.c into xen_machdep.c, there is only one
function.
 1.10 26-Jul-2018  maxv Remove dead code. This looks like a leftover from when our Xen port was
being developed (2004), and it seems to have been copied from the Xen
kernel examples. It can't have any use, so get rid of it. Also remove
vprintk, unused.
 1.9 07-Jul-2016  msaitoh branches: 1.9.16; 1.9.18;
KNF. Remove extra spaces. No functional change.
 1.8 17-Jul-2011  joerg branches: 1.8.12; 1.8.30;
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.7 23-Oct-2009  snj Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.6 18-Mar-2009  cegger Ansify function definitions w/o arguments. Generated with sed.
 1.5 22-Nov-2007  bouyer branches: 1.5.18; 1.5.26; 1.5.32;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.4 11-Dec-2005  christos branches: 1.4.30; 1.4.48; 1.4.50; 1.4.54; 1.4.56;
merge ktrace-lwp.
 1.3 09-Mar-2005  bouyer branches: 1.3.4;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.2 07-May-2004  cl branches: 1.2.2; 1.2.6; 1.2.8; 1.2.10;
cleanup debugging code
 1.1 11-Mar-2004  cl branches: 1.1.2;
Add port to the Xen virtual machine monitor.
(see http://www.cl.cam.ac.uk/Research/SRG/netos/xen/)
 1.1.2.1 22-May-2004  he Pull up revision 1.2 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.2.10.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.8.1 29-Apr-2005  kent sync with -current
 1.2.6.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.2.2.5 01-Apr-2005  skrll Sync with HEAD.
 1.2.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.2.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.2.2.2 03-Aug-2004  skrll Sync with HEAD
 1.2.2.1 07-May-2004  skrll file xen_debug.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.3.4.1 07-Dec-2007  yamt sync with head
 1.4.56.1 08-Dec-2007  mjf Sync with HEAD.
 1.4.54.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.50.1 09-Jan-2008  matt sync with HEAD
 1.4.48.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.4.30.1 03-Dec-2007  ad Sync with HEAD.
 1.5.32.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.5.32.2 01-Nov-2009  jym Sync with HEAD.
 1.5.32.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.5.26.1 28-Apr-2009  skrll Sync with HEAD.
 1.5.18.2 11-Mar-2010  yamt sync with head
 1.5.18.1 04-May-2009  yamt sync with head.
 1.8.30.1 09-Jul-2016  skrll Sync with HEAD
 1.8.12.1 03-Dec-2017  jdolecek update from HEAD
 1.9.18.1 10-Jun-2019  christos Sync with HEAD
 1.9.16.1 28-Jul-2018  pgoyette Sync with HEAD
 1.29 17-Oct-2023  riastradh xen_machdep.c: Nix trailing whitespace.

No functional change intended.
 1.28 16-Oct-2023  bouyer Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.
 1.27 20-Aug-2022  riastradh branches: 1.27.4;
x86: Split most of pmap.h into pmap_private.h or vmparam.h.

This way pmap.h only contains the MD definition of the MI pmap(9)
API, which loads of things in the kernel rely on, so changing x86
pmap internals no longer requires recompiling the entire kernel every
time.

Callers needing these internals must now use machine/pmap_private.h.
Note: This is not x86/pmap_private.h because it contains three parts:

1. CPU-specific (different for i386/amd64) definitions used by...

2. common definitions, including Xenisms like xpmap_ptetomach,
further used by...

3. more CPU-specific inlines for pmap_pte_* operations

So {amd64,i386}/pmap_private.h defines 1, includes x86/pmap_private.h
for 2, and then defines 3. Maybe we should split that out into a new
pmap_pte.h to reduce this trouble.

No functional change intended, other than that some .c files must
include machine/pmap_private.h when previously uvm/uvm_pmap.h
polluted the namespace with pmap internals.

Note: This migrates part of i386/pmap.h into i386/vmparam.h --
specifically the parts that are needed for several constants defined
in vmparam.h:

VM_MAXUSER_ADDRESS
VM_MAX_ADDRESS
VM_MAX_KERNEL_ADDRESS
VM_MIN_KERNEL_ADDRESS

Since i386 needs PDP_SIZE in vmparam.h, I added it there on amd64
too, just to keep things parallel.
 1.26 04-Jun-2020  msaitoh G.C. No functional change. OK'd by bouyer@.
 1.25 19-May-2020  ad Remove duplicate def of tsc_get_timecount().
 1.24 02-May-2020  bouyer Introduce Xen PVH support in GENERIC.
This is compiled in with
options XENPVHVM
x86 changes:
- add Xen section and xen pvh entry points to locore.S. Set vm_guest
to VM_GUEST_XENPVH in this entry point.
Most of the boot procedure (especially page table setup and switch to
paged mode) is shared with native.
- change some x86_delay() to delay_func(), which points to x86_delay() for
native/HVM, and xen_delay() for PVH

Xen changes:
- remove Xen bits from init_x86_64_ksyms() and init386_ksyms()
and move to xen_init_ksyms(), used for both PV and PVH
- set ISA no-legacy-devices property for PVH
- factor out code from Xen's cpu_bootconf() to xen_bootconf()
in xen_machdep.c
- set up a specific pvh_consinit() which starts with printk()
(which uses a simple hypercall that is available early) and switch to
xencons when we can use pmap_kenter_pa().
 1.23 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.22 09-Mar-2019  maxv branches: 1.22.10;
Start replacing the x86 PTE bits.
 1.21 13-Feb-2019  cherry Further restrict the scope of XENPV to relevant parts.
 1.20 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.19 26-Jul-2018  maxv Merge the content of xen_debug.c into xen_machdep.c, there is only one
function.
 1.18 30-Jun-2018  kre Build fix bandaid.

This allows the builds including XEN to complete, but XEN kernels
built from these sources (at least, DomU) do not boot successfully.
 1.17 30-Jun-2018  riastradh Use xcall to suspend/resume clocks on _all_ CPUs, not just current.

Compile-tested only -- kernel still runs but I haven't exercised this
code path.

By code inspection, this change or something like it is obviously
_necessary_ for Xen MP suspend/resume -- otherwise there's no logic
to suspend/resume the clocks on any CPU except the one that actually
triggers sysctl -w machdep.xen.suspend=1.

Whether this change is _sufficient_ for Xen MP suspend/resume to
work, I don't know.

ok cherry
 1.16 24-Jun-2018  jdolecek branches: 1.16.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.15 08-Jun-2016  jnemeth branches: 1.15.16;
Feeding uninitialised garbage to the hypervisor is likely to be a bad idea.
 1.14 29-May-2016  bouyer Switch to elf notes for amd64 instead of the old key=value list to describe the
guest requirements and support.
Add infrastructure to query the hypervisor about features support.
For verbose boot, print the features suppoted by the hypervisor for this
guest.
 1.13 28-Jul-2012  matt branches: 1.13.2; 1.13.16;
Fix some -fno-common fallout.
 1.12 22-Jul-2012  jym Don't leak stack content when reading the Xen suspend value.

XXX pull-up to -6.
 1.11 30-Jun-2012  jym Extend the xpmap API, as described in [1]. This change is mechanical and
avoids exposing the MD phys_to_machine/machine_to_phys tables directly.
Added:

- xpmap_ptom handles PFN (pseudo physical) to MFN (machine frame number)
translations, and is under control of the domain.
- xpmap_mtop is its counterpart (MFN to PFN), and is under control of
hypervisor.

xpmap_ptom_map() map a pseudo-phys address to a machine address
xpmap_ptom_unmap() unmap a pseudo-phys address (invalidation)
xpmap_ptom_isvalid() check for pseudo-phys address validity

The parameters are physical/machine addresses, like bus_dma/bus_space(9).
As x86 MFNs are tracked by u_long (Xen's choice) while machine addresses
can be 64 bits entities (PAE), use ptoa() to avoid truncation when bit
shifting by PAGE_SHIFT.

I kept the same namespace (xpmap_) to avoid code churn.

[1] http://mail-index.netbsd.org/port-xen/2009/05/09/msg004951.html

XXX will document ptoa/atop/trunc_page separately.
 1.10 12-Feb-2012  jym branches: 1.10.2;
Xen clock management routines keep track of CPU (following MP merge).
Reflect this change in the suspend/resume routines so they can cope with
domU CPU suspend, instead of setting their cpu_info pointer to NULL.

Avoid copy/pasting by using the resume routines during attachement.

ok releng@.

No regression observed, and allows domU to suspend successfully again.
Restore is a different beast as PD/PT flags are marked "invalid" by Xen-4
hypervisor, and blocks resuming. Looking into it.
 1.9 20-Nov-2011  jym branches: 1.9.2;
Expose pmap_pdp_cache publicly to x86/xen pmap. Provide suspend/resume
callbacks for Xen pmap.

Turn static internal callbacks of pmap_pdp_cache.

XXX the implementation of pool_cache_invalidate(9) is still wrong, and
IMHO this needs fixing before -6. See
http://mail-index.netbsd.org/tech-kern/2011/11/18/msg011924.html
 1.8 20-Sep-2011  jym branches: 1.8.2;
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.7 23-Oct-2009  snj Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.6 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.5 13-Feb-2009  bouyer Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.4 10-May-2008  ad branches: 1.4.6; 1.4.8; 1.4.12; 1.4.16;
Make xen build after tsc changes.
 1.3 05-May-2008  ad branches: 1.3.2;
Export tsc_freq via sysctl.
 1.2 22-Nov-2007  bouyer branches: 1.2.2; 1.2.4; 1.2.8; 1.2.16; 1.2.22; 1.2.24; 1.2.26;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.1 17-Oct-2007  bouyer branches: 1.1.2; 1.1.4;
file xen_machdep.c was initially added on branch bouyer-xenamd64.
 1.1.4.1 08-Dec-2007  mjf Sync with HEAD.
 1.1.2.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.2.26.3 11-Mar-2010  yamt sync with head
 1.2.26.2 04-May-2009  yamt sync with head.
 1.2.26.1 16-May-2008  yamt sync with head.
 1.2.24.1 18-May-2008  yamt sync with head.
 1.2.22.1 02-Jun-2008  mjf Sync with HEAD.
 1.2.16.2 09-Jan-2008  matt sync with HEAD
 1.2.16.1 22-Nov-2007  matt file xen_machdep.c was added on branch matt-armv6 on 2008-01-09 01:50:22 +0000
 1.2.8.2 07-Dec-2007  yamt sync with head
 1.2.8.1 22-Nov-2007  yamt file xen_machdep.c was added on branch yamt-lazymbuf on 2007-12-07 17:27:24 +0000
 1.2.4.2 03-Dec-2007  ad Sync with HEAD.
 1.2.4.1 22-Nov-2007  ad file xen_machdep.c was added on branch vmlocking on 2007-12-03 19:04:46 +0000
 1.2.2.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.2.1 22-Nov-2007  joerg file xen_machdep.c was added on branch jmcneill-pm on 2007-11-27 19:36:32 +0000
 1.3.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.4.16.1 21-Apr-2010  matt sync to netbsd-5
 1.4.12.10 27-Aug-2011  jym Rename the functions for suspend to reflect that Xen does not hijack
the ACPI "sleepstate" sysctl(7) node anymore.

Add a boolean value to mark that the save/suspend operation has been
notified by dom0, so as to avoid possible errors where admin would like
to schedule the domain for sleep without dom0 being prepared for that. Fail
with EAGAIN in this case.

Sprinkle some KNF.
 1.4.12.9 24-Aug-2011  jym Now that Xen sysctl(7) moved under machdep, create a machdep.xen.suspend
node to command suspension.
 1.4.12.8 26-May-2011  jym balloon is now supported, update comment.
 1.4.12.7 10-Jan-2011  jym Sync with HEAD
 1.4.12.6 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.4.12.5 01-Nov-2009  jym Sync with HEAD.
 1.4.12.4 24-Jul-2009  jym - rework the page pinning API, so that now a function is provided for
each level of indirection encountered during virtual memory translations. Update
pmap accordingly. Pinning looks cleaner that way, and it offers the possibility
to pin lower level pages if necessary (NetBSD does not do it currently).

- some fixes and comments to explain how page validation/invalidation take
place during save/restore/migrate under Xen. L2 shadow entries from PAE are now
handled, so basically, suspend/resume works with PAE.

- fixes an issue reported by Christoph (cegger@) for xencons suspend/resume
in dom0.

TODO:

- PAE save/restore is currently limited to single-user only, multi-user
support requires modifications in PAE pmap that should be discussed first. See
the comments about the L2 shadow pages cached in pmap_pdp_cache in this commit.

- grant table bug is still there; do not use the kernels of this branch
to test suspend/resume, unless you want to experience bad crashes in dom0,
and push the big red button.

Now there is light at the end of the tunnel :)

Note: XEN2 kernels will neither build nor work with this branch.
 1.4.12.3 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.4.12.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.4.12.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.4.8.1 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.4.6.1 03-Mar-2009  skrll Sync with HEAD.
 1.8.2.2 30-Oct-2012  yamt sync with head
 1.8.2.1 17-Apr-2012  yamt sync with head
 1.9.2.1 18-Feb-2012  mrg merge to -current.
 1.10.2.1 25-Jul-2012  jdc Pull up revision 1.12 (requested by jym in ticket #437).

Don't leak stack content when reading the Xen suspend value.

XXX pull-up to -6.
 1.13.16.1 09-Jul-2016  skrll Sync with HEAD
 1.13.2.1 03-Dec-2017  jdolecek update from HEAD
 1.15.16.2 28-Jul-2018  pgoyette Sync with HEAD
 1.15.16.1 25-Jun-2018  pgoyette Sync with HEAD
 1.16.2.1 10-Jun-2019  christos Sync with HEAD
 1.22.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.27.4.1 18-Oct-2023  martin Pull up following revision(s) (requested by bouyer in ticket #428):

sys/arch/xen/xen/xen_machdep.c: revision 1.28
sys/arch/x86/pci/pci_machdep.c: revision 1.97
sys/arch/xen/xen/genfb_xen.c: revision 1.1
sys/arch/xen/xen/genfb_xen.c: revision 1.2
sys/arch/xen/include/hypervisor.h: revision 1.59
sys/arch/i386/conf/XEN3PAE_DOM0: revision 1.41 (patch)
sys/arch/x86/x86/genfb_machdep.c: revision 1.22
sys/arch/xen/x86/consinit.c: revision 1.18
sys/arch/xen/x86/autoconf.c: revision 1.26
sys/external/mit/xen-include-public/dist/xen/include/public/platform.h: revision 1.2
sys/arch/xen/conf/files.xen: revision 1.188
sys/arch/x86/x86/consinit.c: revision 1.37
sys/arch/xen/conf/files.xen: revision 1.189
sys/arch/x86/x86/consinit.c: revision 1.38
sys/external/mit/xen-include-public/dist/xen/include/public/xen.h: revision 1.2
sys/arch/x86/include/genfb_machdep.h: revision 1.7
sys/arch/xen/x86/pvh_consinit.c: revision 1.5
sys/arch/xen/x86/pvh_consinit.c: revision 1.6
sys/arch/amd64/conf/XEN3_DOM0: revision 1.201

Move the pvh_xencons so xen_machdep.c as early_xencons, so it can be
used in the future as early ouput for plain PV guests too.

Support non-VGA framebuffers for Xen dom0. This is mandatory for graphic
console on EFI-only hardware.

Add a xen_genfb_getbtinfo() function which will return a btinfo_framebuffer
structure, filled in with parameters provided by Xen

when runing as a Xen dom0, call xen_genfb_getbtinfo() instead of
lookup_bootinfo(BTINFO_FRAMEBUFFER) when adding properties to the
PCI graphic device (when genfb is attached) and in x86_genfb_init()
when genfb is used as console.

x86/x86/consinit.c: If running as a Xen dom0, use xen_genfb_getbtinfo()
to check if we have a genfb console

xen/x86/consinit.c: support genfb as possible console

xen/x86/consinit.c: use the hypervior IO as console until a better one
is found. If the hypervisor is using a serial port for boot messages,
we'll get NetBSD's boot message on the serial port too until
the real console takes over.

xen/x86/autoconf.c: rework device_register() to be closer to the x86 version.
Especially make sure that device_pci_register() is called.

Make sure to always fall back to xen_early_console, even for dom0

Enable genfb in DOM0 kernels

Add ext_lfb_base to dom0_vga_console_info, from recent Xen. We know if it's
present or not by checking dom0.info_size

Add XENPF_get_dom0_console, which gets a dom0_vga_console_info stucture
from the hypervisor. To be used by PVH dom0 kernels.

XENPVH option is not used. Fix consinit.c to use XENPVHVM as intended
and XENPVH from defflag
for a dom0 PVH, the dom0_vga_console_info structure has to be retrieved
using a platform hypercall; do so in the XENPVHVM case.

Now genfb works in a PVH dom0 running on Xen 4.18 (Xen 4.15 doesn't support
this platoform op, so no way to make it work here).
 1.7 17-Mar-2018  christos dedup microcode handling.
 1.6 17-Mar-2018  christos sync with x86; the version code could be merged.
 1.5 07-Jan-2015  ozaki-r branches: 1.5.16;
Pass a correct firmware size (instead of 0) to firmware_free

firmware_free now uses kmem_free(9) instead of free(9),
so we need to pass a correct size to it.
 1.4 06-Jul-2013  gdt branches: 1.4.8;
Add #endif comments.
 1.3 17-Oct-2012  drochner branches: 1.3.2;
put binary compatibility support for the old AMD-only CPU microcode
update API inside COMPAT_60
 1.2 29-Aug-2012  drochner branches: 1.2.2;
Extend the CPU microcode update framework to support Intel x86 CPUs.
Contrary to the AMD implementation, it doesn't use xcalls to distribute
the update to all CPUs but relies on cpuctl(8) to bind itself to the
right CPU -- to keep it simple and avoid possible problems with
hyperthreading.
Also, it doesn't parse the vendor supplied file to pick the right
part for the present CPU model but relies on userland to prepare
files with specific filenames. I'll commit a pkg for this in a minute
(pkgsrc/sysutils/intel-microcode).
The ioctl interface changed; compatibility is provided (should be
limited to COMPAT_NETBSD6 as soon as this is available).
 1.1 13-Jan-2012  cegger branches: 1.1.4; 1.1.6;
Support CPU microcode loading via cpuctl(8).
Implemented and enabled via CPU_UCODE kernel config option
for x86 and Xen Dom0.
Tested on different AMD machines with different
CPU families.

ok wiz@ for the manpages
ok releng@
ok core@ via releng@
 1.1.6.4 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.1.6.3 30-Oct-2012  yamt sync with head
 1.1.6.2 17-Apr-2012  yamt sync with head
 1.1.6.1 13-Jan-2012  yamt file xen_ucode.c was added on branch yamt-pagecache on 2012-04-17 00:07:12 +0000
 1.1.4.2 18-Feb-2012  mrg merge to -current.
 1.1.4.1 13-Jan-2012  mrg file xen_ucode.c was added on branch jmcneill-usbmp on 2012-02-18 07:33:47 +0000
 1.2.2.3 03-Dec-2017  jdolecek update from HEAD
 1.2.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.2.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.3.2.1 28-Aug-2013  rmind sync with head
 1.4.8.1 06-Apr-2015  skrll Sync with HEAD
 1.5.16.1 17-Mar-2018  pgoyette Import christos's changes for the compat_60 cpu_ucode stuff
 1.53 25-Feb-2023  riastradh xencons(4): Nix trailing whitespace.

No functional change intended.
 1.52 25-Feb-2023  riastradh 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.
 1.51 25-Feb-2023  riastradh 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.
 1.50 07-May-2020  maxv branches: 1.50.20;
Localify.
 1.49 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.48 02-Feb-2019  cherry branches: 1.48.10;
Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.47 24-Dec-2018  cherry 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.46 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.45 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.44 24-Jun-2018  jdolecek branches: 1.44.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.43 11-Nov-2017  riastradh branches: 1.43.2;
Restore MP-safety annotations in intr_establish_xname.

In the old API, event_set_handler would assume MP-unsafe for IPL_VM
and MP-safe for all other levels (IPL_SCHED, IPL_HIGH). The recent
Xen interrupt rototill started passing known_mpsafe=true for _all_
interrupt handlers. Change it to known_mpsafe=false for the IPL_VM
(= IPL_CLOCK, IPL_NET) ones.
 1.42 06-Nov-2017  cherry Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.41 25-Jul-2014  dholland Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.40 16-Mar-2014  dholland branches: 1.40.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.39 07-Dec-2011  cegger branches: 1.39.6; 1.39.10;
switch from xen3-public to xen-public.
 1.38 20-Sep-2011  jym branches: 1.38.2; 1.38.6;
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.37 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.36 24-Apr-2011  rmind Rename ttymalloc() to tty_alloc(), and ttyfree() to tty_free() for
consistency. Remove some unnecessary malloc.h inclusions as well.
 1.35 23-Oct-2009  snj branches: 1.35.4; 1.35.6;
Remove 3rd and 4th clauses. OK cl@ (copyright holder).
 1.34 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.33 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.32 09-Jun-2009  cegger Fix crash on Dom0 resume.
A xencons interrupt may occur before xencons device has been resumed.
Fix tested in jym-xensuspend branch.
 1.31 16-Jan-2009  jym branches: 1.31.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.30 13-Nov-2008  cegger Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.29 21-Oct-2008  cegger branches: 1.29.2;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.28 10-Aug-2008  cegger - beggining -> beginning
- xenconscn_attach: ansify
- xenconscn_putc: KNF
 1.27 16-Apr-2008  cegger branches: 1.27.4; 1.27.6; 1.27.10;
device_t / softc split
reviewed, tested and approved by bouyer
 1.26 06-Apr-2008  cegger use aprint_*_dev and device_xname
 1.25 22-Nov-2007  bouyer branches: 1.25.14;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.24 19-Nov-2007  ad - Factor out too many copies of the same bit of tty code.
- Fix another tty signalling/wakeup problem.
 1.23 18-Oct-2007  joerg branches: 1.23.2;
Initialise the callbacks for tty.t_rstrt_ch in ttymalloc
as all drivers but Sun/SPARC's kd.c use the same arguments.
Separate callout_reset into callout_schedule and the initial
callout_setfunc using that.
 1.22 04-Mar-2007  christos branches: 1.22.2; 1.22.18; 1.22.20; 1.22.24;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.21 29-Jan-2007  hubertf branches: 1.21.2;
Remove more duplicate headers.
Patch by Slava Semushin <slava.semushin@gmail.com>

Again, this was tested by comparing obj files from a pristine and a patched
source tree against an i386/ALL kernel, and also for src/sbin/fsck_ffs,
src/sbin/fsdb and src/usr.sbin/makefs. Only changes in assert() line numbers
were detected in 'objdump -d' output.
 1.20 07-Jan-2007  bouyer - Add or change memory barrier to reflect the related operation
- In xencons_handler(), update in_cons inside the loop, otherwise,
we would trigger the xenconscn_getc() workaround wich reset cons and prod
to their original values, and this creates an infinite loop

Should fix the console hang reported by several users on port-xen@.
 1.19 01-Oct-2006  elad branches: 1.19.2;
Adapt MD code to KAUTH_DEVICE_TTY_OPEN, batch #2 from Matt Fleming, thanks!

Also, add forgotten splx() calls in some places.
 1.18 23-Jul-2006  ad branches: 1.18.4; 1.18.6;
Use the LWP cached credentials where sane.
 1.17 14-May-2006  elad integrate kauth.
 1.16 07-May-2006  bouyer Initialise a few things earlier, so that getc() works before the console
is probed for dom0. This makes -c and -d work on Xen3/dom0 kernels.
The kernel still gets a trap on Xen2/dom0.
 1.15 03-May-2006  bouyer Fix a race between xenconscn_getc() and xencons_handler() (which seems to be
triggered by recent xen3 hypervisor or tools): check sc->polling in
xencons_handler() and just return if set. Also, run xenconscn_getc() and putc()
at spltty().
While there, mark xencons_interface as volatile and use __UNVOLATILE() where
appropriate; and update the pointers in xencons_handler() if xenconscn_getc()
changed them (avoids replaying ddb commands in shell if ddb got called
by the magic string).
 1.14 28-Mar-2006  thorpej Use device_unit().
 1.13 06-Mar-2006  bouyer branches: 1.13.2; 1.13.4;
Add back a cn_check_magic() call which was commented out by mistake in
previous commit.
 1.12 06-Mar-2006  bouyer xenconscn_getc(): if xencons_console_device is NULL loop here instead
of in ddb.
 1.11 15-Jan-2006  bouyer branches: 1.11.2; 1.11.4; 1.11.6;
Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.10 11-Dec-2005  christos branches: 1.10.2;
merge ktrace-lwp.
 1.9 06-Sep-2005  kleink Change the driver open function's conditional for overriding exclusive tty
use from checking the proc's uid to suser(9), and account for the use of
privileges. Noted by David Holland in PR kern/31126.
 1.8 20-Apr-2005  bouyer branches: 1.8.2;
Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.

Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.7 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.6 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.5 11-Apr-2005  yamt xencons_start: retry when queue is full.
 1.4 10-Mar-2005  bouyer branches: 1.4.2;
Report which IRQ is used.
 1.3 10-Mar-2005  xtraeme Add missing #ifdef DDB for db_max_line()
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 24-Apr-2004  cl branches: 1.1.2; 1.1.4; 1.1.8; 1.1.10; 1.1.12;
Consistently use xencons for eveything reffering to Xen's virtual console.

rename arch/xen/xen/console.c -> arch/xen/xen/xencons.c
 1.1.12.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.10.1 29-Apr-2005  kent sync with -current
 1.1.8.4 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.1.8.3 18-Jan-2005  bouyer Support domain0 console
 1.1.8.2 17-Dec-2004  bouyer Preliminary implementation of xenconscn_pollc() and xenconscn_getc().
Enouth to set the boot disk, need more work for ddb (needs to make it
work without interrupts at all).
 1.1.8.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.1.4.7 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.4.6 01-Apr-2005  skrll Sync with HEAD.
 1.1.4.5 04-Feb-2005  skrll Adapt to branch.
 1.1.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.1.4.2 03-Aug-2004  skrll Sync with HEAD
 1.1.4.1 24-Apr-2004  skrll file xencons.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.1.2.2 22-May-2004  he Pull up revision 1.1 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.1.2.1 24-Apr-2004  he file xencons.c was added on branch netbsd-2-0 on 2004-05-22 15:59:21 +0000
 1.4.2.7 08-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1625):
sys/arch/xen/xen/xencons.c: revision 1.20
- Add or change memory barrier to reflect the related operation
- In xencons_handler(), update in_cons inside the loop, otherwise,
we would trigger the xenconscn_getc() workaround wich reset cons and prod
to their original values, and this creates an infinite loop
Should fix the console hang reported by several users on port-xen@.
 1.4.2.6 09-May-2006  tron branches: 1.4.2.6.2;
Pull up following revision(s) (requested by bouyer in ticket #1305):
sys/arch/xen/xen/xencons.c: revision 1.15
Fix a race between xenconscn_getc() and xencons_handler() (which seems to be
triggered by recent xen3 hypervisor or tools): check sc->polling in
xencons_handler() and just return if set. Also, run xenconscn_getc() and putc()
at spltty().
While there, mark xencons_interface as volatile and use __UNVOLATILE() where
appropriate; and update the pointers in xencons_handler() if xenconscn_getc()
changed them (avoids replaying ddb commands in shell if ddb got called
by the magic string).
 1.4.2.5 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.4 28-Apr-2005  tron Pull up revision 1.8 (requested by bouyer in ticket #193):
Add polling support to the domain controller. Use this in xencons instead
of relying on the fact that the domain controller is still getting interrupts
when xenconscn_getc()/xenconscn_putc() is called.
Now that polling is fixed, move IPL_CTRL down between IPL_SOFTSERIAL and
IPL_TTY, fixing port-xen/29999 by YAMAMOTO Takashi.
Now that IPL_CTRL is low enouth, remove the softintr stuff from the
domain controller, and call wakeup() directly.
Thanks to YAMAMOTO Takashi and Christian Limpach for feedback and suggestions.
 1.4.2.3 28-Apr-2005  tron Pull up revision 1.7 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.4.2.2 28-Apr-2005  tron Pull up revision 1.6 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4.2.1 13-Apr-2005  tron Pull up revision 1.5 (requested by yamt in ticket #144):
xencons_start: retry when queue is full.
 1.4.2.6.2.1 08-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #1625):
sys/arch/xen/xen/xencons.c: revision 1.20
- Add or change memory barrier to reflect the related operation
- In xencons_handler(), update in_cons inside the loop, otherwise,
we would trigger the xenconscn_getc() workaround wich reset cons and prod
to their original values, and this creates an infinite loop
Should fix the console hang reported by several users on port-xen@.
 1.8.2.6 07-Dec-2007  yamt sync with head
 1.8.2.5 27-Oct-2007  yamt sync with head.
 1.8.2.4 03-Sep-2007  yamt sync with head.
 1.8.2.3 26-Feb-2007  yamt sync with head.
 1.8.2.2 30-Dec-2006  yamt sync with head.
 1.8.2.1 21-Jun-2006  yamt sync with head.
 1.10.2.1 01-Feb-2006  yamt sync with head.
 1.11.6.4 11-Aug-2006  yamt sync with head
 1.11.6.3 24-May-2006  yamt sync with head.
 1.11.6.2 01-Apr-2006  yamt sync with head.
 1.11.6.1 13-Mar-2006  yamt sync with head.
 1.11.4.2 01-Jun-2006  kardel Sync with head.
 1.11.4.1 22-Apr-2006  simonb Sync with head.
 1.11.2.1 09-Sep-2006  rpaulo sync with head
 1.13.4.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.13.4.1 31-Mar-2006  tron Merge 2006-03-31 NetBSD-current into the "peter-altq" branch.
 1.13.2.5 13-May-2006  elad sprinkle some #include <sys/kauth.h> in files that use kauth kpi but
don't include it yet. hopefully this will prevent some fallout.
 1.13.2.4 11-May-2006  elad sync with head
 1.13.2.3 19-Apr-2006  elad sync with head - hopefully this will work
 1.13.2.2 10-Mar-2006  elad generic_authorize() -> kauth_authorize_generic().
 1.13.2.1 08-Mar-2006  elad Adapt to kernel authorization KPI.

I expect *some* lossage here...
 1.18.6.1 22-Oct-2006  yamt sync with head
 1.18.4.3 01-Feb-2007  ad Sync with head.
 1.18.4.2 12-Jan-2007  ad Sync with head.
 1.18.4.1 18-Nov-2006  ad Sync with head.
 1.19.2.1 08-Jan-2007  tron Pull up following revision(s) (requested by bouyer in ticket #342):
sys/arch/xen/xen/xencons.c: revision 1.20
- Add or change memory barrier to reflect the related operation
- In xencons_handler(), update in_cons inside the loop, otherwise,
we would trigger the xenconscn_getc() workaround wich reset cons and prod
to their original values, and this creates an infinite loop
Should fix the console hang reported by several users on port-xen@.
 1.21.2.1 12-Mar-2007  rmind Sync with HEAD.
 1.22.24.3 21-Nov-2007  bouyer Sync with HEAD
 1.22.24.2 25-Oct-2007  bouyer Sync with HEAD.
 1.22.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.22.20.2 09-Jan-2008  matt sync with HEAD
 1.22.20.1 06-Nov-2007  matt sync with HEAD
 1.22.18.3 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.22.18.2 21-Nov-2007  joerg Sync with HEAD.
 1.22.18.1 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.22.2.2 03-Dec-2007  ad Sync with HEAD.
 1.22.2.1 23-Oct-2007  ad Sync with head.
 1.23.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.25.14.3 17-Jan-2009  mjf Sync with HEAD.
 1.25.14.2 28-Sep-2008  mjf Sync with HEAD.
 1.25.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.27.10.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.27.10.1 19-Oct-2008  haad Sync with HEAD.
 1.27.6.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.27.4.4 11-Mar-2010  yamt sync with head
 1.27.4.3 19-Aug-2009  yamt sync with head.
 1.27.4.2 20-Jun-2009  yamt sync with head
 1.27.4.1 04-May-2009  yamt sync with head.
 1.29.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.31.2.10 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.31.2.9 07-May-2011  jym KNF.
 1.31.2.8 02-May-2011  jym Sync with head.
 1.31.2.7 24-Oct-2010  jym Sync with HEAD
 1.31.2.6 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.31.2.5 01-Nov-2009  jym Sync with HEAD.
 1.31.2.4 24-Jul-2009  jym - rework the page pinning API, so that now a function is provided for
each level of indirection encountered during virtual memory translations. Update
pmap accordingly. Pinning looks cleaner that way, and it offers the possibility
to pin lower level pages if necessary (NetBSD does not do it currently).

- some fixes and comments to explain how page validation/invalidation take
place during save/restore/migrate under Xen. L2 shadow entries from PAE are now
handled, so basically, suspend/resume works with PAE.

- fixes an issue reported by Christoph (cegger@) for xencons suspend/resume
in dom0.

TODO:

- PAE save/restore is currently limited to single-user only, multi-user
support requires modifications in PAE pmap that should be discussed first. See
the comments about the L2 shadow pages cached in pmap_pdp_cache in this commit.

- grant table bug is still there; do not use the kernels of this branch
to test suspend/resume, unless you want to experience bad crashes in dom0,
and push the big red button.

Now there is light at the end of the tunnel :)

Note: XEN2 kernels will neither build nor work with this branch.
 1.31.2.3 23-Jul-2009  jym Sync with HEAD.
 1.31.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.31.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.35.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.35.4.1 31-May-2011  rmind sync with head
 1.38.6.1 18-Feb-2012  mrg merge to -current.
 1.38.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.38.2.1 17-Apr-2012  yamt sync with head
 1.39.10.1 18-May-2014  rmind sync with head
 1.39.6.2 03-Dec-2017  jdolecek update from HEAD
 1.39.6.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.40.2.1 10-Aug-2014  tls Rebase.
 1.43.2.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.43.2.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.43.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.44.2.1 10-Jun-2019  christos Sync with HEAD
 1.48.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.50.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.68 22-Sep-2022  bouyer Fix fallout from previous: ci_ipending is in the low bytes of ci_istate,
so we should not left-shift by 8 the ci_ipending value we want to add there.
Should fix KASSERT in Xen's idle_block() reported by Frank Kardel in private
mail.
 1.67 07-Sep-2022  knakahara NetBSD/x86: Raise the number of interrupt sources per CPU from 32 to 56.

There has been no objection for three years.
https://mail-index.netbsd.org/port-amd64/2019/09/22/msg003012.html
Implemented by nonaka@n.o, updated by me.
 1.66 04-Sep-2022  bouyer Release mutexes earlier in xenevt_free(), to call *destroy and kmem_free
without holding spin locks. Fixes an occasional deadlock seen when the qemu
backing a HVM domU exits.
 1.65 27-May-2022  bouyer xenevt_setipending(): also increase xenevt_ih->ih_pending, so that
the handler will be called when the IPL is lowered.
 1.64 24-Jul-2021  andvar Fix all remaining typos, mainly in comments but also in few definitions and log messages, reported by me in PR kern/54889.
Also fixed some additional typos in comments, found on review of same files or typos.
 1.63 11-Jan-2021  skrll branches: 1.63.4;
Trailing whitespace
 1.62 11-Jan-2021  skrll Fix spelling of preferred in comments. While here improve grammar in
one of the comments.
 1.61 30-Nov-2020  bouyer Work in progress on dom0 PVH support. kernel boots and xl info works,
but we can't start a domU yet.
 1.60 07-May-2020  bouyer branches: 1.60.2;
Go back using cpu_info_primary, all events are bound to vCPU 0 by default.
Register the event handler on cpu_info_primary.
While there. update the event counter on interrupts.
Finally this should be MP-safe.
 1.59 06-May-2020  bouyer The event channel is not necesserely bound to the primary CPU.
Use the CPU returned in the interrupt handler instead.
 1.58 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.57 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.56 07-Apr-2020  jdolecek branches: 1.56.2;
switch from malloc() to kmem_zalloc()
 1.55 06-Apr-2020  jdolecek just pass true as known_mpsafe for interrupt establish, drop the
(level != IPL_VM); NFCI, this uses IPL_HIGH so was mpsafe already
 1.54 22-Nov-2019  martin Make this files compilable w/o options DIAGNOSTIC
 1.53 25-Dec-2018  cherry branches: 1.53.4;
Excise XEN specific code out of x86/x86/intr.c into xen/x86/xen_intr.c

While at it, separate the source function tracking so that the interrupt
paths are truly independant.

Use weak symbol exporting to provision for future PVHVM co-existence
of both files, but with independant paths. Introduce assembler code
such that in a unified scenario, native interrupts get first priority
in spllower(), followed by XEN event callbacks. IPL management and
semantics are unchanged - native handlers and xen callbacks are
expected to maintain their ipl related semantics.

In summary, after this commit, native and XEN now have completely
unrelated interrupt handling mechanisms, including
intr_establish_xname() and assembler stubs and intr handler
management.

Happy Christmas!
 1.52 24-Dec-2018  cherry 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.51 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.50 17-Oct-2018  cherry The known_mpsafe parameter is used by intr.c:intr_establish_xname()
especially in the non -D MULTIPROCESSOR case. We used it incorrectly.

Fix this.
 1.49 10-Oct-2018  cherry Do not re-expose the innards of evtchn.c, now that we have a way
to register interrupts via intr.c:intr_establish_xname()

evtchn.c is going to get refactored soon, so use the latter method.
 1.48 30-Nov-2017  christos branches: 1.48.2; 1.48.4;
add fo_name so we can identify the fileops in a simple way.
 1.47 16-Jul-2017  cherry branches: 1.47.2;
Unify the xen and native x86/ interrupt setup functions and
spl traversal data structures.

This is towards PVHVM.
 1.46 01-Jun-2017  chs remove checks for failure after memory allocation calls that cannot fail:

kmem_alloc() with KM_SLEEP
kmem_zalloc() with KM_SLEEP
percpu_alloc()
pserialize_create()
psref_class_create()

all of these paths include an assertion that the allocation has not failed,
so callers should not assert that again.
 1.45 07-Jul-2016  msaitoh KNF. Remove extra spaces. No functional change.
 1.44 20-Aug-2015  christos include "ioconf.h" to get the 'void <driver>attach(int count);' prototype.
 1.43 30-May-2015  joerg Improve wording.
 1.42 22-May-2015  bouyer Fix off by one error, pointed out by Wei Liu in port-xen/49919
 1.41 25-Jul-2014  dholland branches: 1.41.2; 1.41.4;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.40 16-Mar-2014  dholland branches: 1.40.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.39 03-Dec-2011  bouyer branches: 1.39.4; 1.39.8; 1.39.10; 1.39.12; 1.39.18;
hypervisor_unmask_event(): don't check/update evtchn_pending_sel for the
current CPU, but for any CPU which may accept this event.
xen/xenevt.c: more use of atomic ops and locks where appropriate, and some
other SMP fixes. Handle all events on the primary CPU (may be revisited
later). Set/clear ci_evtmask[] for watched events.

This should fix the problems on dom0 kernels reported by jym@
 1.38 11-Aug-2011  cherry branches: 1.38.2;
Make event/interrupt handling MP aware
 1.37 22-May-2011  rmind branches: 1.37.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.36 20-Dec-2009  dsl branches: 1.36.4; 1.36.6;
If a multithreaded app closes an fd while another thread is blocked in
read/write/accept, then the expectation is that the blocked thread will
exit and the close complete.
Since only one fd is affected, but many fd can refer to the same file,
the close code can only request the fs code unblock with ERESTART.
Fixed for pipes and sockets, ERESTART will only be generated after such
a close - so there should be no change for other programs.
Also rename fo_abort() to fo_restart() (this used to be fo_drain()).
Fixes PR/26567
 1.35 09-Dec-2009  dsl Rename fo_drain() to fo_abort(), 'drain' is used to mean 'wait for output
do drain' in many places, whereas fo_drain() was called in order to force
blocking read()/write() etc calls to return to userspace so that a close()
call from a different thread can complete.
In the sockets code comment out the broken code in the inner function,
it was being called from compat code.
 1.34 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.33 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.32 04-Apr-2009  ad Add fileops::fo_drain(), to be called from fd_close() when there is more
than one active reference to a file descriptor. It should dislodge threads
sleeping while holding a reference to the descriptor. Implemented only for
sockets but should be extended to pipes, fifos, etc.

Fixes the case of a multithreaded process doing something like the
following, which would have hung until the process got a signal.

thr0 accept(fd, ...)
thr1 close(fd)
 1.31 10-Mar-2009  bouyer More i386PAE fixes:
- x86_round_page, x86_trunc_page, x86_btop and x86_ptob macros are used with
physical addresses; cast to paddr_t instead of u_long. Issue pointed out
by jym@
- machine_to_phys_mapping[] is a long. This is fine as it holds page
frame numbers (and this fits in a 32bit int as physical addresses are
only 36bits), but cast to paddr_t before << PAGE_SHIFT
- xen_start_info.store_mfn is a long; cast it to paddr_t before << PAGE_SHIFT.
should fix issue pointed out by cegger@
 1.30 17-Dec-2008  cegger branches: 1.30.2;
kill MALLOC and FREE macros.
 1.29 05-Sep-2008  tron branches: 1.29.2; 1.29.4;
Compile NetBSD/amd64 kernels with "-Wextra". Patches contributed by
Juan RP in PR port-amd64/39266.
 1.28 07-Jun-2008  bouyer branches: 1.28.4;
Initialize ih_ilevel for the special xenevt_processevt() handler.
Should fix KASSERT panic reported by Sarton O'Brien and Kazushi Marukawa
on port-xen@
 1.27 19-Apr-2008  cegger branches: 1.27.2; 1.27.4;
Use interrupt biglock wrapper as in x86/x86/intr.c
This change is based on http://mail-index.netbsd.org/port-amd64/2004/02/22/0000.html
OK bouyer
 1.26 14-Apr-2008  cegger branches: 1.26.2;
- use POSIX integer types
- ansify functions
 1.25 22-Mar-2008  ad Make it compile.
 1.24 01-Mar-2008  rmind Fix my previous confuse of the code: use selinit/seldestroy in correct
places, and fix missed selnotify call.
 1.23 01-Mar-2008  rmind Welcome to 4.99.55:

- Add a lot of missing selinit() and seldestroy() calls.

- Merge selwakeup() and selnotify() calls into a single selnotify().

- Add an additional 'events' argument to selnotify() call. It will
indicate which event (POLL_IN, POLL_OUT, etc) happen. If unknown,
zero may be used.

Note: please pass appropriate value of 'events' where possible.
Proposed on: <tech-kern>
 1.22 19-Feb-2008  bouyer branches: 1.22.2; 1.22.6;
The event bitmasks provided by the hypervisor are unsigned long (so 64bits
on amd64). Make sure to use the right type to store and manipulate them.
This fixes amd64, where basically any event channel > 31 was not working
(and you get there after starting/stopping a domU a few times). Things
would occasionally unwedge though the spllower() callbacks.
 1.21 19-Feb-2008  bouyer Fix xenevt to not call softint_schedule() above IPL_HIGH:
Register a ipl callback for IPL_HIGH.
if the current ipl level is too high, just record the event in a bitmap,
and record IPL_HIGH as pending. The callback will process the pending events.
 1.20 17-Feb-2008  bouyer Add missing __KERNEL_RCSID()
 1.19 11-Jan-2008  bouyer 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.18 08-Jan-2008  bouyer Allocating up to 8k on stack isn't a good idea ...
 1.17 03-Dec-2007  ad branches: 1.17.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.16 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.15 17-Oct-2007  garbled branches: 1.15.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.14 08-Jun-2007  jld branches: 1.14.8; 1.14.10; 1.14.14;
Add needed locking in xenevt_fpoll; this fixes at least a lost-wakeup bug.
OK'ed by bouyer@.
 1.13 22-Feb-2007  thorpej branches: 1.13.4; 1.13.6; 1.13.12;
TRUE -> true, FALSE -> false
 1.12 21-Feb-2007  thorpej Replace the Mach-derived boolean_t type with the C99 bool type. A
future commit will replace use of TRUE and FALSE with true and false.
 1.11 10-Sep-2006  bouyer branches: 1.11.4; 1.11.6; 1.11.8;
Hypercalls returns negative error codes, so return -error in ioctl
function.
 1.10 25-Jul-2006  riz branches: 1.10.4;
Adapt to change in args for falloc(). Reported by Hisashi T Fujinaka
on current-users.
 1.9 14-May-2006  elad integrate kauth.
 1.8 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.7 12-Dec-2005  jld branches: 1.7.4; 1.7.6; 1.7.8; 1.7.10; 1.7.12;
Make xenevt.c compile after lwp changes; not tested yet.
 1.6 11-Dec-2005  christos merge ktrace-lwp.
 1.5 19-Apr-2005  bouyer branches: 1.5.2; 1.5.4;
Avoid a race between do_hypervisor_event() and stipending() that could
cause an event to be both handled and marked as pending, or being
marked as pending twice (triggering the diagnostic check
evtch_maskcount[port] == 0 in hypervisor_set_ipending):
mask and clear event by word of 32bit in do_hypervisor_event() or stipending(),
instead of by indiviual bits in do_event() or xenevt_event().
In addition this is marginally more efficient.
 1.4 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.3 11-Apr-2005  yamt fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.2 09-Mar-2005  bouyer branches: 1.2.2; 1.2.4; 1.2.6;
Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 31-Jan-2005  bouyer branches: 1.1.2;
file xenevt.c was initially added on branch bouyer-xen2.
 1.1.2.4 13-Feb-2005  bouyer Adapt for fdclone() change.
 1.1.2.3 12-Feb-2005  bouyer Revert parts of previous commit, which should not have been commited.
 1.1.2.2 12-Feb-2005  bouyer Complete poll implementation (forgot selrecord/selnotify).
 1.1.2.1 31-Jan-2005  bouyer First pieces of domain control operations:
- use an up to date xenio.h
- update privcmd.c for newer ioctls (incomplete, some just return an error for
now)
- add a /dev/xenevt pseudo-device, which provide to userland an interface to
xen events
Now xend starts, and basic xm commands (such as list) work.
 1.2.6.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.2.6.2 01-Apr-2005  skrll Sync with HEAD.
 1.2.6.1 09-Mar-2005  skrll file xenevt.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.2.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.2.4.1 09-Mar-2005  yamt file xenevt.c was added on branch yamt-km on 2005-03-19 08:33:26 +0000
 1.2.2.3 28-Apr-2005  tron Pull up revision 1.5 (requested by bouyer in ticket #192):
Avoid a race between do_hypervisor_event() and stipending() that could
cause an event to be both handled and marked as pending, or being
marked as pending twice (triggering the diagnostic check
evtch_maskcount[port] == 0 in hypervisor_set_ipending):
mask and clear event by word of 32bit in do_hypervisor_event() or stipending(),
instead of by indiviual bits in do_event() or xenevt_event().
In addition this is marginally more efficient.
 1.2.2.2 28-Apr-2005  tron Pull up revision 1.4 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.2.2.1 13-Apr-2005  tron Pull up revision 1.3 (requested by yamt in ticket #146):
fix a bug which corrupts runqueue.
when dealing with events, which are handed to xenevt pseudo device,
don't call wakeup(9)/selnotify(9) at too high IPL. PR/29792.
 1.5.4.9 24-Mar-2008  yamt sync with head.
 1.5.4.8 17-Mar-2008  yamt sync with head.
 1.5.4.7 27-Feb-2008  yamt sync with head.
 1.5.4.6 21-Jan-2008  yamt sync with head
 1.5.4.5 07-Dec-2007  yamt sync with head
 1.5.4.4 03-Sep-2007  yamt sync with head.
 1.5.4.3 26-Feb-2007  yamt sync with head.
 1.5.4.2 30-Dec-2006  yamt sync with head.
 1.5.4.1 21-Jun-2006  yamt sync with head.
 1.5.2.2 29-Apr-2005  kent sync with -current
 1.5.2.1 19-Apr-2005  kent file xenevt.c was added on branch kent-audio2 on 2005-04-29 11:28:30 +0000
 1.7.12.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.7.10.2 11-May-2006  elad sync with head
 1.7.10.1 08-Mar-2006  elad Adapt to kernel authorization KPI.

I expect *some* lossage here...
 1.7.8.3 14-Sep-2006  yamt sync with head.
 1.7.8.2 11-Aug-2006  yamt sync with head
 1.7.8.1 24-May-2006  yamt sync with head.
 1.7.6.1 01-Jun-2006  kardel Sync with head.
 1.7.4.1 09-Sep-2006  rpaulo sync with head
 1.10.4.1 18-Nov-2006  ad Sync with head.
 1.11.8.2 03-Jun-2008  skrll Sync with netbsd-4.
 1.11.8.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.11.6.1 27-Feb-2007  yamt - sync with head.
- move sched_changepri back to kern_synch.c as it doesn't know PPQ anymore.
 1.11.4.2 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.11.4.1 16-Jul-2007  liamjfoy Pull up following revision(s) (requested by jld in ticket #763):
sys/arch/xen/xen/xenevt.c: revision 1.14
Add needed locking in xenevt_fpoll; this fixes at least a lost-wakeup bug.
OK'ed by bouyer@.
 1.13.12.1 26-Jun-2007  garbled Sync with HEAD.
 1.13.6.1 11-Jul-2007  mjf Sync with head.
 1.13.4.2 03-Dec-2007  ad Sync with HEAD.
 1.13.4.1 15-Jul-2007  ad Sync with head.
 1.14.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.14.10.3 23-Mar-2008  matt sync with HEAD
 1.14.10.2 09-Jan-2008  matt sync with HEAD
 1.14.10.1 06-Nov-2007  matt sync with HEAD
 1.14.8.2 09-Dec-2007  jmcneill Sync with HEAD.
 1.14.8.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.15.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.15.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.17.6.2 08-Jan-2008  bouyer Make XEN kernels build again.
 1.17.6.1 08-Jan-2008  bouyer Sync with HEAD
 1.22.6.5 17-Jan-2009  mjf Sync with HEAD.
 1.22.6.4 28-Sep-2008  mjf Sync with HEAD.
 1.22.6.3 29-Jun-2008  mjf Sync with HEAD.
 1.22.6.2 02-Jun-2008  mjf Sync with HEAD.
 1.22.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.22.2.1 24-Mar-2008  keiichi sync with head.
 1.26.2.2 17-Jun-2008  yamt sync with head.
 1.26.2.1 18-May-2008  yamt sync with head.
 1.27.4.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.27.4.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.27.2.3 11-Mar-2010  yamt sync with head
 1.27.2.2 19-Aug-2009  yamt sync with head.
 1.27.2.1 04-May-2009  yamt sync with head.
 1.28.4.1 19-Oct-2008  haad Sync with HEAD.
 1.29.4.3 22-May-2015  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1965):
sys/arch/xen/xen/xenevt.c: revision 1.42

Fix off by one error, addresses port-xen/49919.
 1.29.4.2 30-Sep-2009  snj branches: 1.29.4.2.2; 1.29.4.2.6;
Pull up following revision(s) (requested by bouyer in ticket #1040):
sys/arch/i386/include/param.h: revision 1.71
sys/arch/i386/i386/db_memrw.c: revision 1.25
sys/arch/xen/include/xenpmap.h: revision 1.22
sys/arch/xen/xen/xenevt.c: revision 1.31
More i386PAE fixes:
- x86_round_page, x86_trunc_page, x86_btop and x86_ptob macros are used with
physical addresses; cast to paddr_t instead of u_long. Issue pointed out
by jym@
- machine_to_phys_mapping[] is a long. This is fine as it holds page
frame numbers (and this fits in a 32bit int as physical addresses are
only 36bits), but cast to paddr_t before << PAGE_SHIFT
- xen_start_info.store_mfn is a long; cast it to paddr_t before << PAGE_SHIFT.
should fix issue pointed out by cegger@
 1.29.4.1 04-Apr-2009  snj branches: 1.29.4.1.4;
Pull up following revision(s) (requested by ad in ticket #661):
sys/arch/xen/xen/xenevt.c: revision 1.32
sys/compat/svr4/svr4_net.c: revision 1.56
sys/compat/svr4_32/svr4_32_net.c: revision 1.19
sys/dev/dmover/dmover_io.c: revision 1.32
sys/dev/putter/putter.c: revision 1.21
sys/kern/kern_descrip.c: revision 1.190
sys/kern/kern_drvctl.c: revision 1.23
sys/kern/kern_event.c: revision 1.64
sys/kern/sys_mqueue.c: revision 1.14
sys/kern/sys_pipe.c: revision 1.109
sys/kern/sys_socket.c: revision 1.59
sys/kern/uipc_syscalls.c: revision 1.136
sys/kern/vfs_vnops.c: revision 1.164
sys/kern/uipc_socket.c: revision 1.188
sys/net/bpf.c: revision 1.144
sys/net/if_tap.c: revision 1.55
sys/opencrypto/cryptodev.c: revision 1.47
sys/sys/file.h: revision 1.67
sys/sys/param.h: patch
sys/sys/socketvar.h: revision 1.119
Add fileops::fo_drain(), to be called from fd_close() when there is more
than one active reference to a file descriptor. It should dislodge threads
sleeping while holding a reference to the descriptor. Implemented only for
sockets but should be extended to pipes, fifos, etc.
Fixes the case of a multithreaded process doing something like the
following, which would have hung until the process got a signal.
thr0 accept(fd, ...)
thr1 close(fd)
 1.29.4.2.6.1 22-May-2015  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1965):
sys/arch/xen/xen/xenevt.c: revision 1.42

Fix off by one error, addresses port-xen/49919.
 1.29.4.2.2.1 22-May-2015  sborrill Pull up the following revisions(s) (requested by bouyer in ticket #1965):
sys/arch/xen/xen/xenevt.c: revision 1.42

Fix off by one error, addresses port-xen/49919.
 1.29.4.1.4.1 21-Apr-2010  matt sync to netbsd-5
 1.29.2.2 28-Apr-2009  skrll Sync with HEAD.
 1.29.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.30.2.4 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.30.2.3 24-Oct-2010  jym Sync with HEAD
 1.30.2.2 01-Nov-2009  jym Sync with HEAD.
 1.30.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.36.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.36.4.1 31-May-2011  rmind sync with head
 1.37.2.1 04-Aug-2011  cherry first cut at per-cpu event handling
 1.38.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.38.2.1 17-Apr-2012  yamt sync with head
 1.39.18.1 27-May-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1299):
sys/arch/xen/xen/xenevt.c: revision 1.42
Fix off by one error, pointed out by Wei Liu in port-xen/49919
 1.39.12.1 18-May-2014  rmind sync with head
 1.39.10.1 27-May-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1299):
sys/arch/xen/xen/xenevt.c: revision 1.42
Fix off by one error, pointed out by Wei Liu in port-xen/49919
 1.39.8.2 03-Dec-2017  jdolecek update from HEAD
 1.39.8.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.39.4.1 27-May-2015  msaitoh Pull up following revision(s) (requested by bouyer in ticket #1299):
sys/arch/xen/xen/xenevt.c: revision 1.42
Fix off by one error, pointed out by Wei Liu in port-xen/49919
 1.40.2.1 10-Aug-2014  tls Rebase.
 1.41.4.3 28-Aug-2017  skrll Sync with HEAD
 1.41.4.2 22-Sep-2015  skrll Sync with HEAD
 1.41.4.1 06-Jun-2015  skrll Sync with HEAD
 1.41.2.1 22-May-2015  snj Pull up following revision(s) (requested by bouyer in ticket #796):
sys/arch/xen/xen/xenevt.c: revision 1.42
Fix off by one error, pointed out by Wei Liu in port-xen/49919
 1.47.2.2 16-Jul-2017  cherry 2302677
 1.47.2.1 16-Jul-2017  cherry file xenevt.c was added on branch perseant-stdc-iso10646 on 2017-07-16 14:02:49 +0000
 1.48.4.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.48.4.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.48.4.1 10-Jun-2019  christos Sync with HEAD
 1.48.2.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.48.2.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.48.2.1 20-Oct-2018  pgoyette Sync with head
 1.53.4.1 22-Nov-2019  bouyer Pull up following revision(s) (requested by martin in ticket #463):
sys/arch/xen/xen/xenevt.c: revision 1.54
sys/arch/arm/omap/obio_mputmr.c: revision 1.9
sys/arch/evbarm/beagle/beagle_machdep.c: revision 1.79
Make this files compilable w/o options DIAGNOSTIC
 1.56.2.1 12-Apr-2020  bouyer Get rid of xen-specific ci_x* interrupt handling:
- use the general SIR mechanism, reserving 3 more slots for IPL_VM, IPL_SCHED
and IPL_HIGH
- remove specific handling from C sources, or change to ipending
- convert IPL number to SIR number in various places
- Remove XUNMASK/XPENDING in assembly or change to IUNMASK/IPENDING
- remove Xen-specific ci_xsources, ci_xmask, ci_xunmask, ci_xpending from
struct cpu_info
- for now remove a KASSERT that there are no pending interrupts in
idle_block(). We can get there with some software interrupts pending
in autoconf XXX needs to be looked at.
 1.60.2.2 03-Apr-2021  thorpej Sync with HEAD.
 1.60.2.1 14-Dec-2020  thorpej Sync w/ HEAD.
 1.63.4.1 01-Aug-2021  thorpej Sync with HEAD.
 1.41 25-Feb-2023  riastradh xengnt(4): Nix trailing whitespace.

No functional change intended.
 1.40 25-Feb-2023  riastradh xengnt(4): Membar audit.

This had the sense of membars reversed, presumably because xen_rmb
and xen_wmb had gotten reversed at some point.
 1.39 03-Jun-2022  bouyer branches: 1.39.4;
Restore (again) support for grant tables v1: pvshim doesn't support
v2 (it doesn't even support GNTTABOP_set_version) some setups will
likely require pvshim in the future.
 1.38 13-May-2020  jdolecek need to set the version on resume same as during initialization

part of PR port-xen/55207
 1.37 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.36 20-Apr-2020  bouyer Pull from bouyer-xenpvh:
Bad news:
* grant table v2 is not supported for HVM guests on 4.11 at last.
* see xen/arch/x86/hvm/hypercall.c in Xen sources (missing
* GNTTABOP_get_status_frames)

So restore grant table v1 for !XENPV
 1.35 14-Apr-2020  jdolecek DRY - use pages for sizeof() for the frame allocations, so it get's allocated
the correct size without repeating the type
 1.34 14-Apr-2020  jdolecek actually fix also the allocated memory size for the getstatus frames on i386
 1.33 10-Apr-2020  jdolecek gnttab_get_status_frames frame_list is array if uint64_t values, not u_long
like gnttab_setup_table, fix to use uint64_t

fixes compilation on i386 with XEN3PAE_DOM[0U], pointed out by kre@
 1.32 10-Apr-2020  jdolecek add KASSERT() for owned grant_lock in xengnt_get_entry()
 1.31 09-Apr-2020  jdolecek update to __XEN_INTERFACE_VERSION__ 0x0003020a aka Xen 3.2.10

this brings grant memory v2 support:
- status separated from flags - revoking access needs just memory barrier,
no need for expensive cmpxchg16 any more
- sub-page hypervisor copy-only grants, to be used by xennet(4)
- 64-bit frame, i.e. support for DomU RAM >16TB

the grant table is now always allocated on boot to maximum size, it's now
never grown in runtime; switch back to regular kmem_alloc()/kmem_free()

code now requires v2 support, no compatibility for grant version 1 retained -
Xen v2 support predates all currently supported Xen versions

also interface for baloon changed slightly, code updated
 1.30 09-Apr-2020  bouyer xengnt_more_entries() can be called from interrupt context so use
kmem_intr_alloc()/free() here
 1.29 07-Apr-2020  jdolecek branches: 1.29.2;
convert from malloc() to kmem_alloc()
 1.28 05-Apr-2020  jdolecek remove xengnt_grant_transfer() and xengnt_revoke_transfer(), nothing
uses it any more
 1.27 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.26 06-Feb-2019  cherry Add the codepath for grant table updates for VMs with an auto
translated physmap.
 1.25 24-Oct-2012  royger branches: 1.25.38;
xen: don't use grants 0-8

Not all grants from the first frame can be used, grants from 0 to 8
(both included) are reserved for external tools. Using this grants
caused system crashes and fs corruption.

Closes PR port-xen/47057 and port-xen/47056
Reviewed by bouyer@
 1.24 30-Jun-2012  jym branches: 1.24.2;
Use setter to set xenguest_handles.
 1.23 23-Feb-2012  bouyer Fix locking; xengnt_get_entry() needs to have the lock held to avoid
allocating the same entry twice. xengnt_more_entries() also needs the lock
for the same reason.
 1.22 04-Feb-2012  riz branches: 1.22.2;
Add a 'void' to unbreak this with -Wold-style-definition
 1.21 09-Dec-2011  cherry Use mutex(9) instead of spl(9). Add further locks around grant table access.
 1.20 20-Sep-2011  jym branches: 1.20.2; 1.20.6;
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.19 15-Jun-2011  rmind - privpgop_fault: call pmap_update() before uvmfault_unlockall().
- privcmd_ioctl, xengnt_more_entries: add missing pmap_update().
 1.18 26-May-2011  jym branches: 1.18.2;
Split KASSERT(... && ...) in two, so it's easier to spot which one
fired with DIAGNOSTIC.
 1.17 23-Jan-2010  cegger branches: 1.17.4; 1.17.6;
fix address overflow with 32bit PAE.
Reported and tested by Mark Davies on port-xen@.
 1.16 07-Nov-2009  cegger Add a flags argument to pmap_kenter_pa(9).
Patch showed on tech-kern@ http://mail-index.netbsd.org/tech-kern/2009/11/04/msg006434.html
No objections.
 1.15 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.14 16-Mar-2009  cegger ansify function definitions
 1.13 19-Jan-2009  jym branches: 1.13.2;
- fix some spaces
- add comments

No functional change.
 1.12 16-Jan-2009  jym 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 13-Nov-2008  cegger Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.10 25-Oct-2008  jym branches: 1.10.2; 1.10.4; 1.10.8;
- add and fix some comments in xennet and xbd code
- add BEST_SUSPENDED macro, the xennet equivalent of BLKIF_STATE_SUSPENDED
for xbd (indicate that backend is in suspended state)
- use GNTST_okay to check the grant table setup status returned by hypercall,
rather than 0

No functional changes.
 1.9 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.8 05-Sep-2008  tron Compile NetBSD/amd64 kernels with "-Wextra". Patches contributed by
Juan RP in PR port-amd64/39266.
 1.7 27-Apr-2008  bouyer branches: 1.7.2; 1.7.6;
xengnt_more_entries(): fix memory leak, noticed by Jean-Yves Migeon
 1.6 17-Feb-2008  bouyer branches: 1.6.6; 1.6.8; 1.6.10;
Add missing __KERNEL_RCSID()
 1.5 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.4 17-Oct-2007  garbled branches: 1.4.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.3 05-Sep-2007  bouyer branches: 1.3.4;
Switch to dynamically-sized grant tables, available since Xen-3.1.0.
This removes a hard limit that would prevent a guest from running with more
than 7 virtual network interface.
If running on a hypervisor that doesn't support GNTTABOP_query_size, fall back
to a 4-pages grant table, so this change is backward-compatible.
 1.2 19-Mar-2006  bouyer branches: 1.2.2; 1.2.4; 1.2.10; 1.2.16; 1.2.20; 1.2.26; 1.2.34; 1.2.36; 1.2.40; 1.2.44; 1.2.46;
Don't include debug code by default.
 1.1 06-Mar-2006  bouyer branches: 1.1.2; 1.1.4; 1.1.6;
Implement granttables operations.
 1.1.6.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.1.4.3 01-Apr-2006  yamt sync with head.
 1.1.4.2 13-Mar-2006  yamt sync with head.
 1.1.4.1 06-Mar-2006  yamt file xengnt.c was added on branch yamt-pdpolicy on 2006-03-13 09:07:07 +0000
 1.1.2.1 19-Apr-2006  elad sync with head - hopefully this will work
 1.2.46.3 23-Mar-2008  matt sync with HEAD
 1.2.46.2 09-Jan-2008  matt sync with HEAD
 1.2.46.1 06-Nov-2007  matt sync with HEAD
 1.2.44.2 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.44.1 02-Oct-2007  joerg Sync with HEAD.
 1.2.40.1 10-Sep-2007  skrll Sync with HEAD.
 1.2.36.2 03-Jun-2008  skrll Sync with netbsd-4.
 1.2.36.1 23-Sep-2007  wrstuden Sync with somewhat-recent netbsd-4.
 1.2.34.1 03-Oct-2007  garbled Sync with HEAD
 1.2.26.2 03-Dec-2007  ad Sync with HEAD.
 1.2.26.1 09-Oct-2007  ad Sync with head.
 1.2.20.2 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.2.20.1 11-Sep-2007  xtraeme Pull up following revision(s) (requested by bouyer in ticket #875):
sys/arch/xen/include/granttables.h: revision 1.2
sys/arch/xen/include/xen3-public/grant_table.h: revision 1.4
sys/arch/xen/xen/xengnt.c: revision 1.3
Switch to dynamically-sized grant tables, available since Xen-3.1.0.
This removes a hard limit that would prevent a guest from running with more
than 7 virtual network interface.
If running on a hypervisor that doesn't support GNTTABOP_query_size, fall back
to a 4-pages grant table, so this change is backward-compatible.
 1.2.16.2 09-Sep-2006  rpaulo sync with head
 1.2.16.1 19-Mar-2006  rpaulo file xengnt.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.2.10.5 27-Feb-2008  yamt sync with head.
 1.2.10.4 07-Dec-2007  yamt sync with head
 1.2.10.3 27-Oct-2007  yamt sync with head.
 1.2.10.2 21-Jun-2006  yamt sync with head.
 1.2.10.1 19-Mar-2006  yamt file xengnt.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.2.4.2 22-Apr-2006  simonb Sync with head.
 1.2.4.1 19-Mar-2006  simonb file xengnt.c was added on branch simonb-timecounters on 2006-04-22 11:38:11 +0000
 1.2.2.3 15-Oct-2007  riz Pull up following revision(s) (requested by bouyer in ticket #1836):
sys/arch/xen/include/granttables.h: revision 1.2 via patch
sys/arch/xen/include/xen3-public/grant_table.h: revision 1.4 via patch
sys/arch/xen/xen/xengnt.c: revision 1.3 via patch
Switch to dynamically-sized grant tables, available since Xen-3.1.0.
This removes a hard limit that would prevent a guest from running with more
than 7 virtual network interface.
If running on a hypervisor that doesn't support GNTTABOP_query_size, fall back
to a 4-pages grant table, so this change is backward-compatible.
 1.2.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.2.2.1 19-Mar-2006  tron file xengnt.c was added on branch netbsd-3 on 2006-04-07 12:51:26 +0000
 1.3.4.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.2.2 18-Feb-2008  mjf Sync with HEAD.
 1.4.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.6.10.3 11-Mar-2010  yamt sync with head
 1.6.10.2 04-May-2009  yamt sync with head.
 1.6.10.1 16-May-2008  yamt sync with head.
 1.6.8.1 18-May-2008  yamt sync with head.
 1.6.6.3 17-Jan-2009  mjf Sync with HEAD.
 1.6.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.6.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.7.6.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.7.6.1 19-Oct-2008  haad Sync with HEAD.
 1.7.2.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.10.8.1 21-Apr-2010  matt sync to netbsd-5
 1.10.4.2 26-Oct-2012  sborrill Pull up the following revisions(s) (requested by royger in ticket #1805):
sys/arch/xen/xen/xengnt.c: revision 1.25 via patch

Prevents a memory corruption issue that freezes a Xen DomU and can also
cause fs corruption. Addresses PR port-xen/47057 and port-xen/47056
 1.10.4.1 30-Jan-2010  snj Pull up following revision(s) (requested by bouyer in ticket #1271):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.16
sys/arch/xen/xen/xengnt.c: revision 1.17 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.33
fix address overflow with 32bit PAE.
Reported and tested by Mark Davies on port-xen@.
 1.10.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.10.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.10.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.13.2.7 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.13.2.6 26-May-2011  jym Pull-up some modifications from -current to my branch.
 1.13.2.5 24-Oct-2010  jym Sync with HEAD
 1.13.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.13.2.3 01-Nov-2009  jym Sync with HEAD.
 1.13.2.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.13.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.17.6.1 06-Jun-2011  jruoho Sync with HEAD.
 1.17.4.1 31-May-2011  rmind sync with head
 1.18.2.2 04-Sep-2011  mhitch Use a mutex to protect updates to gnt_entries. Fixes a KASSERT panic
with an MP kernel.
 1.18.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.20.6.2 24-Feb-2012  mrg sync to -current.
 1.20.6.1 18-Feb-2012  mrg merge to -current.
 1.20.2.2 30-Oct-2012  yamt sync with head
 1.20.2.1 17-Apr-2012  yamt sync with head
 1.22.2.2 31-Oct-2012  riz Pull up following revision(s) (requested by royger in ticket #640):
sys/arch/xen/xen/xengnt.c: revision 1.25
xen: don't use grants 0-8
Not all grants from the first frame can be used, grants from 0 to 8
(both included) are reserved for external tools. Using this grants
caused system crashes and fs corruption.
Closes PR port-xen/47057 and port-xen/47056
Reviewed by bouyer@
 1.22.2.1 23-Feb-2012  riz branches: 1.22.2.1.4;
Pull up following revision(s) (requested by bouyer in ticket #40):
sys/arch/xen/xen/xengnt.c: revision 1.23
Fix locking; xengnt_get_entry() needs to have the lock held to avoid
allocating the same entry twice. xengnt_more_entries() also needs the lock
for the same reason.
 1.22.2.1.4.1 31-Oct-2012  riz Pull up following revision(s) (requested by royger in ticket #640):
sys/arch/xen/xen/xengnt.c: revision 1.25
xen: don't use grants 0-8
Not all grants from the first frame can be used, grants from 0 to 8
(both included) are reserved for external tools. Using this grants
caused system crashes and fs corruption.
Closes PR port-xen/47057 and port-xen/47056
Reviewed by bouyer@
 1.24.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.25.38.4 21-Apr-2020  martin Sync with HEAD
 1.25.38.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.25.38.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.25.38.1 10-Jun-2019  christos Sync with HEAD
 1.29.2.3 21-Apr-2020  bouyer It is possible to use grant_v2 with HVM guest; but the status table has
to be mapped using XENMEM_add_to_physmap.
 1.29.2.2 20-Apr-2020  bouyer Bad news:
* grant table v2 is not supported for HVM guests on 4.11 at last.
* see xen/arch/x86/hvm/hypercall.c in Xen sources (missing
* GNTTABOP_get_status_frames)

So restore grant table v1 for !XENPV
 1.29.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.39.4.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.4 17-Apr-2005  bouyer Remove
xenkbc at hypervisor
vga at hypervisor
they have been obsoleted by PCI and ISA support, and don't compile any more.
 1.3 26-Apr-2004  cl branches: 1.3.2; 1.3.4; 1.3.8; 1.3.10; 1.3.16;
Add mouse support.
 1.2 25-Apr-2004  cl Don't attach vga and keyboard if the domain is not privileged.
Also cleanup print command if attachment fails.
 1.1 24-Apr-2004  cl Add keyboard support and wscons config options.
 1.3.16.1 25-Apr-2005  tron Pull up file removal (requested by bouyer in ticket #186):
Remove
xenkbc at hypervisor
vga at hypervisor
they have been obsoleted by PCI and ISA support, and don't compile any more.
 1.3.10.1 29-Apr-2005  kent sync with -current
 1.3.8.2 08-Mar-2005  bouyer Add support for ISA bus.
Clean up console attachement, and add support for VGA/pckbc console.
Add support for USB devices, including USB audio (which means others audio
devices should work too)
Add some more generic options to XEN0.
 1.3.8.1 13-Dec-2004  bouyer Commit files from netbsd-2.0-xen-sparse/sys/arch/xen in the Xen-2.0
distribution. These are the files modified from the 2.0 tree to get
NetBSD/xen working with Xen 2.
 1.3.4.5 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.3.4.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.3.4.3 18-Sep-2004  skrll Sync with HEAD.
 1.3.4.2 03-Aug-2004  skrll Sync with HEAD
 1.3.4.1 26-Apr-2004  skrll file xenkbc.c was added on branch ktrace-lwp on 2004-08-03 10:43:19 +0000
 1.3.2.2 22-May-2004  he Pull up revisions 1.1-1.3 (requested by cl in ticket #337):
Upgrade xen support:
- add block device driver
- network device driver bug fixes
- support for vga/keyboard/mouse
- support for domain0 operations
- fix /dev/mem and i386_iopl, reboot, event dispatch
- fix clock support, cpu speed report, lazy fpu switching
- add xen12load loader
- sys/arch/xen parts of build.sh release support
[cl, ticket #337]
 1.3.2.1 26-Apr-2004  he file xenkbc.c was added on branch netbsd-2-0 on 2004-05-22 15:57:43 +0000
 1.5 20-Dec-2023  thorpej Remove unnecessary <sys/malloc.h>.
 1.4 02-Sep-2022  kre Fix the syntax of the non _LP64 code fragment to match that in the
_LP64 fragment, so hopefully 32 bit ports with XEN (i386) will
build again.
 1.3 01-Sep-2022  bouyer Call extent_alloc_subregion() with EX_MALLOCOK; under heavy I/O load we need
lots of them and the preallocatted extent descriptors are not enough.
 1.2 31-Aug-2022  bouyer Remove XENPV support from xenmem; it's only used for !XENPV at this time.
 1.1 31-Aug-2022  bouyer Work in progress on dom0 PVH support: ioctl support for tools.
Basically, in PVH mode (where XENFEAT_auto_translated_physmap is enabled),
the hypervisor will not map foreing ressources in our virtual address
space for us. Instead, we have to pass it an address in our physical
address space (but not mapped to some RAM) where the ressource will show up
and then enter this PA in pour page table.

For this, introduce xenmem_* which manage the PA space. In PVH mode this
is just allocated from the iomem_ex extent.

With this, I can start a PV domU, and the guest's kernel boots (and
the console works). It hangs because the backend driver can't map the
frontend ressources (yet).

Note that, per https://xenbits.xen.org/docs/unstable/support-matrix.html,
dom0 PVH support is still considered experimental by Xen.
 1.14 04-May-2020  jdolecek remove IPv4 csum offloading for xennet(4) - it's not complete, and even
if it was, it doesn't work with Linux Dom0 as it expects the IPv4 csum present
 1.13 03-May-2020  jdolecek return EOPNOTSUPP for unknown IP protocol (same as unknown ethernet type),
so that EINVAL is returned only when the packet is too short
 1.12 01-May-2020  jdolecek make the csum blank/undefer counters per interface
 1.11 26-Apr-2020  jdolecek if part of checksum was undeferred via sw and part not, count it as both
 1.10 22-Mar-2020  jdolecek actually in data_validated case, there is no need to inspect the data for Rx,
simply set the supported csum offload flags to skip the software csum
verification
 1.9 22-Mar-2020  jdolecek add support for skipping IPv6 checksum validation aka offloading -
for xennet(4) both Rx and Tx, for xvif(4) only Tx for now
 1.8 19-Mar-2020  jdolecek add support for skipping IPv4 checksums
 1.7 18-Mar-2020  jdolecek use NET[RT]XF_data_validated flag to mark when Tx packet has valid
checksum; this is used to skip software checksum validation on
xennet Rx side when configured for Rx offloading

in Dom0 assume that checksum is valid when the Tx mbuf has no offload flags
- in that case either it's local packet where checksum has just been
computed in software, or forwarded external packet already
verified when received on Dom0

practical offshot of this is that DomU doesn't re-verify checksum of
packets forwarded from external hosts, e.g. via bridge(4)
 1.6 16-Mar-2020  jdolecek drop the disabled M_EXT_ROMAP mbuf code, convert xennet_checksum_fill()
to use in_undefer_cksum() instead of custom code to compute the checksum
for Rx packets, and set csum_data appropriately for eventual hw offloading

make it possible to skip the sw checksum computation by appropriate Rx
flag similarily as we do for Tx

XXX for now, the Rx flag is mostly for testing as it only works for
dom0<->domu, need some further network stack changes to arrange for
the checksum to be eventually computed when packets goes outside xen
 1.5 16-Mar-2020  jdolecek drop the disabled M_EXT_ROMAP mbuf code, and convert xennet_checksum_fill()
to use in_undefer_cksum() instead of custom code to compute the checksum
for incoming packets

make it possible to defer/skip the checksum computation by appropriate Rx
flag similarily as we do for Tx; with
 1.4 14-Mar-2020  jdolecek make the packet length check less strict, allow the physical packet longer
than IP payload

this fixes problem where checksum was not recomputed for short
packets coming from Windows DOMU on same physical host, seems Windows
does some padding; reported by sborrill@

XXX pullup-9
 1.3 22-Nov-2007  bouyer branches: 1.3.80; 1.3.98; 1.3.106; 1.3.110;
Pull up the bouyer-xenamd64 branch to HEAD. This brings in amd64 support
to NetBSD/Xen, both Dom0 and DomU.
 1.2 10-Nov-2006  yamt branches: 1.2.4; 1.2.10; 1.2.28; 1.2.30; 1.2.34; 1.2.36;
xennet_checksum_fill: remove a debug printf.
 1.1 12-Jul-2006  yamt branches: 1.1.2; 1.1.6; 1.1.8; 1.1.10; 1.1.12;
implement a simple NETTXF_csum_blank/NETRXF_csum_blank workaround
so that we can talk with linux guests at least.
just fill checksum field of received packets if the flag is set.
maybe should be revisited later.
 1.1.12.1 10-Dec-2006  yamt sync with head.
 1.1.10.2 09-Sep-2006  rpaulo sync with head
 1.1.10.1 12-Jul-2006  rpaulo file xennet_checksum.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.1.8.1 18-Nov-2006  ad Sync with head.
 1.1.6.2 11-Aug-2006  yamt sync with head
 1.1.6.1 12-Jul-2006  yamt file xennet_checksum.c was added on branch yamt-pdpolicy on 2006-08-11 15:43:16 +0000
 1.1.2.2 13-Jul-2006  gdamore Merge from HEAD.
 1.1.2.1 12-Jul-2006  gdamore file xennet_checksum.c was added on branch gdamore-uart on 2006-07-13 17:49:07 +0000
 1.2.36.1 08-Dec-2007  mjf Sync with HEAD.
 1.2.34.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.2.30.1 09-Jan-2008  matt sync with HEAD
 1.2.28.1 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.2.10.1 03-Dec-2007  ad Sync with HEAD.
 1.2.4.3 07-Dec-2007  yamt sync with head
 1.2.4.2 30-Dec-2006  yamt sync with head.
 1.2.4.1 10-Nov-2006  yamt file xennet_checksum.c was added on branch yamt-lazymbuf on 2006-12-30 20:47:25 +0000
 1.3.110.1 16-Mar-2020  sborrill Pull up the following revisions(s) (requested by jdolecek in ticket #781):
sys/arch/xen/xen/xennet_checksum.c: revision 1.4

Make the packet length check less strict, allow the physical packet longer
than IP payload. This fixes problem where checksum was not recomputed for
short packets coming from Windows domU on same physical host as it seems
Windows does some padding.
 1.3.106.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.3.98.1 16-Mar-2020  sborrill Pull up the following revisions(s) (requested by jdolecek in ticket #1521):
sys/arch/xen/xen/xennet_checksum.c: revision 1.4

Make the packet length check less strict, allow the physical packet longer
than IP payload. This fixes problem where checksum was not recomputed for
short packets coming from Windows domU on same physical host as it seems
Windows does some padding.
 1.3.80.1 16-Mar-2020  sborrill Pull up the following revisions(s) (requested by jdolecek in ticket #1726):
sys/arch/xen/xen/xennet_checksum.c: revision 1.4

Make the packet length check less strict, allow the physical packet longer
than IP payload. This fixes problem where checksum was not recomputed for
short packets coming from Windows domU on same physical host as it seems
Windows does some padding.
 1.37 29-Jul-2009  cegger remove Xen2 support.
ok bouyer@
 1.36 18-Mar-2009  cegger Ansify function definitions w/o arguments. Generated with sed.
 1.35 16-Jan-2009  jym branches: 1.35.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.34 21-Oct-2008  cegger branches: 1.34.2;
introduce two macros: xendomain_is_dom0() and xendomain_is_privileged(). Use them.
 1.33 04-Jun-2008  ad branches: 1.33.4;
vm_page: put TAILQ_ENTRY into a union with LIST_ENTRY, so we can use both.
 1.32 14-Apr-2008  cegger branches: 1.32.2; 1.32.4; 1.32.6;
- use POSIX integer types
- ansify functions
 1.31 17-Feb-2008  bouyer branches: 1.31.6;
Add missing __KERNEL_RCSID()
 1.30 11-Jan-2008  bouyer 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.29 03-Dec-2007  ad branches: 1.29.6;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.28 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.27 07-Nov-2007  ad Merge from vmlocking:

- pool_cache changes.
- Debugger/procfs locking fixes.
- Other minor changes.
 1.26 17-Oct-2007  garbled branches: 1.26.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.25 26-Aug-2007  dyoung branches: 1.25.2; 1.25.6;
Constify.
 1.24 12-Mar-2007  ad branches: 1.24.8; 1.24.12; 1.24.16;
Pass an ipl argument to pool_init/POOL_INIT to be used when initializing
the pool's lock.
 1.23 04-Mar-2007  christos branches: 1.23.2;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.22 23-May-2006  bouyer branches: 1.22.10; 1.22.12; 1.22.16;
Don't forget to softintr_disestablish() when destroying a xvif.
 1.21 15-May-2006  dogcow branches: 1.21.2;
gcc4: initalize uninitialized data (one valid, one not quite as valid)
 1.20 01-Feb-2006  bouyer branches: 1.20.2; 1.20.4; 1.20.8;
Check the destination ethernet address when not in promiscous mode.
Fix problem where packets would be duplicated, possibly looping, when
a domU is doing IP routing.
Problem reported and fix tested by Mike M. Volokhov on port-xen

While there, add some __predict_false/true in conditionnals where
appropriate, remove a always-true test, and fix handling of
mbuf shortage.
 1.19 23-Jan-2006  yamt branches: 1.19.2;
merge xen pmap_enter, pmap_enter_ma, pmap_remap_pages.
ok'ed by Manuel Bouyer.
 1.18 15-Jan-2006  bouyer Snapshot of work in progress on NetBSD port to Xen3:
- kernel (both dom0 and domU) boot, console is functionnal and it can starts
software from a ramdisk
- there is no driver front-end expect console for domU yet.
- dom0 can probe devices and ex(4) work when Xen3 is booted without acpi
and apic support. But the on-board IDE doens't get interrupts.
The PCI code still needs work (it's hardcoded to mode 1). Some of this
code should be shared with ../x86
The physical insterrupt code needs to get MPBIOS and ACPI support, and
do interrupt routing to properly interract with Xen.
To enable Xen-3.0 support, add
options XEN3
to your kernel config file (this will disable Xen2 support)
Changes affecting Xen-2.0 support (no functionnal changes intended):
- get more constants from genassym for assembly code
- remove some unneeded registers move from start()
- map the shared info page from start(), and remove the pte = 0xffffffff hack
- vector.S: in hypervisor_callback() make sure %esi points to
HYPERVISOR_shared_info before accessing the info page. Remplace some
hand-written assembly with the equivalent macro defined in frameasm.h
- more debug code, dissabled by default.

while here added my copyright on some files I worked on in 2005.
 1.17 11-Dec-2005  christos branches: 1.17.2;
merge ktrace-lwp.
 1.16 08-Oct-2005  bouyer Also remove pkt_next now that packets are not recorded in a per-page list.
 1.15 08-Oct-2005  bouyer If a domain has more than one interface, a page can hold packets for different
xennetback instances. To support this, more fields from xni_page to xni_pkt.
This would also have the effect to move more code into the
while (!SLIST_EMPTY(&pkt_page->xni_pkt_head)) loop in xennetback_tx_free().
By passing xni_pkt instead of xni_page to xennetback_tx_free we can
avoid the loop, and the xni_pkt_head list completely. There is even a sligh
performance increase if the domU deals with xni_txring->event properly.

Based on comments from YAMAMOTO Takashi.
 1.14 03-Oct-2005  bouyer Fix problem pointed out by YAMAMOTO Takashi:
In theory mbufs can have an infinite life time and could block the transmit
ring (as slots are released when the mbuf external storage is freed). To
avoid this, when we're processing the last slot of the ring copy the buffer
and release the slot immediatly.
 1.13 02-Oct-2005  bouyer Major improvements to the network backend, leading to a 0-copy transmit and
receive system:
- on the receive side, attach the mapped buffer as external storage
instead of copying it. As a mapped buffer may not live much longer, we
have to deal with the fact that one page of buffer may containt several
packets, and it's not possible to map them several times. Use a hashed list
to keep track of mapped pages, and use reference counters.
- on the transmit side, when MCLBYTES == PAGE_SIZE, give away the mbuf
cluster page when possible instead of copying it. Keep a pool of physical
pages to map in place of the page we give away. When copying, use a
pool_cache(9) to manage copy buffers (use mclpool_cache when
MCLBYTES == PAGE_SIZE, otherwise use a private pool/pool_cache) instead
of a local list. This should reduce the number of hypercalls and MMU
operations in the copy case as well.
 1.12 10-Sep-2005  bouyer Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.11 18-May-2005  bouyer branches: 1.11.2;
- There is a possible race condition with the rx->event stuff, causing
event not to be sent when one is needed. Fixing this would require
one hypercall per packet, instead of one per NB_XMIT_PAGES_BATCH pages.
It's not worth it, so always send an event at the end of xennetback_ifstart()
- there is no callback mechanism to notify us when a guest has handled
packets we sent. If we stop transmitting because the ring is full or we're
out of pages when the ifq is also full, nothing will call
xennetback_ifstart() again and transmit is stalled. Abuse the watchdog
to kick the transmit queue once second after an out of ressources
condition.
 1.10 28-Apr-2005  yamt branches: 1.10.2;
xennetback_ifstart: test a correct variable, rather than a function.
 1.9 17-Apr-2005  bouyer Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.8 16-Apr-2005  bouyer Remove functions that just call another one.
Inline a function used at only one place.
 1.7 16-Apr-2005  bouyer Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.

While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.6 01-Apr-2005  yamt branches: 1.6.2;
merge yamt-km branch.
- don't use managed mappings/backing objects for wired memory allocations.
save some resources like pv_entry. also fix (most of) PR/27030.
- simplify kernel memory management API.
- simplify pmap bootstrap of some ports.
- some related cleanups.
 1.5 26-Mar-2005  bouyer Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.4 11-Mar-2005  bouyer branches: 1.4.2; 1.4.4;
Properly track connected/disconnected state, and set IFF_RUNNING only when
in connected state. Avoid a panic when the interface is configured
before being in connected state (e.g. when configured automatically by xend
when a domain is created).
 1.3 10-Mar-2005  bouyer - Don't reallocate transmit pages from xennetback_get_xmit_page(), as the
previously-allocated pages may not have been loaned to the remote domain
yet. Let the called deal with this.
- clean up the TX and RX loops to use less MASK_NETIF_xX_IDX() calls
- When we're done with the queued TX requests, try again in case some new
ones are available
- avoid leaking a xmit page if we queued NB_XMIT_PAGES_BATCH requests.
- remove dead code.
 1.2 09-Mar-2005  bouyer Merge the bouyer-xen2 branch. This add supports for the Xen 2.0 virtual
machine kernel (both privileged and non-privileged domains), and remove support
for the old xen 1.2.
 1.1 16-Feb-2005  bouyer branches: 1.1.2;
file xennetback.c was initially added on branch bouyer-xen2.
 1.1.2.4 08-Mar-2005  bouyer Improve ressource shortage recovery: if xen_shm_map() fails, register a
callback wich will retry the I/O when xen_shm_unmap() is called.
 1.1.2.3 08-Mar-2005  bouyer When a virtual device is destroyed (e.g. because a domain is destroyed),
properly free ressources and unregister callbacks.
 1.1.2.2 18-Feb-2005  bouyer The network device backend is now functionnal enouth to allow ping and
ftp between the domain0 and a guest. If shows up as an ethernet interface
in domain0, which can then be bridged, nated, routed, etc ... to real network
devices, but I didn't try this yet (but there's no reasons it wouldn't work :)
TODO:
- deal with ressources shortages
- properly release ressources on domain shutdown
- load balancing
- try to avoid packet copies in some cases.
 1.1.2.1 16-Feb-2005  bouyer Add a skeleton driver for network backend interface. It just reply to
control messages for now.
 1.4.4.4 20-Mar-2005  yamt fix a mistake in the previous.
 1.4.4.3 19-Mar-2005  yamt convert to the new API.
 1.4.4.2 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.4.4.1 11-Mar-2005  yamt file xennetback.c was added on branch yamt-km on 2005-03-19 08:33:26 +0000
 1.4.2.8 05-Feb-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1155):
sys/arch/xen/xen/if_xennet.c: revision 1.42 via patch
sys/arch/xen/xen/xennetback.c: revision 1.20 via patch
Check the destination ethernet address when not in promiscous mode.
Fix problem where packets would be duplicated, possibly looping, when
a domU is doing IP routing.
Problem reported and fix tested by Mike M. Volokhov on port-xen

While there, add some __predict_false/true in conditionnals where
appropriate, remove a always-true test, and fix handling of
mbuf shortage.
 1.4.2.7 20-Jan-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1104):
sys/arch/xen/xen/privcmd.c: revision 1.6
sys/arch/xen/i386/pmap.c: revision 1.14
sys/arch/xen/include/pmap.h: revision 1.5
sys/arch/xen/xen/xennetback.c: revision 1.12 via patch
sys/arch/xen/xen/xbdback.c: revision 1.15
Add a vm_prot_t parameter to pmap_remap_pages(), and use it for the new PTE
instead to always trying PG_RW and falling back to PG_RO if this fails.
Use uvm_map_checkprot() in IOCTL_PRIVCMD_MMAP and IOCTL_PRIVCMD_MMAP_BATCH
to compute the appropriate vm_prot_t for pmap_remap_pages().
Thanks to Jed Davis for pointing out uvm_map_checkprot().
 1.4.2.6 27-May-2005  riz branches: 1.4.2.6.2;
Pull up revision 1.11 (requested by bouyer in ticket #327):
- There is a possible race condition with the rx->event stuff, causing
event not to be sent when one is needed. Fixing this would require
one hypercall per packet, instead of one per NB_XMIT_PAGES_BATCH pages.
It's not worth it, so always send an event at the end of xennetback_ifstart()
- there is no callback mechanism to notify us when a guest has handled
packets we sent. If we stop transmitting because the ring is full or we're
out of pages when the ifq is also full, nothing will call
xennetback_ifstart() again and transmit is stalled. Abuse the watchdog
to kick the transmit queue once second after an out of ressources
condition.
 1.4.2.5 01-May-2005  tron Pull up revision 1.10 (requested by yamt in ticket #239):
xennetback_ifstart: test a correct variable, rather than a function.
 1.4.2.4 28-Apr-2005  tron Pull up revision 1.9 (requested by bouyer in ticket #192):
Allow to pass a more descriptive name to event_set_handler() (e.g. IRQ number
for physical IRQ, or device name for xen device drivers). This makes
systat and vmstat output more usable, especially as the channel numbers
change each time a guest reboots.
 1.4.2.3 28-Apr-2005  tron Pull up revision 1.8 (requested by bouyer in ticket #192):
Remove functions that just call another one.
Inline a function used at only one place.
 1.4.2.2 28-Apr-2005  tron Pull up revision 1.7 (requested by bouyer in ticket #192):
Get rid of the event to pseudo-irq mapping. We are limited to 32 pseudo-irq,
including soft interrupt, and this is way too low in some use (lots of domains,
or domains with lots of xennet, or even hardware with lots of devices at
different interrupts).
Based on idea from YAMAMOTO Takashi, keep one list of handler per-event and
one per-IPL (so the same handler is now in 2 lists). In the common case were
an event is received at low IPL, we can call the handlers quickly (there
is usually only one handler per event, unless the event is mapped to a
physical interrupt and this interrupt is shared by different devices).
Deffered events and software interrupts are handled by a bitmask (as before)
with one bit per IPL. When one IPL has an event pending all handlers for
this IPL will be called.
With this change, it is now possible to have all the 1024 events active.
While here, handle debug event in a special way: the handler is always called,
regardless of the current IPL. Make the handler print usefull informations
about events and IPL states.
Also remove code not used on Xen in files inherited from the x86 port.
 1.4.2.1 30-Mar-2005  tron Pull up revision 1.5 (requested by bouyer in ticket #72):
Remplace __insn_barrier() with x86_lfence() where appropriate.
While here, replace a panic() with a return of error code in xbdback.c.
 1.4.2.6.2.1 05-Feb-2006  riz Pull up following revision(s) (requested by bouyer in ticket #1155):
sys/arch/xen/xen/if_xennet.c: revision 1.42 via patch
sys/arch/xen/xen/xennetback.c: revision 1.20 via patch
Check the destination ethernet address when not in promiscous mode.
Fix problem where packets would be duplicated, possibly looping, when
a domU is doing IP routing.
Problem reported and fix tested by Mike M. Volokhov on port-xen

While there, add some __predict_false/true in conditionnals where
appropriate, remove a always-true test, and fix handling of
mbuf shortage.
 1.6.2.3 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.6.2.2 01-Apr-2005  skrll Sync with HEAD.
 1.6.2.1 01-Apr-2005  skrll file xennetback.c was added on branch ktrace-lwp on 2005-04-01 14:29:11 +0000
 1.10.2.2 29-Apr-2005  kent sync with -current
 1.10.2.1 28-Apr-2005  kent file xennetback.c was added on branch kent-audio2 on 2005-04-29 11:28:30 +0000
 1.11.2.6 27-Feb-2008  yamt sync with head.
 1.11.2.5 21-Jan-2008  yamt sync with head
 1.11.2.4 07-Dec-2007  yamt sync with head
 1.11.2.3 15-Nov-2007  yamt sync with head.
 1.11.2.2 03-Sep-2007  yamt sync with head.
 1.11.2.1 21-Jun-2006  yamt sync with head.
 1.17.2.2 18-Feb-2006  yamt sync with head.
 1.17.2.1 01-Feb-2006  yamt sync with head.
 1.19.2.1 09-Sep-2006  rpaulo sync with head
 1.20.8.1 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.20.4.1 24-May-2006  yamt sync with head.
 1.20.2.1 01-Jun-2006  kardel Sync with head.
 1.21.2.1 19-Jun-2006  chap Sync with head.
 1.22.16.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.22.12.2 24-Mar-2007  yamt sync with head.
 1.22.12.1 12-Mar-2007  rmind Sync with HEAD.
 1.22.10.1 23-Mar-2008  jdc Pull up revisions (requested by bouyer in ticket #1086):
src/sys/arch/xen/i386/xen_shm_machdep.c 1.3
src/sys/arch/xen/x86/xenfunc.c 1.4
src/sys/arch/xen/xen/pci_intr_machdep.c 1.4
src/sys/arch/xen/xen/pci_machdep.c 1.12
src/sys/arch/xen/xen/xbdback.c 1.30
src/sys/arch/xen/xen/xbdback_xenbus.c 1.14
src/sys/arch/xen/xen/xen_acpi_machdep.c 1.4
src/sys/arch/xen/xen/xenevt.c 1.20
src/sys/arch/xen/xen/xengnt.c 1.6
src/sys/arch/xen/xen/xennetback.c 1.31

Add missing __KERNEL_RCSID()
 1.23.2.3 03-Dec-2007  ad Sync with HEAD.
 1.23.2.2 09-Oct-2007  ad Sync with head.
 1.23.2.1 13-Mar-2007  ad Sync with head.
 1.24.16.4 09-Dec-2007  jmcneill Sync with HEAD.
 1.24.16.3 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.24.16.2 11-Nov-2007  joerg Sync with HEAD.
 1.24.16.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.24.12.1 03-Sep-2007  skrll Sync with HEAD.
 1.24.8.1 03-Oct-2007  garbled Sync with HEAD
 1.25.6.2 13-Nov-2007  bouyer Sync with HEAD
 1.25.6.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.25.2.4 23-Mar-2008  matt sync with HEAD
 1.25.2.3 09-Jan-2008  matt sync with HEAD
 1.25.2.2 08-Nov-2007  matt sync with -HEAD
 1.25.2.1 06-Nov-2007  matt sync with HEAD
 1.26.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.26.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.26.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.29.6.1 13-Dec-2007  bouyer Convert to new pmap functions.
 1.31.6.3 17-Jan-2009  mjf Sync with HEAD.
 1.31.6.2 05-Jun-2008  mjf Sync with HEAD.

Also fix build.
 1.31.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.32.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.32.4.2 19-Aug-2009  yamt sync with head.
 1.32.4.1 04-May-2009  yamt sync with head.
 1.32.2.1 17-Jun-2008  yamt sync with head.
 1.33.4.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.34.2.2 28-Apr-2009  skrll Sync with HEAD.
 1.34.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.35.2.2 01-Nov-2009  jym Sync with HEAD.
 1.35.2.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.126 05-Jul-2024  rin sys: Drop redundant NULL check before m_freem(9)

m_freem(9) safely has accepted NULL argument at least since 4.2BSD:
https://www.tuhs.org/cgi-bin/utree.pl?file=4.2BSD/usr/src/sys/sys/uipc_mbuf.c

Compile-tested on amd64/ALL.

Suggested by knakahara@
 1.125 30-Dec-2023  riastradh branches: 1.125.2;
xennetback_xenbus.c: Nix trailing whitespace again.

No functional change intended.
 1.124 22-Dec-2023  bouyer sanity check the fragments we get from the guest and reject the whole packet
if it fails. Make sure we don't KASSERT() on data we get from the frontend.
 1.123 09-Aug-2023  riastradh xvif(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.
 1.122 09-Aug-2023  riastradh xvif(4): Omit needless 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 updates the shared req_prod and
then issues xen_mb, which is all that we need between the update of
shared req_prod and hypervisor_notify_via_evtchn.

(Between updating the shared req_prod and issuing
hypervisor_notify_via_evtchn, only xen_wmb is needed. But after
writing to the shared req_prod, RING_PUSH_REQUESTS_AND_CHECK_NOTIFY
must also read from the shared rsp_event, which requires the
store-before-load ordering that only xen_mb provides.)
 1.121 09-Aug-2023  riastradh xvif(4): Simplify while loop in xennetback_evthandler.

No functional change intended.
 1.120 09-Aug-2023  riastradh xvif(4): Omit needless membars in xennetback_evthandler.

This should improve throughput without any impact on correctness.
 1.119 09-Aug-2023  riastradh xvif(4): Move expensive xen_mb out of xennetback_evthandler loop.

Use the cheaper RING_HAS_UNCONFIRMED_REQUESTS for most of the loop.

This should improve throughput without any impact on correctness.
 1.118 09-Aug-2023  riastradh xvif(4): Omit local variable aliasing xneti->xni_txring.req_cons.

No functional change intended.
 1.117 09-Aug-2023  riastradh xvif(4): Add missing xen_rmb in xennetback_evthandler.
 1.116 09-Aug-2023  riastradh xvif(4): Comment on memory barriers in xennetback_evthandler.

Note which ones appear unnecessary and which ones appear too strong,
but don't change them.

No functional change intended.
 1.115 04-Aug-2023  riastradh Revert "xennetback(4): Fix xennetback_evthandler loop."

PR kern/57560
 1.114 04-Aug-2023  riastradh Revert "xennetback(4): Fix membars in xennetback_rx_copy_process."

PR kern/57560
 1.113 04-Aug-2023  riastradh Revert "xennetback(4): Omit needless membars in xennetback_connect."

PR kern/57560
 1.112 25-Feb-2023  riastradh xennetback(4): Nix trailing whitespace.

No functional change intended.
 1.111 25-Feb-2023  riastradh 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.
 1.110 25-Feb-2023  riastradh 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.
 1.109 25-Feb-2023  riastradh 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.

XXX pullup-8 (requires patch; at least add xen_rmb between
RING_FINAL_CHECK_FOR_REQUESTS and RING_COPY_REQUEST)
XXX pullup-9 (requires patch; at least add xen_rmb between
RING_FINAL_CHECK_FOR_REQUESTS and RING_COPY_REQUEST)
XXX pullup-10
 1.108 02-Sep-2022  thorpej branches: 1.108.4;
Remove unnecessary inclusion of <net/netisr.h>.
 1.107 01-Sep-2022  bouyer in backend drivers, use xen_shm_(un)map for the rings instead of inline,
mostly duplicate code.
 1.106 01-Sep-2022  bouyer Add PVH support for backend drivers grant operation.
Now a domU in a PVH dom0 boots multiuser.
 1.105 05-May-2020  bouyer Make DOM0OPS build for PVH/PVHVM too
 1.104 04-May-2020  jdolecek remove IPv4 csum offloading for xennet(4) - it's not complete, and even
if it was, it doesn't work with Linux Dom0 as it expects the IPv4 csum present
 1.103 03-May-2020  jdolecek add support for scatter-gather also for frontend Rx path (backend -> frontend)

enable ETHERCAP_JUMBO_MTU and feature-sg
 1.102 02-May-2020  jdolecek fix gref offset when setting up copy of multi-fragment packet so data
for second and further fragments gets copied into correct place
 1.101 01-May-2020  jdolecek destroy dma maps in xennetback_xenbus_destroy() to avoid memory leak
when xvif(4) is destroyed
 1.100 01-May-2020  jdolecek make the csum blank/undefer counters per interface
 1.99 30-Apr-2020  jdolecek add support for scatter-gather when accepting packets on frontend Tx path
(frontend -> backend)

don't enable ETHERCAP_JUMBO_MTU nor feature-sg yet, need to implement
support also for the frontend Rx side
 1.98 26-Apr-2020  jdolecek bump send queue to 2*NET_TX_RING_SIZE to make it less likely packets
are dropped on load

m_defrag() short packets before calling bus_dmamap_load_mbuf() -
if it's fragmented load_mbuf would fail anyway, and even with
eventual feature-sg support it's way faster to pass the short packet
in single fragment
 1.97 25-Apr-2020  jdolecek use m_defrag() instead of local code now that it returns single mbuf
 1.96 11-Apr-2020  jdolecek convert to bus_dma(9), no explicit xpmap_*() calls any more

as part of this move some global arrays into struct xnetback_instance,
and fix race for xnetif_lookup()
 1.95 09-Apr-2020  jdolecek remove check for matching ethernet address on Tx - the higher levels do this
check anyway, and the check did not handle VLANs
 1.94 07-Apr-2020  jdolecek branches: 1.94.2;
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.93 06-Apr-2020  jdolecek pass and use feature-ipv6-csum-offload for ipv6 csum support, matches
Linux Dom0/DomU
 1.92 05-Apr-2020  jdolecek remove support for legacy rx-flip mode for xennet(4)/xvif(4), making
rx-copy (first shipped in NetBSD 6.0 in 2012) the only supported
mode

this is mostly to simplify maintenance and future development

rx-flip is not supported by Linux Dom0/DomU, and NetBSD Dom0/DomU
defaults to rx-copy for over 8 years now too, so there is little
need to keep the support for compatibility

besides compatibility there is no other reason to keep rx-flip -
page transfer is generally slower than copy due to necessary MMU/TLB
manipulation, especially on MP systems
 1.91 04-Apr-2020  jdolecek enable Rx checkum offload (i.e. skip) for xen netback; it works well
as long as netfront in DomU is configured to fill the checksum

documentation will be updated to recommend how the flags should be setup
on each side of the xennet(4)/xvif(4) pair
 1.90 30-Mar-2020  jdolecek don't check for IFF_OACTIVE, nothing sets it
 1.89 30-Mar-2020  jdolecek batch the GNTTABOP_copy hypervisor calls in xvif(4) when processing
DomU requests in xennetback_evthandler()

some 25% speed increase observed over the single-copy version
 1.88 29-Mar-2020  jdolecek completely g/c the #if 0'ed xennetback_tx_free(), it's not used any more
 1.87 29-Mar-2020  jdolecek in xennetback_evthandler() just copy the DomU packet into destination
mbuf via hypervisor GNTTABOP_copy op instead of mapping the buffer into
Dom0 memory

no performance difference observed for now - it would probably make more
difference if Dom0 was MP, or when Dom0 is under VM pressure

this will eventually be updated to batch copy the DomU packets
 1.86 27-Mar-2020  jdolecek use standard deferred if_start framework instead of custom variant
 1.85 22-Mar-2020  jdolecek actually in data_validated case, there is no need to inspect the data for Rx,
simply set the supported csum offload flags to skip the software csum
verification
 1.84 22-Mar-2020  jdolecek add support for skipping IPv6 checksum validation aka offloading -
for xennet(4) both Rx and Tx, for xvif(4) only Tx for now
 1.83 21-Mar-2020  jdolecek need to retain csum_flags in xennetback_copymbuf() when creating
new mbuf for the COPY transmit path, so that later NETRXF_csum_blank
flag is set correctly when passing request over to DomU

fixes bad checksums in DomU when xvif(4) TX offload is enabled;
problem observed when testing new IPv6 offloading, but it should
affect also existing IPv4 offloading

Note: code in xennet(4) is unaffected, there code consults the original
mbuf, rather than the new_m one
 1.82 19-Mar-2020  jdolecek allow skipping IPv4 checksum on Tx
 1.81 18-Mar-2020  jdolecek for now disable Rx offloading for xvif(4) again, network stack requires
some changes to support deferred checksum
 1.80 18-Mar-2020  jdolecek use NET[RT]XF_data_validated flag to mark when Tx packet has valid
checksum; this is used to skip software checksum validation on
xennet Rx side when configured for Rx offloading

in Dom0 assume that checksum is valid when the Tx mbuf has no offload flags
- in that case either it's local packet where checksum has just been
computed in software, or forwarded external packet already
verified when received on Dom0

practical offshot of this is that DomU doesn't re-verify checksum of
packets forwarded from external hosts, e.g. via bridge(4)
 1.79 17-Mar-2020  kre #if 0 the declaration and definition of xennetback_tx_free()

Since it is never otherwise referenced, this cannot make any
current operational difference, but should fix the build.
(-Werror=unused-function)

The added #if/#endif lines are /* XXX */ commented, so they will be
easy to remove when needed.
 1.78 16-Mar-2020  jdolecek drop the disabled M_EXT_ROMAP mbuf code, convert xennet_checksum_fill()
to use in_undefer_cksum() instead of custom code to compute the checksum
for Rx packets, and set csum_data appropriately for eventual hw offloading

make it possible to skip the sw checksum computation by appropriate Rx
flag similarily as we do for Tx

XXX for now, the Rx flag is mostly for testing as it only works for
dom0<->domu, need some further network stack changes to arrange for
the checksum to be eventually computed when packets goes outside xen
 1.77 16-Mar-2020  jdolecek drop the disabled M_EXT_ROMAP mbuf code, and convert xennet_checksum_fill()
to use in_undefer_cksum() instead of custom code to compute the checksum
for incoming packets

make it possible to defer/skip the checksum computation by appropriate Rx
flag similarily as we do for Tx; with
 1.76 29-Jan-2020  thorpej Adopt <net/if_stats.h>.
 1.75 09-Mar-2019  maxv branches: 1.75.4; 1.75.6;
Start replacing the x86 PTE bits.
 1.74 05-Feb-2019  msaitoh Remove very old IFF_NOTRAILERS flag.
 1.73 24-Dec-2018  cherry 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.72 23-Dec-2018  bouyer Cleanup the TX path:
- split in sub-functions
- ratelimit printf for mbuf allocation failure
- don't loop forever on mbuf allocation failure
 1.71 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.70 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.69 03-Sep-2018  riastradh Rename min/max -> uimin/uimax for better honesty.

These functions are defined on unsigned int. The generic name
min/max should not silently truncate to 32 bits on 64-bit systems.
This is purely a name change -- no functional change intended.

HOWEVER! Some subsystems have

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

even though our standard name for that is MIN/MAX. Although these
may invite multiple evaluation bugs, these do _not_ cause integer
truncation.

To avoid `fixing' these cases, I first changed the name in libkern,
and then compile-tested every file where min/max occurred in order to
confirm that it failed -- and thus confirm that nothing shadowed
min/max -- before changing it.

I have left a handful of bootloaders that are too annoying to
compile-test, and some dead code:

cobalt ews4800mips hp300 hppa ia64 luna68k vax
acorn32/if_ie.c (not included in any kernels)
macppc/if_gm.c (superseded by gem(4))

It should be easy to fix the fallout once identified -- this way of
doing things fails safe, and the goal here, after all, is to _avoid_
silent integer truncations, not introduce them.

Maybe one day we can reintroduce min/max as type-generic things that
never silently truncate. But we should avoid doing that for a while,
so that existing code has a chance to be detected by the compiler for
conversion to uimin/uimax without changing the semantics until we can
properly audit it all. (Who knows, maybe in some cases integer
truncation is actually intended!)
 1.68 11-Aug-2018  jdolecek disestablish interrupt handler regardless of the state whenever it was
setup, i.e. also when the instance is DISCONNECTING already

fixes PR port-xen/53487 by Bert Kiers; fix found and reported by
Dennis Lindroos

XXX pullup netbsd-8
 1.67 09-Aug-2018  maxv Use an independent pool, don't steal pages from mcl_cache. This was a bad
hack.

No particular functional change, since the (MCLBYTES != PAGE_SIZE)
condition is already true.
 1.66 09-Aug-2018  maxv style a bit
 1.65 26-Jun-2018  msaitoh branches: 1.65.2;
Implement the BPF direction filter (BIOC[GS]DIRECTION). It provides backward
compatibility with BIOC[GS]SEESENT ioctl. The userland interface is the same
as FreeBSD.

This change also fixes a bug that the direction is misunderstand on some
environment by passing the direction to bpf_mtap*() instead of checking
m->m_pkthdr.rcvif.
 1.64 24-Jun-2018  jdolecek fix panic of DOM0 in xennetback_xenbus_destroy() on xl destroy of
DOMU with created, but non CONNECTED xennet (such as when DOMU
panics during boot); only try to disestablish the intr if it was
actually setup

while here protect xnetback_instances with mutex, and switch to use
kmem_zalloc() + KM_SLEEP / kmem_free() like xbdback_xenbus.c; add XXXSMP
to the other global variables, and at least mark them static
 1.63 22-Jun-2018  msaitoh It's not required to include net/bpfdesc.h. Remove it.
 1.62 27-Apr-2018  maxv M_CLUSTER -> M_EXT_CLUSTER
 1.61 11-Nov-2017  riastradh branches: 1.61.2;
Restore MP-safety annotations in intr_establish_xname.

In the old API, event_set_handler would assume MP-unsafe for IPL_VM
and MP-safe for all other levels (IPL_SCHED, IPL_HIGH). The recent
Xen interrupt rototill started passing known_mpsafe=true for _all_
interrupt handlers. Change it to known_mpsafe=false for the IPL_VM
(= IPL_CLOCK, IPL_NET) ones.
 1.60 06-Nov-2017  cherry Switch XEN drivers to use intr_establish_xname()/intr_disestablish()

This completes the API transition.
 1.59 30-Aug-2017  maxv Make these pages non-executable, and style.
 1.58 15-Dec-2016  ozaki-r branches: 1.58.8;
Move bpf_mtap and if_ipackets++ on Rx of each driver to percpuq if_input

The benefits of the change are:
- We can reduce codes
- We can provide the same behavior between drivers
- Where/When if_ipackets is counted up
- Note that some drivers still update packet statistics in their own
way (periodical update)
- Moved bpf_mtap run in softint
- This makes it easy to MP-ify bpf

Proposed on tech-kern and tech-net
 1.57 10-Jun-2016  ozaki-r branches: 1.57.2;
Introduce m_set_rcvif and m_reset_rcvif

The API is used to set (or reset) a received interface of a mbuf.
They are counterpart of m_get_rcvif, which will come in another
commit, hide internal of rcvif operation, and reduce the diff of
the upcoming change.

No functional change.
 1.56 09-May-2016  christos Account for the CRC len (Jean-Jacques.Puig)
 1.55 09-Feb-2016  ozaki-r Introduce softint-based if_input

This change intends to run the whole network stack in softint context
(or normal LWP), not hardware interrupt context. Note that the work is
still incomplete by this change; to that end, we also have to softint-ify
if_link_state_change (and bpf) which can still run in hardware interrupt.

This change softint-ifies at ifp->if_input that is called from
each device driver (and ieee80211_input) to ensure Layer 2 runs
in softint (e.g., ether_input and bridge_input). To this end,
we provide a framework (called percpuq) that utlizes softint(9)
and percpu ifqueues. With this patch, rxintr of most drivers just
queues received packets and schedules a softint, and the softint
dequeues packets and does rest packet processing.

To minimize changes to each driver, percpuq is allocated in struct
ifnet for now and that is initialized by default (in if_attach).
We probably have to move percpuq to softc of each driver, but it's
future work. At this point, only wm(4) has percpuq in its softc
as a reference implementation.

Additional information including performance numbers can be found
in the thread at tech-kern@ and tech-net@:
http://mail-index.netbsd.org/tech-kern/2016/01/14/msg019997.html

Acknowledgment: riastradh@ greatly helped this work.
Thank you very much!
 1.54 06-Jan-2016  bouyer Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.53 19-Nov-2015  christos Enable the VLAN mtu capability and check for the adjusted packet size
(Jean-Jacques.Puig at espci.fr).
Factor out the packet-size checking function for clarity.
 1.52 20-Oct-2013  bouyer branches: 1.52.4; 1.52.6; 1.52.8;
Fix memory leak if the provided mac address isn't valid.
 1.51 06-Mar-2013  yamt branches: 1.51.6;
fix debug print
 1.50 30-Jun-2012  jym branches: 1.50.2;
Use setter to set xenguest_handles.
 1.49 30-Jun-2012  jym Extend the xpmap API, as described in [1]. This change is mechanical and
avoids exposing the MD phys_to_machine/machine_to_phys tables directly.
Added:

- xpmap_ptom handles PFN (pseudo physical) to MFN (machine frame number)
translations, and is under control of the domain.
- xpmap_mtop is its counterpart (MFN to PFN), and is under control of
hypervisor.

xpmap_ptom_map() map a pseudo-phys address to a machine address
xpmap_ptom_unmap() unmap a pseudo-phys address (invalidation)
xpmap_ptom_isvalid() check for pseudo-phys address validity

The parameters are physical/machine addresses, like bus_dma/bus_space(9).
As x86 MFNs are tracked by u_long (Xen's choice) while machine addresses
can be 64 bits entities (PAE), use ptoa() to avoid truncation when bit
shifting by PAGE_SHIFT.

I kept the same namespace (xpmap_) to avoid code churn.

[1] http://mail-index.netbsd.org/port-xen/2009/05/09/msg004951.html

XXX will document ptoa/atop/trunc_page separately.
 1.48 27-Jun-2012  jym Retire XEN_COMPAT_030001 as detailed on port-xen@:

http://mail-index.netbsd.org/port-xen/2012/06/25/msg007431.html

The xen_p2m API comes next.

ok bouyer@.
Tested on i386 PAE and amd64 (Xen 3.3 on private test bed, and
Xen 3.4 for Amazon EC2).

FWIW, Amazon always reported:

hypervisor0 at mainbus0: Xen version 3.4.3-kaos_t1micro

multiple times for Europe and US West-1, so I guess they are now at
3.4 (32 and 64 bits).
 1.47 28-Aug-2011  jym branches: 1.47.2; 1.47.8; 1.47.12; 1.47.14;
KNF, white spaces and comment typo fixes.
 1.46 30-May-2011  joerg branches: 1.46.2;
Use format string for the device name
 1.45 29-Apr-2011  jym Move the connection code of xbdback(4) and xvif(4) backends in separate
functions. The frontend watch function is easier to read, and mixing
switch() with goto's error paths is rather error-prone.

While here, sprinkle some aprint_*.

Tested under amd64 dom0 with i386 PAE and amd64 domUs.
 1.44 25-Apr-2011  jym use __KERNEL_RCSID()
 1.43 25-Apr-2011  jym Check that xvif(4) is not already connected before proceeding in the
XenbusStateConnected mode. Under rare occasions, the xenbus watcher
can fire multiple times, overwriting the I/O ring memory mappings with
invalid values. This will lead sooner or later to dom0 panic().

Will ask for pullup. FWIW, xbdback(4) is not affected.
 1.42 21-Apr-2011  jym Disestablish softint in the error path.
 1.41 20-Apr-2011  jym Unmap rings before freeing their associated VAs, or we will get a
non-recoverable fault in the error path.
 1.40 06-Apr-2011  jym Alright, set xvif(4) syntax once and for all. Tested with ipf(4) under
XEN3_DOM0 amd64.

Fixes PR misc/39376.

See http://mail-index.netbsd.org/port-xen/2011/04/06/msg006603.html
 1.39 03-Apr-2011  jym Now that pkgsrc-2011Q1 has arrived, and before -6 chimes in, change
ifxname for xvif(4) from xvif%d.%d to xvif%d-%d. This is needed
to avoid sysctl(9) EINVAL errors when creating interface nodes.

See http://mail-index.netbsd.org/port-xen/2011/01/11/msg006405.html
 1.38 18-Jan-2011  jym Fix bad pasto cooking: xennet backend is not xbdback: use
aprint_error_ifnet() with the proper ifnet device for error messages.
 1.37 11-Jan-2011  jym branches: 1.37.2;
Introduce "vifname" keys for Xen domains. Its value is the interface
name for the vif, e.g. xvif(4) for dom0, and xennet(4) for domU.

ok bouyer@.

See http://mail-index.netbsd.org/port-xen/2011/01/11/msg006405.html
 1.36 11-Jan-2011  jym Typo fix.
 1.35 08-Jan-2011  jym Move if_xname setting earlier for xvif creation, so we can grab domid
and handle values sooner for error cases.
 1.34 05-Apr-2010  joerg Push the bpf_ops usage back into bpf.h. Push the common ifp->if_bpf
check into the inline functions as well the fourth argument for
bpf_attach.
 1.33 23-Jan-2010  cegger branches: 1.33.2; 1.33.4;
fix address overflow with 32bit PAE.
Reported and tested by Mark Davies on port-xen@.
 1.32 19-Jan-2010  pooka Redefine bpf linkage through an always present op vector, i.e.
#if NBPFILTER is no longer required in the client. This change
doesn't yet add support for loading bpf as a module, since drivers
can register before bpf is attached. However, callers of bpf can
now be modularized.

Dynamically loadable bpf could probably be done fairly easily with
coordination from the stub driver and the real driver by registering
attachments in the stub before the real driver is loaded and doing
a handoff. ... and I'm not going to ponder the depths of unload
here.

Tested with i386/MONOLITHIC, modified MONOLITHIC without bpf and rump.
 1.31 19-Oct-2009  bouyer Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.30 27-Sep-2009  bouyer Don't disestablish a soft interrupt which has not been established.
 1.29 25-Sep-2009  bouyer Announce feature-rx-copy and feature-rx-flip.
Add support for request-rx-copy. Tested with a Debian lenny install.
Should fix PR port-xen/40650
 1.28 12-Feb-2009  bouyer Remove dead code
 1.27 16-Jan-2009  jym branches: 1.27.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.26 06-Jan-2009  jym - fix some typos in comments
- remove trailing spaces
- replace a check against 0 with a check against GNTST_okay

No functional change.
 1.25 13-Nov-2008  cegger Finish preparation to new interface.
New interface not yet used by default. It needs some testing first.
 1.24 24-Oct-2008  jym branches: 1.24.2; 1.24.4; 1.24.8;
- 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.23 04-Jun-2008  ad branches: 1.23.4;
vm_page: put TAILQ_ENTRY into a union with LIST_ENTRY, so we can use both.
 1.22 14-Apr-2008  cegger branches: 1.22.2; 1.22.4; 1.22.6;
- use POSIX integer types
- ansify functions
 1.21 03-Dec-2007  ad branches: 1.21.14;
Interrupt handling changes, in discussion since February:

- Reduce available SPL levels for hardware devices to none, vm, sched, high.
- Acquire kernel_lock only for interrupts at IPL_VM.
- Implement threaded soft interrupts.
 1.20 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.19 07-Nov-2007  ad Make it compile.
 1.18 07-Nov-2007  ad Merge from vmlocking:

- pool_cache changes.
- Debugger/procfs locking fixes.
- Other minor changes.
 1.17 17-Oct-2007  garbled branches: 1.17.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.16 26-Aug-2007  dyoung branches: 1.16.2; 1.16.6;
Constify.
 1.15 12-Mar-2007  ad branches: 1.15.8; 1.15.12; 1.15.16;
Pass an ipl argument to pool_init/POOL_INIT to be used when initializing
the pool's lock.
 1.14 05-Mar-2007  dogcow branches: 1.14.2;
fix more caddr_t -> void * fallout.
 1.13 04-Mar-2007  christos Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.12 15-Oct-2006  bouyer branches: 1.12.4;
Set appropriate value for if_snd.ifq_maxlen; keeping it too low has a
bad impact on performances.
 1.11 12-Jul-2006  yamt branches: 1.11.4; 1.11.6; 1.11.8;
implement NETRXF_csum_blank/NETTXF_csum_blank tx checksum "offloading".
 1.10 12-Jul-2006  yamt implement a simple NETTXF_csum_blank/NETRXF_csum_blank workaround
so that we can talk with linux guests at least.
just fill checksum field of received packets if the flag is set.
maybe should be revisited later.
 1.9 02-Jul-2006  bouyer Fix a bogus gcc4 uninitialized warning.
 1.8 02-Jul-2006  bouyer Fix a off by one error in xstart_mmu[] that would cause bad corruption
when handling full batch of requests.
Linux expects to have 16 btyes free between the start of the RX buffer and the
start of the packet, handle this.
Get rid of the "hotplug-status" hack, we now have proper tools to deal with
it in userland.
Various minor fixes and code reorg.
 1.7 25-Jun-2006  bouyer Use the right test for ring full condition. Avoid an infinite loop when the
ring is full (because the domU is in ddb for example).
 1.6 25-Jun-2006  bouyer Don't loop forever if we receive a packet while the interface is down.
 1.5 25-Jun-2006  bouyer Add a 'base' argument to xenbus_read_ul, so that we can read number is base
other than 10.
 1.4 25-Jun-2006  bouyer Adapt for new xen_shm_* signatures.
While here add more xenbus_dev_fatal() calls for some failure cases.
 1.3 27-May-2006  bouyer branches: 1.3.2; 1.3.4; 1.3.6; 1.3.8;
A lot of work is needed in the tcp stack to handle read-only ext storage
so always copy packets from the domU to a fresh mbuf for now.
 1.2 25-May-2006  bouyer Use xen_shm_* to map the packet sent to us by the domU, and attach it
as external storage to the mbuf.
 1.1 23-May-2006  bouyer branches: 1.1.2; 1.1.4;
Add a network backend driver for Xen3. This will appear as a xvif pseudo
device in kernel config files.
 1.1.4.2 24-May-2006  tron Merge 2006-05-24 NetBSD-current into the "peter-altq" branch.
 1.1.4.1 23-May-2006  tron file xennetback_xenbus.c was added on branch peter-altq on 2006-05-24 15:48:26 +0000
 1.1.2.4 11-Aug-2006  yamt sync with head
 1.1.2.3 26-Jun-2006  yamt sync with head.
 1.1.2.2 24-May-2006  yamt sync with head.
 1.1.2.1 23-May-2006  yamt file xennetback_xenbus.c was added on branch yamt-pdpolicy on 2006-05-24 10:57:23 +0000
 1.3.8.6 07-Dec-2007  yamt sync with head
 1.3.8.5 15-Nov-2007  yamt sync with head.
 1.3.8.4 03-Sep-2007  yamt sync with head.
 1.3.8.3 30-Dec-2006  yamt sync with head.
 1.3.8.2 21-Jun-2006  yamt sync with head.
 1.3.8.1 27-May-2006  yamt file xennetback_xenbus.c was added on branch yamt-lazymbuf on 2006-06-21 14:58:23 +0000
 1.3.6.2 19-Jun-2006  chap Sync with head.
 1.3.6.1 27-May-2006  chap file xennetback_xenbus.c was added on branch chap-midi on 2006-06-19 03:45:36 +0000
 1.3.4.1 13-Jul-2006  gdamore Merge from HEAD.
 1.3.2.2 01-Jun-2006  kardel Sync with head.
 1.3.2.1 27-May-2006  kardel file xennetback_xenbus.c was added on branch simonb-timecounters on 2006-06-01 22:35:39 +0000
 1.11.8.1 22-Oct-2006  yamt sync with head
 1.11.6.2 09-Sep-2006  rpaulo sync with head
 1.11.6.1 12-Jul-2006  rpaulo file xennetback_xenbus.c was added on branch rpaulo-netinet-merge-pcb on 2006-09-09 02:45:05 +0000
 1.11.4.1 18-Nov-2006  ad Sync with head.
 1.12.4.2 24-Mar-2007  yamt sync with head.
 1.12.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.14.2.3 03-Dec-2007  ad Sync with HEAD.
 1.14.2.2 09-Oct-2007  ad Sync with head.
 1.14.2.1 13-Mar-2007  ad Sync with head.
 1.15.16.4 09-Dec-2007  jmcneill Sync with HEAD.
 1.15.16.3 27-Nov-2007  joerg Sync with HEAD. amd64 Xen support needs testing.
 1.15.16.2 11-Nov-2007  joerg Sync with HEAD.
 1.15.16.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.15.12.1 03-Sep-2007  skrll Sync with HEAD.
 1.15.8.1 03-Oct-2007  garbled Sync with HEAD
 1.16.6.2 13-Nov-2007  bouyer Sync with HEAD
 1.16.6.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.16.2.3 09-Jan-2008  matt sync with HEAD
 1.16.2.2 08-Nov-2007  matt sync with -HEAD
 1.16.2.1 06-Nov-2007  matt sync with HEAD
 1.17.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.17.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.21.14.3 17-Jan-2009  mjf Sync with HEAD.
 1.21.14.2 05-Jun-2008  mjf Sync with HEAD.

Also fix build.
 1.21.14.1 02-Jun-2008  mjf Sync with HEAD.
 1.22.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.22.4.3 11-Aug-2010  yamt sync with head.
 1.22.4.2 11-Mar-2010  yamt sync with head
 1.22.4.1 04-May-2009  yamt sync with head.
 1.22.2.1 17-Jun-2008  yamt sync with head.
 1.23.4.1 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.24.8.1 21-Apr-2010  matt sync to netbsd-5
 1.24.4.4 19-May-2011  bouyer Pull up following revision(s) (requested by jym in ticket #1609):
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.50
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.43
Check that xvif(4) is not already connected before proceeding in the
XenbusStateConnected mode. Under rare occasions, the xenbus watcher
can fire multiple times, overwriting the I/O ring memory mappings with
invalid values. This will lead sooner or later to dom0 panic().
Will ask for pullup. FWIW, xbdback(4) is not affected.
Check status before proceeding further. Avoids spurious watch calls.
 1.24.4.3 30-Jan-2010  snj Pull up following revision(s) (requested by bouyer in ticket #1271):
sys/arch/xen/x86/xen_bus_dma.c: revision 1.16
sys/arch/xen/xen/xengnt.c: revision 1.17 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.33
fix address overflow with 32bit PAE.
Reported and tested by Mark Davies on port-xen@.
 1.24.4.2 28-Sep-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1030):
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.30
Don't disestablish a soft interrupt which has not been established.
 1.24.4.1 28-Sep-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1030):
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.29 via patch
Announce feature-rx-copy and feature-rx-flip.
Add support for request-rx-copy. Tested with a Debian lenny install.
Should fix PR port-xen/40650
 1.24.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.24.2.1 19-Jan-2009  skrll 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 02-May-2011  jym Sync with head.
 1.27.2.7 28-Mar-2011  jym Sync with HEAD. TODO before merge:
- shortcut for suspend code in sysmon, when powerd(8) is not running.
Borrow ``xs_watch'' thread context?
- bug hunting in xbd + xennet resume. Rings are currently thrashed upon
resume, so current implementation force flush them on suspend. It's not
really needed.
 1.27.2.6 10-Jan-2011  jym Sync with HEAD
 1.27.2.5 24-Oct-2010  jym Sync with HEAD
 1.27.2.4 01-Nov-2009  jym Sync with HEAD.
 1.27.2.3 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.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 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.33.4.4 31-May-2011  rmind sync with head
 1.33.4.3 21-Apr-2011  rmind sync with head
 1.33.4.2 05-Mar-2011  rmind sync with head
 1.33.4.1 30-May-2010  rmind sync with head
 1.33.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.37.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.46.2.2 20-Sep-2011  cherry Remove the "xpq lock", since we have per-cpu mmu queues now. This may need further testing. Also add some preliminary locking around queue-ops in the network backend driver
 1.46.2.1 21-Aug-2011  cherry add locking around mmu flush xpq ops
 1.47.14.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.47.12.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.47.8.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1358):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.47.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.47.2.1 30-Oct-2012  yamt sync with head
 1.50.2.3 03-Dec-2017  jdolecek update from HEAD
 1.50.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.50.2.1 23-Jun-2013  tls resync from head
 1.51.6.1 18-May-2014  rmind sync with head
 1.52.8.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1071):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.52.6.5 05-Feb-2017  skrll Sync with HEAD
 1.52.6.4 09-Jul-2016  skrll Sync with HEAD
 1.52.6.3 29-May-2016  skrll Sync with HEAD
 1.52.6.2 19-Mar-2016  skrll Sync with HEAD
 1.52.6.1 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.52.4.2 24-Sep-2017  snj Pull up following revision(s) (requested by manu in ticket #1409):
sys/arch/xen/xen/if_xennet_xenbus.c: 1.65
sys/arch/xen/xen/xennetback_xenbus.c: 1.53, 1.56 via patch
sys/net/if_bridge.c: 1.105
sys/net/if_ether.h: 1.65
sys/net/if_ethersubr.c: 1.215, 1.235
sys/net/if_vlan.c: 1.76, 1.77, 1.83, 1.88, 1.94
Protect vlan_unconfig with a mutex
It is not thread-safe but is likely to be executed in concurrent.
See PR 49264 for more detail.
--
Tweak vlan_unconfig
No functional change.
--
Add handling of VLAN packets in if_bridge where the parent interface supports
them (Jean-Jacques.Puig%espci.fr@localhost). Factor out the vlan_mtu enabling and
disabling code.
--
Enable the VLAN mtu capability and check for the adjusted packet size
(Jean-Jacques.Puig at espci.fr).
Factor out the packet-size checking function for clarity.
--
Don't increment the reference count only when it was 0...
From Jean-Jacques.Puig
--
Account for the CRC len (Jean-Jacques.Puig)
--
Fix a bug that the parent interface's callback wasn't called when the vlan
interface is configured. A callback function uses VLAN_ATTACHED() function
which check ec->ec_nvlans, the value should be incremented before calling the
callback. This bug was added in if_vlan.c rev. 1.83 (2015/11/19).
 1.52.4.1 08-Jan-2016  snj Pull up following revision(s) (requested by bouyer in ticket #1071):
sys/arch/xen/include/xen-public/io/ring.h: revision 1.3 via patch
sys/arch/xen/xen/pciback.c: revision 1.10 via patch
sys/arch/xen/xen/xbdback_xenbus.c: revision 1.62 via patch
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.54 via patch
Apply patch from xsa155: make sure that the backend won't read parts of the
request again (possibly because of compiler optimisations), by using
copies and barrier.
From XSA155:
The compiler can emit optimizations in the PV backend drivers which
can lead to double fetch vulnerabilities. Specifically the shared
memory between the frontend and backend can be fetched twice (during
which time the frontend can alter the contents) possibly leading to
arbitrary code execution in backend.
 1.57.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.58.8.3 15-Aug-2018  martin Pull up following revision(s) (requested by dolecek in ticket #973):

sys/arch/xen/xen/xennetback_xenbus.c: revision 1.68 (patch)

disestablish interrupt handler regardless of the state whenever it was
setup, i.e. also when the instance is DISCONNECTING already
fixes PR port-xen/53487 by Bert Kiers; fix found and reported by
Dennis Lindroos

XXX pullup netbsd-8
 1.58.8.2 26-Jul-2018  snj Pull up following revision(s) (requested by msaitoh in ticket #938):
sys/arch/acorn32/podulebus/if_ie.c: revision 1.41
sys/arch/amiga/dev/if_es.c: revision 1.58
sys/arch/amiga/dev/if_qn.c: revision 1.45
sys/arch/arm/at91/at91emac.c: revision 1.20
sys/arch/arm/ep93xx/epe.c: revision 1.37
sys/arch/emips/ebus/if_le_ebus.c: revision 1.14
sys/arch/emips/ebus/if_le_ebus.c: revision 1.15
sys/arch/mac68k/dev/if_mc.c: revision 1.46
sys/arch/macppc/dev/am79c950.c: revision 1.39
sys/arch/newsmips/apbus/if_sn.c: revision 1.40
sys/arch/next68k/dev/mb8795.c: revision 1.59
sys/arch/playstation2/dev/if_smap.c: revision 1.25
sys/arch/playstation2/dev/if_smap.c: revision 1.26
sys/arch/sun2/dev/if_ec.c: revision 1.28
sys/arch/sun3/dev/if_ie.c: revision 1.63
sys/arch/x68k/dev/if_ne_intio.c: revision 1.19
sys/arch/xen/xen/if_xennet_xenbus.c: revision 1.75
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.63
sys/dev/bi/if_ni.c: revision 1.45
sys/dev/cadence/if_cemac.c: revision 1.12
sys/dev/ic/am7990.c: revision 1.78
sys/dev/ic/am79900.c: revision 1.27
sys/dev/ic/an.c: revision 1.67
sys/dev/ic/cs89x0.c: revision 1.40
sys/dev/ic/dm9000.c: revision 1.13
sys/dev/ic/dm9000.c: revision 1.14
sys/dev/ic/dp8390.c: revision 1.88
sys/dev/ic/elink3.c: revision 1.141
sys/dev/ic/elinkxl.c: revision 1.122
sys/dev/ic/hme.c: revision 1.98
sys/dev/ic/i82586.c: revision 1.77
sys/dev/ic/lance.c: revision 1.53
sys/dev/ic/mb86950.c: revision 1.27
sys/dev/ic/mb86960.c: revision 1.86
sys/dev/ic/mtd803.c: revision 1.34
sys/dev/ic/pdq_ifsubr.c: revision 1.59
sys/dev/ic/rrunner.c: revision 1.86
sys/dev/ic/seeq8005.c: revision 1.58
sys/dev/ic/sgec.c: revision 1.47
sys/dev/ic/smc90cx6.c: revision 1.72
sys/dev/ic/smc91cxx.c: revision 1.96
sys/dev/ic/tropic.c: revision 1.49
sys/dev/ic/wi.c: revision 1.245
sys/dev/isa/if_eg.c: revision 1.93
sys/dev/isa/if_el.c: revision 1.95
sys/dev/isa/if_iy.c: revision 1.101
sys/dev/ofw/ofnet.c: revision 1.58
sys/dev/pci/if_alc.c: revision 1.27
sys/dev/pci/if_de.c: revision 1.152
sys/dev/pci/if_fpa.c: revision 1.61
sys/dev/pci/if_jme.c: revision 1.34
sys/dev/pci/if_tl.c: revision 1.108
sys/dev/pci/if_vte.c: revision 1.19
sys/dev/pci/ixgbe/ixgbe.h: revision 1.50
sys/dev/pcmcia/if_cnw.c: revision 1.62
sys/dev/pcmcia/if_malo_pcmcia.c: revision 1.17
sys/dev/pcmcia/if_ray.c: revision 1.89
sys/dev/pcmcia/if_xi.c: revision 1.81
sys/dev/pcmcia/mhzc.c: revision 1.51
sys/dev/pcmcia/xirc.c: revision 1.34
sys/dev/qbus/if_de.c: revision 1.33
sys/dev/qbus/if_qe.c: revision 1.78
sys/dev/qbus/if_qt.c: revision 1.22
sys/dev/sbus/be.c: revision 1.87
sys/dev/sbus/qe.c: revision 1.68
sys/dev/scsipi/if_se.c: revision 1.96
sys/dev/usb/if_atu.c: revision 1.59
sys/net/if_l2tp.c: revision 1.28 via patch
sys/net/if_ppp.c: revision 1.160
It's not required to include net/bpfdesc.h. Remove it.
--
Simplify like other drivers. NULL check of ifp->if_bpf is done in
bpf_mtap(), so it's not required to do it here.
--
Remove duplicated inclusion of net/bpf.h.
--
Remove duplicated inclusion of net/bpf.h.
--
Simplify bpf_mtap() call. No functional change.
 1.58.8.1 15-Jul-2018  martin Pull up following revision(s) (requested by jdolecek in ticket #909):

sys/arch/xen/xen/xennetback_xenbus.c: revision 1.64 (patch)

Fix panic of DOM0 in xennetback_xenbus_destroy() on xl destroy of
DOMU with created, but non CONNECTED xennet (such as when DOMU
panics during boot); only try to disestablish the intr if it was
actually setup.

While here protect xnetback_instances with mutex, and switch to use
kmem_zalloc() + KM_SLEEP / kmem_free() like xbdback_xenbus.c; add XXXSMP
to the other global variables, and at least mark them static
 1.61.2.6 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.61.2.5 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.61.2.4 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.61.2.3 28-Jul-2018  pgoyette Sync with HEAD
 1.61.2.2 25-Jun-2018  pgoyette Sync with HEAD
 1.61.2.1 02-May-2018  pgoyette Synch with HEAD
 1.65.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.65.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.65.2.1 10-Jun-2019  christos Sync with HEAD
 1.75.6.1 29-Feb-2020  ad Sync with head.
 1.75.4.2 24-Feb-2021  martin Apply patch, requested by jdolecek in ticket 1213:

Do not panic Dom0 when hypervisor grant_table_op(GNTTABOP_copy) fails
in xennet(4) backend driver, that can be triggered by malicious or buggy
DomU via bad grant reference.

Part of fixes for XSA-362.
 1.75.4.1 23-Feb-2021  martin Pull up following revision(s) (requested by jdolecek in ticket #1211):

sys/arch/xen/xen/xennetback_xenbus.c: revision 1.92 (via patch)

remove support for legacy rx-flip mode for xennet(4)/xvif(4), making
rx-copy (first shipped in NetBSD 6.0 in 2012) the only supported
mode

this is mostly to simplify maintenance and future development
rx-flip is not supported by Linux Dom0/DomU, and NetBSD Dom0/DomU
defaults to rx-copy for over 8 years now too, so there is little
need to keep the support for compatibility

besides compatibility there is no other reason to keep rx-flip -
page transfer is generally slower than copy due to necessary MMU/TLB
manipulation, especially on MP systems
 1.94.2.1 20-Apr-2020  bouyer Sync with HEAD
 1.108.4.3 22-Dec-2023  martin Pull up following revision(s) (requested by bouyer in ticket #514):

sys/arch/xen/xen/xennetback_xenbus.c: revision 1.124 (patch)

sanity check the fragments we get from the guest and reject the whole packet
if it fails. Make sure we don't KASSERT() on data we get from the frontend.
 1.108.4.2 04-Aug-2023  martin Pull up following revision(s) (requested by riastradh in ticket #312):

sys/arch/xen/xen/xennetback_xenbus.c: revision 1.113
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.114
sys/arch/xen/xen/xennetback_xenbus.c: revision 1.115

Revert "xennetback(4): Omit needless membars in xennetback_connect."
PR kern/57560

Revert "xennetback(4): Fix membars in xennetback_rx_copy_process."
PR kern/57560

Revert "xennetback(4): Fix xennetback_evthandler loop."
PR kern/57560
 1.108.4.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.125.2.1 02-Aug-2025  perseant Sync with HEAD
 1.1 03-Jun-2011  cherry branches: 1.1.2;
file xentests.c was initially added on branch cherry-xenmp.
 1.1.2.3 17-Aug-2011  cherry Pullup relevant changes from -current
 1.1.2.2 05-Aug-2011  cherry pmap_enter() test needs an address that work for both i386 and amd64
 1.1.2.1 03-Jun-2011  cherry Initial import of xen MP sources, with kernel and userspace tests.
- this is a source priview.
- boots to single user.
- spurious interrupt and pmap related panics are normal
 1.29 23-Jun-2024  riastradh pci: Pass cookie through pci_find_device, pci_enumerate_bus, take 2.

New functions pci_find_device1 and pci_enumerate_bus1 have the cookie
argument. Existing symbols pci_find_device and pci_enumerate_bus are
now wrappers for the cookieless version.

This will allow pci_find_device callers to pass a cookie through to
the match function so they can keep state or pass in extra parameters
like b/d/f numbers, which will allow us to nix some horrible kludges
in the Linux PCI API emulation for drm (and, perhaps, Intel wifi).

This change drops the symbol pci_probe_device, in favour of a new
pci_probe_device1 with the cookie argument. But I don't think that
requires a revbump because it's only called by MD pci_enumerate_bus1
implementations, which don't live in modules anyway.

Take 2: Make sure to handle NULL match function.
 1.28 20-May-2024  riastradh Revert "pci: Pass cookie through pci_find_device, pci_enumerate_bus."

Evidently something is wrong with this, to be diagnosed and redone
once the builds and tests are in better shape.
 1.27 20-May-2024  riastradh pci: Pass cookie through pci_find_device, pci_enumerate_bus.

New functions pci_find_device1 and pci_enumerate_bus1 have the cookie
argument. Existing symbols pci_find_device and pci_enumerate_bus are
now wrappers for the cookieless version.

This drops the symbol pci_probe_device, in favour of a new
pci_probe_device1 with the cookie argument. But I don't think that
requires a revbump because it's only called by MD pci_enumerate_bus1
implementations, which don't live in modules anyway.
 1.26 09-Feb-2024  andvar fix spelling mistakes, mainly in comments and log messages.
 1.25 07-Aug-2021  thorpej branches: 1.25.6;
Merge thorpej-cfargs2.
 1.24 24-Apr-2021  thorpej branches: 1.24.8;
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.23 07-Apr-2020  jdolecek branches: 1.23.6;
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.22 02-Feb-2019  cherry Switch NetBSD/xen to use XEN api tag RELEASE-4.11.1

The headers for this api are in sys/external/mit/xen-include-public/dist/
 1.21 24-Dec-2018  cherry 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 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.19 08-Oct-2018  cherry Convert the last remaining users of event_set_handler() to
intr_establish_xname(,&xen_pic,...)
 1.18 19-Aug-2018  riastradh This makes no sense static. (Can't break unused code anyway!)
 1.17 13-Aug-2018  maxv Clarify, remove dead code, and add XXXSMP; really this static variable
looks like a great bug.
 1.16 24-Jun-2018  jdolecek branches: 1.16.2;
mark with XXXSMP all remaining spl*() and tsleep() calls
 1.15 29-Mar-2017  msaitoh branches: 1.15.12;
Fix 0x%d and 0x%u.
 1.14 13-Oct-2013  tsutsui branches: 1.14.6; 1.14.10; 1.14.14;
Fix pasto in previous; pass a correct device_t to config_pending_derc().
 1.13 13-Oct-2013  riz Catch up to recent changes in config_pending_{incr,decr}().
 1.12 05-Dec-2012  jakllsch branches: 1.12.2;
remove trailing whitespace
 1.11 05-Mar-2012  jakllsch branches: 1.11.2;
As per xpci_conf_read, use size argument in xpci_conf_write.
 1.10 02-Feb-2012  tls Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.9 07-Dec-2011  cegger switch from xen3-public to xen-public.
 1.8 01-Sep-2011  christos branches: 1.8.2; 1.8.6;
Add bus_dma overrides. From dyoung
 1.7 27-Aug-2011  christos use c99 struct initializers
 1.6 17-May-2011  dyoung PCI_FLAGS_IO_ENABLED and PCI_FLAGS_MEM_ENABLED changed their functional
role in NetBSD (drivers are no longer supposed to write these to
pa_flags) without changing name. Correct that.

Rename PCI_FLAGS_IO_ENABLED to PCI_FLAGS_IO_OKAY and
PCI_FLAGS_MEM_ENABLED to PCI_FLAGS_MEM_OKAY, thus making their names
consistent with the other PCI flags and poisoning 3rd-party driver
sources that use the flags in the old bad way.

This patch produces no binary changes in this set of PCI kernels when
they are compiled w/o 'options DIAGNOSTIC' and w/ -V MKREPRO=yes:

algor P4032 P5064 P6032
alpha GENERIC
amd64 GENERIC XEN3_DOM0
arc GENERIC
atari HADES MILAN-PCIIDE
bebox GENERIC
cats GENERIC
cobalt GENERIC
evbarm-el ADI_BRH ARMADILLO9 CP3100 GEMINI GEMINI_MASTER GEMINI_SLAVE
evbarm-el GUMSTIX HDL_G IMX31LITE INTEGRATOR IQ31244 IQ80310 IQ80321
evbarm-el IXDP425 IXM1200 KUROBOX_PRO
evbarm-el LUBBOCK MARVELL_NAS NAPPI NSLU2 SHEEVAPLUG SMDK2800 TEAMASA_NPWR
evbarm-el TEAMASA_NPWR_FC TS7200 TWINTAIL ZAO425
evbmips-el AP30 DBAU1500 DBAU1550 MALTA MERAKI MTX-1 OMSAL400 RB153 WGT624V3
evbmips64-el XLSATX
evbppc EV64260 MPC8536DS MPC8548CDS OPENBLOCKS200 OPENBLOCKS266
evbppc OPENBLOCKS266_OPT P2020RDB PMPPC RB800 WALNUT
hp700 GENERIC
i386 ALL XEN3_DOM0 XEN3_DOMU
ibmnws GENERIC
iyonix GENERIC
landisk GENERIC
macppc GENERIC
mvmeppc GENERIC
netwinder GENERIC
ofppc GENERIC
prep GENERIC
sandpoint GENERIC
sbmips-el GENERIC
sgimips GENERIC32_IP2x GENERIC32_IP3x
sparc GENERIC_SUN4U KRUPS
sparc64 GENERIC
 1.5 04-Apr-2011  dyoung Neither pci_dma64_available(), pci_probe_device(), pci_mapreg_map(9),
pci_find_rom(), pci_intr_map(9), pci_enumerate_bus(), nor the match
predicate passed to pciide_compat_intr_establish() should ever modify
their pci_attach_args argument, so make their pci_attach_args arguments
const and deal with the fallout throughout the kernel.

For the most part, these changes add a 'const' where there was no
'const' before, however, some drivers and MD code used to modify
pci_attach_args. Now those drivers either copy their pci_attach_args
and modify the copy, or refrain from modifying pci_attach_args:

Xen: according to Manuel Bouyer, writing to pci_attach_args in
pci_intr_map() was a leftover from Xen 2. Probably a bug. I
stopped writing it. I have not tested this change.

siside(4): sis_hostbr_match() needlessly wrote to pci_attach_args.
Probably a bug. I use a temporary variable. I have not tested this
change.

slide(4): sl82c105_chip_map() overwrote the caller's pci_attach_args.
Probably a bug. Use a local pci_attach_args. I have not tested
this change.

viaide(4): via_sata_chip_map() and via_sata_chip_map_new() overwrote the
caller's pci_attach_args. Probably a bug. Make a local copy of the
caller's pci_attach_args and modify the copy. I have not tested
this change.

While I'm here, make pci_mapreg_submap() static.

With these changes in place, I have tested the compilation of these
kernels:

alpha GENERIC
amd64 GENERIC XEN3_DOM0
arc GENERIC
atari HADES MILAN-PCIIDE
bebox GENERIC
cats GENERIC
cobalt GENERIC
evbarm-eb NSLU2
evbarm-el ADI_BRH ARMADILLO9 CP3100 GEMINI GEMINI_MASTER GEMINI_SLAVE GUMSTIX
HDL_G IMX31LITE INTEGRATOR IQ31244 IQ80310 IQ80321 IXDP425 IXM1200
KUROBOX_PRO LUBBOCK MARVELL_NAS NAPPI SHEEVAPLUG SMDK2800 TEAMASA_NPWR
TEAMASA_NPWR_FC TS7200 TWINTAIL ZAO425
evbmips-el AP30 DBAU1500 DBAU1550 MALTA MERAKI MTX-1 OMSAL400 RB153 WGT624V3
evbmips64-el XLSATX
evbppc EV64260 MPC8536DS MPC8548CDS OPENBLOCKS200 OPENBLOCKS266
OPENBLOCKS266_OPT P2020RDB PMPPC RB800 WALNUT
hp700 GENERIC
i386 ALL XEN3_DOM0 XEN3_DOMU
ibmnws GENERIC
macppc GENERIC
mvmeppc GENERIC
netwinder GENERIC
ofppc GENERIC
prep GENERIC
sandpoint GENERIC
sgimips GENERIC32_IP2x
sparc GENERIC_SUN4U KRUPS
sparc64 GENERIC

As of Sun Apr 3 15:26:26 CDT 2011, I could not compile these kernels
with or without my patches in place:

### evbmips-el GDIUM

nbmake: nbmake: don't know how to make /home/dyoung/pristine-nbsd/src/sys/arch/mips/mips/softintr.c. Stop

### evbarm-el MPCSA_GENERIC
src/sys/arch/evbarm/conf/MPCSA_GENERIC:318: ds1672rtc*: unknown device `ds1672rtc'

### ia64 GENERIC

/tmp/genassym.28085/assym.c: In function 'f111':
/tmp/genassym.28085/assym.c:67: error: invalid application of 'sizeof' to incomplete type 'struct pcb'
/tmp/genassym.28085/assym.c:76: error: dereferencing pointer to incomplete type

### sgimips GENERIC32_IP3x

crmfb.o: In function `crmfb_attach':
crmfb.c:(.text+0x2304): undefined reference to `ddc_read_edid'
crmfb.c:(.text+0x2304): relocation truncated to fit: R_MIPS_26 against `ddc_read_edid'
crmfb.c:(.text+0x234c): undefined reference to `edid_parse'
crmfb.c:(.text+0x234c): relocation truncated to fit: R_MIPS_26 against `edid_parse'
crmfb.c:(.text+0x2354): undefined reference to `edid_print'
crmfb.c:(.text+0x2354): relocation truncated to fit: R_MIPS_26 against `edid_print'
 1.4 28-Apr-2010  dyoung branches: 1.4.2;
On x86, change the bus_space_tag_t to a pointer to a struct
bus_space_tag. For now, bus_space_tag's only member is
bst_type, the type of space, which is either X86_BUS_SPACE_IO
or X86_BUS_SPACE_MEM. In the future, new bus_space_tag members
will refer to override-functions installed by a new function,
bus_space_tag_create(9).

Add pointers to constant struct bus_space_tag, x86_bus_space_io and
x86_bus_space_mem. Use them to replace most uses of X86_BUS_SPACE_IO
and X86_BUS_SPACE_MEM.

Add an x86-specific bus_space_is_equal(9) implementation that compares
the two tags' bst_type.
 1.3 19-Oct-2009  bouyer branches: 1.3.2; 1.3.4; 1.3.6;
Remove closes 3 & 4 from my licence. Lots of thanks to Soren Jacobsen
for the booring work !
 1.2 06-Mar-2009  bouyer branches: 1.2.2; 1.2.4; 1.2.6;
Don't emulate a cold boot here, this breaks drivers using config_interrupt().
 1.1 13-Feb-2009  bouyer branches: 1.1.2;
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS

back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=

frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.

backend is work in progress; support in xentools is not there yet.
 1.1.2.3 28-Apr-2009  skrll Sync with HEAD.
 1.1.2.2 03-Mar-2009  skrll Sync with HEAD.
 1.1.2.1 13-Feb-2009  skrll file xpci_xenbus.c was added on branch nick-hppapmap on 2009-03-03 18:29:49 +0000
 1.2.6.3 04-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/xen/xpci_xenbus.c: revision 1.2
Don't emulate a cold boot here, this breaks drivers using config_interrupt().
 1.2.6.2 03-Oct-2009  snj Pull up following revision(s) (requested by bouyer in ticket #1054):
sys/arch/xen/conf/files.xen: revision 1.93
sys/arch/xen/include/pci_machdep.h: revision 1.11
sys/arch/xen/include/xen.h: revision 1.31
sys/arch/xen/xen/hypervisor.c: revision 1.44
sys/arch/xen/xen/pci_intr_machdep.c: revision 1.8
sys/arch/xen/xen/pciback.c: revision 1.1
sys/arch/xen/xen/xen_machdep.c: revision 1.5
sys/arch/xen/xen/xpci_xenbus.c: revision 1.1
Work in progress on PCI front-end/back-end support
front-end:
- add a xpci* at xenbus? which provides pci busses from the dom0
xpci provides support routines for PCI config space operations and
enumeration in xpci_xenbus.c
- hypervisor.c: do dom0-style PCI attach only ifdef DOM0OPS
- pci_intr_machdep.c: check line value only if DOM0OPS
back-end:
- add a pciback* at pci? device which takes precedences over all
other PCI devices (match return 500) and matches all devices passed
to pciback.hide option on boot command line.
It exports the PCI device informations to files in /kern/xen/pci/
- hypervisor.c: create /kern/xen earlier so pciback can create its
entries while PCI devices are probed
- xen_machdep.c: add handling for pciback.hide=
frontend is know working on Xen 3.1.x dom0 with ahc(4) and pciide(4)
devices. uhci(4) fail when trying to allocate a large contigous DMA
buffer.
backend is work in progress; support in xentools is not there yet.
 1.2.6.1 06-Mar-2009  snj file xpci_xenbus.c was added on branch netbsd-5 on 2009-10-03 23:54:05 +0000
 1.2.4.6 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.2.4.5 02-May-2011  jym Sync with head.
 1.2.4.4 24-Oct-2010  jym Sync with HEAD
 1.2.4.3 01-Nov-2009  jym Sync with HEAD.
 1.2.4.2 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.2.4.1 06-Mar-2009  jym file xpci_xenbus.c was added on branch jym-xensuspend on 2009-05-13 17:18:51 +0000
 1.2.2.4 11-Aug-2010  yamt sync with head.
 1.2.2.3 11-Mar-2010  yamt sync with head
 1.2.2.2 04-May-2009  yamt sync with head.
 1.2.2.1 06-Mar-2009  yamt file xpci_xenbus.c was added on branch yamt-nfs-mp on 2009-05-04 08:12:14 +0000
 1.3.6.2 21-Apr-2010  matt sync to netbsd-5
 1.3.6.1 19-Oct-2009  matt file xpci_xenbus.c was added on branch matt-nb5-mips64 on 2010-04-21 00:33:45 +0000
 1.3.4.3 31-May-2011  rmind sync with head
 1.3.4.2 21-Apr-2011  rmind sync with head
 1.3.4.1 30-May-2010  rmind sync with head
 1.3.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.4.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.8.6.2 06-Mar-2012  mrg sync to -current
 1.8.6.1 18-Feb-2012  mrg merge to -current.
 1.8.2.3 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.8.2.2 16-Jan-2013  yamt sync with (a bit old) head
 1.8.2.1 17-Apr-2012  yamt sync with head
 1.11.2.3 03-Dec-2017  jdolecek update from HEAD
 1.11.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.11.2.1 25-Feb-2013  tls resync with head
 1.12.2.1 18-May-2014  rmind sync with head
 1.14.14.1 21-Apr-2017  bouyer Sync with HEAD
 1.14.10.1 26-Apr-2017  pgoyette Sync with HEAD
 1.14.6.1 28-Aug-2017  skrll Sync with HEAD
 1.15.12.5 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.15.12.4 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.15.12.3 20-Oct-2018  pgoyette Sync with head
 1.15.12.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.15.12.1 25-Jun-2018  pgoyette Sync with HEAD
 1.16.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.16.2.1 10-Jun-2019  christos Sync with HEAD
 1.23.6.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.24.8.1 04-Aug-2021  thorpej Adapt to CFARGS().
 1.25.6.1 04-Oct-2024  martin Pull up following revision(s) (requested by rin in ticket #928):

sys/external/bsd/drm2/dist/drm/drm_gem.c: revision 1.25
sys/external/bsd/drm2/dist/drm/radeon/radeon_ci_dpm.c: revision 1.7
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/priv.h: revision 1.4
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_acpi.c: revision 1.4
sys/external/bsd/drm2/dist/drm/i915/display/intel_opregion.h: revision 1.6
sys/external/bsd/drm2/dist/drm/i915/i915_drv.h: revision 1.49
sys/external/bsd/drm2/include/linux/mxm-wmi.h: revision 1.1
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/pci/nouveau_nvkm_subdev_pci_pcie.c: revision 1.4
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_base.c: revision 1.13
sys/external/bsd/common/include/linux/bitops.h: revision 1.17
sys/external/bsd/drm2/nouveau/files.nouveau: revision 1.40
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.30
sys/external/bsd/drm2/dist/drm/radeon/radeon_drv.h: revision 1.5
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/pci/nouveau_nvkm_subdev_pci_pcie.c: revision 1.5
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/mxm/nouveau_nvkm_subdev_mxm_base.c: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_gart.c: revision 1.12
sys/external/bsd/drm2/dist/drm/radeon/radeon_rv770.c: revision 1.3
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/disp/nouveau_nvkm_engine_disp_sorgm200.c: revision 1.3
sys/external/bsd/common/include/linux/printk.h: revision 1.14
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/instmem/nouveau_nvkm_subdev_instmem_gk20a.c: revision 1.10
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_vi.c: revision 1.4
sys/external/bsd/drm2/include/linux/acpi.h: revision 1.11
sys/external/bsd/drm2/drm/drm_cdevsw.c: revision 1.31
sys/external/bsd/drm2/dist/drm/radeon/radeon_si.c: revision 1.6
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_acpi.c: revision 1.5
sys/external/bsd/drm2/dist/drm/i915/display/intel_acpi.h: revision 1.5
sys/external/bsd/drm2/include/acpi/video.h: revision 1.3
sys/external/bsd/drm2/dist/drm/radeon/radeon_evergreen.c: revision 1.6
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_acpi.h: revision 1.4
sys/arch/sparc64/include/pci_machdep.h: revision 1.31
sys/arch/sparc64/dev/pci_machdep.c: revision 1.83
sys/external/bsd/drm2/include/linux/kref.h: revision 1.14
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_pci.c: revision 1.12
sys/external/bsd/drm2/linux/linux_dma_buf.c: revision 1.17
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/bios/nouveau_nvkm_subdev_bios_shadowacpi.c: revision 1.4
sys/external/bsd/drm2/drm/drm_module.c: revision 1.32
sys/external/bsd/drm2/dist/drm/i915/i915_gem.h: revision 1.8
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_dmabuf.c: revision 1.7
sys/external/bsd/drm2/include/linux/smp.h: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_si.c: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_device.c: revision 1.20
sys/arch/x86/x86/bus_dma.c: revision 1.91
sys/external/bsd/drm2/radeon/files.radeon: revision 1.40
sys/external/bsd/drm2/include/acpi/acpi_bus.h: revision 1.1
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_drv.h: revision 1.5
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_device.c: revision 1.21
sys/external/bsd/common/include/asm/barrier.h: revision 1.20
sys/external/bsd/drm2/include/linux/nbsd-namespace-acpi.h: revision 1.2
sys/external/bsd/common/include/asm/barrier.h: revision 1.21
sys/modules/drmkms/drmkms_pci.h: revision 1.1
sys/external/bsd/drm2/dist/drm/drm_dp_helper.c: revision 1.17
sys/external/bsd/drm2/radeon/radeon_pci.c: revision 1.23
sys/external/bsd/drm2/linux/linux_xa.c: revision 1.4
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.23
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.24
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.25
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.26
sys/dev/pci/pcivar.h: revision 1.120
sys/arch/xen/include/pci_machdep.h: revision 1.24
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.26
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.27
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.27
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.28
sys/external/bsd/drm2/dist/drm/radeon/radeon_cik.c: revision 1.8
sys/external/bsd/drm2/ttm/ttm_bo_vm.c: revision 1.28
sys/external/bsd/drm2/linux/linux_pci.c: revision 1.29
sys/external/bsd/drm2/include/linux/pci.h: revision 1.57
sys/external/bsd/drm2/include/linux/pci.h: revision 1.58
sys/external/bsd/drm2/dist/drm/radeon/radeon_acpi.c: revision 1.5
sys/external/bsd/drm2/dist/drm/nouveau/nouveau_display.c: revision 1.6
sys/external/bsd/drm2/dist/drm/amd/powerplay/hwmgr/amdgpu_hwmgr.c: revision 1.3
sys/external/bsd/drm2/dist/drm/amd/display/dc/core/amdgpu_dc_stream.c: revision 1.3
share/man/man9/bus_dma.9: revision 1.69
sys/external/bsd/drm2/drm/drm_gem_cma_helper.c: revision 1.15
sys/external/bsd/drm2/dist/drm/radeon/radeon_acpi.c: revision 1.6
sys/external/bsd/drm2/dist/drm/radeon/radeon.h: revision 1.12
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_cik.c: revision 1.7
sys/dev/acpi/acpi_mcfg.c: revision 1.29
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_acpi.c: revision 1.6
sys/external/bsd/drm2/dist/drm/radeon/radeon_r600.c: revision 1.7
sys/external/bsd/drm2/dist/drm/radeon/radeon_bios.c: revision 1.13
sys/modules/amdgpu/Makefile: revision 1.9
sys/external/bsd/drm2/dist/drm/radeon/radeon_bios.c: revision 1.14
sys/external/bsd/common/linux/linux_tasklet.c: revision 1.12
sys/external/bsd/drm2/dist/drm/nouveau/include/nvkm/core/device.h: revision 1.10
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_mman.c: revision 1.23
sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu.h: revision 1.9
sys/external/bsd/drm2/include/linux/interval_tree.h: revision 1.14
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_mman.c: revision 1.26
sys/external/bsd/drm2/dist/drm/amd/powerplay/hwmgr/amdgpu_smu7_hwmgr.c: revision 1.5
sys/dev/pci/pci.c: revision 1.168
sys/external/bsd/drm2/dist/drm/i915/gem/i915_gem_mman.c: revision 1.27
sys/external/bsd/drm2/dist/drm/radeon/radeon_si_dpm.c: revision 1.9
sys/external/bsd/drm2/pci/files.drmkms_pci: revision 1.18
sys/external/bsd/drm2/linux/linux_sync_file.c: revision 1.3
sys/external/bsd/drm2/amdgpu/files.amdgpu: revision 1.31
sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/device/nouveau_nvkm_engine_device_tegra.c: revision 1.4
sys/external/bsd/drm2/dist/drm/drm_gem.c: revision 1.24
sys/arch/xen/xen/xpci_xenbus.c: revision 1.29

drm: Eliminate __HAVE_ATOMIC_AS_MEMBAR conditionals.
Discussed on tech-kern:
https://mail-index.netbsd.org/tech-kern/2023/02/23/msg028729.html

linux asm/barrier.h: Fix !MULTIPROCESSOR build.

remove "nouveau" from a comment. noted by jmcneill.

drm: KASSERT(A && B) -> KASSERT(A); KASSERT(B)
comment a function that has a clear overbounds read but it isn't used.
found by GCC 12.

nix the NetBSD specific GEM_BUG_ON().
avoids GCC 12 warnings, and matches upstream closer.
avoid uninitialised variable usage in drm_gem_cma_create_internal().
in the case nothing has returned 'error', 'nsegs' and the dma info
are (potentially) uninitialised, so consider this an error.
found by GCC 12.

avoid a GCC 12 warning.
there's a 1-element long array and a loop conditional that tries to see
if indexes for it are not identical. as these indexes will always both
be 0, the only valid index, the condition is always false. GCC 12
triggers a strange warning on this code that can never run (see below),
so simply assert the array size is 1 and comment the rest.
amdgpu_dc_stream.c:470:55: error: array subscript [0, 0] is outside array bounds of 'struct dc_writeback_info[1]' [-Werror=array-bounds]
470 | stream->writeback_info[j] = stream->writeback_info[i];

convert a KASSERT() into an if () panic() sequence to appease GCC 12.
OK riastradh@.

drm: Fix conditionals around drmkms_pci and agp.
Kernel should build now with all pci drm drivers stripped out but
DRM_LEGACY still enabled. (Might not be very useful, but it'll
build. Maybe we should also have DRM_LEGACY_PCI so those drivers can
be modloaded later.)

drmkms: Fix module build.
avoid an unlikely array bounds issue picked up by GCC 12.
nvkm_pcie_speed() can return -1, which is then used as an array index,
so make this default return PCIe 1.0 speeds.

drm: enable almost all PCIe functionality
linux_pci.c revisions 1.24 and 1.25 implemented most of the remaining
missing PCIe backends, but only enabled them for some amdgpu portions.
this enables all code marked with "XXX amdgpu pcie", "XXX radeon pcie",
and "XXX pcie speed". for most of it, simply removing #ifndefs __NetBSD__
to enable compliation was required, once the new "bus->max_bus_speed"
member was added to struct pci_bus. add an "always fails" backend for
pci_enable_atomic_ops_to_root() which seems to only be necessary
for virtual GPU functionality (and could be implemented if needed.)
tested on radeon 5450, 7750, R7 240 [radeon], and RX 550 [amdgpu], and
nvidia 750 and 1030 [nouveau].
this still does not quite work on nvidia cards. there are two problems
that remain:
- the call to set the link speed is skipped because the speed is set
to the default value of "-1". nvkm_pcie_set_link() will actually
determine the right value for this and for some cards, calling this
function if the current speed is -1 helps set the link speed. it
may be that on linux other paths we don't have enabled properly
would set this (there's one via debugfs, and a jetson specific one,
though perhaps setting either AC or DC speed values as boot options
(after hooking up these for netbsd) would currently work.
- worse, cards newer than kepler - geforce 900, 1000, and newer, are
all lacking the backing support to set pcie link speed. the GT 1030
card i have been testing with remains at pcie 1.0.

radeon: fix and enable ACPI methods for getting ROM BIOS
The hacky way of getting the BIOS mapped only works on x86. ACPI
should be preferred if available. Makes BIOS reading though VFCT
work on aarch64 with EDK2. (But only if EDK2 has POSTed the GPU.)
XXX amdgpu should get the same treatment.

drm: put_cpu() should enable preemption, not disable it again

drm(4): make pr_debug equivalent to aprint_debug
significantly reduces the default spam from amdgpu(4).

drm: Set CONFIG_ACPI in linux/acpi.h and make it build.

Leave a little ACPI-related functionality disabled for now, like
getting EDID out of ACPI -- needs a bit more work to make this work,
and I don't have hardware to work on that.
Should help with failures of the forms:
- unable to locate a BIOS ROM
- bios: unable to locate usable image
on various machines.

radeon_acpi.c: ifdef out unused function on NetBSD.
Should fix syzkaller build.

drm(4): Fix st_rdev in stat.
dminor->index already has the 64*type adjustment, as allocated in
drm_minor_alloc.
PR kern/58180

linux_sync_file: Fix missing init/fini steps.
Noted by rjs@.
PR kern/58210

ttm: Sync ttm_bo_uvm_fault_idle better with Linux.
PR xsrc/58133
ttm: Undo mistake in previous.

PR xsrc/58133
linux: Add a few more cases to pci_get_class.
Should fix crash on boot with amdgpu now that the ACPI business is
enabled.

i915: Fix dmabuf mmap object.

drm: Fix missing bounds checks in dma buf mmap.

drm_gem.c: Fix sense of assertion.
This is the opposite of WARN_ON.
Noted by rjs@.

drm_gem.c: Enable drm_gem_fence_array_add now that we emulate xa.
linux_xa: Delete and replace collision in xa_store as intended.
Don't free the colliding node that's still in the tree.
Noted by rjs@.

i915_gem_mman.c: Apply mmap types via pmap flags.
This way, userland gets buffers mapped write-combining or uncached as
needed.
PR xsrc/58307

x86: Teach bus_dmamem_map about BUS_DMA_PREFETCHABLE.
PR port-amd64/58308

bus_dma(9): Document BUS_DMA_PREFETCHABLE.
Like BUS_DMA_NOCACHE. Doesn't absolve you of the need for
bus_dmamap_sync, but if you later pass the vaddr to bus_dmamap_load,
the DMA map might notice the mapping is write-combining and use this
to make bus_dmamap_sync cheaper.
PR kern/58309

nouveau_nvkm_subdev_instmem_gk20a.c: Use BUS_DMA_PREFETCHABLE.
Matches Linux's pgprot_writecombine.
Unclear where the appropriate bus_dmamap_sync happens, or is supposed
to happen -- not using it would be wrong, but asking for a
prefetchable mapping may paper over symptoms, at least!

ttm: Sync more with Linux.
Add the original copyright and attribution since this is now,
intentionally, a modified copy of the original and not just roughly
the same algorithm.

ttm: Respect PGO_ALLPAGES.
Not sure this is useful but it reduces XXX's and makes this match
udv_fault better so it's easier to understand.

ttm: Sync cacheability flag logic with Linux.

ttm: Add XXX about readahead fault failures.

pci: Pass cookie through pci_find_device, pci_enumerate_bus, take 2.
New functions pci_find_device1 and pci_enumerate_bus1 have the cookie
argument. Existing symbols pci_find_device and pci_enumerate_bus are
now wrappers for the cookieless version.
This will allow pci_find_device callers to pass a cookie through to
the match function so they can keep state or pass in extra parameters
like b/d/f numbers, which will allow us to nix some horrible kludges
in the Linux PCI API emulation for drm (and, perhaps, Intel wifi).
This change drops the symbol pci_probe_device, in favour of a new
pci_probe_device1 with the cookie argument. But I don't think that
requires a revbump because it's only called by MD pci_enumerate_bus1
implementations, which don't live in modules anyway.
Take 2: Make sure to handle NULL match function.
linux_pci: Nix pci enumeration kludges.
Now that we can pass a cookie through, this stuff will be a little
less fragile.

i915: Omit needless i915_gem_object_pin/unpin_pages cycle in fault.
vm_fault_cpu and vm_fault_gtt, called by i915_gem_fault, already do
the pinning and unpinning internally, so there is no need for
i915_gem_fault to do it.
No functional change intended, except that the transient pin count
will be one lower than before during the fault routine (but it will
still be positive).

i915: Match Linux fault routine return code actions.
Omit needless EINTR interception -- this is now handled by
i915_error_to_vmf_fault.
Earlier revert was over a false alarm -- bisection shows the new
warnings arose from linux_pci.c 1.29 here:
https://mail-index.netbsd.org/source-changes/2024/06/23/msg151929.html

linux_pci: Fix shifto in pci_get_class.
It looks like Linux's pci_get_class also matches the interface part
of the PCI class register (but not the revision part), and I hadn't
noticed that in the previous shim structured differently.

With GCC12 kernel ALL/amd64 triggers "'sor' may be used uninitialized".
If "sublinks & 3" is zero GCC is right and sor[1] may be returned unitialized.
Fix by initializing "sor" to zero to return -1 instead of uninitialized value.
Ok: Taylor R Campbell <riastradh@>

amdgpu: Map BAR 2, not BAR 5, on pre-bonaire chips.
PR kern/58384

amdgpu: Map consecutive pages, not the same one over and over again.
PR kern/58385

linux/bitops: Fix overestimate for BITS_TO_LONGS(9)
Fortunately, this seems harmless except for allocating
excessive buffer memory.
Pointed out by nonaka@, OK riastradh@.
 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