Re: Rust BoF and maintainer minutes and planning the roadmap to Rust

2024-10-03 Thread Warner Losh
On Thu, Oct 3, 2024 at 2:53 AM Warner Losh  wrote:

>
>
> On Thu, Sep 26, 2024 at 8:24 AM Alex Bennée 
> wrote:
>
>> One output from this discussion should be a clear statement that we are
>> going forward with this work and the road map. A rough roadmap might
>> look like:
>>
>>   - 9.2   --enable-rust is available and developers can build with it.
>>   rust devices have -x-device or -rust-device CLI flags for
>>   runtime selection.
>>
>>   - 10.x  rust devices feature complete and migration compatible, enabled
>>   by default when rust compiler detected. No CLI selection
>>   required as legacy portions won't be built. Any partial
>>   conversions should be behind --enable-prototype-rust configure
>>   flag.
>>
>>   - 11.x  distros have enough infrastructure to build on supported
>>   platforms. Rust becomes a mandatory dependency, old C versions
>>   of converted code removed from build.
>>
>>   - xx.y  QEMU becomes a pure native rust program and all C is expunged.
>>   We may never get to this point.
>>
>> We should publish the intention and the road map prominently although it
>> was unclear if a blog post would be the best place vs expanding a
>> section in the developers manual. Perhaps both make sense with a blog
>> post for the statement of intent and rough timeline and the developer
>> manual being expanded with any new rules and standards to follow?
>>
>
> FreeBSD is Tier 1 in rust only for amd64 (x86_64). It's Tier 2 for i386
> (which
> admittedly is going away) and Tier 3 for everything else.
>

oops, I should have said it's Tier 2 with hosts for amd64, Tier 2 w/o hosts
and
tier 3 for aarch64 (and everything else). In FreeBSD, amd64 and aarch64 are
tier 1 supported platforms and I got those confused. It is an important
difference
and later in my email I refer to it, so I thought a correction was in order.


> There was some concern about the missing gaps in the support matrix
>> especially as we support a number of "legacy" TCG backends. While *-user
>> support is more insulated from the effects of rust conversions due to
>> its relatively low set of dependencies it will still be a problem if we
>> convert the core CPU QOM classes to rust.
>>
>
> Indeed. I have great concerns here, though we've already dropped
> 32-bit host support for bsd-user. The status of aarch64 support and rumored
> difficulty getting that rust support upgraded give me pause for concern
> because it's a FreeBSD Tier 1 platform. While it basically works, the lack
> of
> commitment by the Rust community is troubling. Even more troubling because
> rust still uses the old FreeBSD 11 compat syscalls, despite upgraded
> being available for years at this point (though maybe this info has changed
> in the last month or two, the years long delay in moving off the interfaces
> that the FreeBSD project obsoleted about 8 years ago is troubling on its
> own).
> Much of the resistance I'm told (I'm not a big rust person, so I have to
> reply
> on others) has been in the rust team because they don't have enough
> familiarity
> with FreeBSD to make any kind of decision so even properly solved issues
> linger in the official upstream. The FreeBSD project critically depends on
> bsd-user for its release process, though that dependency so far has been
> only on x86 and aarch64, both of which work almost all the time, even if
> they aren't Tier 1 rust platforms.
>
> For -system use, this could limit where qemu runs, though to be honest
> the only platform I know has users that might be affected running -system
> might be RISCV.
>
> There's similar issues with other BSDs, but I've heard even less reliable
> information
> about them, so I'll just leave it at that.
>
> So a strawman timeline of 2 years strikes me as unrealistically agressive
> for it to be an absolute requirement given the slow rate of change I've
> seen
> with upstream rust WRT FreeBSD. At the very least, it would put qemu on
> non-x86/non-aarch64 platforms at risk. While not a huge audience, there are
> some users there. The Tier 2 status for Rust at best for FreeBSD is also a
> bit worrying for elimination of all C or a big reliance on rust in the
> core that
> can't realistically be avoided. I'm not sure this should gate the start of
> the rust
> experiment, but I raise it now so as that experiment progresses towards
> production
> people think to talk to me or others in the FreeBSD community as they
> progress.
>
> Warner
>


Re: Rust BoF and maintainer minutes and planning the roadmap to Rust

2024-10-03 Thread Warner Losh
On Thu, Sep 26, 2024 at 8:24 AM Alex Bennée  wrote:

> One output from this discussion should be a clear statement that we are
> going forward with this work and the road map. A rough roadmap might
> look like:
>
>   - 9.2   --enable-rust is available and developers can build with it.
>   rust devices have -x-device or -rust-device CLI flags for
>   runtime selection.
>
>   - 10.x  rust devices feature complete and migration compatible, enabled
>   by default when rust compiler detected. No CLI selection
>   required as legacy portions won't be built. Any partial
>   conversions should be behind --enable-prototype-rust configure
>   flag.
>
>   - 11.x  distros have enough infrastructure to build on supported
>   platforms. Rust becomes a mandatory dependency, old C versions
>   of converted code removed from build.
>
>   - xx.y  QEMU becomes a pure native rust program and all C is expunged.
>   We may never get to this point.
>
> We should publish the intention and the road map prominently although it
> was unclear if a blog post would be the best place vs expanding a
> section in the developers manual. Perhaps both make sense with a blog
> post for the statement of intent and rough timeline and the developer
> manual being expanded with any new rules and standards to follow?
>

FreeBSD is Tier 1 in rust only for amd64 (x86_64). It's Tier 2 for i386
(which
admittedly is going away) and Tier 3 for everything else.

There was some concern about the missing gaps in the support matrix
> especially as we support a number of "legacy" TCG backends. While *-user
> support is more insulated from the effects of rust conversions due to
> its relatively low set of dependencies it will still be a problem if we
> convert the core CPU QOM classes to rust.
>

Indeed. I have great concerns here, though we've already dropped
32-bit host support for bsd-user. The status of aarch64 support and rumored
difficulty getting that rust support upgraded give me pause for concern
because it's a FreeBSD Tier 1 platform. While it basically works, the lack
of
commitment by the Rust community is troubling. Even more troubling because
rust still uses the old FreeBSD 11 compat syscalls, despite upgraded
being available for years at this point (though maybe this info has changed
in the last month or two, the years long delay in moving off the interfaces
that the FreeBSD project obsoleted about 8 years ago is troubling on its
own).
Much of the resistance I'm told (I'm not a big rust person, so I have to
reply
on others) has been in the rust team because they don't have enough
familiarity
with FreeBSD to make any kind of decision so even properly solved issues
linger in the official upstream. The FreeBSD project critically depends on
bsd-user for its release process, though that dependency so far has been
only on x86 and aarch64, both of which work almost all the time, even if
they aren't Tier 1 rust platforms.

For -system use, this could limit where qemu runs, though to be honest
the only platform I know has users that might be affected running -system
might be RISCV.

There's similar issues with other BSDs, but I've heard even less reliable
information
about them, so I'll just leave it at that.

So a strawman timeline of 2 years strikes me as unrealistically agressive
for it to be an absolute requirement given the slow rate of change I've seen
with upstream rust WRT FreeBSD. At the very least, it would put qemu on
non-x86/non-aarch64 platforms at risk. While not a huge audience, there are
some users there. The Tier 2 status for Rust at best for FreeBSD is also a
bit worrying for elimination of all C or a big reliance on rust in the core
that
can't realistically be avoided. I'm not sure this should gate the start of
the rust
experiment, but I raise it now so as that experiment progresses towards
production
people think to talk to me or others in the FreeBSD community as they
progress.

Warner


Re: Rust BoF and maintainer minutes and planning the roadmap to Rust

2024-10-03 Thread Warner Losh
On Thu, Oct 3, 2024, 3:53 AM Daniel P. Berrangé  wrote:

> On Thu, Oct 03, 2024 at 02:53:08AM -0600, Warner Losh wrote:
> > On Thu, Sep 26, 2024 at 8:24 AM Alex Bennée 
> wrote:
> >
> > > One output from this discussion should be a clear statement that we are
> > > going forward with this work and the road map. A rough roadmap might
> > > look like:
> > >
> > >   - 9.2   --enable-rust is available and developers can build with it.
> > >   rust devices have -x-device or -rust-device CLI flags for
> > >   runtime selection.
> > >
> > >   - 10.x  rust devices feature complete and migration compatible,
> enabled
> > >   by default when rust compiler detected. No CLI selection
> > >   required as legacy portions won't be built. Any partial
> > >   conversions should be behind --enable-prototype-rust
> configure
> > >   flag.
> > >
> > >   - 11.x  distros have enough infrastructure to build on supported
> > >   platforms. Rust becomes a mandatory dependency, old C
> versions
> > >   of converted code removed from build.
> > >
> > >   - xx.y  QEMU becomes a pure native rust program and all C is
> expunged.
> > >   We may never get to this point.
> > >
> > > We should publish the intention and the road map prominently although
> it
> > > was unclear if a blog post would be the best place vs expanding a
> > > section in the developers manual. Perhaps both make sense with a blog
> > > post for the statement of intent and rough timeline and the developer
> > > manual being expanded with any new rules and standards to follow?
> > >
> >
> > FeeBSD is Tier 1 in rust only for amd64 (x86_64). It's Tier 2 for i386
> > (which
> > admittedly is going away) and Tier 3 for everything else.
> >
> > > There was some concern about the missing gaps in the support matrix
> > > especially as we support a number of "legacy" TCG backends. While
> *-user
> > > support is more insulated from the effects of rust conversions due to
> > > its relatively low set of dependencies it will still be a problem if we
> > > convert the core CPU QOM classes to rust.
> > >
> >
> > Indeed. I have great concerns here, though we've already dropped
> > 32-bit host support for bsd-user. The status of aarch64 support and
> rumored
> > difficulty getting that rust support upgraded give me pause for concern
> > because it's a FreeBSD Tier 1 platform. While it basically works, the
> lack
> > of commitment by the Rust community is troubling. Even more troubling
> because
> > rust still uses the old FreeBSD 11 compat syscalls, despite upgraded
> > being available for years at this point (though maybe this info has
> changed
> > in the last month or two, the years long delay in moving off the
> interfaces
> > that the FreeBSD project obsoleted about 8 years ago is troubling on its
> > own).
> > Much of the resistance I'm told (I'm not a big rust person, so I have to
> > reply on others) has been in the rust team because they don't have enough
> > familiarity
> > with FreeBSD to make any kind of decision so even properly solved issues
> > linger in the official upstream. The FreeBSD project critically depends
> on
> > bsd-user for its release process, though that dependency so far has been
> > only on x86 and aarch64, both of which work almost all the time, even if
> > they aren't Tier 1 rust platforms.
>
> I don't think we should over-think the upstream Rust support tiers for
> QEMU.
> Rust is one of very few OSS projects that bothers to classify platforms
> they target to this degree of detail. Most OSS projects will test primarily
> on Linux, and x86_64 / aarch64, with most everything else done on an adhoc
> basis, relying on user feedback & patches. eg If I look at what Rust says
>
>   "Tier 3 targets are those which the Rust codebase has support
>for, but which the Rust project does not build or test
>automatically, so they may or may not work."
>
> I would say that describes the status of most non-Linux / non-x86/arm64
> platform combinations, across a very large part of the OSS software
> ecosystem, including a lot of libraries QEMU depends on.
>
> Admittedly, compilers are the area where platform support is taken most
> seriously, given they are the foundation for everything else above, and
> the comparison benchmark in this case is the support matrix offered by
&g

Re: [PATCH v2 0/2] *-user: Handle short reads in mmap_h_gt_g

2024-08-23 Thread Warner Losh
On Tue, Aug 20, 2024 at 4:34 AM Philippe Mathieu-Daudé 
wrote:

> On 20/8/24 07:08, Richard Henderson wrote:
>
> > Richard Henderson (2):
> >linux-user: Handle short reads in mmap_h_gt_g
> >bsd-user: Handle short reads in mmap_h_gt_g
>
> Series:
> Reviewed-by: Philippe Mathieu-Daudé 
>

Series:

Reviewed-by: Warner Losh 


Re: [PATCH 10/18] bsd-user: Add RISC-V thread setup and initialization support

2024-08-02 Thread Warner Losh
On Fri, Aug 2, 2024 at 7:30 AM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 8/2/24 18:34, Ajeet Singh wrote:
> > From: Mark Corbin 
> >
> > Implemented functions for setting up and initializing threads in the
> > RISC-V architecture.
> > The 'target_thread_set_upcall' function sets up the stack pointer,
> > program counter, and function argument for new threads.
> > The 'target_thread_init' function initializes thread registers based on
> > the provided image information.
> >
> > Signed-off-by: Mark Corbin 
> > Signed-off-by: Ajeet Singh 
> > Co-authored-by: Jessica Clarke 
> > Co-authored-by: Kyle Evans 
> > ---
> >   bsd-user/riscv/target_arch_thread.h | 47 +
> >   1 file changed, 47 insertions(+)
> >   create mode 100644 bsd-user/riscv/target_arch_thread.h
> >
> > diff --git a/bsd-user/riscv/target_arch_thread.h
> b/bsd-user/riscv/target_arch_thread.h
> > new file mode 100644
> > index 00..faabb9fb45
> > --- /dev/null
> > +++ b/bsd-user/riscv/target_arch_thread.h
> > @@ -0,0 +1,47 @@
> > +/*
> > + *  RISC-V thread support
> > + *
> > + *  Copyright (c) 2019 Mark Corbin
> > + *
> > + *  This program is free software; you can redistribute it and/or modify
> > + *  it under the terms of the GNU General Public License as published by
> > + *  the Free Software Foundation; either version 2 of the License, or
> > + *  (at your option) any later version.
> > + *
> > + *  This program is distributed in the hope that it will be useful,
> > + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + *  GNU General Public License for more details.
> > + *
> > + *  You should have received a copy of the GNU General Public License
> > + *  along with this program; if not, see  >.
> > + */
> > +
> > +#ifndef TARGET_ARCH_THREAD_H
> > +#define TARGET_ARCH_THREAD_H
> > +
> > +/* Compare with cpu_set_upcall() in riscv/riscv/vm_machdep.c */
> > +static inline void target_thread_set_upcall(CPURISCVState *regs,
> > +abi_ulong entry, abi_ulong arg, abi_ulong stack_base,
> > +abi_ulong stack_size)
> > +{
> > +abi_ulong sp;
> > +
> > +sp = (abi_ulong)(stack_base + stack_size) & ~(16 - 1);
> > +
> > +regs->gpr[xSP] = sp;
> > +regs->pc = entry;
> > +regs->gpr[xA0] = arg;
> > +}
> > +
> > +/* Compare with exec_setregs() in riscv/riscv/machdep.c */
>
> exec_machdep.c.
>
> > +static inline void target_thread_init(struct target_pt_regs *regs,
> > +struct image_info *infop)
> > +{
> > +regs->sepc = infop->entry;
> > +regs->regs[xRA] = infop->entry;
> > +regs->regs[10] = infop->start_stack;   /* a0 */
>
> xA0
>
> > +regs->regs[xSP] = infop->start_stack & ~(16 - 1);
>
> ROUND_DOWN.
>

Oh, I should do a pass through our tree for rounding like this to add this
macro.

Warner


>
> r~
>


Re: [PATCH 15/18] bsd-user: Implement RISC-V signal trampoline setup functions

2024-08-02 Thread Warner Losh
On Fri, Aug 2, 2024 at 7:46 AM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 8/2/24 18:34, Ajeet Singh wrote:
> > From: Mark Corbin 
> >
> > Added functions for setting up the RISC-V signal trampoline and signal
> > frame:
> >
> > 'set_sigtramp_args()': Configures the RISC-V CPU state with arguments
> > for the signal handler. It sets up the registers with the signal
> > number,pointers to the signal info and user context, the signal handler
> > address, and the signal frame pointer.
> >
> > 'setup_sigframe_arch()': Initializes the signal frame with the current
> > machine context.This function copies the context from the CPU state to
> > the signal frame, preparing it for the signal handler.
> >
> > Signed-off-by: Mark Corbin 
> > Signed-off-by: Ajeet Singh 
> > Signed-off-by: Warner Losh 
> > Co-authored-by: Warner Losh 
> > ---
> >   bsd-user/riscv/signal.c | 63 +
> >   1 file changed, 63 insertions(+)
> >   create mode 100644 bsd-user/riscv/signal.c
> >
> > diff --git a/bsd-user/riscv/signal.c b/bsd-user/riscv/signal.c
> > new file mode 100644
> > index 00..005eb53cab
> > --- /dev/null
> > +++ b/bsd-user/riscv/signal.c
> > @@ -0,0 +1,63 @@
> > +/*
> > + *  RISC-V signal definitions
> > + *
> > + *  Copyright (c) 2019 Mark Corbin
> > + *
> > + *  This program is free software; you can redistribute it and/or modify
> > + *  it under the terms of the GNU General Public License as published by
> > + *  the Free Software Foundation; either version 2 of the License, or
> > + *  (at your option) any later version.
> > + *
> > + *  This program is distributed in the hope that it will be useful,
> > + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + *  GNU General Public License for more details.
> > + *
> > + *  You should have received a copy of the GNU General Public License
> > + *  along with this program; if not, see <http://www.gnu.org/licenses/
> >.
> > + */
> > +#include "qemu/osdep.h"
> > +
> > +#include "qemu.h"
> > +
> > +/*
> > + * Compare with sendsig() in riscv/riscv/exec_machdep.c
> > + * Assumes that target stack frame memory is locked.
> > + */
> > +abi_long
> > +set_sigtramp_args(CPURISCVState *regs, int sig, struct target_sigframe
> *frame,
> > +abi_ulong frame_addr, struct target_sigaction *ka)
> > +{
> > +/*
> > + * Arguments to signal handler:
> > + *  a0 (10) = signal number
> > + *  a1 (11) = siginfo pointer
> > + *  a2 (12) = ucontext pointer
> > + *  pc  = signal pointer handler
> > + *  sp (2)  = sigframe pointer
> > + *  ra (1)  = sigtramp at base of user stack
> > + */
> > +
> > + regs->gpr[10] = sig;
> > + regs->gpr[11] = frame_addr +
> > + offsetof(struct target_sigframe, sf_si);
> > + regs->gpr[12] = frame_addr +
> > + offsetof(struct target_sigframe, sf_uc);
>
> xA0 .. xA2
>
> > + regs->pc = ka->_sa_handler;
> > + regs->gpr[2] = frame_addr;
> > + regs->gpr[1] = TARGET_PS_STRINGS - TARGET_SZSIGCODE;
>
> xRA, xSP.
>

So to be clear, this is 'use these constants rather than the raw numbers
for the array subscripts' right?

Warner

Otherwise,
> Reviewed-by: Richard Henderson 
>
> r~
>


[PATCH 16/17] bsd-user: Define validate_prot_to_pageflags and use in mprotect

2024-08-02 Thread Warner Losh
Define validate_prot_to_pageflags. Use it in target_mprotect to validate
the flags. Our taraget_mmap needs more work before it can be used there,
do don't copy linux-user's use of it there. This should hvae no net
functional change, but does make target_mprotect more similar to
linux-user's.

Signed-off-by: Warner Losh 
---
 bsd-user/mmap.c | 28 +++-
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index ffecf52a72a..3c48a188e88 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -60,12 +60,26 @@ void mmap_fork_end(int child)
 pthread_mutex_unlock(&mmap_mutex);
 }
 
+/*
+ * Validate target prot bitmask.
+ * Return the prot bitmask for the host in *HOST_PROT.
+ * Return 0 if the target prot bitmask is invalid, otherwise
+ * the internal qemu page_flags (which will include PAGE_VALID).
+ */
+static int validate_prot_to_pageflags(int prot)
+{
+int valid = PROT_READ | PROT_WRITE | PROT_EXEC;
+int page_flags = (prot & PAGE_RWX) | PAGE_VALID;
+
+return prot & ~valid ? 0 : page_flags;
+}
+
 /* NOTE: all the constants are the HOST ones, but addresses are target. */
 int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
 {
 int host_page_size = qemu_real_host_page_size();
 abi_ulong end, host_start, host_end, addr;
-int prot1, ret;
+int prot1, ret, page_flags;
 
 qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
   " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
@@ -74,14 +88,18 @@ int target_mprotect(abi_ulong start, abi_ulong len, int 
target_prot)
   target_prot & PROT_EXEC ? 'x' : '-');
 if ((start & ~TARGET_PAGE_MASK) != 0)
 return -EINVAL;
+page_flags = validate_prot_to_pageflags(target_prot);
+if (!page_flags) {
+return -TARGET_EINVAL;
+}
 len = TARGET_PAGE_ALIGN(len);
+if (len == 0)
+return 0;
 if (!guest_range_valid_untagged(start, len)) {
 return -ENOMEM;
 }
-end = start + len;
 target_prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
-if (len == 0)
-return 0;
+end = start + len;
 
 mmap_lock();
 host_start = start & -host_page_size;
@@ -122,7 +140,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int 
target_prot)
 if (ret != 0)
 goto error;
 }
-page_set_flags(start, start + len - 1, target_prot | PAGE_VALID);
+page_set_flags(start, start + len - 1, page_flags);
 mmap_unlock();
 return 0;
 error:
-- 
2.45.1




[PATCH 14/17] bsd-user: target_mmap*: change prot to target_prot

2024-08-02 Thread Warner Losh
Adopt the linux-user convention of using target_prot for passed in
protections. no functional change.

Signed-off-by: Warner Losh 
---
 bsd-user/mmap.c | 47 ---
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index d34075c5c64..2118972f073 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -152,7 +152,7 @@ error:
  */
 static int mmap_frag(abi_ulong real_start,
  abi_ulong start, abi_ulong end,
- int prot, int flags, int fd, abi_ulong offset)
+ int target_prot, int flags, int fd, abi_ulong offset)
 {
 abi_ulong real_end, addr;
 void *host_start;
@@ -170,20 +170,20 @@ static int mmap_frag(abi_ulong real_start,
 
 if (prot1 == 0) {
 /* no page was there, so we allocate one. See also above. */
-void *p = mmap(host_start, qemu_host_page_size, prot,
+void *p = mmap(host_start, qemu_host_page_size, target_prot,
flags | ((fd != -1) ? MAP_ANON : 0), -1, 0);
 if (p == MAP_FAILED)
 return -1;
-prot1 = prot;
+prot1 = target_prot;
 }
 prot1 &= PAGE_RWX;
 
-prot_new = prot | prot1;
+prot_new = target_prot | prot1;
 if (fd != -1) {
 /* msync() won't work here, so we return an error if write is
possible while it is a shared mapping */
 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
-(prot & PROT_WRITE))
+(target_prot & PROT_WRITE))
 return -1;
 
 /* adjust protection to be able to read */
@@ -367,7 +367,7 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
 }
 
 /* NOTE: all the constants are the HOST ones */
-abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
+abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot,
  int flags, int fd, off_t offset)
 {
 abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
@@ -377,9 +377,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int 
prot,
 qemu_log("mmap: start=0x" TARGET_ABI_FMT_lx
  " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c flags=",
  start, len,
- prot & PROT_READ ? 'r' : '-',
- prot & PROT_WRITE ? 'w' : '-',
- prot & PROT_EXEC ? 'x' : '-');
+ target_prot & PROT_READ ? 'r' : '-',
+ target_prot & PROT_WRITE ? 'w' : '-',
+ target_prot & PROT_EXEC ? 'x' : '-');
 if (flags & MAP_ALIGNMENT_MASK) {
 qemu_log("MAP_ALIGNED(%u) ",
  (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT);
@@ -416,13 +416,14 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int 
prot,
 goto fail;
 }
 if (flags & MAP_STACK) {
-if ((fd != -1) || ((prot & (PROT_READ | PROT_WRITE)) !=
-(PROT_READ | PROT_WRITE))) {
+if (fd != -1 ||
+((target_prot & (PROT_READ | PROT_WRITE)) !=
+ (PROT_READ | PROT_WRITE))) {
 errno = EINVAL;
 goto fail;
 }
 }
-if ((flags & MAP_GUARD) && (prot != PROT_NONE || fd != -1 ||
+if ((flags & MAP_GUARD) && (target_prot != PROT_NONE || fd != -1 ||
 offset != 0 || (flags & (MAP_SHARED | MAP_PRIVATE |
 /* MAP_PREFAULT | */ /* MAP_PREFAULT not in mman.h */
 MAP_PREFAULT_READ | MAP_ANON | MAP_STACK)) != 0)) {
@@ -512,14 +513,14 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int 
prot,
  * especially important if qemu_host_page_size >
  * qemu_real_host_page_size
  */
-p = mmap(g2h_untagged(start), host_len, prot,
+p = mmap(g2h_untagged(start), host_len, target_prot,
  flags | MAP_FIXED | ((fd != -1) ? MAP_ANON : 0), -1, 0);
 if (p == MAP_FAILED)
 goto fail;
 /* update start so that it points to the file position at 'offset' */
 host_start = (unsigned long)p;
 if (fd != -1) {
-p = mmap(g2h_untagged(start), len, prot,
+p = mmap(g2h_untagged(start), len, target_prot,
  flags | MAP_FIXED, fd, host_offset);
 if (p == MAP_FAILED) {
 munmap(g2h_untagged(start), host_len);
@@ -557,11 +558,11 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int 
prot,
  * possible while it is a shared mapping
  */
 if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
-(prot & PROT_WRITE)) {
+(target_prot &

[PATCH 15/17] bsd-user: target_mprotect: use helper host_page_size local

2024-08-02 Thread Warner Losh
Use helper variable for host_page_size. Linux-user uses a similar helper
to make the code smaller after the multi-page-size migration.

Signed-off-by: Warner Losh 
---
 bsd-user/mmap.c | 15 ---
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index 2118972f073..ffecf52a72a 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -63,6 +63,7 @@ void mmap_fork_end(int child)
 /* NOTE: all the constants are the HOST ones, but addresses are target. */
 int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
 {
+int host_page_size = qemu_real_host_page_size();
 abi_ulong end, host_start, host_end, addr;
 int prot1, ret;
 
@@ -83,7 +84,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int 
target_prot)
 return 0;
 
 mmap_lock();
-host_start = start & qemu_host_page_mask;
+host_start = start & -host_page_size;
 host_end = HOST_PAGE_ALIGN(end);
 if (start > host_start) {
 /* handle host page containing start */
@@ -91,28 +92,28 @@ int target_mprotect(abi_ulong start, abi_ulong len, int 
target_prot)
 for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
 prot1 |= page_get_flags(addr);
 }
-if (host_end == host_start + qemu_host_page_size) {
+if (host_end == host_start + host_page_size) {
 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
 prot1 |= page_get_flags(addr);
 }
 end = host_end;
 }
 ret = mprotect(g2h_untagged(host_start),
-   qemu_host_page_size, prot1 & PAGE_RWX);
+   host_page_size, prot1 & PAGE_RWX);
 if (ret != 0)
 goto error;
-host_start += qemu_host_page_size;
+host_start += host_page_size;
 }
 if (end < host_end) {
 prot1 = target_prot;
 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
 prot1 |= page_get_flags(addr);
 }
-ret = mprotect(g2h_untagged(host_end - qemu_host_page_size),
-   qemu_host_page_size, prot1 & PAGE_RWX);
+ret = mprotect(g2h_untagged(host_end - host_page_size),
+   host_page_size, prot1 & PAGE_RWX);
 if (ret != 0)
 goto error;
-host_end -= qemu_host_page_size;
+host_end -= host_page_size;
 }
 
 /* handle the pages in the middle */
-- 
2.45.1




[PATCH 10/17] bsd-user: Pass image name down the stack

2024-08-02 Thread Warner Losh
Pass the image name down the stack so that we can give better error
messages. Inspired by similar work in linux-user, and more likely to
come.

Signed-off-by: Warner Losh 
---
 bsd-user/elfload.c | 19 +++
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index caf8a1adf2d..dba03f17465 100644
--- a/bsd-user/elfload.c
+++ b/bsd-user/elfload.c
@@ -36,8 +36,8 @@ abi_ulong target_stksiz;
 abi_ulong target_stkbas;
 
 static int elf_core_dump(int signr, CPUArchState *env);
-static int load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr,
-int fd, abi_ulong rbase, abi_ulong *baddrp);
+static int load_elf_sections(const char *image_name, const struct elfhdr *hdr,
+struct elf_phdr *phdr, int fd, abi_ulong rbase, abi_ulong *baddrp);
 
 static inline void memcpy_fromfs(void *to, const void *from, unsigned long n)
 {
@@ -268,7 +268,8 @@ static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
 }
 }
 
-static abi_ulong load_elf_interp(struct elfhdr *interp_elf_ex,
+static abi_ulong load_elf_interp(const char *elf_interpreter,
+ struct elfhdr *interp_elf_ex,
  int interpreter_fd,
  abi_ulong *interp_load_addr)
 {
@@ -335,7 +336,7 @@ static abi_ulong load_elf_interp(struct elfhdr 
*interp_elf_ex,
 }
 }
 
-error = load_elf_sections(interp_elf_ex, elf_phdata, interpreter_fd, rbase,
+error = load_elf_sections(elf_interpreter, interp_elf_ex, elf_phdata, 
interpreter_fd, rbase,
 &baddr);
 if (error != 0) {
 perror("load_elf_sections");
@@ -526,8 +527,9 @@ int is_target_elf_binary(int fd)
 }
 
 static int
-load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr, int fd,
-abi_ulong rbase, abi_ulong *baddrp)
+load_elf_sections(const char *image_name, const struct elfhdr *hdr,
+  struct elf_phdr *phdr, int fd, abi_ulong rbase,
+  abi_ulong *baddrp)
 {
 struct elf_phdr *elf_ppnt;
 abi_ulong baddr;
@@ -764,7 +766,7 @@ int load_elf_binary(struct bsd_binprm *bprm, struct 
image_info *info)
 
 info->elf_flags = elf_ex.e_flags;
 
-error = load_elf_sections(&elf_ex, elf_phdata, bprm->fd, et_dyn_addr,
+error = load_elf_sections(bprm->filename, &elf_ex, elf_phdata, bprm->fd, 
et_dyn_addr,
 &load_addr);
 for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
 if (elf_ppnt->p_type != PT_LOAD) {
@@ -780,7 +782,8 @@ int load_elf_binary(struct bsd_binprm *bprm, struct 
image_info *info)
 }
 
 if (elf_interpreter) {
-elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
+elf_entry = load_elf_interp(elf_interpreter,
+&interp_elf_ex, interpreter_fd,
 &interp_load_addr);
 reloc_func_desc = interp_load_addr;
 
-- 
2.45.1




[PATCH 00/17] For 9.2: A bunch of cleanups and work towards variable pagesize support

2024-08-02 Thread Warner Losh
This series has a number of changes to reduce diffs between blitz and
qemu-project. These are minor and self-contined.

The second half of these changes are the next round of changes in the quest to
support variable page sizes. These are building towards the final set of changes
that will dynamically allocate arrays rather than have them be hard-coded in a

Stacey Son (1):
  bsd-user: Implement cpu_copy()

Warner Losh (16):
  bsd-user: Delete TaskState next member
  bsd-user: Make init_task_state global
  bsd-user: Make cpu_model and cpu_type file scope
  bsd-user: Eliminate unused regs arg in load_elf_binary
  bsd-user: Remove load_flt_binary prototype
  bsd-user: Remove deprecated -p argument
  bsd-user: Eliminate unused qemu_uname_release
  bsd-user: target_msync unused, remove it
  bsd-user: Pass image name down the stack
  bsd-user: Replace set_brk and padzero with zerobss from linux-user
  bsd-user: Use guest_range_valid_untagged to validate range
  bsd-user: target_mprotect: rename prot to target_prot
  bsd-user: target_mmap*: change prot to target_prot
  bsd-user: target_mprotect: use helper host_page_size local
  bsd-user: Define validate_prot_to_pageflags and use in mprotect
  bsd-user: copy linux-user target_mprotect impl

 bsd-user/bsdload.c |   2 +-
 bsd-user/elfload.c | 132 +
 bsd-user/main.c|  48 +++---
 bsd-user/mmap.c| 234 +++--
 bsd-user/qemu.h|   9 +-
 5 files changed, 247 insertions(+), 178 deletions(-)

-- 
2.45.1




[PATCH 09/17] bsd-user: target_msync unused, remove it

2024-08-02 Thread Warner Losh
Nothing calls target_msync in the upstream or blitz fork, so remove it.
It will save us having to modernize it.

Signed-off-by: Warner Losh 
---
 bsd-user/mmap.c | 17 -
 bsd-user/qemu.h |  1 -
 2 files changed, 18 deletions(-)

diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index f3a4f1712da..fc69cb43ebd 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -739,20 +739,3 @@ int target_munmap(abi_ulong start, abi_ulong len)
 mmap_unlock();
 return ret;
 }
-
-int target_msync(abi_ulong start, abi_ulong len, int flags)
-{
-abi_ulong end;
-
-if (start & ~TARGET_PAGE_MASK)
-return -EINVAL;
-len = TARGET_PAGE_ALIGN(len);
-end = start + len;
-if (end < start)
-return -EINVAL;
-if (end == start)
-return 0;
-
-start &= qemu_host_page_mask;
-return msync(g2h_untagged(start), end - start, flags);
-}
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index ed6044cfdaf..a2bc14eea50 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -236,7 +236,6 @@ int target_munmap(abi_ulong start, abi_ulong len);
 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
abi_ulong new_size, unsigned long flags,
abi_ulong new_addr);
-int target_msync(abi_ulong start, abi_ulong len, int flags);
 extern abi_ulong mmap_next_start;
 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
 void mmap_reserve(abi_ulong start, abi_ulong size);
-- 
2.45.1




[PATCH 11/17] bsd-user: Replace set_brk and padzero with zerobss from linux-user

2024-08-02 Thread Warner Losh
The zero_bss interface from linux-user is much better at doing this. Use
it in preference to set_brk (badly named) and padzero. These both have
issues with the new variable page size code, so it's best to just retire
them and reuse the code from linux-user. Also start to use the error
reporting code that linux-user uses to give better error messages on
failure.

Signed-off-by: Warner Losh 
---
 bsd-user/elfload.c | 110 +++--
 1 file changed, 57 insertions(+), 53 deletions(-)

diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index dba03f17465..0a2f2379c93 100644
--- a/bsd-user/elfload.c
+++ b/bsd-user/elfload.c
@@ -22,6 +22,7 @@
 #include "qemu.h"
 #include "disas/disas.h"
 #include "qemu/path.h"
+#include "qapi/error.h"
 
 static abi_ulong target_auxents;   /* Where the AUX entries are in target */
 static size_t target_auxents_sz;   /* Size of AUX entries including AT_NULL */
@@ -210,62 +211,63 @@ static void setup_arg_pages(struct bsd_binprm *bprm, 
struct image_info *info,
 }
 }
 
-static void set_brk(abi_ulong start, abi_ulong end)
+/**
+ * zero_bss:
+ *
+ * Map and zero the bss.  We need to explicitly zero any fractional pages
+ * after the data section (i.e. bss).  Return false on mapping failure.
+ */
+static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss,
+ int prot, Error **errp)
 {
-/* page-align the start and end addresses... */
-start = HOST_PAGE_ALIGN(start);
-end = HOST_PAGE_ALIGN(end);
-if (end <= start) {
-return;
-}
-if (target_mmap(start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC,
-MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
-perror("cannot mmap brk");
-exit(-1);
+abi_ulong align_bss;
+
+/* We only expect writable bss; the code segment shouldn't need this. */
+if (!(prot & PROT_WRITE)) {
+error_setg(errp, "PT_LOAD with non-writable bss");
+return false;
 }
-}
 
+align_bss = TARGET_PAGE_ALIGN(start_bss);
+end_bss = TARGET_PAGE_ALIGN(end_bss);
 
-/*
- * We need to explicitly zero any fractional pages after the data
- * section (i.e. bss).  This would contain the junk from the file that
- * should not be in memory.
- */
-static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
-{
-abi_ulong nbyte;
+if (start_bss < align_bss) {
+int flags = page_get_flags(start_bss);
 
-if (elf_bss >= last_bss) {
-return;
-}
+if (!(flags & PAGE_RWX)) {
+/*
+ * The whole address space of the executable was reserved
+ * at the start, therefore all pages will be VALID.
+ * But assuming there are no PROT_NONE PT_LOAD segments,
+ * a PROT_NONE page means no data all bss, and we can
+ * simply extend the new anon mapping back to the start
+ * of the page of bss.
+ */
+align_bss -= TARGET_PAGE_SIZE;
+} else {
+/*
+ * The start of the bss shares a page with something.
+ * The only thing that we expect is the data section,
+ * which would already be marked writable.
+ * Overlapping the RX code segment seems malformed.
+ */
+if (!(flags & PAGE_WRITE)) {
+error_setg(errp, "PT_LOAD with bss overlapping "
+   "non-writable page");
+return false;
+}
 
-/*
- * XXX: this is really a hack : if the real host page size is
- * smaller than the target page size, some pages after the end
- * of the file may not be mapped. A better fix would be to
- * patch target_mmap(), but it is more complicated as the file
- * size must be known.
- */
-if (qemu_real_host_page_size() < qemu_host_page_size) {
-abi_ulong end_addr, end_addr1;
-end_addr1 = REAL_HOST_PAGE_ALIGN(elf_bss);
-end_addr = HOST_PAGE_ALIGN(elf_bss);
-if (end_addr1 < end_addr) {
-mmap((void *)g2h_untagged(end_addr1), end_addr - end_addr1,
- PROT_READ | PROT_WRITE | PROT_EXEC,
- MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
+/* The page is already mapped and writable. */
+memset(g2h_untagged(start_bss), 0, align_bss - start_bss);
 }
 }
-
-nbyte = elf_bss & (qemu_host_page_size - 1);
-if (nbyte) {
-nbyte = qemu_host_page_size - nbyte;
-do {
-/* FIXME - what to do if put_user() fails? */
-put_user_u8(0, elf_bss);
-elf_bss++;
-} while (--nbyte);
+if (align_bss < end_bss &&
+target_mmap(align_bss, end_bss - align_bss, prot,
+MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
+error_setg_errno(errp, errno, &quo

[PATCH 07/17] bsd-user: Remove deprecated -p argument

2024-08-02 Thread Warner Losh
FreeBSD never really used the -p argument, so it's safe to remove
entirely.

Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 8 
 1 file changed, 8 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 9ad31bd1efe..709ab10ddc1 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -388,14 +388,6 @@ int main(int argc, char **argv)
 }
 } else if (!strcmp(r, "L")) {
 interp_prefix = argv[optind++];
-} else if (!strcmp(r, "p")) {
-unsigned size, want = qemu_real_host_page_size();
-
-r = argv[optind++];
-if (qemu_strtoui(r, NULL, 10, &size) || size != want) {
-warn_report("Deprecated page size option cannot "
-"change host page size (%u)", want);
-}
 } else if (!strcmp(r, "g")) {
 gdbstub = g_strdup(argv[optind++]);
 } else if (!strcmp(r, "r")) {
-- 
2.45.1




[PATCH 17/17] bsd-user: copy linux-user target_mprotect impl

2024-08-02 Thread Warner Losh
Now that we're closer to the linux-user target_mprotect code, go ahead
and grab the rest of the implementation. This moves from a stard, end
impl to a start, last which will allow last page mapping, etc. This also
moves to a more general algorithm. We're close enough that this jump
isn't so large, and doing it incrementally further has become too
much work for too little gain.

Signed-off-by: Warner Losh 
---
 bsd-user/mmap.c | 138 +++-
 1 file changed, 90 insertions(+), 48 deletions(-)

diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index 3c48a188e88..a4de7674bec 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -60,6 +60,17 @@ void mmap_fork_end(int child)
 pthread_mutex_unlock(&mmap_mutex);
 }
 
+/*
+ * Map target protection mask to host. Identity on FreeBSD.
+ */
+static abi_ulong target_to_host_prot(abi_ulong prot)
+{
+return (prot);
+}
+
+/* Helpful temporary #define to reduce diffs with linux-user mmap.c */
+#define trace_target_mprotect(start, len, target_prot)
+
 /*
  * Validate target prot bitmask.
  * Return the prot bitmask for the host in *HOST_PROT.
@@ -78,72 +89,103 @@ static int validate_prot_to_pageflags(int prot)
 int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
 {
 int host_page_size = qemu_real_host_page_size();
-abi_ulong end, host_start, host_end, addr;
-int prot1, ret, page_flags;
-
-qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
-  " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
-  target_prot & PROT_READ ? 'r' : '-',
-  target_prot & PROT_WRITE ? 'w' : '-',
-  target_prot & PROT_EXEC ? 'x' : '-');
-if ((start & ~TARGET_PAGE_MASK) != 0)
-return -EINVAL;
+abi_ulong starts[3];
+abi_ulong lens[3];
+int prots[3];
+abi_ulong host_start, host_last, last;
+int prot1, ret, page_flags, nranges;
+
+trace_target_mprotect(start, len, target_prot);
+
+if ((start & ~TARGET_PAGE_MASK) != 0) {
+return -TARGET_EINVAL;
+}
 page_flags = validate_prot_to_pageflags(target_prot);
 if (!page_flags) {
 return -TARGET_EINVAL;
 }
-len = TARGET_PAGE_ALIGN(len);
-if (len == 0)
+if (len == 0) {
 return 0;
+}
+len = TARGET_PAGE_ALIGN(len);
 if (!guest_range_valid_untagged(start, len)) {
-return -ENOMEM;
+return -TARGET_ENOMEM;
 }
-target_prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
-end = start + len;
 
-mmap_lock();
+last = start + len - 1;
 host_start = start & -host_page_size;
-host_end = HOST_PAGE_ALIGN(end);
-if (start > host_start) {
-/* handle host page containing start */
+host_last = ROUND_UP(last, host_page_size) - 1;
+nranges = 0;
+
+mmap_lock();
+
+if (host_last - host_start < host_page_size) {
+/* Single host page contains all guest pages: sum the prot. */
 prot1 = target_prot;
-for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
-prot1 |= page_get_flags(addr);
+for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) {
+prot1 |= page_get_flags(a);
+}
+for (abi_ulong a = last; a < host_last; a += TARGET_PAGE_SIZE) {
+prot1 |= page_get_flags(a + 1);
 }
-if (host_end == host_start + host_page_size) {
-for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
-prot1 |= page_get_flags(addr);
+starts[nranges] = host_start;
+lens[nranges] = host_page_size;
+prots[nranges] = prot1;
+nranges++;
+} else {
+if (host_start < start) {
+/* Host page contains more than one guest page: sum the prot. */
+prot1 = target_prot;
+for (abi_ulong a = host_start; a < start; a += TARGET_PAGE_SIZE) {
+prot1 |= page_get_flags(a);
+}
+/* If the resulting sum differs, create a new range. */
+if (prot1 != target_prot) {
+starts[nranges] = host_start;
+lens[nranges] = host_page_size;
+prots[nranges] = prot1;
+nranges++;
+host_start += host_page_size;
 }
-end = host_end;
 }
-ret = mprotect(g2h_untagged(host_start),
-   host_page_size, prot1 & PAGE_RWX);
-if (ret != 0)
-goto error;
-host_start += host_page_size;
-}
-if (end < host_end) {
-prot1 = target_prot;
-for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
-prot1 |= page_get_flags(addr);
+
+if (last < host_last) {
+   

[PATCH 06/17] bsd-user: Remove load_flt_binary prototype

2024-08-02 Thread Warner Losh
bsd-user doesn't have support for loading FLT binaries.

Signed-off-by: Warner Losh 
---
 bsd-user/qemu.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index f18a54cc933..b97a902a4c2 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -164,8 +164,6 @@ int loader_exec(const char *filename, char **argv, char 
**envp,
 struct bsd_binprm *bprm);
 
 int load_elf_binary(struct bsd_binprm *bprm, struct image_info *info);
-int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
-struct image_info *info);
 int is_target_elf_binary(int fd);
 
 abi_long memcpy_to_target(abi_ulong dest, const void *src,
-- 
2.45.1




[PATCH 12/17] bsd-user: Use guest_range_valid_untagged to validate range

2024-08-02 Thread Warner Losh
This is the generic validation function, so remove some hand-rolled
ones.

Signed-off-by: Warner Losh 
---
 bsd-user/mmap.c | 11 +++
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index fc69cb43ebd..ed8d31a9048 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -74,9 +74,10 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
 if ((start & ~TARGET_PAGE_MASK) != 0)
 return -EINVAL;
 len = TARGET_PAGE_ALIGN(len);
+if (!guest_range_valid_untagged(start, len)) {
+return -ENOMEM;
+}
 end = start + len;
-if (end < start)
-return -EINVAL;
 prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
 if (len == 0)
 return 0;
@@ -689,11 +690,13 @@ int target_munmap(abi_ulong start, abi_ulong len)
TARGET_ABI_FMT_lx "\n",
start, len);
 #endif
-if (start & ~TARGET_PAGE_MASK)
+if (start & ~TARGET_PAGE_MASK) {
 return -EINVAL;
+}
 len = TARGET_PAGE_ALIGN(len);
-if (len == 0)
+if (len == 0 || !guest_range_valid_untagged(start, len)) {
 return -EINVAL;
+}
 mmap_lock();
 end = start + len;
 real_start = start & qemu_host_page_mask;
-- 
2.45.1




[PATCH 08/17] bsd-user: Eliminate unused qemu_uname_release

2024-08-02 Thread Warner Losh
bsd-user has never supported this, and FreeBSD make it easy to set this
on a per-jail basis, so that the normal reporting routines that we pass
through just work. Since this was never used, and never even in the
usage(), retire it to cut down on the clutter. It was literally just a
write-only variable.

Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 3 ---
 bsd-user/qemu.h | 1 -
 2 files changed, 4 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 709ab10ddc1..8c52fb43ff1 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -92,7 +92,6 @@ static const char *cpu_type;
 unsigned long reserved_va;
 
 const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
-const char *qemu_uname_release;
 
 unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
 unsigned long target_dfldsiz = TARGET_DFLDSIZ;   /* initial data size limit */
@@ -390,8 +389,6 @@ int main(int argc, char **argv)
 interp_prefix = argv[optind++];
 } else if (!strcmp(r, "g")) {
 gdbstub = g_strdup(argv[optind++]);
-} else if (!strcmp(r, "r")) {
-qemu_uname_release = argv[optind++];
 } else if (!strcmp(r, "cpu")) {
 cpu_model = argv[optind++];
 if (is_help_option(cpu_model)) {
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index b97a902a4c2..ed6044cfdaf 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -122,7 +122,6 @@ struct TaskState {
 void init_task_state(TaskState *ts);
 void stop_all_tasks(void);
 extern const char *interp_prefix;
-extern const char *qemu_uname_release;
 
 /*
  * TARGET_ARG_MAX defines the number of bytes allocated for arguments
-- 
2.45.1




[PATCH 04/17] bsd-user: Implement cpu_copy()

2024-08-02 Thread Warner Losh
From: Stacey Son 

Catch up with 30ba0ee52d15 and implement cpu_copy(). It's needed for
threading. Stacey's original code, with bug fixes from Jessica, Justin
and myself.

Signed-off-by: Stacey Son 
Signed-off-by: Jessica Clarke 
Signed-off-by: Justin Hibbits 
Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 31 +++
 1 file changed, 31 insertions(+)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 1533fd51168..9ad31bd1efe 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -224,6 +224,37 @@ void init_task_state(TaskState *ts)
 };
 }
 
+CPUArchState *cpu_copy(CPUArchState *env)
+{
+CPUState *cpu = env_cpu(env);
+CPUState *new_cpu = cpu_create(cpu_type);
+CPUArchState *new_env = cpu_env(new_cpu);
+CPUBreakpoint *bp;
+CPUWatchpoint *wp;
+
+/* Reset non arch specific state */
+cpu_reset(new_cpu);
+
+new_cpu->tcg_cflags = cpu->tcg_cflags;
+memcpy(new_env, env, sizeof(CPUArchState));
+
+/*
+ * Clone all break/watchpoints.
+ * Note: Once we support ptrace with hw-debug register access, make sure
+ * BP_CPU break/watchpoints are handled correctly on clone.
+ */
+QTAILQ_INIT(&cpu->breakpoints);
+QTAILQ_INIT(&cpu->watchpoints);
+QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
+cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
+}
+QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
+cpu_watchpoint_insert(new_cpu, wp->vaddr, wp->len, wp->flags, NULL);
+}
+
+return new_env;
+}
+
 void gemu_log(const char *fmt, ...)
 {
 va_list ap;
-- 
2.45.1




[PATCH 05/17] bsd-user: Eliminate unused regs arg in load_elf_binary

2024-08-02 Thread Warner Losh
Signed-off-by: Warner Losh 
---
 bsd-user/bsdload.c | 2 +-
 bsd-user/elfload.c | 3 +--
 bsd-user/qemu.h| 3 +--
 3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/bsd-user/bsdload.c b/bsd-user/bsdload.c
index 5b3c061a452..dcf3ca14fcc 100644
--- a/bsd-user/bsdload.c
+++ b/bsd-user/bsdload.c
@@ -193,7 +193,7 @@ int loader_exec(const char *filename, char **argv, char 
**envp,
 && bprm->buf[1] == 'E'
 && bprm->buf[2] == 'L'
 && bprm->buf[3] == 'F') {
-retval = load_elf_binary(bprm, regs, infop);
+retval = load_elf_binary(bprm, infop);
 } else {
 fprintf(stderr, "Unknown binary format\n");
 return -1;
diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index 833fa3bd057..caf8a1adf2d 100644
--- a/bsd-user/elfload.c
+++ b/bsd-user/elfload.c
@@ -597,8 +597,7 @@ load_elf_sections(const struct elfhdr *hdr, struct elf_phdr 
*phdr, int fd,
 return 0;
 }
 
-int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
-struct image_info *info)
+int load_elf_binary(struct bsd_binprm *bprm, struct image_info *info)
 {
 struct elfhdr elf_ex;
 struct elfhdr interp_elf_ex;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index c7f78096734..f18a54cc933 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -163,8 +163,7 @@ int loader_exec(const char *filename, char **argv, char 
**envp,
 struct target_pt_regs *regs, struct image_info *infop,
 struct bsd_binprm *bprm);
 
-int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
-struct image_info *info);
+int load_elf_binary(struct bsd_binprm *bprm, struct image_info *info);
 int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
 struct image_info *info);
 int is_target_elf_binary(int fd);
-- 
2.45.1




[PATCH 03/17] bsd-user: Make cpu_model and cpu_type file scope

2024-08-02 Thread Warner Losh
linux-user already does this since 2278b93941d4. That same commit just
added them with main() scope to bsd-user. We need the cpu_type, like
linux-user does, to create new CPUs outside of main to support
threading. Move both cpu_model and cpu_type to mirror linux-user/main.c.

Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 4d29e13a8f5..1533fd51168 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -62,6 +62,8 @@ intptr_t qemu_host_page_mask;
 static bool opt_one_insn_per_tb;
 uintptr_t guest_base;
 bool have_guest_base;
+static const char *cpu_model;
+static const char *cpu_type;
 /*
  * When running 32-on-64 we should make sure we can fit all of the possible
  * guest address space into a contiguous chunk of virtual host memory.
@@ -251,8 +253,6 @@ adjust_ssize(void)
 int main(int argc, char **argv)
 {
 const char *filename;
-const char *cpu_model;
-const char *cpu_type;
 const char *log_file = NULL;
 const char *log_mask = NULL;
 const char *seed_optarg = NULL;
-- 
2.45.1




[PATCH 13/17] bsd-user: target_mprotect: rename prot to target_prot

2024-08-02 Thread Warner Losh
Linux-user's target_mprotect uses this convention, so move to it.

Signed-off-by: Warner Losh 
---
 bsd-user/mmap.c | 18 +-
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index ed8d31a9048..d34075c5c64 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -61,16 +61,16 @@ void mmap_fork_end(int child)
 }
 
 /* NOTE: all the constants are the HOST ones, but addresses are target. */
-int target_mprotect(abi_ulong start, abi_ulong len, int prot)
+int target_mprotect(abi_ulong start, abi_ulong len, int target_prot)
 {
 abi_ulong end, host_start, host_end, addr;
 int prot1, ret;
 
 qemu_log_mask(CPU_LOG_PAGE, "mprotect: start=0x" TARGET_ABI_FMT_lx
   " len=0x" TARGET_ABI_FMT_lx " prot=%c%c%c\n", start, len,
-  prot & PROT_READ ? 'r' : '-',
-  prot & PROT_WRITE ? 'w' : '-',
-  prot & PROT_EXEC ? 'x' : '-');
+  target_prot & PROT_READ ? 'r' : '-',
+  target_prot & PROT_WRITE ? 'w' : '-',
+  target_prot & PROT_EXEC ? 'x' : '-');
 if ((start & ~TARGET_PAGE_MASK) != 0)
 return -EINVAL;
 len = TARGET_PAGE_ALIGN(len);
@@ -78,7 +78,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
 return -ENOMEM;
 }
 end = start + len;
-prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
+target_prot &= PROT_READ | PROT_WRITE | PROT_EXEC;
 if (len == 0)
 return 0;
 
@@ -87,7 +87,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int prot)
 host_end = HOST_PAGE_ALIGN(end);
 if (start > host_start) {
 /* handle host page containing start */
-prot1 = prot;
+prot1 = target_prot;
 for (addr = host_start; addr < start; addr += TARGET_PAGE_SIZE) {
 prot1 |= page_get_flags(addr);
 }
@@ -104,7 +104,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int 
prot)
 host_start += qemu_host_page_size;
 }
 if (end < host_end) {
-prot1 = prot;
+prot1 = target_prot;
 for (addr = end; addr < host_end; addr += TARGET_PAGE_SIZE) {
 prot1 |= page_get_flags(addr);
 }
@@ -117,11 +117,11 @@ int target_mprotect(abi_ulong start, abi_ulong len, int 
prot)
 
 /* handle the pages in the middle */
 if (host_start < host_end) {
-ret = mprotect(g2h_untagged(host_start), host_end - host_start, prot);
+ret = mprotect(g2h_untagged(host_start), host_end - host_start, 
target_prot);
 if (ret != 0)
 goto error;
 }
-page_set_flags(start, start + len - 1, prot | PAGE_VALID);
+page_set_flags(start, start + len - 1, target_prot | PAGE_VALID);
 mmap_unlock();
 return 0;
 error:
-- 
2.45.1




[PATCH 01/17] bsd-user: Delete TaskState next member

2024-08-02 Thread Warner Losh
The next struct member of TaskState is unused. Remove it.

Signed-off-by: Warner Losh 
---
 bsd-user/qemu.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 3736c417860..4ccbee265a1 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -83,7 +83,6 @@ struct emulated_sigtable {
 struct TaskState {
 pid_t ts_tid; /* tid (or pid) of this task */
 
-struct TaskState *next;
 struct bsd_binprm *bprm;
 struct image_info *info;
 
-- 
2.45.1




[PATCH 02/17] bsd-user: Make init_task_state global

2024-08-02 Thread Warner Losh
Restore init_task_state to its global status. It's needed for threading
support outside of main.

Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 2 +-
 bsd-user/qemu.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index cc980e6f401..4d29e13a8f5 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -213,7 +213,7 @@ void qemu_cpu_kick(CPUState *cpu)
 }
 
 /* Assumes contents are already zeroed.  */
-static void init_task_state(TaskState *ts)
+void init_task_state(TaskState *ts)
 {
 ts->sigaltstack_used = (struct target_sigaltstack) {
 .ss_sp = 0,
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 4ccbee265a1..c7f78096734 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -119,6 +119,7 @@ struct TaskState {
 struct target_sigaltstack sigaltstack_used;
 } __attribute__((aligned(16)));
 
+void init_task_state(TaskState *ts);
 void stop_all_tasks(void);
 extern const char *interp_prefix;
 extern const char *qemu_uname_release;
-- 
2.45.1




Re: [PATCH] bsd-user/main: Allow setting tb-size

2024-08-01 Thread Warner Losh
On Thu, Aug 1, 2024 at 2:25 AM Ilya Leoshkevich  wrote:

> On Wed, 2024-07-31 at 15:21 -0600, Warner Losh wrote:
>
> On Wed, Jul 31, 2024 at 8: 45 AM Ilya Leoshkevich 
> wrote: While qemu-system can set tb-size using -accel tcg,tb-size=n, there
> is no similar knob for qemu-bsd-user. Add one in a way similar to how
> one-insn-per-tb is already
> On Wed, Jul 31, 2024 at 8:45 AM Ilya Leoshkevich 
> wrote:
>
> While qemu-system can set tb-size using -accel tcg,tb-size=n, there
> is no similar knob for qemu-bsd-user. Add one in a way similar to how
> one-insn-per-tb is already handled.
>
>
> Cool! Are you using bsd-user and need this for some reason? Or is this
> purely theoretical? Is there a larger context I can read about somewhere?
>
>
> I needed this on Linux in order to debug an issue where I suspected full
> TB invalidation may be an issue.
> It turned out to be something completely different, but I found it useful:
> setting it to, e.g., 4096 makes full TB invalidation very rare, so if a
> problem is still reproducible, then the root causes is something else.
> Philippe suggested to implement this for BSD as well in order to keep the
> interfaces in sync.
>

Excellent! Thank you for taking the time to do this! And the other bug fix.
Both have been queued to my first post 9.1 pull branch.

Warner


Re: [PATCH] bsd-user/x86_64/target_arch_thread.h: Align stack

2024-07-31 Thread Warner Losh
On Wed, Jul 31, 2024 at 5:03 PM Warner Losh  wrote:

> Hmmm... All platforms likely need this
> Oh, they all have it except x86, both 32 and 64-bit but i386 is
> already properly aligned (at least in the
> FreeBSD kernel), so maybe that's not needed.
>
>
> On Wed, Jul 31, 2024 at 4:43 PM Richard Henderson <
> richard.hender...@linaro.org> wrote:
>
>> On 8/1/24 00:44, Ilya Leoshkevich wrote:
>> > bsd-user qemu-x86_64 almost immediately dies with:
>> >
>> >  qemu: 0x4002201a68: unhandled CPU exception 0xd - aborting
>> >
>> > on FreeBSD 14.1-RELEASE. This is an instruction that requires
>> > alignment:
>> >
>> >  (gdb) x/i 0x4002201a68
>> > 0x4002201a68:movaps %xmm0,-0x40(%rbp)
>> >
>> > and the argument is not aligned:
>> >
>> >  (gdb) p/x env->regs[5]
>> >  $1 = 0x822443b58
>> >
>> > A quick experiment shows that the userspace entry point expects
>> > misaligned rsp:
>> >
>> >  (gdb) starti
>> >  (gdb) p/x $rsp
>> >  $1 = 0x7fffeaa8
>> >
>> > Emulate this behavior in bsd-user.
>> >
>> > Signed-off-by: Ilya Leoshkevich 
>> > ---
>> >   bsd-user/x86_64/target_arch_thread.h | 2 +-
>> >   1 file changed, 1 insertion(+), 1 deletion(-)
>> >
>> > diff --git a/bsd-user/x86_64/target_arch_thread.h
>> b/bsd-user/x86_64/target_arch_thread.h
>> > index 52c28906d6d..25233443c14 100644
>> > --- a/bsd-user/x86_64/target_arch_thread.h
>> > +++ b/bsd-user/x86_64/target_arch_thread.h
>> > @@ -31,7 +31,7 @@ static inline void target_thread_init(struct
>> target_pt_regs *regs,
>> >   struct image_info *infop)
>> >   {
>> >   regs->rax = 0;
>> > -regs->rsp = infop->start_stack;
>> > +regs->rsp = (infop->start_stack & ~0xfUL) - 8;
>>
>> The formula in sys/amd64/amd64/exec_machdep.c, exec_setregs is
>>
>>((stack - 8) & ~0xful) + 8
>>
>> With that,
>> Reviewed-by: Richard Henderson 
>>
>
> Agreed.
>
> Reviewed-by: Warner Losh 
>
> There's a *lot* that's missing on for amd64 emulation relative to the
> other platforms.
>

Forgot to mention that I've queued this change with Richard's suggested
modification.

Warner


Re: [PATCH] bsd-user/main: Allow setting tb-size

2024-07-31 Thread Warner Losh
On Wed, Jul 31, 2024 at 3:42 PM Philippe Mathieu-Daudé 
wrote:

> On 31/7/24 23:21, Warner Losh wrote:
> > On Wed, Jul 31, 2024 at 8:45 AM Ilya Leoshkevich  > <mailto:i...@linux.ibm.com>> wrote:
> >
> > While qemu-system can set tb-size using -accel tcg,tb-size=n, there
> > is no similar knob for qemu-bsd-user. Add one in a way similar to how
> > one-insn-per-tb is already handled.
> >
> >
> > Cool! Are you using bsd-user and need this for some reason? Or is this
> > purely theoretical? Is there a larger context I can read about somewhere?
>
> Trying to keep user interface parity between linux/bsd.
>
> Ideally this duplication should be unified in common-user/.
>

I'd love that.

Anyway, both of these patches queued to my branch


> > I'll merge it either way (so none of the above is a criticism, I'm
> genuinely
> > curious) , but I don't get too many bsd-user fixes and this one is
> unusual.
> >
> > Suggested-by: Philippe Mathieu-Daudé  > <mailto:phi...@linaro.org>>
> > Signed-off-by: Ilya Leoshkevich  > <mailto:i...@linux.ibm.com>>
> > ---
> >   bsd-user/main.c | 9 +
> >   1 file changed, 9 insertions(+)
>
>


Re: [PATCH] bsd-user/x86_64/target_arch_thread.h: Align stack

2024-07-31 Thread Warner Losh
Hmmm... All platforms likely need this
Oh, they all have it except x86, both 32 and 64-bit but i386 is already
properly aligned (at least in the
FreeBSD kernel), so maybe that's not needed.


On Wed, Jul 31, 2024 at 4:43 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 8/1/24 00:44, Ilya Leoshkevich wrote:
> > bsd-user qemu-x86_64 almost immediately dies with:
> >
> >  qemu: 0x4002201a68: unhandled CPU exception 0xd - aborting
> >
> > on FreeBSD 14.1-RELEASE. This is an instruction that requires
> > alignment:
> >
> >  (gdb) x/i 0x4002201a68
> > 0x4002201a68:movaps %xmm0,-0x40(%rbp)
> >
> > and the argument is not aligned:
> >
> >  (gdb) p/x env->regs[5]
> >  $1 = 0x822443b58
> >
> > A quick experiment shows that the userspace entry point expects
> > misaligned rsp:
> >
> >  (gdb) starti
> >  (gdb) p/x $rsp
> >  $1 = 0x7fffeaa8
> >
> > Emulate this behavior in bsd-user.
> >
> > Signed-off-by: Ilya Leoshkevich 
> > ---
> >   bsd-user/x86_64/target_arch_thread.h | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/bsd-user/x86_64/target_arch_thread.h
> b/bsd-user/x86_64/target_arch_thread.h
> > index 52c28906d6d..25233443c14 100644
> > --- a/bsd-user/x86_64/target_arch_thread.h
> > +++ b/bsd-user/x86_64/target_arch_thread.h
> > @@ -31,7 +31,7 @@ static inline void target_thread_init(struct
> target_pt_regs *regs,
> >   struct image_info *infop)
> >   {
> >   regs->rax = 0;
> > -regs->rsp = infop->start_stack;
> > +regs->rsp = (infop->start_stack & ~0xfUL) - 8;
>
> The formula in sys/amd64/amd64/exec_machdep.c, exec_setregs is
>
>((stack - 8) & ~0xful) + 8
>
> With that,
> Reviewed-by: Richard Henderson 
>

Agreed.

Reviewed-by: Warner Losh 

There's a *lot* that's missing on for amd64 emulation relative to the other
platforms.

Warner


Re: [PATCH] bsd-user/main: Allow setting tb-size

2024-07-31 Thread Warner Losh
On Wed, Jul 31, 2024 at 8:45 AM Ilya Leoshkevich  wrote:

> While qemu-system can set tb-size using -accel tcg,tb-size=n, there
> is no similar knob for qemu-bsd-user. Add one in a way similar to how
> one-insn-per-tb is already handled.
>

Cool! Are you using bsd-user and need this for some reason? Or is this
purely theoretical? Is there a larger context I can read about somewhere?

I'll merge it either way (so none of the above is a criticism, I'm genuinely
curious) , but I don't get too many bsd-user fixes and this one is unusual.


> Suggested-by: Philippe Mathieu-Daudé 
> Signed-off-by: Ilya Leoshkevich 
> ---
>  bsd-user/main.c | 9 +
>  1 file changed, 9 insertions(+)
>
> diff --git a/bsd-user/main.c b/bsd-user/main.c
> index cc980e6f401..7c230b0c7a5 100644
> --- a/bsd-user/main.c
> +++ b/bsd-user/main.c
> @@ -60,6 +60,7 @@ uintptr_t qemu_host_page_size;
>  intptr_t qemu_host_page_mask;
>
>  static bool opt_one_insn_per_tb;
> +static unsigned long opt_tb_size;
>  uintptr_t guest_base;
>  bool have_guest_base;
>  /*
> @@ -169,6 +170,7 @@ static void usage(void)
> "  (use '-d help' for a list of log items)\n"
> "-D logfilewrite logs to 'logfile' (default stderr)\n"
> "-one-insn-per-tb  run with one guest instruction per emulated
> TB\n"
> +   "-tb-size size TCG translation block cache size\n"
> "-strace   log system calls\n"
> "-trace
> [[enable=]][,events=][,file=]\n"
> "  specify tracing options\n"
> @@ -387,6 +389,11 @@ int main(int argc, char **argv)
>  seed_optarg = optarg;
>  } else if (!strcmp(r, "one-insn-per-tb")) {
>  opt_one_insn_per_tb = true;
> +} else if (!strcmp(r, "tb-size")) {
> +r = argv[optind++];
> +if (qemu_strtoul(r, NULL, 0, &opt_tb_size)) {
> +usage();
> +}
>  } else if (!strcmp(r, "strace")) {
>  do_strace = 1;
>  } else if (!strcmp(r, "trace")) {
> @@ -452,6 +459,8 @@ int main(int argc, char **argv)
>  accel_init_interfaces(ac);
>  object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
>   opt_one_insn_per_tb, &error_abort);
> +object_property_set_int(OBJECT(accel), "tb-size",
> +opt_tb_size, &error_abort);
>  ac->init_machine(NULL);
>  }
>

Reviewed-by: Warner Losh 

I'll queue this to my bsd-user-2024-q3-2 branch. I hope to land it, just
after 9.1.0 release.

Warner


[PULL 15/15] bsd-user: Add target.h for aarch64.

2024-07-24 Thread Warner Losh
For aarch64, it's a 64-bit API, so there's no pairing of registers for
64-bit values.

Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target.h | 20 
 1 file changed, 20 insertions(+)
 create mode 100644 bsd-user/aarch64/target.h

diff --git a/bsd-user/aarch64/target.h b/bsd-user/aarch64/target.h
new file mode 100644
index 000..702aeb7fc57
--- /dev/null
+++ b/bsd-user/aarch64/target.h
@@ -0,0 +1,20 @@
+/*
+ * Aarch64 general target stuff that's common to all aarch details
+ *
+ * Copyright (c) 2022 M. Warner Losh 
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef TARGET_H
+#define TARGET_H
+
+/*
+ * aaarch64 ABI does not 'lump' the registers for 64-bit args.
+ */
+static inline bool regpairs_aligned(void *cpu_env)
+{
+return false;
+}
+
+#endif /* TARGET_H */
-- 
2.45.1




[PULL 04/15] bsd-user:Add ARM AArch64 signal handling support

2024-07-24 Thread Warner Losh
From: Stacey Son 

Added sigcode setup function for signal trampoline which initializes a sequence 
of instructions
to handle signal returns and exits, copying this code to the target offset.
Defined ARM AArch64 specific signal definitions including register indices and 
sizes,
and introduced structures to represent general purpose registers, floating 
point registers, and machine context.
Added function to set up signal handler arguments, populating register values 
in `CPUARMState`
based on the provided signal, signal frame, signal action, and frame address.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Co-authored-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-5-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c   | 53 
 bsd-user/aarch64/target_arch_signal.h   | 80 +
 bsd-user/aarch64/target_arch_sigtramp.h | 48 +++
 3 files changed, 181 insertions(+)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
new file mode 100644
index 000..98861f9ab3b
--- /dev/null
+++ b/bsd-user/aarch64/signal.c
@@ -0,0 +1,53 @@
+/*
+ * ARM AArch64 specific signal definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+
+#include "qemu.h"
+
+/*
+ * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Assumes that target stack frame memory is locked.
+ */
+abi_long set_sigtramp_args(CPUARMState *regs, int sig,
+   struct target_sigframe *frame,
+   abi_ulong frame_addr,
+   struct target_sigaction *ka)
+{
+/*
+ * Arguments to signal handler:
+ *  x0 = signal number
+ *  x1 = siginfo pointer
+ *  x2 = ucontext pointer
+ *  pc/elr = signal handler pointer
+ *  sp = sigframe struct pointer
+ *  lr = sigtramp at base of user stack
+ */
+
+regs->xregs[0] = sig;
+regs->xregs[1] = frame_addr +
+offsetof(struct target_sigframe, sf_si);
+regs->xregs[2] = frame_addr +
+offsetof(struct target_sigframe, sf_uc);
+
+regs->pc = ka->_sa_handler;
+regs->xregs[TARGET_REG_SP] = frame_addr;
+regs->xregs[TARGET_REG_LR] = TARGET_PS_STRINGS - TARGET_SZSIGCODE;
+
+return 0;
+}
diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
new file mode 100644
index 000..df171733166
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -0,0 +1,80 @@
+/*
+ * ARM AArch64 specific signal definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_SIGNAL_H
+#define TARGET_ARCH_SIGNAL_H
+
+#include "cpu.h"
+
+#define TARGET_REG_X0   0
+#define TARGET_REG_X30  30
+#define TARGET_REG_X31  31
+#define TARGET_REG_LR   TARGET_REG_X30
+#define TARGET_REG_SP   TARGET_REG_X31
+
+#define TARGET_INSN_SIZE4   /* arm64 instruction size */
+
+/* Size of the signal trampolin code. See _sigtramp(). */
+#define TARGET_SZSIGCODE((abi_ulong)(9 * TARGET_INSN_SIZE))
+
+/* compare to sys/arm64/include/_limits.h */
+#define TARGET_MINSIGSTKSZ  (1024 * 4)  /* min sig stack size 
*/
+#define TARGET_SIGSTKSZ (TARGET_MINSIGSTKSZ + 32768)  /* recommended size 
*/
+
+/* struct __mcontext in sys/arm64/include/ucontext.h */
+
+str

[PULL 02/15] bsd-user:Add AArch64 register handling and related functions

2024-07-24 Thread Warner Losh
From: Stacey Son 

Added header file for managing CPU register states in FreeBSD user mode.
Introduced prototypes for setting and getting thread-local storage (TLS).
Implemented AArch64 sysarch() system call emulation and a printing function.
Added function for setting up thread upcall to add thread support to BSD-USER.
Initialized thread's register state during thread setup.
Updated ARM AArch64 VM parameter definitions for bsd-user, including address 
spaces for FreeBSD/arm64 and
a function for getting the stack pointer from CPU and setting a return value.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Jessica Clarke 
Co-authored-by: Sean Bruno 
Co-authored-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-3-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch.h | 28 +++
 bsd-user/aarch64/target_arch_reg.h | 56 +
 bsd-user/aarch64/target_arch_sysarch.h | 42 
 bsd-user/aarch64/target_arch_thread.h  | 61 +++
 bsd-user/aarch64/target_arch_vmparam.h | 68 ++
 5 files changed, 255 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
 create mode 100644 bsd-user/aarch64/target_arch_thread.h
 create mode 100644 bsd-user/aarch64/target_arch_vmparam.h

diff --git a/bsd-user/aarch64/target_arch.h b/bsd-user/aarch64/target_arch.h
new file mode 100644
index 000..27f47de8eb3
--- /dev/null
+++ b/bsd-user/aarch64/target_arch.h
@@ -0,0 +1,28 @@
+/*
+ * ARM AArch64 specific prototypes for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_H
+#define TARGET_ARCH_H
+
+#include "qemu.h"
+
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
+target_ulong target_cpu_get_tls(CPUARMState *env);
+
+#endif /* TARGET_ARCH_H */
diff --git a/bsd-user/aarch64/target_arch_reg.h 
b/bsd-user/aarch64/target_arch_reg.h
new file mode 100644
index 000..5c7154f0c18
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_reg.h
@@ -0,0 +1,56 @@
+/*
+ *  FreeBSD arm64 register structures
+ *
+ *  Copyright (c) 2015 Stacey Son
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_REG_H
+#define TARGET_ARCH_REG_H
+
+/* See sys/arm64/include/reg.h */
+typedef struct target_reg {
+uint64_tx[30];
+uint64_tlr;
+uint64_tsp;
+uint64_telr;
+uint64_tspsr;
+} target_reg_t;
+
+typedef struct target_fpreg {
+__uint128_t fp_q[32];
+uint32_tfp_sr;
+uint32_tfp_cr;
+} target_fpreg_t;
+
+#define tswapreg(ptr)   tswapal(ptr)
+
+static inline void target_copy_regs(target_reg_t *regs, CPUARMState *env)
+{
+int i;
+
+for (i = 0; i < 30; i++) {
+regs->x[i] = tswapreg(env->xregs[i]);
+}
+regs->lr = tswapreg(env->xregs[30]);
+regs->sp = tswapreg(env->xregs[31]);
+regs->elr = tswapreg(env->pc);
+regs->spsr = tswapreg(pstate_read(env));
+}
+
+#undef tswapreg
+
+#endif /* TARGET_ARCH_REG_H */
diff --git a/bsd-user/aarch64/target_arch_sysarch.h 
b/bsd-user/aarch64/target_arch_sysarch.h
new file mode 100644
index 000..b003015daf4
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_sysarch.h
@@ -0,0 +1,42 @@
+/*
+ * ARM AArch64 sysarch() system call emulation for bsd-user.
+ *
+ * Copyright (c) 2015 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms o

[PULL 03/15] bsd-user:Add ARM AArch64 support and capabilities

2024-07-24 Thread Warner Losh
Added function to access rval2 by accessing the x1 register.
Defined ARM AArch64 ELF parameters including mmap and dynamic load addresses.
Introduced extensive hardware capability definitions and macros for retrieving 
hardware capability (hwcap) flags.
Implemented function to retrieve ARM AArch64 hardware capabilities using the 
`GET_FEATURE_ID` macro.
Added function to retrieve extended ARM AArch64 hardware capability flags.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Co-authored-by: Kyle Evans 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-4-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch.h |   1 +
 bsd-user/aarch64/target_arch_elf.h | 163 +
 bsd-user/aarch64/target_arch_vmparam.h |   6 +
 3 files changed, 170 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_elf.h

diff --git a/bsd-user/aarch64/target_arch.h b/bsd-user/aarch64/target_arch.h
index 27f47de8eb3..4815a56ae3c 100644
--- a/bsd-user/aarch64/target_arch.h
+++ b/bsd-user/aarch64/target_arch.h
@@ -21,6 +21,7 @@
 #define TARGET_ARCH_H
 
 #include "qemu.h"
+#include "target/arm/cpu-features.h"
 
 void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
 target_ulong target_cpu_get_tls(CPUARMState *env);
diff --git a/bsd-user/aarch64/target_arch_elf.h 
b/bsd-user/aarch64/target_arch_elf.h
new file mode 100644
index 000..cc87f475b3f
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_elf.h
@@ -0,0 +1,163 @@
+/*
+ * ARM AArch64 ELF definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_ELF_H
+#define TARGET_ARCH_ELF_H
+
+#define ELF_START_MMAP 0x8000
+#define ELF_ET_DYN_LOAD_ADDR0x10
+
+#define elf_check_arch(x) ((x) == EM_AARCH64)
+
+#define ELF_CLASS   ELFCLASS64
+#define ELF_DATAELFDATA2LSB
+#define ELF_ARCHEM_AARCH64
+
+#define USE_ELF_CORE_DUMP
+#define ELF_EXEC_PAGESIZE   4096
+
+enum {
+ARM_HWCAP_A64_FP= 1 << 0,
+ARM_HWCAP_A64_ASIMD = 1 << 1,
+ARM_HWCAP_A64_EVTSTRM   = 1 << 2,
+ARM_HWCAP_A64_AES   = 1 << 3,
+ARM_HWCAP_A64_PMULL = 1 << 4,
+ARM_HWCAP_A64_SHA1  = 1 << 5,
+ARM_HWCAP_A64_SHA2  = 1 << 6,
+ARM_HWCAP_A64_CRC32 = 1 << 7,
+ARM_HWCAP_A64_ATOMICS   = 1 << 8,
+ARM_HWCAP_A64_FPHP  = 1 << 9,
+ARM_HWCAP_A64_ASIMDHP   = 1 << 10,
+ARM_HWCAP_A64_CPUID = 1 << 11,
+ARM_HWCAP_A64_ASIMDRDM  = 1 << 12,
+ARM_HWCAP_A64_JSCVT = 1 << 13,
+ARM_HWCAP_A64_FCMA  = 1 << 14,
+ARM_HWCAP_A64_LRCPC = 1 << 15,
+ARM_HWCAP_A64_DCPOP = 1 << 16,
+ARM_HWCAP_A64_SHA3  = 1 << 17,
+ARM_HWCAP_A64_SM3   = 1 << 18,
+ARM_HWCAP_A64_SM4   = 1 << 19,
+ARM_HWCAP_A64_ASIMDDP   = 1 << 20,
+ARM_HWCAP_A64_SHA512= 1 << 21,
+ARM_HWCAP_A64_SVE   = 1 << 22,
+ARM_HWCAP_A64_ASIMDFHM  = 1 << 23,
+ARM_HWCAP_A64_DIT   = 1 << 24,
+ARM_HWCAP_A64_USCAT = 1 << 25,
+ARM_HWCAP_A64_ILRCPC= 1 << 26,
+ARM_HWCAP_A64_FLAGM = 1 << 27,
+ARM_HWCAP_A64_SSBS  = 1 << 28,
+ARM_HWCAP_A64_SB= 1 << 29,
+ARM_HWCAP_A64_PACA  = 1 << 30,
+ARM_HWCAP_A64_PACG  = 1UL << 31,
+
+ARM_HWCAP2_A64_DCPODP   = 1 << 0,
+ARM_HWCAP2_A64_SVE2 = 1 << 1,
+ARM_HWCAP2_A64_SVEAES   = 1 << 2,
+ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
+ARM_HWCAP2_A64_SVEBITPERM   = 1 << 4,
+ARM_HWCAP2_A64_SVESHA3  = 1 << 5,
+ARM_HWCAP2_A64_SVESM4   = 1 << 6,
+ARM_HWCAP2_A64_FLAGM2   = 1 << 7,
+ARM_HWCAP2_A64_FRINT= 1 << 8,
+ARM_HWCAP2_A64_SVEI8MM  = 1 << 9,
+ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
+ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
+ARM_HWCAP2_A64_SVEBF16  = 1 << 12,
+ARM_HWCAP2_A64_I8MM = 1 <

[PULL 13/15] bsd-user: Make compile for non-linux user-mode stuff

2024-07-24 Thread Warner Losh
We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use
them unconditionally. Restrict its use to Linux-only.

"It's ugly, but it's not actually wrong."

Signed-off-by: Warner Losh 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Richard Henderson 
---
 target/arm/gdbstub64.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/target/arm/gdbstub64.c b/target/arm/gdbstub64.c
index c8cef8cbc0e..5221381cc85 100644
--- a/target/arm/gdbstub64.c
+++ b/target/arm/gdbstub64.c
@@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray 
*buf, int reg)
 
 int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
 {
+#if defined(CONFIG_LINUX)
 ARMCPU *cpu = ARM_CPU(cs);
 CPUARMState *env = &cpu->env;
 
@@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, 
int reg)
 arm_set_mte_tcf0(env, tcf);
 
 return 1;
+#else
+return 0;
+#endif
 }
 
 static void handle_q_memtag(GArray *params, void *user_ctx)
-- 
2.45.1




[PULL 00/15] Bsd user for 9.1 patches

2024-07-24 Thread Warner Losh
The following changes since commit 3cce8bd4d737f2ca688bbdcb92cd5cc683245bbd:

  Merge tag 'ui-pull-request' of https://gitlab.com/marcandre.lureau/qemu into 
staging (2024-07-23 15:23:05 +1000)

are available in the Git repository at:

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

for you to fetch changes up to ec018b76798e1196882ebcbf3df15e6d86ac9d7c:

  bsd-user: Add target.h for aarch64. (2024-07-24 16:02:07 -0600)


bsd-user: Misc changes for 9.1 (I hope)

V2: Add missing bsd-user/aarch64/target.h

This patch series includes two main sets of patches. To make it simple to
review, I've included the changes from my student which the later changes depend
on. I've included a change from Jessica and Doug as well. I've reviewed them,
but more eyes never hurt.

I've also included a number of 'touch up' patches needed either to get the
aarch64 building, or to implmement suggestions from prior review cycles. The
main one is what's charitably described as a kludge: force aarch64 to use 4k
pages. The qemu-project (and blitz branch) hasn't had the necessary changes to
bsd-user needed to support variable page size.

Sorry this is so late... Live has conspired to delay me.
-BEGIN PGP SIGNATURE-
Comment: GPGTools - https://gpgtools.org

iQIzBAABCgAdFiEEIDX4lLAKo898zeG3bBzRKH2wEQAFAmahejwACgkQbBzRKH2w
EQCXuQ/+Pj1Izmox/y9X1trn1T8KC7JdMtimdLiGMaS4C6+gcThXJkIB4l9ZStbV
7rI540mpqVf0KSRLYwc2/ATyhYU7Ffsz02WPn7Xn/NvmmITp4kjw9Z0gd7C7mPVq
fS8DJbTyFQDy5dO8FUKLaTfnlYQe+NCnL421t9wFkIrlEepFygRaBaJN5yWVoC+0
1Ob6dG+JEV5BmNguMufvvI3S7nEFEnSBGpNqW3ljrRHAZjdNhv8d9GBYbj1laR1r
HQ6r5+u4ZmKCuUbchS0jxGkug0DjuQC7iq+rQ/7fhLYLChkPZ4P2RxNv8ibzKjEV
wlTy5LaM+WZNzKWdcHfDFMomeSnnUkOOfAMipMney2jedEjTIwCFDnP4zCAuG83V
RbdXWfleP1rDto3AQ765pFneqm3+su2Dh4TKaTSnq6gd1eORJ2IL8dubCfcVwZCy
TofemXPWh0HX3kwlD9IB9rqplQZFL78TkQ47btftxinHCLCQOOHRDPVG0IahQPjo
pgK4yVH7WA7pWV2Xbo4ngG3sX5U1TyBCbfkkAwhq+P3gjnU8zxonx8Tk/qLeEDdH
KEypi/pkGFQKZY0wc/y4XM+XQh6E1l8gMaQ4gJWK1qlyVtUKM1BiNQ2lweohYzC8
p6WAfBQLPpzY4mDWfJMF6DsgObLwWmYbgKzuOtHgST1D/Ebk3Zo=
=RPuN
-END PGP SIGNATURE-



Doug Rabson (1):
  bsd-user: Simplify the implementation of execve

Jessica Clarke (1):
  bsd-user: Sync fork_start/fork_end with linux-user

Stacey Son (6):
  bsd-user:Add CPU initialization and management functions
  bsd-user:Add AArch64 register handling and related functions
  bsd-user:Add ARM AArch64 signal handling support
  bsd-user:Add get_mcontext function for ARM AArch64
  bsd-user:Add set_mcontext function for ARM AArch64
  bsd-user:Add AArch64 improvements and signal handling functions

Warner Losh (7):
  bsd-user:Add ARM AArch64 support and capabilities
  bsd-user:Add setup_sigframe_arch function for ARM AArch64
  bsd-user: Hard wire aarch64 to be 4k pages only
  bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack
  bsd-user: Make compile for non-linux user-mode stuff
  bsd-user: Add aarch64 build to tree
  bsd-user: Add target.h for aarch64.

 bsd-user/aarch64/signal.c   | 137 +
 bsd-user/aarch64/target.h   |  20 +++
 bsd-user/aarch64/target_arch.h  |  29 
 bsd-user/aarch64/target_arch_cpu.c  |  31 
 bsd-user/aarch64/target_arch_cpu.h  | 189 
 bsd-user/aarch64/target_arch_elf.h  | 163 
 bsd-user/aarch64/target_arch_reg.h  |  56 +++
 bsd-user/aarch64/target_arch_signal.h   |  82 ++
 bsd-user/aarch64/target_arch_sigtramp.h |  48 ++
 bsd-user/aarch64/target_arch_sysarch.h  |  42 ++
 bsd-user/aarch64/target_arch_thread.h   |  61 
 bsd-user/aarch64/target_arch_vmparam.h  |  74 ++
 bsd-user/aarch64/target_syscall.h   |  51 +++
 bsd-user/arm/target_arch_signal.h   |   2 +
 bsd-user/freebsd/os-proc.c  | 118 +--
 bsd-user/i386/target_arch_signal.h  |   2 +
 bsd-user/main.c |  44 ++
 bsd-user/qemu.h |   3 +
 bsd-user/signal.c   |   9 +-
 bsd-user/x86_64/target_arch_signal.h|   2 +
 configs/targets/aarch64-bsd-user.mak|   3 +
 target/arm/cpu-param.h  |   4 +
 target/arm/gdbstub64.c  |   4 +
 23 files changed, 1021 insertions(+), 153 deletions(-)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target.h
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_arch_elf.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h
 create mode 100644 bsd-user/aarch64/target_a

[PULL 12/15] bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack

2024-07-24 Thread Warner Losh
Most (all?) targets require stacks to be properly aligned. Rather than a
series of ifdefs in bsd-user/signal.h, instead use a manditory #define
for all architectures.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 bsd-user/aarch64/target_arch_signal.h | 2 ++
 bsd-user/arm/target_arch_signal.h | 2 ++
 bsd-user/i386/target_arch_signal.h| 2 ++
 bsd-user/signal.c | 9 +
 bsd-user/x86_64/target_arch_signal.h  | 2 ++
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
index bff752a67ab..b72ba7aa504 100644
--- a/bsd-user/aarch64/target_arch_signal.h
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -77,4 +77,6 @@ struct target_sigframe {
 target_ucontext_t   sf_uc;  /* saved ucontext */
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/arm/target_arch_signal.h 
b/bsd-user/arm/target_arch_signal.h
index 02b2b33e07a..10f96b8bfc9 100644
--- a/bsd-user/arm/target_arch_signal.h
+++ b/bsd-user/arm/target_arch_signal.h
@@ -86,4 +86,6 @@ struct target_sigframe {
 target_mcontext_vfp_t sf_vfp; /* actual saved VFP context */
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/i386/target_arch_signal.h 
b/bsd-user/i386/target_arch_signal.h
index 279dadc22c7..2c14153ab6b 100644
--- a/bsd-user/i386/target_arch_signal.h
+++ b/bsd-user/i386/target_arch_signal.h
@@ -88,4 +88,6 @@ struct target_sigframe {
 uint32_t__spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/signal.c b/bsd-user/signal.c
index 8b6654b91da..da49b9bffc1 100644
--- a/bsd-user/signal.c
+++ b/bsd-user/signal.c
@@ -728,14 +728,7 @@ static inline abi_ulong get_sigframe(struct 
target_sigaction *ka,
 sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
 }
 
-/* TODO: make this a target_arch function / define */
-#if defined(TARGET_ARM)
-return (sp - frame_size) & ~7;
-#elif defined(TARGET_AARCH64)
-return (sp - frame_size) & ~15;
-#else
-return sp - frame_size;
-#endif
+return ROUND_DOWN(sp - frame_size, TARGET_SIGSTACK_ALIGN);
 }
 
 /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
diff --git a/bsd-user/x86_64/target_arch_signal.h 
b/bsd-user/x86_64/target_arch_signal.h
index ca24bf1e7f7..f833ee66cef 100644
--- a/bsd-user/x86_64/target_arch_signal.h
+++ b/bsd-user/x86_64/target_arch_signal.h
@@ -97,4 +97,6 @@ struct target_sigframe {
 uint32_t__spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
-- 
2.45.1




[PULL 10/15] bsd-user: Hard wire aarch64 to be 4k pages only

2024-07-24 Thread Warner Losh
Only support 4k pages for aarch64 binaries. The variable page size stuff
isn't working just yet, so put in this lessor-of-evils kludge until that
is complete.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 target/arm/cpu-param.h | 4 
 1 file changed, 4 insertions(+)

diff --git a/target/arm/cpu-param.h b/target/arm/cpu-param.h
index 2d5f3aa312c..fa6cae0e3aa 100644
--- a/target/arm/cpu-param.h
+++ b/target/arm/cpu-param.h
@@ -21,9 +21,13 @@
 #ifdef CONFIG_USER_ONLY
 # ifdef TARGET_AARCH64
 #  define TARGET_TAGGED_ADDRESSES
+# ifdef __FreeBSD__
+#  define TARGET_PAGE_BITS 12
+# else
 /* Allow user-only to vary page size from 4k */
 #  define TARGET_PAGE_BITS_VARY
 #  define TARGET_PAGE_BITS_MIN  12
+# endif
 # else
 #  define TARGET_PAGE_BITS 12
 # endif
-- 
2.45.1




[PULL 08/15] bsd-user:Add AArch64 improvements and signal handling functions

2024-07-24 Thread Warner Losh
From: Stacey Son 

Added get_ucontext_sigreturn function to check processor state ensuring current 
execution mode is EL0 and no flags
indicating interrupts or exceptions are set.
Updated AArch64 code to use CF directly without reading/writing the entire 
processor state, improving efficiency.
Changed FP data structures to use Int128 instead of __uint128_t, leveraging 
QEMU's generic mechanism for referencing this type.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-9-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 20 +++-
 bsd-user/aarch64/target_arch_cpu.h|  7 ++-
 bsd-user/aarch64/target_arch_reg.h|  2 +-
 bsd-user/aarch64/target_arch_signal.h |  2 +-
 bsd-user/qemu.h   |  3 +++
 5 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 13faac8ce60..6bc73a798f3 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -21,7 +21,7 @@
 #include "qemu.h"
 
 /*
- * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Compare to sendsig() in sys/arm64/arm64/exec_machdep.c
  * Assumes that target stack frame memory is locked.
  */
 abi_long set_sigtramp_args(CPUARMState *regs, int sig,
@@ -117,3 +117,21 @@ abi_long set_mcontext(CPUARMState *regs, target_mcontext_t 
*mcp, int srflag)
 
 return err;
 }
+
+/* Compare to sys_sigreturn() in  arm64/arm64/machdep.c */
+abi_long get_ucontext_sigreturn(CPUARMState *regs, abi_ulong target_sf,
+abi_ulong *target_uc)
+{
+uint32_t pstate = pstate_read(regs);
+
+*target_uc = 0;
+
+if ((pstate & PSTATE_M) != PSTATE_MODE_EL0t  ||
+(pstate & (PSTATE_F | PSTATE_I | PSTATE_A | PSTATE_D)) != 0) {
+return -TARGET_EINVAL;
+}
+
+*target_uc = target_sf;
+
+return 0;
+}
diff --git a/bsd-user/aarch64/target_arch_cpu.h 
b/bsd-user/aarch64/target_arch_cpu.h
index 5c150bb7e9c..b288e0d069b 100644
--- a/bsd-user/aarch64/target_arch_cpu.h
+++ b/bsd-user/aarch64/target_arch_cpu.h
@@ -48,7 +48,6 @@ static inline void target_cpu_loop(CPUARMState *env)
 CPUState *cs = env_cpu(env);
 int trapnr, ec, fsc, si_code, si_signo;
 uint64_t code, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-uint32_t pstate;
 abi_long ret;
 
 for (;;) {
@@ -88,18 +87,16 @@ static inline void target_cpu_loop(CPUARMState *env)
  * The carry bit is cleared for no error; set for error.
  * See arm64/arm64/vm_machdep.c cpu_set_syscall_retval()
  */
-pstate = pstate_read(env);
 if (ret >= 0) {
-pstate &= ~PSTATE_C;
+env->CF = 0;
 env->xregs[0] = ret;
 } else if (ret == -TARGET_ERESTART) {
 env->pc -= 4;
 break;
 } else if (ret != -TARGET_EJUSTRETURN) {
-pstate |= PSTATE_C;
+env->CF = 1;
 env->xregs[0] = -ret;
 }
-pstate_write(env, pstate);
 break;
 
 case EXCP_INTERRUPT:
diff --git a/bsd-user/aarch64/target_arch_reg.h 
b/bsd-user/aarch64/target_arch_reg.h
index 5c7154f0c18..b53302e7f7a 100644
--- a/bsd-user/aarch64/target_arch_reg.h
+++ b/bsd-user/aarch64/target_arch_reg.h
@@ -31,7 +31,7 @@ typedef struct target_reg {
 } target_reg_t;
 
 typedef struct target_fpreg {
-__uint128_t fp_q[32];
+Int128  fp_q[32];
 uint32_tfp_sr;
 uint32_tfp_cr;
 } target_fpreg_t;
diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
index df171733166..bff752a67ab 100644
--- a/bsd-user/aarch64/target_arch_signal.h
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -49,7 +49,7 @@ struct target_gpregs {
 };
 
 struct target_fpregs {
-__uint128_t fp_q[32];
+Int128  fp_q[32];
 uint32_tfp_sr;
 uint32_tfp_cr;
 uint32_tfp_flags;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 9d2fc7148eb..3736c417860 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -17,6 +17,9 @@
 #ifndef QEMU_H
 #define QEMU_H
 
+#include 
+
+#include "qemu/int128.h"
 #include "cpu.h"
 #include "qemu/units.h"
 #include "exec/cpu_ldst.h"
-- 
2.45.1




[PULL 01/15] bsd-user:Add CPU initialization and management functions

2024-07-24 Thread Warner Losh
From: Stacey Son 

Added function to initialize ARM CPU and check if it supports 64-bit mode.
Implemented CPU loop function to handle exceptions and emulate execution of 
instructions.
Added function to clone CPU state to create a new thread.
Included AArch64 specific CPU functions for bsd-user to set and receive 
thread-local-storage
value from the tpidr_el0 register.
Introduced structure for storing CPU register states for BSD-USER.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Kyle Evans 
Co-authored-by: Sean Bruno 
Co-authored-by: Jessica Clarke 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-2-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch_cpu.c |  31 +
 bsd-user/aarch64/target_arch_cpu.h | 192 +
 bsd-user/aarch64/target_syscall.h  |  51 
 3 files changed, 274 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_syscall.h

diff --git a/bsd-user/aarch64/target_arch_cpu.c 
b/bsd-user/aarch64/target_arch_cpu.c
new file mode 100644
index 000..b2fa59efaf6
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_cpu.c
@@ -0,0 +1,31 @@
+/*
+ * ARM AArch64 specific CPU for bsd-user
+ *
+ * Copyright (c) 2015 Stacey Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+#include "target_arch.h"
+
+/* See cpu_set_user_tls() in arm64/arm64/vm_machdep.c */
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls)
+{
+env->cp15.tpidr_el[0] = newtls;
+}
+
+target_ulong target_cpu_get_tls(CPUARMState *env)
+{
+return env->cp15.tpidr_el[0];
+}
diff --git a/bsd-user/aarch64/target_arch_cpu.h 
b/bsd-user/aarch64/target_arch_cpu.h
new file mode 100644
index 000..5c150bb7e9c
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_cpu.h
@@ -0,0 +1,192 @@
+/*
+ *  ARM AArch64 cpu init and loop
+ *
+ * Copyright (c) 2015 Stacey Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_CPU_H
+#define TARGET_ARCH_CPU_H
+
+#include "target_arch.h"
+#include "signal-common.h"
+#include "target/arm/syndrome.h"
+
+#define TARGET_DEFAULT_CPU_MODEL "any"
+
+static inline void target_cpu_init(CPUARMState *env,
+struct target_pt_regs *regs)
+{
+int i;
+
+if (!(arm_feature(env, ARM_FEATURE_AARCH64))) {
+fprintf(stderr, "The selected ARM CPU does not support 64 bit mode\n");
+exit(1);
+}
+for (i = 0; i < 31; i++) {
+env->xregs[i] = regs->regs[i];
+}
+env->pc = regs->pc;
+env->xregs[31] = regs->sp;
+}
+
+
+static inline void target_cpu_loop(CPUARMState *env)
+{
+CPUState *cs = env_cpu(env);
+int trapnr, ec, fsc, si_code, si_signo;
+uint64_t code, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+uint32_t pstate;
+abi_long ret;
+
+for (;;) {
+cpu_exec_start(cs);
+trapnr = cpu_exec(cs);
+cpu_exec_end(cs);
+process_queued_cpu_work(cs);
+
+switch (trapnr) {
+case EXCP_SWI:
+/* See arm64/arm64/trap.c cpu_fetch_syscall_args() */
+code = env->xregs[8];
+if (code == TARGET_FREEBSD_NR_syscall ||
+code == TARGET_FREEBSD_NR___syscall) {
+code = env->xregs[0];
+arg1 = env->xregs[1];
+arg2 = env->xregs[2];
+arg3 = env->xregs[3];
+arg4 = env->xregs[4];
+arg5 = env-&g

[PULL 05/15] bsd-user:Add get_mcontext function for ARM AArch64

2024-07-24 Thread Warner Losh
From: Stacey Son 

function to retrieve machine context,it populates the provided
target_mcontext_t structure with information from the CPUARMState
registers.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Kyle Evans 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-6-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 30 ++
 1 file changed, 30 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 98861f9ab3b..ab3bf8558ab 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -51,3 +51,33 @@ abi_long set_sigtramp_args(CPUARMState *regs, int sig,
 
 return 0;
 }
+
+/*
+ * Compare to get_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if mcp points to user memory.
+ */
+abi_long get_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int flags)
+{
+int err = 0, i;
+uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+mcp->mc_gpregs.gp_spsr = pstate_read(regs);
+if (flags & TARGET_MC_GET_CLEAR_RET) {
+gr[0] = 0UL;
+mcp->mc_gpregs.gp_spsr &= ~CPSR_C;
+} else {
+gr[0] = tswap64(regs->xregs[0]);
+}
+
+for (i = 1; i < 30; i++) {
+gr[i] = tswap64(regs->xregs[i]);
+}
+
+mcp->mc_gpregs.gp_sp = tswap64(regs->xregs[TARGET_REG_SP]);
+mcp->mc_gpregs.gp_lr = tswap64(regs->xregs[TARGET_REG_LR]);
+mcp->mc_gpregs.gp_elr = tswap64(regs->pc);
+
+/* XXX FP? */
+
+return err;
+}
-- 
2.45.1




[PULL 07/15] bsd-user:Add set_mcontext function for ARM AArch64

2024-07-24 Thread Warner Losh
From: Stacey Son 

The function copies register values from the provided target_mcontext_t
structure to the CPUARMState registers.
Note:FP is unfinished upstream but will be a separate commit coming soon.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-8-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 43c886e6036..13faac8ce60 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -95,3 +95,25 @@ abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong 
frame_addr,
 return 0;
 }
 
+/*
+ * Compare to set_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long set_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int srflag)
+{
+int err = 0, i;
+const uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+for (i = 0; i < 30; i++) {
+regs->xregs[i] = tswap64(gr[i]);
+}
+
+regs->xregs[TARGET_REG_SP] = tswap64(mcp->mc_gpregs.gp_sp);
+regs->xregs[TARGET_REG_LR] = tswap64(mcp->mc_gpregs.gp_lr);
+regs->pc = mcp->mc_gpregs.gp_elr;
+pstate_write(regs, mcp->mc_gpregs.gp_spsr);
+
+/* XXX FP? */
+
+return err;
+}
-- 
2.45.1




[PULL 06/15] bsd-user:Add setup_sigframe_arch function for ARM AArch64

2024-07-24 Thread Warner Losh
The function utilizes the `get_mcontext` function to retrieve the machine
context for the current CPUARMState

Signed-off-by: Warner Losh 
Signed-off-by: Ajeet Singh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-7-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index ab3bf8558ab..43c886e6036 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -81,3 +81,17 @@ abi_long get_mcontext(CPUARMState *regs, target_mcontext_t 
*mcp, int flags)
 
 return err;
 }
+
+/*
+ * Compare to arm64/arm64/exec_machdep.c sendsig()
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong frame_addr,
+ struct target_sigframe *frame, int flags)
+{
+target_mcontext_t *mcp = &frame->sf_uc.uc_mcontext;
+
+get_mcontext(env, mcp, flags);
+return 0;
+}
+
-- 
2.45.1




[PULL 14/15] bsd-user: Add aarch64 build to tree

2024-07-24 Thread Warner Losh
Add the aarch64 bsd-user fragments needed to build the new aarch64 code.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 configs/targets/aarch64-bsd-user.mak | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 configs/targets/aarch64-bsd-user.mak

diff --git a/configs/targets/aarch64-bsd-user.mak 
b/configs/targets/aarch64-bsd-user.mak
new file mode 100644
index 000..8aaa5d8c802
--- /dev/null
+++ b/configs/targets/aarch64-bsd-user.mak
@@ -0,0 +1,3 @@
+TARGET_ARCH=aarch64
+TARGET_BASE_ARCH=arm
+TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml 
gdb-xml/aarch64-pauth.xml
-- 
2.45.1




[PULL 11/15] bsd-user: Sync fork_start/fork_end with linux-user

2024-07-24 Thread Warner Losh
From: Jessica Clarke 

This reorders some of the calls, deduplicates code between branches and,
most importantly, fixes a double end_exclusive call in the parent that
will cause exclusive_context_count to go negative.

Signed-off-by: Jessica Clarke 
Pull-Request: https://github.com/qemu-bsd-user/qemu-bsd-user/pull/52
Reviewed-by: Warner Losh 
Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 bsd-user/main.c | 26 ++
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 82e94a03160..cc980e6f401 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -35,6 +35,7 @@
 #include "qemu/path.h"
 #include "qemu/help_option.h"
 #include "qemu/module.h"
+#include "qemu/plugin.h"
 #include "exec/exec-all.h"
 #include "user/guest-base.h"
 #include "tcg/startup.h"
@@ -103,8 +104,9 @@ unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount 
to grow stack */
 void fork_start(void)
 {
 start_exclusive();
-cpu_list_lock();
 mmap_fork_start();
+cpu_list_lock();
+qemu_plugin_user_prefork_lock();
 gdbserver_fork_start();
 }
 
@@ -112,31 +114,31 @@ void fork_end(pid_t pid)
 {
 bool child = pid == 0;
 
+qemu_plugin_user_postfork(child);
+mmap_fork_end(child);
 if (child) {
 CPUState *cpu, *next_cpu;
 /*
- * Child processes created by fork() only have a single thread.  
Discard
- * information about the parent threads.
+ * Child processes created by fork() only have a single thread.
+ * Discard information about the parent threads.
  */
 CPU_FOREACH_SAFE(cpu, next_cpu) {
 if (cpu != thread_cpu) {
 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
 }
 }
-mmap_fork_end(child);
-/*
- * qemu_init_cpu_list() takes care of reinitializing the exclusive
- * state, so we don't need to end_exclusive() here.
- */
 qemu_init_cpu_list();
 get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
-gdbserver_fork_end(thread_cpu, pid);
 } else {
-mmap_fork_end(child);
 cpu_list_unlock();
-gdbserver_fork_end(thread_cpu, pid);
-end_exclusive();
 }
+gdbserver_fork_end(thread_cpu, pid);
+/*
+ * qemu_init_cpu_list() reinitialized the child exclusive state, but we
+ * also need to keep current_cpu consistent, so call end_exclusive() for
+ * both child and parent.
+ */
+end_exclusive();
 }
 
 void cpu_loop(CPUArchState *env)
-- 
2.45.1




[PULL 09/15] bsd-user: Simplify the implementation of execve

2024-07-24 Thread Warner Losh
From: Doug Rabson 

This removes the logic which prepends the emulator to each call to
execve and fexecve. This is not necessary with the existing
imgact_binmisc support and it avoids the need to install the emulator
binary into jail environments when using 'binmiscctl --pre-open'.

Signed-off-by: Doug Rabson 
Reviewed-by: Warner Losh 
Signed-off-by: Warner Losh 
Acked-by: Richard Henderson 
---
 bsd-user/freebsd/os-proc.c | 118 +
 bsd-user/main.c|  18 --
 2 files changed, 3 insertions(+), 133 deletions(-)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index e0203e259b0..bf993f1b662 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -26,65 +26,13 @@ struct kinfo_proc;
 
 #include "qemu.h"
 
-/*
- * Get the filename for the given file descriptor.
- * Note that this may return NULL (fail) if no longer cached in the kernel.
- */
-static char *
-get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
-{
-char *ret = NULL;
-unsigned int cnt;
-struct procstat *procstat = NULL;
-struct kinfo_proc *kp = NULL;
-struct filestat_list *head = NULL;
-struct filestat *fst;
-
-procstat = procstat_open_sysctl();
-if (procstat == NULL) {
-goto out;
-}
-
-kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt);
-if (kp == NULL) {
-goto out;
-}
-
-head = procstat_getfiles(procstat, kp, 0);
-if (head == NULL) {
-goto out;
-}
-
-STAILQ_FOREACH(fst, head, next) {
-if (fd == fst->fs_fd) {
-if (fst->fs_path != NULL) {
-(void)strlcpy(filename, fst->fs_path, len);
-ret = filename;
-}
-break;
-}
-}
-
-out:
-if (head != NULL) {
-procstat_freefiles(procstat, head);
-}
-if (kp != NULL) {
-procstat_freeprocs(procstat, kp);
-}
-if (procstat != NULL) {
-procstat_close(procstat);
-}
-return ret;
-}
-
 /*
  * execve/fexecve
  */
 abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
 abi_ulong guest_envp, int do_fexec)
 {
-char **argp, **envp, **qargp, **qarg1, **qarg0, **qargend;
+char **argp, **envp, **qarg0;
 int argc, envc;
 abi_ulong gp;
 abi_ulong addr;
@@ -117,9 +65,7 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong 
guest_argp,
 qarg0 = argp = g_new0(char *, argc + 9);
 /* save the first argument for the emulator */
 *argp++ = (char *)getprogname();
-qargp = argp;
 *argp++ = (char *)getprogname();
-qarg1 = argp;
 envp = g_new0(char *, envc + 1);
 for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
 if (get_user_ual(addr, gp)) {
@@ -137,7 +83,6 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong 
guest_argp,
 total_size += strlen(*q) + 1;
 }
 *q++ = NULL;
-qargend = q;
 
 for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
 if (get_user_ual(addr, gp)) {
@@ -166,71 +111,14 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, 
abi_ulong guest_argp,
 }
 
 if (do_fexec) {
-if (((int)path_or_fd > 0 &&
-is_target_elf_binary((int)path_or_fd)) == 1) {
-char execpath[PATH_MAX];
-
-/*
- * The executable is an elf binary for the target
- * arch.  execve() it using the emulator if we can
- * determine the filename path from the fd.
- */
-if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath,
-sizeof(execpath)) != NULL) {
-memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-qarg1[1] = qarg1[0];
-qarg1[0] = (char *)"-0";
-qarg1 += 2;
-qargend += 2;
-*qarg1 = execpath;
-#ifndef DONT_INHERIT_INTERP_PREFIX
-memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-*qarg1++ = (char *)"-L";
-*qarg1++ = (char *)interp_prefix;
-#endif
-ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
-} else {
-/* Getting the filename path failed. */
-ret = -TARGET_EBADF;
-goto execve_end;
-}
-} else {
-ret = get_errno(fexecve((int)path_or_fd, argp, envp));
-}
+ret = get_errno(fexecve((int)path_or_fd, argp, envp));
 } else {
-int fd;
-
 p = lock_user_string(path_or_fd);
 if (p == NULL) {
 ret = -TARGET_EFAULT;
 goto execve_end;
 }
-
-/*
- * Check the header and see if it a target elf binary.  If so
- * then execute using qemu user mode emulator.
- */
-fd = open(p, O_

Re: [PULL 00/14] Bsd user for 9.1 patches

2024-07-24 Thread Warner Losh
On Wed, Jul 24, 2024 at 2:55 AM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 7/24/24 04:07, Warner Losh wrote:
> > The following changes since commit
> 3cce8bd4d737f2ca688bbdcb92cd5cc683245bbd:
> >
> >Merge tag 'ui-pull-request' ofhttps://
> gitlab.com/marcandre.lureau/qemu into staging (2024-07-23 15:23:05 +1000)
> >
> > are available in the Git repository at:
> >
> >g...@gitlab.com:bsdimp/qemu.git tags/bsd-user-for-9.1-pull-request
> >
> > for you to fetch changes up to afdb6be1bd8528395af65a087bd668bf7a42ab99:
> >
> >bsd-user: Add aarch64 build to tree (2024-07-23 10:56:30 -0600)
> >
> > 
> > bsd-user: Misc changes for 9.1 (I hope)
> >
> > This patch series includes two main sets of patches. To make it simple to
> > review, I've included the changes from my student which the later
> changes depend
> > on. I've included a change from Jessica and Doug as well. I've reviewed
> them,
> > but more eyes never hurt.
> >
> > I've also included a number of 'touch up' patches needed either to get
> the
> > aarch64 building, or to implmement suggestions from prior review cycles.
> The
> > main one is what's charitably described as a kludge: force aarch64 to
> use 4k
> > pages. The qemu-project (and blitz branch) hasn't had the necessary
> changes to
> > bsd-user needed to support variable page size.
> >
> > Sorry this is so late... Live has conspired to delay me.
>
> Something didn't get committed properly, as it doesn't build:
>
> $ ninja
> [343/1144] Compiling C object
> libqemu-aarch64-bsd-user.a.p/bsd-user_aarch64_target_arch_cpu.c.o
> FAILED: libqemu-aarch64-bsd-user.a.p/bsd-user_aarch64_target_arch_cpu.c.o
> cc -m64 -Ilibqemu-aarch64-bsd-user.a.p -I. -I../src -Itarget/arm
> -I../src/target/arm
> -I../src/common-user/host/x86_64 -I../src/bsd-user/include
> -Ibsd-user/freebsd
> -I../src/bsd-user/freebsd -I../src/bsd-user/host/x86_64 -Ibsd-user
> -I../src/bsd-user
> -I../src/bsd-user/aarch64 -Iqapi -Itrace -Iui/shader
> -I/usr/local/include/capstone
> -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include
> -I/usr/local/include
> -fdiagnostics-color=auto -Wall -Winvalid-pch -std=gnu11 -O2 -g
> -fstack-protector-strong
> -Wempty-body -Wendif-labels -Wexpansion-to-defined -Wformat-security
> -Wformat-y2k
> -Wignored-qualifiers -Winit-self -Wmissing-format-attribute
> -Wmissing-prototypes
> -Wnested-externs -Wold-style-definition -Wredundant-decls
> -Wstrict-prototypes
> -Wtype-limits -Wundef -Wvla -Wwrite-strings
> -Wno-gnu-variable-sized-type-not-at-end
> -Wno-initializer-overrides -Wno-missing-include-dirs -Wno-psabi
> -Wno-shift-negative-value
> -Wno-string-plus-int -Wno-tautological-type-limit-compare
> -Wno-typedef-redefinition
> -Wthread-safety -iquote . -iquote /home/rth/qemu/src -iquote
> /home/rth/qemu/src/include
> -iquote /home/rth/qemu/src/host/include/x86_64 -iquote
> /home/rth/qemu/src/host/include/generic -iquote
> /home/rth/qemu/src/tcg/i386 -pthread
> -msse2 -mcx16 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
> -fno-strict-aliasing -fno-common -fwrapv -ftrivial-auto-var-init=zero
> -fzero-call-used-regs=used-gpr -fPIE -DCOMPILING_PER_TARGET
> '-DCONFIG_TARGET="aarch64-bsd-user-config-target.h"'
> '-DCONFIG_DEVICES="aarch64-bsd-user-config-devices.h"' -MD -MQ
> libqemu-aarch64-bsd-user.a.p/bsd-user_aarch64_target_arch_cpu.c.o -MF
> libqemu-aarch64-bsd-user.a.p/bsd-user_aarch64_target_arch_cpu.c.o.d -o
> libqemu-aarch64-bsd-user.a.p/bsd-user_aarch64_target_arch_cpu.c.o -c
> ../src/bsd-user/aarch64/target_arch_cpu.c
> In file included from ../src/bsd-user/aarch64/target_arch_cpu.c:20:
> In file included from ../src/bsd-user/aarch64/target_arch.h:23:
> ../src/bsd-user/qemu.h:38:10: fatal error: 'target.h' file not found
> 38 | #include "target.h"
>|  ^~
> 1 error generated.
>

Doh! A missing git add indeed. That change didn't cherry-pick from blitz
(since
it was something I'd invented for an upstream review that didn't fold back
into
blitz properly and I didn't notice until I started prepping this series and
fixed it
in blitz)... I goofed up doing it by hand. Will send v2 out shortly.  Sorry
for the noise.

Warner


[PULL 09/14] bsd-user: Simplify the implementation of execve

2024-07-23 Thread Warner Losh
From: Doug Rabson 

This removes the logic which prepends the emulator to each call to
execve and fexecve. This is not necessary with the existing
imgact_binmisc support and it avoids the need to install the emulator
binary into jail environments when using 'binmiscctl --pre-open'.

Signed-off-by: Doug Rabson 
Reviewed-by: Warner Losh 
Signed-off-by: Warner Losh 
Acked-by: Richard Henderson 
---
 bsd-user/freebsd/os-proc.c | 118 +
 bsd-user/main.c|  18 --
 2 files changed, 3 insertions(+), 133 deletions(-)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index e0203e259b0..bf993f1b662 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -26,65 +26,13 @@ struct kinfo_proc;
 
 #include "qemu.h"
 
-/*
- * Get the filename for the given file descriptor.
- * Note that this may return NULL (fail) if no longer cached in the kernel.
- */
-static char *
-get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
-{
-char *ret = NULL;
-unsigned int cnt;
-struct procstat *procstat = NULL;
-struct kinfo_proc *kp = NULL;
-struct filestat_list *head = NULL;
-struct filestat *fst;
-
-procstat = procstat_open_sysctl();
-if (procstat == NULL) {
-goto out;
-}
-
-kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt);
-if (kp == NULL) {
-goto out;
-}
-
-head = procstat_getfiles(procstat, kp, 0);
-if (head == NULL) {
-goto out;
-}
-
-STAILQ_FOREACH(fst, head, next) {
-if (fd == fst->fs_fd) {
-if (fst->fs_path != NULL) {
-(void)strlcpy(filename, fst->fs_path, len);
-ret = filename;
-}
-break;
-}
-}
-
-out:
-if (head != NULL) {
-procstat_freefiles(procstat, head);
-}
-if (kp != NULL) {
-procstat_freeprocs(procstat, kp);
-}
-if (procstat != NULL) {
-procstat_close(procstat);
-}
-return ret;
-}
-
 /*
  * execve/fexecve
  */
 abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
 abi_ulong guest_envp, int do_fexec)
 {
-char **argp, **envp, **qargp, **qarg1, **qarg0, **qargend;
+char **argp, **envp, **qarg0;
 int argc, envc;
 abi_ulong gp;
 abi_ulong addr;
@@ -117,9 +65,7 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong 
guest_argp,
 qarg0 = argp = g_new0(char *, argc + 9);
 /* save the first argument for the emulator */
 *argp++ = (char *)getprogname();
-qargp = argp;
 *argp++ = (char *)getprogname();
-qarg1 = argp;
 envp = g_new0(char *, envc + 1);
 for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
 if (get_user_ual(addr, gp)) {
@@ -137,7 +83,6 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong 
guest_argp,
 total_size += strlen(*q) + 1;
 }
 *q++ = NULL;
-qargend = q;
 
 for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
 if (get_user_ual(addr, gp)) {
@@ -166,71 +111,14 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, 
abi_ulong guest_argp,
 }
 
 if (do_fexec) {
-if (((int)path_or_fd > 0 &&
-is_target_elf_binary((int)path_or_fd)) == 1) {
-char execpath[PATH_MAX];
-
-/*
- * The executable is an elf binary for the target
- * arch.  execve() it using the emulator if we can
- * determine the filename path from the fd.
- */
-if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath,
-sizeof(execpath)) != NULL) {
-memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-qarg1[1] = qarg1[0];
-qarg1[0] = (char *)"-0";
-qarg1 += 2;
-qargend += 2;
-*qarg1 = execpath;
-#ifndef DONT_INHERIT_INTERP_PREFIX
-memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-*qarg1++ = (char *)"-L";
-*qarg1++ = (char *)interp_prefix;
-#endif
-ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
-} else {
-/* Getting the filename path failed. */
-ret = -TARGET_EBADF;
-goto execve_end;
-}
-} else {
-ret = get_errno(fexecve((int)path_or_fd, argp, envp));
-}
+ret = get_errno(fexecve((int)path_or_fd, argp, envp));
 } else {
-int fd;
-
 p = lock_user_string(path_or_fd);
 if (p == NULL) {
 ret = -TARGET_EFAULT;
 goto execve_end;
 }
-
-/*
- * Check the header and see if it a target elf binary.  If so
- * then execute using qemu user mode emulator.
- */
-fd = open(p, O_

[PULL 01/14] bsd-user:Add CPU initialization and management functions

2024-07-23 Thread Warner Losh
From: Stacey Son 

Added function to initialize ARM CPU and check if it supports 64-bit mode.
Implemented CPU loop function to handle exceptions and emulate execution of 
instructions.
Added function to clone CPU state to create a new thread.
Included AArch64 specific CPU functions for bsd-user to set and receive 
thread-local-storage
value from the tpidr_el0 register.
Introduced structure for storing CPU register states for BSD-USER.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Kyle Evans 
Co-authored-by: Sean Bruno 
Co-authored-by: Jessica Clarke 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-2-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch_cpu.c |  31 +
 bsd-user/aarch64/target_arch_cpu.h | 192 +
 bsd-user/aarch64/target_syscall.h  |  51 
 3 files changed, 274 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_syscall.h

diff --git a/bsd-user/aarch64/target_arch_cpu.c 
b/bsd-user/aarch64/target_arch_cpu.c
new file mode 100644
index 000..b2fa59efaf6
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_cpu.c
@@ -0,0 +1,31 @@
+/*
+ * ARM AArch64 specific CPU for bsd-user
+ *
+ * Copyright (c) 2015 Stacey Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+#include "target_arch.h"
+
+/* See cpu_set_user_tls() in arm64/arm64/vm_machdep.c */
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls)
+{
+env->cp15.tpidr_el[0] = newtls;
+}
+
+target_ulong target_cpu_get_tls(CPUARMState *env)
+{
+return env->cp15.tpidr_el[0];
+}
diff --git a/bsd-user/aarch64/target_arch_cpu.h 
b/bsd-user/aarch64/target_arch_cpu.h
new file mode 100644
index 000..5c150bb7e9c
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_cpu.h
@@ -0,0 +1,192 @@
+/*
+ *  ARM AArch64 cpu init and loop
+ *
+ * Copyright (c) 2015 Stacey Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_CPU_H
+#define TARGET_ARCH_CPU_H
+
+#include "target_arch.h"
+#include "signal-common.h"
+#include "target/arm/syndrome.h"
+
+#define TARGET_DEFAULT_CPU_MODEL "any"
+
+static inline void target_cpu_init(CPUARMState *env,
+struct target_pt_regs *regs)
+{
+int i;
+
+if (!(arm_feature(env, ARM_FEATURE_AARCH64))) {
+fprintf(stderr, "The selected ARM CPU does not support 64 bit mode\n");
+exit(1);
+}
+for (i = 0; i < 31; i++) {
+env->xregs[i] = regs->regs[i];
+}
+env->pc = regs->pc;
+env->xregs[31] = regs->sp;
+}
+
+
+static inline void target_cpu_loop(CPUARMState *env)
+{
+CPUState *cs = env_cpu(env);
+int trapnr, ec, fsc, si_code, si_signo;
+uint64_t code, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+uint32_t pstate;
+abi_long ret;
+
+for (;;) {
+cpu_exec_start(cs);
+trapnr = cpu_exec(cs);
+cpu_exec_end(cs);
+process_queued_cpu_work(cs);
+
+switch (trapnr) {
+case EXCP_SWI:
+/* See arm64/arm64/trap.c cpu_fetch_syscall_args() */
+code = env->xregs[8];
+if (code == TARGET_FREEBSD_NR_syscall ||
+code == TARGET_FREEBSD_NR___syscall) {
+code = env->xregs[0];
+arg1 = env->xregs[1];
+arg2 = env->xregs[2];
+arg3 = env->xregs[3];
+arg4 = env->xregs[4];
+arg5 = env-&g

[PULL 11/14] bsd-user: Sync fork_start/fork_end with linux-user

2024-07-23 Thread Warner Losh
From: Jessica Clarke 

This reorders some of the calls, deduplicates code between branches and,
most importantly, fixes a double end_exclusive call in the parent that
will cause exclusive_context_count to go negative.

Signed-off-by: Jessica Clarke 
Pull-Request: https://github.com/qemu-bsd-user/qemu-bsd-user/pull/52
Reviewed-by: Warner Losh 
Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 bsd-user/main.c | 26 ++
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 82e94a03160..cc980e6f401 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -35,6 +35,7 @@
 #include "qemu/path.h"
 #include "qemu/help_option.h"
 #include "qemu/module.h"
+#include "qemu/plugin.h"
 #include "exec/exec-all.h"
 #include "user/guest-base.h"
 #include "tcg/startup.h"
@@ -103,8 +104,9 @@ unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount 
to grow stack */
 void fork_start(void)
 {
 start_exclusive();
-cpu_list_lock();
 mmap_fork_start();
+cpu_list_lock();
+qemu_plugin_user_prefork_lock();
 gdbserver_fork_start();
 }
 
@@ -112,31 +114,31 @@ void fork_end(pid_t pid)
 {
 bool child = pid == 0;
 
+qemu_plugin_user_postfork(child);
+mmap_fork_end(child);
 if (child) {
 CPUState *cpu, *next_cpu;
 /*
- * Child processes created by fork() only have a single thread.  
Discard
- * information about the parent threads.
+ * Child processes created by fork() only have a single thread.
+ * Discard information about the parent threads.
  */
 CPU_FOREACH_SAFE(cpu, next_cpu) {
 if (cpu != thread_cpu) {
 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
 }
 }
-mmap_fork_end(child);
-/*
- * qemu_init_cpu_list() takes care of reinitializing the exclusive
- * state, so we don't need to end_exclusive() here.
- */
 qemu_init_cpu_list();
 get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
-gdbserver_fork_end(thread_cpu, pid);
 } else {
-mmap_fork_end(child);
 cpu_list_unlock();
-gdbserver_fork_end(thread_cpu, pid);
-end_exclusive();
 }
+gdbserver_fork_end(thread_cpu, pid);
+/*
+ * qemu_init_cpu_list() reinitialized the child exclusive state, but we
+ * also need to keep current_cpu consistent, so call end_exclusive() for
+ * both child and parent.
+ */
+end_exclusive();
 }
 
 void cpu_loop(CPUArchState *env)
-- 
2.45.1




[PULL 12/14] bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack

2024-07-23 Thread Warner Losh
Most (all?) targets require stacks to be properly aligned. Rather than a
series of ifdefs in bsd-user/signal.h, instead use a manditory #define
for all architectures.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 bsd-user/aarch64/target_arch_signal.h | 2 ++
 bsd-user/arm/target_arch_signal.h | 2 ++
 bsd-user/i386/target_arch_signal.h| 2 ++
 bsd-user/signal.c | 9 +
 bsd-user/x86_64/target_arch_signal.h  | 2 ++
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
index bff752a67ab..b72ba7aa504 100644
--- a/bsd-user/aarch64/target_arch_signal.h
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -77,4 +77,6 @@ struct target_sigframe {
 target_ucontext_t   sf_uc;  /* saved ucontext */
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/arm/target_arch_signal.h 
b/bsd-user/arm/target_arch_signal.h
index 02b2b33e07a..10f96b8bfc9 100644
--- a/bsd-user/arm/target_arch_signal.h
+++ b/bsd-user/arm/target_arch_signal.h
@@ -86,4 +86,6 @@ struct target_sigframe {
 target_mcontext_vfp_t sf_vfp; /* actual saved VFP context */
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/i386/target_arch_signal.h 
b/bsd-user/i386/target_arch_signal.h
index 279dadc22c7..2c14153ab6b 100644
--- a/bsd-user/i386/target_arch_signal.h
+++ b/bsd-user/i386/target_arch_signal.h
@@ -88,4 +88,6 @@ struct target_sigframe {
 uint32_t__spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/signal.c b/bsd-user/signal.c
index 8b6654b91da..da49b9bffc1 100644
--- a/bsd-user/signal.c
+++ b/bsd-user/signal.c
@@ -728,14 +728,7 @@ static inline abi_ulong get_sigframe(struct 
target_sigaction *ka,
 sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
 }
 
-/* TODO: make this a target_arch function / define */
-#if defined(TARGET_ARM)
-return (sp - frame_size) & ~7;
-#elif defined(TARGET_AARCH64)
-return (sp - frame_size) & ~15;
-#else
-return sp - frame_size;
-#endif
+return ROUND_DOWN(sp - frame_size, TARGET_SIGSTACK_ALIGN);
 }
 
 /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
diff --git a/bsd-user/x86_64/target_arch_signal.h 
b/bsd-user/x86_64/target_arch_signal.h
index ca24bf1e7f7..f833ee66cef 100644
--- a/bsd-user/x86_64/target_arch_signal.h
+++ b/bsd-user/x86_64/target_arch_signal.h
@@ -97,4 +97,6 @@ struct target_sigframe {
 uint32_t__spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
-- 
2.45.1




[PULL 07/14] bsd-user:Add set_mcontext function for ARM AArch64

2024-07-23 Thread Warner Losh
From: Stacey Son 

The function copies register values from the provided target_mcontext_t
structure to the CPUARMState registers.
Note:FP is unfinished upstream but will be a separate commit coming soon.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-8-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 43c886e6036..13faac8ce60 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -95,3 +95,25 @@ abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong 
frame_addr,
 return 0;
 }
 
+/*
+ * Compare to set_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long set_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int srflag)
+{
+int err = 0, i;
+const uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+for (i = 0; i < 30; i++) {
+regs->xregs[i] = tswap64(gr[i]);
+}
+
+regs->xregs[TARGET_REG_SP] = tswap64(mcp->mc_gpregs.gp_sp);
+regs->xregs[TARGET_REG_LR] = tswap64(mcp->mc_gpregs.gp_lr);
+regs->pc = mcp->mc_gpregs.gp_elr;
+pstate_write(regs, mcp->mc_gpregs.gp_spsr);
+
+/* XXX FP? */
+
+return err;
+}
-- 
2.45.1




[PULL 02/14] bsd-user:Add AArch64 register handling and related functions

2024-07-23 Thread Warner Losh
From: Stacey Son 

Added header file for managing CPU register states in FreeBSD user mode.
Introduced prototypes for setting and getting thread-local storage (TLS).
Implemented AArch64 sysarch() system call emulation and a printing function.
Added function for setting up thread upcall to add thread support to BSD-USER.
Initialized thread's register state during thread setup.
Updated ARM AArch64 VM parameter definitions for bsd-user, including address 
spaces for FreeBSD/arm64 and
a function for getting the stack pointer from CPU and setting a return value.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Jessica Clarke 
Co-authored-by: Sean Bruno 
Co-authored-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-3-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch.h | 28 +++
 bsd-user/aarch64/target_arch_reg.h | 56 +
 bsd-user/aarch64/target_arch_sysarch.h | 42 
 bsd-user/aarch64/target_arch_thread.h  | 61 +++
 bsd-user/aarch64/target_arch_vmparam.h | 68 ++
 5 files changed, 255 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
 create mode 100644 bsd-user/aarch64/target_arch_thread.h
 create mode 100644 bsd-user/aarch64/target_arch_vmparam.h

diff --git a/bsd-user/aarch64/target_arch.h b/bsd-user/aarch64/target_arch.h
new file mode 100644
index 000..27f47de8eb3
--- /dev/null
+++ b/bsd-user/aarch64/target_arch.h
@@ -0,0 +1,28 @@
+/*
+ * ARM AArch64 specific prototypes for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_H
+#define TARGET_ARCH_H
+
+#include "qemu.h"
+
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
+target_ulong target_cpu_get_tls(CPUARMState *env);
+
+#endif /* TARGET_ARCH_H */
diff --git a/bsd-user/aarch64/target_arch_reg.h 
b/bsd-user/aarch64/target_arch_reg.h
new file mode 100644
index 000..5c7154f0c18
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_reg.h
@@ -0,0 +1,56 @@
+/*
+ *  FreeBSD arm64 register structures
+ *
+ *  Copyright (c) 2015 Stacey Son
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_REG_H
+#define TARGET_ARCH_REG_H
+
+/* See sys/arm64/include/reg.h */
+typedef struct target_reg {
+uint64_tx[30];
+uint64_tlr;
+uint64_tsp;
+uint64_telr;
+uint64_tspsr;
+} target_reg_t;
+
+typedef struct target_fpreg {
+__uint128_t fp_q[32];
+uint32_tfp_sr;
+uint32_tfp_cr;
+} target_fpreg_t;
+
+#define tswapreg(ptr)   tswapal(ptr)
+
+static inline void target_copy_regs(target_reg_t *regs, CPUARMState *env)
+{
+int i;
+
+for (i = 0; i < 30; i++) {
+regs->x[i] = tswapreg(env->xregs[i]);
+}
+regs->lr = tswapreg(env->xregs[30]);
+regs->sp = tswapreg(env->xregs[31]);
+regs->elr = tswapreg(env->pc);
+regs->spsr = tswapreg(pstate_read(env));
+}
+
+#undef tswapreg
+
+#endif /* TARGET_ARCH_REG_H */
diff --git a/bsd-user/aarch64/target_arch_sysarch.h 
b/bsd-user/aarch64/target_arch_sysarch.h
new file mode 100644
index 000..b003015daf4
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_sysarch.h
@@ -0,0 +1,42 @@
+/*
+ * ARM AArch64 sysarch() system call emulation for bsd-user.
+ *
+ * Copyright (c) 2015 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms o

[PULL 10/14] bsd-user: Hard wire aarch64 to be 4k pages only

2024-07-23 Thread Warner Losh
Only support 4k pages for aarch64 binaries. The variable page size stuff
isn't working just yet, so put in this lessor-of-evils kludge until that
is complete.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 target/arm/cpu-param.h | 4 
 1 file changed, 4 insertions(+)

diff --git a/target/arm/cpu-param.h b/target/arm/cpu-param.h
index 2d5f3aa312c..fa6cae0e3aa 100644
--- a/target/arm/cpu-param.h
+++ b/target/arm/cpu-param.h
@@ -21,9 +21,13 @@
 #ifdef CONFIG_USER_ONLY
 # ifdef TARGET_AARCH64
 #  define TARGET_TAGGED_ADDRESSES
+# ifdef __FreeBSD__
+#  define TARGET_PAGE_BITS 12
+# else
 /* Allow user-only to vary page size from 4k */
 #  define TARGET_PAGE_BITS_VARY
 #  define TARGET_PAGE_BITS_MIN  12
+# endif
 # else
 #  define TARGET_PAGE_BITS 12
 # endif
-- 
2.45.1




[PULL 03/14] bsd-user:Add ARM AArch64 support and capabilities

2024-07-23 Thread Warner Losh
Added function to access rval2 by accessing the x1 register.
Defined ARM AArch64 ELF parameters including mmap and dynamic load addresses.
Introduced extensive hardware capability definitions and macros for retrieving 
hardware capability (hwcap) flags.
Implemented function to retrieve ARM AArch64 hardware capabilities using the 
`GET_FEATURE_ID` macro.
Added function to retrieve extended ARM AArch64 hardware capability flags.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Co-authored-by: Kyle Evans 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-4-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch.h |   1 +
 bsd-user/aarch64/target_arch_elf.h | 163 +
 bsd-user/aarch64/target_arch_vmparam.h |   6 +
 3 files changed, 170 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_elf.h

diff --git a/bsd-user/aarch64/target_arch.h b/bsd-user/aarch64/target_arch.h
index 27f47de8eb3..4815a56ae3c 100644
--- a/bsd-user/aarch64/target_arch.h
+++ b/bsd-user/aarch64/target_arch.h
@@ -21,6 +21,7 @@
 #define TARGET_ARCH_H
 
 #include "qemu.h"
+#include "target/arm/cpu-features.h"
 
 void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
 target_ulong target_cpu_get_tls(CPUARMState *env);
diff --git a/bsd-user/aarch64/target_arch_elf.h 
b/bsd-user/aarch64/target_arch_elf.h
new file mode 100644
index 000..cc87f475b3f
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_elf.h
@@ -0,0 +1,163 @@
+/*
+ * ARM AArch64 ELF definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_ELF_H
+#define TARGET_ARCH_ELF_H
+
+#define ELF_START_MMAP 0x8000
+#define ELF_ET_DYN_LOAD_ADDR0x10
+
+#define elf_check_arch(x) ((x) == EM_AARCH64)
+
+#define ELF_CLASS   ELFCLASS64
+#define ELF_DATAELFDATA2LSB
+#define ELF_ARCHEM_AARCH64
+
+#define USE_ELF_CORE_DUMP
+#define ELF_EXEC_PAGESIZE   4096
+
+enum {
+ARM_HWCAP_A64_FP= 1 << 0,
+ARM_HWCAP_A64_ASIMD = 1 << 1,
+ARM_HWCAP_A64_EVTSTRM   = 1 << 2,
+ARM_HWCAP_A64_AES   = 1 << 3,
+ARM_HWCAP_A64_PMULL = 1 << 4,
+ARM_HWCAP_A64_SHA1  = 1 << 5,
+ARM_HWCAP_A64_SHA2  = 1 << 6,
+ARM_HWCAP_A64_CRC32 = 1 << 7,
+ARM_HWCAP_A64_ATOMICS   = 1 << 8,
+ARM_HWCAP_A64_FPHP  = 1 << 9,
+ARM_HWCAP_A64_ASIMDHP   = 1 << 10,
+ARM_HWCAP_A64_CPUID = 1 << 11,
+ARM_HWCAP_A64_ASIMDRDM  = 1 << 12,
+ARM_HWCAP_A64_JSCVT = 1 << 13,
+ARM_HWCAP_A64_FCMA  = 1 << 14,
+ARM_HWCAP_A64_LRCPC = 1 << 15,
+ARM_HWCAP_A64_DCPOP = 1 << 16,
+ARM_HWCAP_A64_SHA3  = 1 << 17,
+ARM_HWCAP_A64_SM3   = 1 << 18,
+ARM_HWCAP_A64_SM4   = 1 << 19,
+ARM_HWCAP_A64_ASIMDDP   = 1 << 20,
+ARM_HWCAP_A64_SHA512= 1 << 21,
+ARM_HWCAP_A64_SVE   = 1 << 22,
+ARM_HWCAP_A64_ASIMDFHM  = 1 << 23,
+ARM_HWCAP_A64_DIT   = 1 << 24,
+ARM_HWCAP_A64_USCAT = 1 << 25,
+ARM_HWCAP_A64_ILRCPC= 1 << 26,
+ARM_HWCAP_A64_FLAGM = 1 << 27,
+ARM_HWCAP_A64_SSBS  = 1 << 28,
+ARM_HWCAP_A64_SB= 1 << 29,
+ARM_HWCAP_A64_PACA  = 1 << 30,
+ARM_HWCAP_A64_PACG  = 1UL << 31,
+
+ARM_HWCAP2_A64_DCPODP   = 1 << 0,
+ARM_HWCAP2_A64_SVE2 = 1 << 1,
+ARM_HWCAP2_A64_SVEAES   = 1 << 2,
+ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
+ARM_HWCAP2_A64_SVEBITPERM   = 1 << 4,
+ARM_HWCAP2_A64_SVESHA3  = 1 << 5,
+ARM_HWCAP2_A64_SVESM4   = 1 << 6,
+ARM_HWCAP2_A64_FLAGM2   = 1 << 7,
+ARM_HWCAP2_A64_FRINT= 1 << 8,
+ARM_HWCAP2_A64_SVEI8MM  = 1 << 9,
+ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
+ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
+ARM_HWCAP2_A64_SVEBF16  = 1 << 12,
+ARM_HWCAP2_A64_I8MM = 1 <

[PULL 05/14] bsd-user:Add get_mcontext function for ARM AArch64

2024-07-23 Thread Warner Losh
From: Stacey Son 

function to retrieve machine context,it populates the provided
target_mcontext_t structure with information from the CPUARMState
registers.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Kyle Evans 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-6-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 30 ++
 1 file changed, 30 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 98861f9ab3b..ab3bf8558ab 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -51,3 +51,33 @@ abi_long set_sigtramp_args(CPUARMState *regs, int sig,
 
 return 0;
 }
+
+/*
+ * Compare to get_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if mcp points to user memory.
+ */
+abi_long get_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int flags)
+{
+int err = 0, i;
+uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+mcp->mc_gpregs.gp_spsr = pstate_read(regs);
+if (flags & TARGET_MC_GET_CLEAR_RET) {
+gr[0] = 0UL;
+mcp->mc_gpregs.gp_spsr &= ~CPSR_C;
+} else {
+gr[0] = tswap64(regs->xregs[0]);
+}
+
+for (i = 1; i < 30; i++) {
+gr[i] = tswap64(regs->xregs[i]);
+}
+
+mcp->mc_gpregs.gp_sp = tswap64(regs->xregs[TARGET_REG_SP]);
+mcp->mc_gpregs.gp_lr = tswap64(regs->xregs[TARGET_REG_LR]);
+mcp->mc_gpregs.gp_elr = tswap64(regs->pc);
+
+/* XXX FP? */
+
+return err;
+}
-- 
2.45.1




[PULL 08/14] bsd-user:Add AArch64 improvements and signal handling functions

2024-07-23 Thread Warner Losh
From: Stacey Son 

Added get_ucontext_sigreturn function to check processor state ensuring current 
execution mode is EL0 and no flags
indicating interrupts or exceptions are set.
Updated AArch64 code to use CF directly without reading/writing the entire 
processor state, improving efficiency.
Changed FP data structures to use Int128 instead of __uint128_t, leveraging 
QEMU's generic mechanism for referencing this type.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-9-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 20 +++-
 bsd-user/aarch64/target_arch_cpu.h|  7 ++-
 bsd-user/aarch64/target_arch_reg.h|  2 +-
 bsd-user/aarch64/target_arch_signal.h |  2 +-
 bsd-user/qemu.h   |  3 +++
 5 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 13faac8ce60..6bc73a798f3 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -21,7 +21,7 @@
 #include "qemu.h"
 
 /*
- * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Compare to sendsig() in sys/arm64/arm64/exec_machdep.c
  * Assumes that target stack frame memory is locked.
  */
 abi_long set_sigtramp_args(CPUARMState *regs, int sig,
@@ -117,3 +117,21 @@ abi_long set_mcontext(CPUARMState *regs, target_mcontext_t 
*mcp, int srflag)
 
 return err;
 }
+
+/* Compare to sys_sigreturn() in  arm64/arm64/machdep.c */
+abi_long get_ucontext_sigreturn(CPUARMState *regs, abi_ulong target_sf,
+abi_ulong *target_uc)
+{
+uint32_t pstate = pstate_read(regs);
+
+*target_uc = 0;
+
+if ((pstate & PSTATE_M) != PSTATE_MODE_EL0t  ||
+(pstate & (PSTATE_F | PSTATE_I | PSTATE_A | PSTATE_D)) != 0) {
+return -TARGET_EINVAL;
+}
+
+*target_uc = target_sf;
+
+return 0;
+}
diff --git a/bsd-user/aarch64/target_arch_cpu.h 
b/bsd-user/aarch64/target_arch_cpu.h
index 5c150bb7e9c..b288e0d069b 100644
--- a/bsd-user/aarch64/target_arch_cpu.h
+++ b/bsd-user/aarch64/target_arch_cpu.h
@@ -48,7 +48,6 @@ static inline void target_cpu_loop(CPUARMState *env)
 CPUState *cs = env_cpu(env);
 int trapnr, ec, fsc, si_code, si_signo;
 uint64_t code, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-uint32_t pstate;
 abi_long ret;
 
 for (;;) {
@@ -88,18 +87,16 @@ static inline void target_cpu_loop(CPUARMState *env)
  * The carry bit is cleared for no error; set for error.
  * See arm64/arm64/vm_machdep.c cpu_set_syscall_retval()
  */
-pstate = pstate_read(env);
 if (ret >= 0) {
-pstate &= ~PSTATE_C;
+env->CF = 0;
 env->xregs[0] = ret;
 } else if (ret == -TARGET_ERESTART) {
 env->pc -= 4;
 break;
 } else if (ret != -TARGET_EJUSTRETURN) {
-pstate |= PSTATE_C;
+env->CF = 1;
 env->xregs[0] = -ret;
 }
-pstate_write(env, pstate);
 break;
 
 case EXCP_INTERRUPT:
diff --git a/bsd-user/aarch64/target_arch_reg.h 
b/bsd-user/aarch64/target_arch_reg.h
index 5c7154f0c18..b53302e7f7a 100644
--- a/bsd-user/aarch64/target_arch_reg.h
+++ b/bsd-user/aarch64/target_arch_reg.h
@@ -31,7 +31,7 @@ typedef struct target_reg {
 } target_reg_t;
 
 typedef struct target_fpreg {
-__uint128_t fp_q[32];
+Int128  fp_q[32];
 uint32_tfp_sr;
 uint32_tfp_cr;
 } target_fpreg_t;
diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
index df171733166..bff752a67ab 100644
--- a/bsd-user/aarch64/target_arch_signal.h
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -49,7 +49,7 @@ struct target_gpregs {
 };
 
 struct target_fpregs {
-__uint128_t fp_q[32];
+Int128  fp_q[32];
 uint32_tfp_sr;
 uint32_tfp_cr;
 uint32_tfp_flags;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 9d2fc7148eb..3736c417860 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -17,6 +17,9 @@
 #ifndef QEMU_H
 #define QEMU_H
 
+#include 
+
+#include "qemu/int128.h"
 #include "cpu.h"
 #include "qemu/units.h"
 #include "exec/cpu_ldst.h"
-- 
2.45.1




[PULL 14/14] bsd-user: Add aarch64 build to tree

2024-07-23 Thread Warner Losh
Add the aarch64 bsd-user fragments needed to build the new aarch64 code.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 configs/targets/aarch64-bsd-user.mak | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 configs/targets/aarch64-bsd-user.mak

diff --git a/configs/targets/aarch64-bsd-user.mak 
b/configs/targets/aarch64-bsd-user.mak
new file mode 100644
index 000..8aaa5d8c802
--- /dev/null
+++ b/configs/targets/aarch64-bsd-user.mak
@@ -0,0 +1,3 @@
+TARGET_ARCH=aarch64
+TARGET_BASE_ARCH=arm
+TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml 
gdb-xml/aarch64-pauth.xml
-- 
2.45.1




[PULL 04/14] bsd-user:Add ARM AArch64 signal handling support

2024-07-23 Thread Warner Losh
From: Stacey Son 

Added sigcode setup function for signal trampoline which initializes a sequence 
of instructions
to handle signal returns and exits, copying this code to the target offset.
Defined ARM AArch64 specific signal definitions including register indices and 
sizes,
and introduced structures to represent general purpose registers, floating 
point registers, and machine context.
Added function to set up signal handler arguments, populating register values 
in `CPUARMState`
based on the provided signal, signal frame, signal action, and frame address.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Co-authored-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-5-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c   | 53 
 bsd-user/aarch64/target_arch_signal.h   | 80 +
 bsd-user/aarch64/target_arch_sigtramp.h | 48 +++
 3 files changed, 181 insertions(+)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
new file mode 100644
index 000..98861f9ab3b
--- /dev/null
+++ b/bsd-user/aarch64/signal.c
@@ -0,0 +1,53 @@
+/*
+ * ARM AArch64 specific signal definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+
+#include "qemu.h"
+
+/*
+ * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Assumes that target stack frame memory is locked.
+ */
+abi_long set_sigtramp_args(CPUARMState *regs, int sig,
+   struct target_sigframe *frame,
+   abi_ulong frame_addr,
+   struct target_sigaction *ka)
+{
+/*
+ * Arguments to signal handler:
+ *  x0 = signal number
+ *  x1 = siginfo pointer
+ *  x2 = ucontext pointer
+ *  pc/elr = signal handler pointer
+ *  sp = sigframe struct pointer
+ *  lr = sigtramp at base of user stack
+ */
+
+regs->xregs[0] = sig;
+regs->xregs[1] = frame_addr +
+offsetof(struct target_sigframe, sf_si);
+regs->xregs[2] = frame_addr +
+offsetof(struct target_sigframe, sf_uc);
+
+regs->pc = ka->_sa_handler;
+regs->xregs[TARGET_REG_SP] = frame_addr;
+regs->xregs[TARGET_REG_LR] = TARGET_PS_STRINGS - TARGET_SZSIGCODE;
+
+return 0;
+}
diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
new file mode 100644
index 000..df171733166
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -0,0 +1,80 @@
+/*
+ * ARM AArch64 specific signal definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_SIGNAL_H
+#define TARGET_ARCH_SIGNAL_H
+
+#include "cpu.h"
+
+#define TARGET_REG_X0   0
+#define TARGET_REG_X30  30
+#define TARGET_REG_X31  31
+#define TARGET_REG_LR   TARGET_REG_X30
+#define TARGET_REG_SP   TARGET_REG_X31
+
+#define TARGET_INSN_SIZE4   /* arm64 instruction size */
+
+/* Size of the signal trampolin code. See _sigtramp(). */
+#define TARGET_SZSIGCODE((abi_ulong)(9 * TARGET_INSN_SIZE))
+
+/* compare to sys/arm64/include/_limits.h */
+#define TARGET_MINSIGSTKSZ  (1024 * 4)  /* min sig stack size 
*/
+#define TARGET_SIGSTKSZ (TARGET_MINSIGSTKSZ + 32768)  /* recommended size 
*/
+
+/* struct __mcontext in sys/arm64/include/ucontext.h */
+
+str

[PULL 13/14] bsd-user: Make compile for non-linux user-mode stuff

2024-07-23 Thread Warner Losh
We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use
them unconditionally. Restrict its use to Linux-only.

"It's ugly, but it's not actually wrong."

Signed-off-by: Warner Losh 
Reviewed-by: Philippe Mathieu-Daudé 
Reviewed-by: Richard Henderson 
---
 target/arm/gdbstub64.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/target/arm/gdbstub64.c b/target/arm/gdbstub64.c
index c8cef8cbc0e..5221381cc85 100644
--- a/target/arm/gdbstub64.c
+++ b/target/arm/gdbstub64.c
@@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray 
*buf, int reg)
 
 int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
 {
+#if defined(CONFIG_LINUX)
 ARMCPU *cpu = ARM_CPU(cs);
 CPUARMState *env = &cpu->env;
 
@@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, 
int reg)
 arm_set_mte_tcf0(env, tcf);
 
 return 1;
+#else
+return 0;
+#endif
 }
 
 static void handle_q_memtag(GArray *params, void *user_ctx)
-- 
2.45.1




[PULL 06/14] bsd-user:Add setup_sigframe_arch function for ARM AArch64

2024-07-23 Thread Warner Losh
The function utilizes the `get_mcontext` function to retrieve the machine
context for the current CPUARMState

Signed-off-by: Warner Losh 
Signed-off-by: Ajeet Singh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-7-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index ab3bf8558ab..43c886e6036 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -81,3 +81,17 @@ abi_long get_mcontext(CPUARMState *regs, target_mcontext_t 
*mcp, int flags)
 
 return err;
 }
+
+/*
+ * Compare to arm64/arm64/exec_machdep.c sendsig()
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong frame_addr,
+ struct target_sigframe *frame, int flags)
+{
+target_mcontext_t *mcp = &frame->sf_uc.uc_mcontext;
+
+get_mcontext(env, mcp, flags);
+return 0;
+}
+
-- 
2.45.1




[PULL 00/14] Bsd user for 9.1 patches

2024-07-23 Thread Warner Losh
The following changes since commit 3cce8bd4d737f2ca688bbdcb92cd5cc683245bbd:

  Merge tag 'ui-pull-request' of https://gitlab.com/marcandre.lureau/qemu into 
staging (2024-07-23 15:23:05 +1000)

are available in the Git repository at:

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

for you to fetch changes up to afdb6be1bd8528395af65a087bd668bf7a42ab99:

  bsd-user: Add aarch64 build to tree (2024-07-23 10:56:30 -0600)


bsd-user: Misc changes for 9.1 (I hope)

This patch series includes two main sets of patches. To make it simple to
review, I've included the changes from my student which the later changes depend
on. I've included a change from Jessica and Doug as well. I've reviewed them,
but more eyes never hurt.

I've also included a number of 'touch up' patches needed either to get the
aarch64 building, or to implmement suggestions from prior review cycles. The
main one is what's charitably described as a kludge: force aarch64 to use 4k
pages. The qemu-project (and blitz branch) hasn't had the necessary changes to
bsd-user needed to support variable page size.

Sorry this is so late... Live has conspired to delay me.
-BEGIN PGP SIGNATURE-
Comment: GPGTools - https://gpgtools.org

iQIzBAABCgAdFiEEIDX4lLAKo898zeG3bBzRKH2wEQAFAmaf8RUACgkQbBzRKH2w
EQAnYA/9F6NbHwV8C6c5zxiKR2PIXvER21khu+c3wwCMgm+Sy4yEIJxtX+L0St1I
7L4MkgSByeOJmAmw1h0xbWY4IGmTC0ylnyPLq4WIKHP2XnGgs2zYx/CWpEs20QWH
bORg7KXpmTsK3Ag0ilQHTH7HspW3xhZGia4C0uaxYU+E3nns04Mp/TFzbLxq1VfP
xJ8DKjVS39StoBjaWlkKN4TnmD1eXPl4WC+9aElVZJX5DicJHN2Cz3sjIZppJOFg
AXBdeKUXBh5o2oxxRxIv5P/wUROl5NuOpEzE80Ed3B4okbzXdIbsYA07t+UlcoXk
2YMEWfzPmH8nveD5w8T3YtVAlzEJMjWgVeFZg43lT+aHktX3ixstQUkumzAm3A1Z
1TsXtP4D4ZRgeH991V+ZbA/2D0mxg03Z5kldw+jcoYUkjQNOMEy/i0ewNPhX4V/d
l+YIeoxXEu06/v+ibjzL3/WMEvSofYw9nK3/BNGE9rfsNh8tKTFh8Ro+i6wQgKBB
XOgQeuv+X1nwQ1xeZvl8uunFxN5yJqTyw2O6JN6z+3xnPMnBGz8yvkipdQPv5yRg
GJEau8H0C9xesaCtURwNX0AC9BrZpoZ8/5zuLE9MGyyXuoN6jBkt/k6z+e4EL1j5
x7Ezp+srEhlG+g+yozgeDIk3W0YdCPDwBNKDvzwSuNS5prLMrg8=
=Bdnq
-END PGP SIGNATURE-



Doug Rabson (1):
  bsd-user: Simplify the implementation of execve

Jessica Clarke (1):
  bsd-user: Sync fork_start/fork_end with linux-user

Stacey Son (6):
  bsd-user:Add CPU initialization and management functions
  bsd-user:Add AArch64 register handling and related functions
  bsd-user:Add ARM AArch64 signal handling support
  bsd-user:Add get_mcontext function for ARM AArch64
  bsd-user:Add set_mcontext function for ARM AArch64
  bsd-user:Add AArch64 improvements and signal handling functions

Warner Losh (6):
  bsd-user:Add ARM AArch64 support and capabilities
  bsd-user:Add setup_sigframe_arch function for ARM AArch64
  bsd-user: Hard wire aarch64 to be 4k pages only
  bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack
  bsd-user: Make compile for non-linux user-mode stuff
  bsd-user: Add aarch64 build to tree

 bsd-user/aarch64/signal.c   | 137 +
 bsd-user/aarch64/target_arch.h  |  29 
 bsd-user/aarch64/target_arch_cpu.c  |  31 
 bsd-user/aarch64/target_arch_cpu.h  | 189 
 bsd-user/aarch64/target_arch_elf.h  | 163 
 bsd-user/aarch64/target_arch_reg.h  |  56 +++
 bsd-user/aarch64/target_arch_signal.h   |  82 ++
 bsd-user/aarch64/target_arch_sigtramp.h |  48 ++
 bsd-user/aarch64/target_arch_sysarch.h  |  42 ++
 bsd-user/aarch64/target_arch_thread.h   |  61 
 bsd-user/aarch64/target_arch_vmparam.h  |  74 ++
 bsd-user/aarch64/target_syscall.h   |  51 +++
 bsd-user/arm/target_arch_signal.h   |   2 +
 bsd-user/freebsd/os-proc.c  | 118 +--
 bsd-user/i386/target_arch_signal.h  |   2 +
 bsd-user/main.c |  44 ++
 bsd-user/qemu.h |   3 +
 bsd-user/signal.c   |   9 +-
 bsd-user/x86_64/target_arch_signal.h|   2 +
 configs/targets/aarch64-bsd-user.mak|   3 +
 target/arm/cpu-param.h  |   4 +
 target/arm/gdbstub64.c  |   4 +
 22 files changed, 1001 insertions(+), 153 deletions(-)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_arch_elf.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h
 create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
 create mode 100644 bsd-user/aarch64/target_arch_thread.h
 create mode 100644 bsd-user/aarch64/target_arch_vmparam.h
 create mode 100644 bsd-user/aarch64/target_syscall.h
 creat

Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

2024-07-23 Thread Warner Losh
On Tue, Jul 23, 2024 at 12:38 AM Philippe Mathieu-Daudé 
wrote:

> On 23/7/24 03:17, Richard Henderson wrote:
> > On 7/23/24 08:11, Philippe Mathieu-Daudé wrote:
> >> On 23/7/24 00:06, Warner Losh wrote:
> >>>
> >>>
> >>> On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé
> >>> mailto:phi...@linaro.org>> wrote:
> >>>
> >>> Hi Warner,
> >>>
> >>> On 22/7/24 23:43, Warner Losh wrote:
> >>>  > Add the aarch64 bsd-user fragments needed to build the new
> >>> aarch64 code.
> >>>  >
> >>>  > Signed-off-by: Warner Losh  >>> <mailto:i...@bsdimp.com>>
> >>>  > ---
> >>>  >   configs/targets/aarch64-bsd-user.mak | 3 +++
> >>>  >   1 file changed, 3 insertions(+)
> >>>  >   create mode 100644 configs/targets/aarch64-bsd-user.mak
> >>>
> >>> Can we build aarch64 on Cirrus-CI? (not clear on
> >>> https://cirrus-ci.org/guide/FreeBSD/
> >>> <https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add
> >>> a follow-up patch to build that on our CI, patching
> >>> .gitlab-ci.d/cirrus.yml?
> >>>
> >>>
> >>> We can build aarch64 host for bsd-user for sure. I'll see if we can
> >>> do it in cirrus CI.
> >>> If so, I'll try to add it to cirrus.yml.
> >>>
> >>> This patch series adds aarch64 guest...
> >>
> >> Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD
> >> aarch64 bsd-user guest and test it. Am I wrong?
> >>
> >
> > This is adding guest support, so your suggestion is orthogonal.
>
> Ah, got it I guess, this series adds support for aarch64 user-mode
> emulation on any FreeBSD kernels, so IIUC we can test it on the current
> FreeBSD x86 job we have. Currently the x64-freebsd-13-build job uses:
>
>CONFIGURE_ARGS:
>
> --target-list-exclude=arm-softmmu,i386-softmmu,microblaze-softmmu,mips64el-softmmu,mipsel-softmmu,mips-softmmu,ppc-softmmu,sh4eb-softmmu,xtensa-softmmu
>TEST_TARGETS: check
>
> So the aarch64-bsd-user target should be selected automatically,
> is that correct?
>

Yes. It's automatically added to the build just by this file existing.

So it should at least be compile tested by everything.

Warner


> (got it, my request to test aarch64 FreeBSD *host* is orthogonal).
>


Re: [PATCH 13/14] bsd-user: Make compile for non-linux user-mode stuff

2024-07-22 Thread Warner Losh
On Mon, Jul 22, 2024 at 4:01 PM Philippe Mathieu-Daudé 
wrote:

> Hi Warner,
>
> On 22/7/24 23:43, Warner Losh wrote:
> > We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use
> > them unconditionally. Restrict its use to Linux-only.
>
> We should check that in meson, i.e.:
>
> config_host_data.set('CONFIG_PRCTL_PR_MTE_TCF_SHIFT',
>   cc.has_header_symbol('sys/prctl.h',
> 'PR_MTE_TCF_SHIFT'))
>
> (like we do for CONFIG_PRCTL_PR_SET_TIMERSLACK), then rework
> linux-user/aarch64/mte_user_helper.h (and possibly
> tests/tcg/aarch64/mte.h), moving in a common directory.
>

Hmmm... I'll have to check, but since I didn't add the incompatibility, it
will likely be on my backlog for a while...


> That said, your patch matches this file header:
>
>#if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
>#include 
>#include "mte_user_helper.h"
>#endif
>
> Even if this #ifdef'ry is ugly, it isn't wrong, so:
> Reviewed-by: Philippe Mathieu-Daudé 
>
> ¯\_(ツ)_/¯
>

Thanks!

Warner


> > Signed-off-by: Warner Losh 
> > ---
> >   target/arm/gdbstub64.c | 4 
> >   1 file changed, 4 insertions(+)
> >
> > diff --git a/target/arm/gdbstub64.c b/target/arm/gdbstub64.c
> > index 2e2bc2700b8..6dc81aecb2a 100644
> > --- a/target/arm/gdbstub64.c
> > +++ b/target/arm/gdbstub64.c
> > @@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs,
> GByteArray *buf, int reg)
> >
> >   int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
> >   {
> > +#if defined(CONFIG_LINUX)
> >   ARMCPU *cpu = ARM_CPU(cs);
> >   CPUARMState *env = &cpu->env;
> >
> > @@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs,
> uint8_t *buf, int reg)
> >   arm_set_mte_tcf0(env, tcf);
> >
> >   return 1;
> > +#else
> > +return 0;
> > +#endif
> >   }
> >
> >   static void handle_q_memtag(GArray *params, void *user_ctx)
>
>


Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

2024-07-22 Thread Warner Losh
Oh, also, can I get a reviewed-by?

Warner

On Mon, Jul 22, 2024 at 11:08 PM Warner Losh  wrote:

>
>
> On Mon, Jul 22, 2024 at 7:17 PM Richard Henderson <
> richard.hender...@linaro.org> wrote:
>
>> On 7/23/24 08:11, Philippe Mathieu-Daudé wrote:
>> > On 23/7/24 00:06, Warner Losh wrote:
>> >>
>> >>
>> >> On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <
>> phi...@linaro.org
>> >> <mailto:phi...@linaro.org>> wrote:
>> >>
>> >> Hi Warner,
>> >>
>> >> On 22/7/24 23:43, Warner Losh wrote:
>> >>  > Add the aarch64 bsd-user fragments needed to build the new
>> >> aarch64 code.
>> >>  >
>> >>  > Signed-off-by: Warner Losh > i...@bsdimp.com>>
>> >>  > ---
>> >>  >   configs/targets/aarch64-bsd-user.mak | 3 +++
>> >>  >   1 file changed, 3 insertions(+)
>> >>  >   create mode 100644 configs/targets/aarch64-bsd-user.mak
>> >>
>> >> Can we build aarch64 on Cirrus-CI? (not clear on
>> >> https://cirrus-ci.org/guide/FreeBSD/
>> >> <https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add
>> >> a follow-up patch to build that on our CI, patching
>> >> .gitlab-ci.d/cirrus.yml?
>> >>
>> >>
>> >> We can build aarch64 host for bsd-user for sure. I'll see if we can do
>> it in cirrus CI.
>> >> If so, I'll try to add it to cirrus.yml.
>> >>
>> >> This patch series adds aarch64 guest...
>> >
>> > Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD
>> > aarch64 bsd-user guest and test it. Am I wrong?
>> >
>>
>> This is adding guest support, so your suggestion is orthogonal.
>>
>
> Yea...  It's a good suggestion, but not what I'm working on right now...
>
> Warner
>


Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

2024-07-22 Thread Warner Losh
On Mon, Jul 22, 2024 at 7:17 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 7/23/24 08:11, Philippe Mathieu-Daudé wrote:
> > On 23/7/24 00:06, Warner Losh wrote:
> >>
> >>
> >> On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé <
> phi...@linaro.org
> >> <mailto:phi...@linaro.org>> wrote:
> >>
> >> Hi Warner,
> >>
> >> On 22/7/24 23:43, Warner Losh wrote:
> >>  > Add the aarch64 bsd-user fragments needed to build the new
> >> aarch64 code.
> >>  >
> >>  > Signed-off-by: Warner Losh  i...@bsdimp.com>>
> >>  > ---
> >>  >   configs/targets/aarch64-bsd-user.mak | 3 +++
> >>  >   1 file changed, 3 insertions(+)
> >>  >   create mode 100644 configs/targets/aarch64-bsd-user.mak
> >>
> >> Can we build aarch64 on Cirrus-CI? (not clear on
> >> https://cirrus-ci.org/guide/FreeBSD/
> >> <https://cirrus-ci.org/guide/FreeBSD/>). If so, could you add
> >> a follow-up patch to build that on our CI, patching
> >> .gitlab-ci.d/cirrus.yml?
> >>
> >>
> >> We can build aarch64 host for bsd-user for sure. I'll see if we can do
> it in cirrus CI.
> >> If so, I'll try to add it to cirrus.yml.
> >>
> >> This patch series adds aarch64 guest...
> >
> > Yes, we want to use a aarch64 FreeBSD host to build your FreeBSD
> > aarch64 bsd-user guest and test it. Am I wrong?
> >
>
> This is adding guest support, so your suggestion is orthogonal.
>

Yea...  It's a good suggestion, but not what I'm working on right now...

Warner


Re: [PATCH 10/14] bsd-user: Hard wire aarch64 to be 4k pages only

2024-07-22 Thread Warner Losh
On Mon, Jul 22, 2024 at 7:10 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 7/23/24 07:43, Warner Losh wrote:
> > Only support 4k pages for aarch64 binaries. The variable page size stuff
> > isn't working just yet, so put in this lessor-of-evils kludge until that
> > is complete.
>
> Hmm.  In what way is it not working?
>
> What if we limit this to bsd-user/main.c instead?
>
>set_preferred_target_page_bits(TARGET_PAGE_BITS_MIN);
>

It's a compile issue that this won't solve:
In file included from ../bsd-user/aarch64/signal.c:21:
../bsd-user/qemu.h:146:20: warning: variable length array used [-Wvla]
  146 | void *page[MAX_ARG_PAGES];
  |^
../bsd-user/qemu.h:138:23: note: expanded from macro 'MAX_ARG_PAGES'
  138 | #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
  |   ^~~
../bsd-user/qemu.h:146:20: note: initializer of 'target_page' is unknown
../bsd-user/qemu.h:138:41: note: expanded from macro 'MAX_ARG_PAGES'
  138 | #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
  | ^
/dune/imp/git/qemu/include/exec/cpu-all.h:152:37: note: expanded from macro
'TARGET_PAGE_SIZE'
  152 | # define TARGET_PAGE_SIZE(-(int)TARGET_PAGE_MASK)
  | ^
/dune/imp/git/qemu/include/exec/cpu-all.h:150:44: note: expanded from macro
'TARGET_PAGE_MASK'
  150 | #  define TARGET_PAGE_MASK   ((target_long)target_page.mask)
  |^
/dune/imp/git/qemu/include/exec/cpu-all.h:142:29: note: declared here
  142 | extern const TargetPageBits target_page;
  | ^
In file included from ../bsd-user/aarch64/signal.c:21:
../bsd-user/qemu.h:146:15: error: fields must have a constant size:
'variable length array in structure' extension will never be supported
  146 | void *page[MAX_ARG_PAGES];
  |   ^
1 warning and 1 error generated.
ninja: build stopped: subcommand failed.
gmake: *** [Makefile:167: run-ninja] Error 1

that have a couple of additional levels after those issues are fixed...
It's fixable, but will take a fair amount more time that I have before the
deadline...

Warner


Re: [PATCH 14/14] bsd-user: Add aarch64 build to tree

2024-07-22 Thread Warner Losh
On Mon, Jul 22, 2024 at 3:54 PM Philippe Mathieu-Daudé 
wrote:

> Hi Warner,
>
> On 22/7/24 23:43, Warner Losh wrote:
> > Add the aarch64 bsd-user fragments needed to build the new aarch64 code.
> >
> > Signed-off-by: Warner Losh 
> > ---
> >   configs/targets/aarch64-bsd-user.mak | 3 +++
> >   1 file changed, 3 insertions(+)
> >   create mode 100644 configs/targets/aarch64-bsd-user.mak
>
> Can we build aarch64 on Cirrus-CI? (not clear on
> https://cirrus-ci.org/guide/FreeBSD/). If so, could you add
> a follow-up patch to build that on our CI, patching
> .gitlab-ci.d/cirrus.yml?
>

We can build aarch64 host for bsd-user for sure. I'll see if we can do it
in cirrus CI.
If so, I'll try to add it to cirrus.yml.

This patch series adds aarch64 guest...

Warner


[PATCH 13/14] bsd-user: Make compile for non-linux user-mode stuff

2024-07-22 Thread Warner Losh
We include the files that define PR_MTE_TCF_SHIFT only on Linux, but use
them unconditionally. Restrict its use to Linux-only.

Signed-off-by: Warner Losh 
---
 target/arm/gdbstub64.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/target/arm/gdbstub64.c b/target/arm/gdbstub64.c
index 2e2bc2700b8..6dc81aecb2a 100644
--- a/target/arm/gdbstub64.c
+++ b/target/arm/gdbstub64.c
@@ -404,6 +404,7 @@ int aarch64_gdb_get_tag_ctl_reg(CPUState *cs, GByteArray 
*buf, int reg)
 
 int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, int reg)
 {
+#if defined(CONFIG_LINUX)
 ARMCPU *cpu = ARM_CPU(cs);
 CPUARMState *env = &cpu->env;
 
@@ -425,6 +426,9 @@ int aarch64_gdb_set_tag_ctl_reg(CPUState *cs, uint8_t *buf, 
int reg)
 arm_set_mte_tcf0(env, tcf);
 
 return 1;
+#else
+return 0;
+#endif
 }
 
 static void handle_q_memtag(GArray *params, void *user_ctx)
-- 
2.45.1




[PATCH 11/14] bsd-user: Sync fork_start/fork_end with linux-user

2024-07-22 Thread Warner Losh
From: Jessica Clarke 

This reorders some of the calls, deduplicates code between branches and,
most importantly, fixes a double end_exclusive call in the parent that
will cause exclusive_context_count to go negative.

Signed-off-by: Jessica Clarke 
Pull-Request: https://github.com/qemu-bsd-user/qemu-bsd-user/pull/52
Reviewed-by: Warner Losh 
Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 28 ++--
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 82e94a03160..6d81452e51a 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -35,6 +35,7 @@
 #include "qemu/path.h"
 #include "qemu/help_option.h"
 #include "qemu/module.h"
+#include "qemu/plugin.h"
 #include "exec/exec-all.h"
 #include "user/guest-base.h"
 #include "tcg/startup.h"
@@ -103,8 +104,9 @@ unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount 
to grow stack */
 void fork_start(void)
 {
 start_exclusive();
-cpu_list_lock();
 mmap_fork_start();
+cpu_list_lock();
+qemu_plugin_user_prefork_lock();
 gdbserver_fork_start();
 }
 
@@ -112,31 +114,29 @@ void fork_end(pid_t pid)
 {
 bool child = pid == 0;
 
+qemu_plugin_user_postfork(child);
+mmap_fork_end(child);
 if (child) {
 CPUState *cpu, *next_cpu;
-/*
- * Child processes created by fork() only have a single thread.  
Discard
- * information about the parent threads.
- */
+/* Child processes created by fork() only have a single thread.
+   Discard information about the parent threads.  */
 CPU_FOREACH_SAFE(cpu, next_cpu) {
 if (cpu != thread_cpu) {
 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
 }
 }
-mmap_fork_end(child);
-/*
- * qemu_init_cpu_list() takes care of reinitializing the exclusive
- * state, so we don't need to end_exclusive() here.
- */
 qemu_init_cpu_list();
 get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
-gdbserver_fork_end(thread_cpu, pid);
 } else {
-mmap_fork_end(child);
 cpu_list_unlock();
-gdbserver_fork_end(thread_cpu, pid);
-end_exclusive();
 }
+gdbserver_fork_end(thread_cpu, pid);
+/*
+ * qemu_init_cpu_list() reinitialized the child exclusive state, but we
+ * also need to keep current_cpu consistent, so call end_exclusive() for
+ * both child and parent.
+ */
+end_exclusive();
 }
 
 void cpu_loop(CPUArchState *env)
-- 
2.45.1




[PATCH 09/14] bsd-user: Simplify the implementation of execve

2024-07-22 Thread Warner Losh
From: Doug Rabson 

This removes the logic which prepends the emulator to each call to
execve and fexecve. This is not necessary with the existing
imgact_binmisc support and it avoids the need to install the emulator
binary into jail environments when using 'binmiscctl --pre-open'.

Signed-off-by: Doug Rabson 
Reviewed-by: Warner Losh 
Signed-off-by: Warner Losh 
---
 bsd-user/freebsd/os-proc.c | 118 +
 bsd-user/main.c|  18 --
 2 files changed, 3 insertions(+), 133 deletions(-)

diff --git a/bsd-user/freebsd/os-proc.c b/bsd-user/freebsd/os-proc.c
index e0203e259b0..bf993f1b662 100644
--- a/bsd-user/freebsd/os-proc.c
+++ b/bsd-user/freebsd/os-proc.c
@@ -26,65 +26,13 @@ struct kinfo_proc;
 
 #include "qemu.h"
 
-/*
- * Get the filename for the given file descriptor.
- * Note that this may return NULL (fail) if no longer cached in the kernel.
- */
-static char *
-get_filename_from_fd(pid_t pid, int fd, char *filename, size_t len)
-{
-char *ret = NULL;
-unsigned int cnt;
-struct procstat *procstat = NULL;
-struct kinfo_proc *kp = NULL;
-struct filestat_list *head = NULL;
-struct filestat *fst;
-
-procstat = procstat_open_sysctl();
-if (procstat == NULL) {
-goto out;
-}
-
-kp = procstat_getprocs(procstat, KERN_PROC_PID, pid, &cnt);
-if (kp == NULL) {
-goto out;
-}
-
-head = procstat_getfiles(procstat, kp, 0);
-if (head == NULL) {
-goto out;
-}
-
-STAILQ_FOREACH(fst, head, next) {
-if (fd == fst->fs_fd) {
-if (fst->fs_path != NULL) {
-(void)strlcpy(filename, fst->fs_path, len);
-ret = filename;
-}
-break;
-}
-}
-
-out:
-if (head != NULL) {
-procstat_freefiles(procstat, head);
-}
-if (kp != NULL) {
-procstat_freeprocs(procstat, kp);
-}
-if (procstat != NULL) {
-procstat_close(procstat);
-}
-return ret;
-}
-
 /*
  * execve/fexecve
  */
 abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
 abi_ulong guest_envp, int do_fexec)
 {
-char **argp, **envp, **qargp, **qarg1, **qarg0, **qargend;
+char **argp, **envp, **qarg0;
 int argc, envc;
 abi_ulong gp;
 abi_ulong addr;
@@ -117,9 +65,7 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong 
guest_argp,
 qarg0 = argp = g_new0(char *, argc + 9);
 /* save the first argument for the emulator */
 *argp++ = (char *)getprogname();
-qargp = argp;
 *argp++ = (char *)getprogname();
-qarg1 = argp;
 envp = g_new0(char *, envc + 1);
 for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
 if (get_user_ual(addr, gp)) {
@@ -137,7 +83,6 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong 
guest_argp,
 total_size += strlen(*q) + 1;
 }
 *q++ = NULL;
-qargend = q;
 
 for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
 if (get_user_ual(addr, gp)) {
@@ -166,71 +111,14 @@ abi_long freebsd_exec_common(abi_ulong path_or_fd, 
abi_ulong guest_argp,
 }
 
 if (do_fexec) {
-if (((int)path_or_fd > 0 &&
-is_target_elf_binary((int)path_or_fd)) == 1) {
-char execpath[PATH_MAX];
-
-/*
- * The executable is an elf binary for the target
- * arch.  execve() it using the emulator if we can
- * determine the filename path from the fd.
- */
-if (get_filename_from_fd(getpid(), (int)path_or_fd, execpath,
-sizeof(execpath)) != NULL) {
-memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-qarg1[1] = qarg1[0];
-qarg1[0] = (char *)"-0";
-qarg1 += 2;
-qargend += 2;
-*qarg1 = execpath;
-#ifndef DONT_INHERIT_INTERP_PREFIX
-memmove(qarg1 + 2, qarg1, (qargend - qarg1) * sizeof(*qarg1));
-*qarg1++ = (char *)"-L";
-*qarg1++ = (char *)interp_prefix;
-#endif
-ret = get_errno(execve(qemu_proc_pathname, qargp, envp));
-} else {
-/* Getting the filename path failed. */
-ret = -TARGET_EBADF;
-goto execve_end;
-}
-} else {
-ret = get_errno(fexecve((int)path_or_fd, argp, envp));
-}
+ret = get_errno(fexecve((int)path_or_fd, argp, envp));
 } else {
-int fd;
-
 p = lock_user_string(path_or_fd);
 if (p == NULL) {
 ret = -TARGET_EFAULT;
 goto execve_end;
 }
-
-/*
- * Check the header and see if it a target elf binary.  If so
- * then execute using qemu user mode emulator.
- */
-fd = open(p, O_RDONLY | 

[PATCH 05/14] bsd-user:Add get_mcontext function for ARM AArch64

2024-07-22 Thread Warner Losh
From: Stacey Son 

function to retrieve machine context,it populates the provided
target_mcontext_t structure with information from the CPUARMState
registers.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Kyle Evans 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-6-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 30 ++
 1 file changed, 30 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 98861f9ab3b..ab3bf8558ab 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -51,3 +51,33 @@ abi_long set_sigtramp_args(CPUARMState *regs, int sig,
 
 return 0;
 }
+
+/*
+ * Compare to get_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if mcp points to user memory.
+ */
+abi_long get_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int flags)
+{
+int err = 0, i;
+uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+mcp->mc_gpregs.gp_spsr = pstate_read(regs);
+if (flags & TARGET_MC_GET_CLEAR_RET) {
+gr[0] = 0UL;
+mcp->mc_gpregs.gp_spsr &= ~CPSR_C;
+} else {
+gr[0] = tswap64(regs->xregs[0]);
+}
+
+for (i = 1; i < 30; i++) {
+gr[i] = tswap64(regs->xregs[i]);
+}
+
+mcp->mc_gpregs.gp_sp = tswap64(regs->xregs[TARGET_REG_SP]);
+mcp->mc_gpregs.gp_lr = tswap64(regs->xregs[TARGET_REG_LR]);
+mcp->mc_gpregs.gp_elr = tswap64(regs->pc);
+
+/* XXX FP? */
+
+return err;
+}
-- 
2.45.1




[PATCH 14/14] bsd-user: Add aarch64 build to tree

2024-07-22 Thread Warner Losh
Add the aarch64 bsd-user fragments needed to build the new aarch64 code.

Signed-off-by: Warner Losh 
---
 configs/targets/aarch64-bsd-user.mak | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 configs/targets/aarch64-bsd-user.mak

diff --git a/configs/targets/aarch64-bsd-user.mak 
b/configs/targets/aarch64-bsd-user.mak
new file mode 100644
index 000..8aaa5d8c802
--- /dev/null
+++ b/configs/targets/aarch64-bsd-user.mak
@@ -0,0 +1,3 @@
+TARGET_ARCH=aarch64
+TARGET_BASE_ARCH=arm
+TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml 
gdb-xml/aarch64-pauth.xml
-- 
2.45.1




[PATCH 10/14] bsd-user: Hard wire aarch64 to be 4k pages only

2024-07-22 Thread Warner Losh
Only support 4k pages for aarch64 binaries. The variable page size stuff
isn't working just yet, so put in this lessor-of-evils kludge until that
is complete.

Signed-off-by: Warner Losh 
---
 target/arm/cpu-param.h | 4 
 1 file changed, 4 insertions(+)

diff --git a/target/arm/cpu-param.h b/target/arm/cpu-param.h
index 2d5f3aa312c..fa6cae0e3aa 100644
--- a/target/arm/cpu-param.h
+++ b/target/arm/cpu-param.h
@@ -21,9 +21,13 @@
 #ifdef CONFIG_USER_ONLY
 # ifdef TARGET_AARCH64
 #  define TARGET_TAGGED_ADDRESSES
+# ifdef __FreeBSD__
+#  define TARGET_PAGE_BITS 12
+# else
 /* Allow user-only to vary page size from 4k */
 #  define TARGET_PAGE_BITS_VARY
 #  define TARGET_PAGE_BITS_MIN  12
+# endif
 # else
 #  define TARGET_PAGE_BITS 12
 # endif
-- 
2.45.1




[PATCH 02/14] bsd-user:Add AArch64 register handling and related functions

2024-07-22 Thread Warner Losh
From: Stacey Son 

Added header file for managing CPU register states in FreeBSD user mode.
Introduced prototypes for setting and getting thread-local storage (TLS).
Implemented AArch64 sysarch() system call emulation and a printing function.
Added function for setting up thread upcall to add thread support to BSD-USER.
Initialized thread's register state during thread setup.
Updated ARM AArch64 VM parameter definitions for bsd-user, including address 
spaces for FreeBSD/arm64 and
a function for getting the stack pointer from CPU and setting a return value.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Jessica Clarke 
Co-authored-by: Sean Bruno 
Co-authored-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-3-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch.h | 28 +++
 bsd-user/aarch64/target_arch_reg.h | 56 +
 bsd-user/aarch64/target_arch_sysarch.h | 42 
 bsd-user/aarch64/target_arch_thread.h  | 61 +++
 bsd-user/aarch64/target_arch_vmparam.h | 68 ++
 5 files changed, 255 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
 create mode 100644 bsd-user/aarch64/target_arch_thread.h
 create mode 100644 bsd-user/aarch64/target_arch_vmparam.h

diff --git a/bsd-user/aarch64/target_arch.h b/bsd-user/aarch64/target_arch.h
new file mode 100644
index 000..27f47de8eb3
--- /dev/null
+++ b/bsd-user/aarch64/target_arch.h
@@ -0,0 +1,28 @@
+/*
+ * ARM AArch64 specific prototypes for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_H
+#define TARGET_ARCH_H
+
+#include "qemu.h"
+
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
+target_ulong target_cpu_get_tls(CPUARMState *env);
+
+#endif /* TARGET_ARCH_H */
diff --git a/bsd-user/aarch64/target_arch_reg.h 
b/bsd-user/aarch64/target_arch_reg.h
new file mode 100644
index 000..5c7154f0c18
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_reg.h
@@ -0,0 +1,56 @@
+/*
+ *  FreeBSD arm64 register structures
+ *
+ *  Copyright (c) 2015 Stacey Son
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_REG_H
+#define TARGET_ARCH_REG_H
+
+/* See sys/arm64/include/reg.h */
+typedef struct target_reg {
+uint64_tx[30];
+uint64_tlr;
+uint64_tsp;
+uint64_telr;
+uint64_tspsr;
+} target_reg_t;
+
+typedef struct target_fpreg {
+__uint128_t fp_q[32];
+uint32_tfp_sr;
+uint32_tfp_cr;
+} target_fpreg_t;
+
+#define tswapreg(ptr)   tswapal(ptr)
+
+static inline void target_copy_regs(target_reg_t *regs, CPUARMState *env)
+{
+int i;
+
+for (i = 0; i < 30; i++) {
+regs->x[i] = tswapreg(env->xregs[i]);
+}
+regs->lr = tswapreg(env->xregs[30]);
+regs->sp = tswapreg(env->xregs[31]);
+regs->elr = tswapreg(env->pc);
+regs->spsr = tswapreg(pstate_read(env));
+}
+
+#undef tswapreg
+
+#endif /* TARGET_ARCH_REG_H */
diff --git a/bsd-user/aarch64/target_arch_sysarch.h 
b/bsd-user/aarch64/target_arch_sysarch.h
new file mode 100644
index 000..b003015daf4
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_sysarch.h
@@ -0,0 +1,42 @@
+/*
+ * ARM AArch64 sysarch() system call emulation for bsd-user.
+ *
+ * Copyright (c) 2015 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms o

[PATCH 12/14] bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack

2024-07-22 Thread Warner Losh
Most (all?) targets require stacks to be properly aligned. Rather than a
series of ifdefs in bsd-user/signal.h, instead use a manditory #define
for all architectures.

Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch_signal.h | 2 ++
 bsd-user/arm/target_arch_signal.h | 2 ++
 bsd-user/i386/target_arch_signal.h| 2 ++
 bsd-user/signal.c | 9 +
 bsd-user/x86_64/target_arch_signal.h  | 2 ++
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
index bff752a67ab..b72ba7aa504 100644
--- a/bsd-user/aarch64/target_arch_signal.h
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -77,4 +77,6 @@ struct target_sigframe {
 target_ucontext_t   sf_uc;  /* saved ucontext */
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/arm/target_arch_signal.h 
b/bsd-user/arm/target_arch_signal.h
index 02b2b33e07a..10f96b8bfc9 100644
--- a/bsd-user/arm/target_arch_signal.h
+++ b/bsd-user/arm/target_arch_signal.h
@@ -86,4 +86,6 @@ struct target_sigframe {
 target_mcontext_vfp_t sf_vfp; /* actual saved VFP context */
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/i386/target_arch_signal.h 
b/bsd-user/i386/target_arch_signal.h
index 279dadc22c7..2c14153ab6b 100644
--- a/bsd-user/i386/target_arch_signal.h
+++ b/bsd-user/i386/target_arch_signal.h
@@ -88,4 +88,6 @@ struct target_sigframe {
 uint32_t__spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 8
+
 #endif /* TARGET_ARCH_SIGNAL_H */
diff --git a/bsd-user/signal.c b/bsd-user/signal.c
index 8b6654b91da..da49b9bffc1 100644
--- a/bsd-user/signal.c
+++ b/bsd-user/signal.c
@@ -728,14 +728,7 @@ static inline abi_ulong get_sigframe(struct 
target_sigaction *ka,
 sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
 }
 
-/* TODO: make this a target_arch function / define */
-#if defined(TARGET_ARM)
-return (sp - frame_size) & ~7;
-#elif defined(TARGET_AARCH64)
-return (sp - frame_size) & ~15;
-#else
-return sp - frame_size;
-#endif
+return ROUND_DOWN(sp - frame_size, TARGET_SIGSTACK_ALIGN);
 }
 
 /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
diff --git a/bsd-user/x86_64/target_arch_signal.h 
b/bsd-user/x86_64/target_arch_signal.h
index ca24bf1e7f7..f833ee66cef 100644
--- a/bsd-user/x86_64/target_arch_signal.h
+++ b/bsd-user/x86_64/target_arch_signal.h
@@ -97,4 +97,6 @@ struct target_sigframe {
 uint32_t__spare__[2];
 };
 
+#define TARGET_SIGSTACK_ALIGN 16
+
 #endif /* TARGET_ARCH_SIGNAL_H */
-- 
2.45.1




[PATCH 07/14] bsd-user:Add set_mcontext function for ARM AArch64

2024-07-22 Thread Warner Losh
From: Stacey Son 

The function copies register values from the provided target_mcontext_t
structure to the CPUARMState registers.
Note:FP is unfinished upstream but will be a separate commit coming soon.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-8-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 22 ++
 1 file changed, 22 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 43c886e6036..13faac8ce60 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -95,3 +95,25 @@ abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong 
frame_addr,
 return 0;
 }
 
+/*
+ * Compare to set_mcontext() in arm64/arm64/machdep.c
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long set_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int srflag)
+{
+int err = 0, i;
+const uint64_t *gr = mcp->mc_gpregs.gp_x;
+
+for (i = 0; i < 30; i++) {
+regs->xregs[i] = tswap64(gr[i]);
+}
+
+regs->xregs[TARGET_REG_SP] = tswap64(mcp->mc_gpregs.gp_sp);
+regs->xregs[TARGET_REG_LR] = tswap64(mcp->mc_gpregs.gp_lr);
+regs->pc = mcp->mc_gpregs.gp_elr;
+pstate_write(regs, mcp->mc_gpregs.gp_spsr);
+
+/* XXX FP? */
+
+return err;
+}
-- 
2.45.1




[PATCH 08/14] bsd-user:Add AArch64 improvements and signal handling functions

2024-07-22 Thread Warner Losh
From: Stacey Son 

Added get_ucontext_sigreturn function to check processor state ensuring current 
execution mode is EL0 and no flags
indicating interrupts or exceptions are set.
Updated AArch64 code to use CF directly without reading/writing the entire 
processor state, improving efficiency.
Changed FP data structures to use Int128 instead of __uint128_t, leveraging 
QEMU's generic mechanism for referencing this type.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-9-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 20 +++-
 bsd-user/aarch64/target_arch_cpu.h|  7 ++-
 bsd-user/aarch64/target_arch_reg.h|  2 +-
 bsd-user/aarch64/target_arch_signal.h |  2 +-
 bsd-user/qemu.h   |  3 +++
 5 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index 13faac8ce60..6bc73a798f3 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -21,7 +21,7 @@
 #include "qemu.h"
 
 /*
- * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Compare to sendsig() in sys/arm64/arm64/exec_machdep.c
  * Assumes that target stack frame memory is locked.
  */
 abi_long set_sigtramp_args(CPUARMState *regs, int sig,
@@ -117,3 +117,21 @@ abi_long set_mcontext(CPUARMState *regs, target_mcontext_t 
*mcp, int srflag)
 
 return err;
 }
+
+/* Compare to sys_sigreturn() in  arm64/arm64/machdep.c */
+abi_long get_ucontext_sigreturn(CPUARMState *regs, abi_ulong target_sf,
+abi_ulong *target_uc)
+{
+uint32_t pstate = pstate_read(regs);
+
+*target_uc = 0;
+
+if ((pstate & PSTATE_M) != PSTATE_MODE_EL0t  ||
+(pstate & (PSTATE_F | PSTATE_I | PSTATE_A | PSTATE_D)) != 0) {
+return -TARGET_EINVAL;
+}
+
+*target_uc = target_sf;
+
+return 0;
+}
diff --git a/bsd-user/aarch64/target_arch_cpu.h 
b/bsd-user/aarch64/target_arch_cpu.h
index 5c150bb7e9c..b288e0d069b 100644
--- a/bsd-user/aarch64/target_arch_cpu.h
+++ b/bsd-user/aarch64/target_arch_cpu.h
@@ -48,7 +48,6 @@ static inline void target_cpu_loop(CPUARMState *env)
 CPUState *cs = env_cpu(env);
 int trapnr, ec, fsc, si_code, si_signo;
 uint64_t code, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
-uint32_t pstate;
 abi_long ret;
 
 for (;;) {
@@ -88,18 +87,16 @@ static inline void target_cpu_loop(CPUARMState *env)
  * The carry bit is cleared for no error; set for error.
  * See arm64/arm64/vm_machdep.c cpu_set_syscall_retval()
  */
-pstate = pstate_read(env);
 if (ret >= 0) {
-pstate &= ~PSTATE_C;
+env->CF = 0;
 env->xregs[0] = ret;
 } else if (ret == -TARGET_ERESTART) {
 env->pc -= 4;
 break;
 } else if (ret != -TARGET_EJUSTRETURN) {
-pstate |= PSTATE_C;
+env->CF = 1;
 env->xregs[0] = -ret;
 }
-pstate_write(env, pstate);
 break;
 
 case EXCP_INTERRUPT:
diff --git a/bsd-user/aarch64/target_arch_reg.h 
b/bsd-user/aarch64/target_arch_reg.h
index 5c7154f0c18..b53302e7f7a 100644
--- a/bsd-user/aarch64/target_arch_reg.h
+++ b/bsd-user/aarch64/target_arch_reg.h
@@ -31,7 +31,7 @@ typedef struct target_reg {
 } target_reg_t;
 
 typedef struct target_fpreg {
-__uint128_t fp_q[32];
+Int128  fp_q[32];
 uint32_tfp_sr;
 uint32_tfp_cr;
 } target_fpreg_t;
diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
index df171733166..bff752a67ab 100644
--- a/bsd-user/aarch64/target_arch_signal.h
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -49,7 +49,7 @@ struct target_gpregs {
 };
 
 struct target_fpregs {
-__uint128_t fp_q[32];
+Int128  fp_q[32];
 uint32_tfp_sr;
 uint32_tfp_cr;
 uint32_tfp_flags;
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 9d2fc7148eb..3736c417860 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -17,6 +17,9 @@
 #ifndef QEMU_H
 #define QEMU_H
 
+#include 
+
+#include "qemu/int128.h"
 #include "cpu.h"
 #include "qemu/units.h"
 #include "exec/cpu_ldst.h"
-- 
2.45.1




[PATCH 04/14] bsd-user:Add ARM AArch64 signal handling support

2024-07-22 Thread Warner Losh
From: Stacey Son 

Added sigcode setup function for signal trampoline which initializes a sequence 
of instructions
to handle signal returns and exits, copying this code to the target offset.
Defined ARM AArch64 specific signal definitions including register indices and 
sizes,
and introduced structures to represent general purpose registers, floating 
point registers, and machine context.
Added function to set up signal handler arguments, populating register values 
in `CPUARMState`
based on the provided signal, signal frame, signal action, and frame address.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Co-authored-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-5-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c   | 53 
 bsd-user/aarch64/target_arch_signal.h   | 80 +
 bsd-user/aarch64/target_arch_sigtramp.h | 48 +++
 3 files changed, 181 insertions(+)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
new file mode 100644
index 000..98861f9ab3b
--- /dev/null
+++ b/bsd-user/aarch64/signal.c
@@ -0,0 +1,53 @@
+/*
+ * ARM AArch64 specific signal definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+
+#include "qemu.h"
+
+/*
+ * Compare to sendsig() in sys/arm64/arm64/machdep.c
+ * Assumes that target stack frame memory is locked.
+ */
+abi_long set_sigtramp_args(CPUARMState *regs, int sig,
+   struct target_sigframe *frame,
+   abi_ulong frame_addr,
+   struct target_sigaction *ka)
+{
+/*
+ * Arguments to signal handler:
+ *  x0 = signal number
+ *  x1 = siginfo pointer
+ *  x2 = ucontext pointer
+ *  pc/elr = signal handler pointer
+ *  sp = sigframe struct pointer
+ *  lr = sigtramp at base of user stack
+ */
+
+regs->xregs[0] = sig;
+regs->xregs[1] = frame_addr +
+offsetof(struct target_sigframe, sf_si);
+regs->xregs[2] = frame_addr +
+offsetof(struct target_sigframe, sf_uc);
+
+regs->pc = ka->_sa_handler;
+regs->xregs[TARGET_REG_SP] = frame_addr;
+regs->xregs[TARGET_REG_LR] = TARGET_PS_STRINGS - TARGET_SZSIGCODE;
+
+return 0;
+}
diff --git a/bsd-user/aarch64/target_arch_signal.h 
b/bsd-user/aarch64/target_arch_signal.h
new file mode 100644
index 000..df171733166
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_signal.h
@@ -0,0 +1,80 @@
+/*
+ * ARM AArch64 specific signal definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_SIGNAL_H
+#define TARGET_ARCH_SIGNAL_H
+
+#include "cpu.h"
+
+#define TARGET_REG_X0   0
+#define TARGET_REG_X30  30
+#define TARGET_REG_X31  31
+#define TARGET_REG_LR   TARGET_REG_X30
+#define TARGET_REG_SP   TARGET_REG_X31
+
+#define TARGET_INSN_SIZE4   /* arm64 instruction size */
+
+/* Size of the signal trampolin code. See _sigtramp(). */
+#define TARGET_SZSIGCODE((abi_ulong)(9 * TARGET_INSN_SIZE))
+
+/* compare to sys/arm64/include/_limits.h */
+#define TARGET_MINSIGSTKSZ  (1024 * 4)  /* min sig stack size 
*/
+#define TARGET_SIGSTKSZ (TARGET_MINSIGSTKSZ + 32768)  /* recommended size 
*/
+
+/* struct __mcontext in sys/arm64/include/ucontext.h */
+
+str

[PATCH 03/14] bsd-user:Add ARM AArch64 support and capabilities

2024-07-22 Thread Warner Losh
Added function to access rval2 by accessing the x1 register.
Defined ARM AArch64 ELF parameters including mmap and dynamic load addresses.
Introduced extensive hardware capability definitions and macros for retrieving 
hardware capability (hwcap) flags.
Implemented function to retrieve ARM AArch64 hardware capabilities using the 
`GET_FEATURE_ID` macro.
Added function to retrieve extended ARM AArch64 hardware capability flags.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Signed-off-by: Warner Losh 
Co-authored-by: Kyle Evans 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-4-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch.h |   1 +
 bsd-user/aarch64/target_arch_elf.h | 163 +
 bsd-user/aarch64/target_arch_vmparam.h |   6 +
 3 files changed, 170 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_elf.h

diff --git a/bsd-user/aarch64/target_arch.h b/bsd-user/aarch64/target_arch.h
index 27f47de8eb3..4815a56ae3c 100644
--- a/bsd-user/aarch64/target_arch.h
+++ b/bsd-user/aarch64/target_arch.h
@@ -21,6 +21,7 @@
 #define TARGET_ARCH_H
 
 #include "qemu.h"
+#include "target/arm/cpu-features.h"
 
 void target_cpu_set_tls(CPUARMState *env, target_ulong newtls);
 target_ulong target_cpu_get_tls(CPUARMState *env);
diff --git a/bsd-user/aarch64/target_arch_elf.h 
b/bsd-user/aarch64/target_arch_elf.h
new file mode 100644
index 000..cc87f475b3f
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_elf.h
@@ -0,0 +1,163 @@
+/*
+ * ARM AArch64 ELF definitions for bsd-user
+ *
+ * Copyright (c) 2015 Stacey D. Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_ELF_H
+#define TARGET_ARCH_ELF_H
+
+#define ELF_START_MMAP 0x8000
+#define ELF_ET_DYN_LOAD_ADDR0x10
+
+#define elf_check_arch(x) ((x) == EM_AARCH64)
+
+#define ELF_CLASS   ELFCLASS64
+#define ELF_DATAELFDATA2LSB
+#define ELF_ARCHEM_AARCH64
+
+#define USE_ELF_CORE_DUMP
+#define ELF_EXEC_PAGESIZE   4096
+
+enum {
+ARM_HWCAP_A64_FP= 1 << 0,
+ARM_HWCAP_A64_ASIMD = 1 << 1,
+ARM_HWCAP_A64_EVTSTRM   = 1 << 2,
+ARM_HWCAP_A64_AES   = 1 << 3,
+ARM_HWCAP_A64_PMULL = 1 << 4,
+ARM_HWCAP_A64_SHA1  = 1 << 5,
+ARM_HWCAP_A64_SHA2  = 1 << 6,
+ARM_HWCAP_A64_CRC32 = 1 << 7,
+ARM_HWCAP_A64_ATOMICS   = 1 << 8,
+ARM_HWCAP_A64_FPHP  = 1 << 9,
+ARM_HWCAP_A64_ASIMDHP   = 1 << 10,
+ARM_HWCAP_A64_CPUID = 1 << 11,
+ARM_HWCAP_A64_ASIMDRDM  = 1 << 12,
+ARM_HWCAP_A64_JSCVT = 1 << 13,
+ARM_HWCAP_A64_FCMA  = 1 << 14,
+ARM_HWCAP_A64_LRCPC = 1 << 15,
+ARM_HWCAP_A64_DCPOP = 1 << 16,
+ARM_HWCAP_A64_SHA3  = 1 << 17,
+ARM_HWCAP_A64_SM3   = 1 << 18,
+ARM_HWCAP_A64_SM4   = 1 << 19,
+ARM_HWCAP_A64_ASIMDDP   = 1 << 20,
+ARM_HWCAP_A64_SHA512= 1 << 21,
+ARM_HWCAP_A64_SVE   = 1 << 22,
+ARM_HWCAP_A64_ASIMDFHM  = 1 << 23,
+ARM_HWCAP_A64_DIT   = 1 << 24,
+ARM_HWCAP_A64_USCAT = 1 << 25,
+ARM_HWCAP_A64_ILRCPC= 1 << 26,
+ARM_HWCAP_A64_FLAGM = 1 << 27,
+ARM_HWCAP_A64_SSBS  = 1 << 28,
+ARM_HWCAP_A64_SB= 1 << 29,
+ARM_HWCAP_A64_PACA  = 1 << 30,
+ARM_HWCAP_A64_PACG  = 1UL << 31,
+
+ARM_HWCAP2_A64_DCPODP   = 1 << 0,
+ARM_HWCAP2_A64_SVE2 = 1 << 1,
+ARM_HWCAP2_A64_SVEAES   = 1 << 2,
+ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
+ARM_HWCAP2_A64_SVEBITPERM   = 1 << 4,
+ARM_HWCAP2_A64_SVESHA3  = 1 << 5,
+ARM_HWCAP2_A64_SVESM4   = 1 << 6,
+ARM_HWCAP2_A64_FLAGM2   = 1 << 7,
+ARM_HWCAP2_A64_FRINT= 1 << 8,
+ARM_HWCAP2_A64_SVEI8MM  = 1 << 9,
+ARM_HWCAP2_A64_SVEF32MM = 1 << 10,
+ARM_HWCAP2_A64_SVEF64MM = 1 << 11,
+ARM_HWCAP2_A64_SVEBF16  = 1 << 12,
+ARM_HWCAP2_A64_I8MM = 1 <

[PATCH 00/14] bsd-user: Misc changes for 9.1 (I hope)

2024-07-22 Thread Warner Losh
This patch series includes two main sets of patches. To make it simple to
review, I've included the changes from my student which the later changes depend
on. I've included a change from Jessica and Doug as well. I've reviewed them,
but more eyes never hurt.

I've also included a number of 'touch up' patches needed either to get the
aarch64 building, or to implmement suggestions from prior review cycles. The
main one is what's charitably described as a kludge: force aarch64 to use 4k
pages. The qemu-project (and blitz branch) hasn't had the necessary changes to
bsd-user needed to support variable page size.

Sorry this is so late... Live has conspired to delay me.

Doug Rabson (1):
  bsd-user: Simplify the implementation of execve

Jessica Clarke (1):
  bsd-user: Sync fork_start/fork_end with linux-user

Stacey Son (6):
  bsd-user:Add CPU initialization and management functions
  bsd-user:Add AArch64 register handling and related functions
  bsd-user:Add ARM AArch64 signal handling support
  bsd-user:Add get_mcontext function for ARM AArch64
  bsd-user:Add set_mcontext function for ARM AArch64
  bsd-user:Add AArch64 improvements and signal handling functions

Warner Losh (6):
  bsd-user:Add ARM AArch64 support and capabilities
  bsd-user:Add setup_sigframe_arch function for ARM AArch64
  bsd-user: Hard wire aarch64 to be 4k pages only
  bsd-user: Define TARGET_SIGSTACK_ALIGN and use it to round stack
  bsd-user: Make compile for non-linux user-mode stuff
  bsd-user: Add aarch64 build to tree

 bsd-user/aarch64/signal.c   | 137 +
 bsd-user/aarch64/target_arch.h  |  29 
 bsd-user/aarch64/target_arch_cpu.c  |  31 
 bsd-user/aarch64/target_arch_cpu.h  | 189 
 bsd-user/aarch64/target_arch_elf.h  | 163 
 bsd-user/aarch64/target_arch_reg.h  |  56 +++
 bsd-user/aarch64/target_arch_signal.h   |  82 ++
 bsd-user/aarch64/target_arch_sigtramp.h |  48 ++
 bsd-user/aarch64/target_arch_sysarch.h  |  42 ++
 bsd-user/aarch64/target_arch_thread.h   |  61 
 bsd-user/aarch64/target_arch_vmparam.h  |  74 ++
 bsd-user/aarch64/target_syscall.h   |  51 +++
 bsd-user/arm/target_arch_signal.h   |   2 +
 bsd-user/freebsd/os-proc.c  | 118 +--
 bsd-user/i386/target_arch_signal.h  |   2 +
 bsd-user/main.c |  46 ++
 bsd-user/qemu.h |   3 +
 bsd-user/signal.c   |   9 +-
 bsd-user/x86_64/target_arch_signal.h|   2 +
 configs/targets/aarch64-bsd-user.mak|   3 +
 target/arm/cpu-param.h  |   4 +
 target/arm/gdbstub64.c  |   4 +
 22 files changed, 1001 insertions(+), 155 deletions(-)
 create mode 100644 bsd-user/aarch64/signal.c
 create mode 100644 bsd-user/aarch64/target_arch.h
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_arch_elf.h
 create mode 100644 bsd-user/aarch64/target_arch_reg.h
 create mode 100644 bsd-user/aarch64/target_arch_signal.h
 create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h
 create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
 create mode 100644 bsd-user/aarch64/target_arch_thread.h
 create mode 100644 bsd-user/aarch64/target_arch_vmparam.h
 create mode 100644 bsd-user/aarch64/target_syscall.h
 create mode 100644 configs/targets/aarch64-bsd-user.mak

-- 
2.45.1




[PATCH 06/14] bsd-user:Add setup_sigframe_arch function for ARM AArch64

2024-07-22 Thread Warner Losh
The function utilizes the `get_mcontext` function to retrieve the machine
context for the current CPUARMState

Signed-off-by: Warner Losh 
Signed-off-by: Ajeet Singh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-7-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/signal.c | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
index ab3bf8558ab..43c886e6036 100644
--- a/bsd-user/aarch64/signal.c
+++ b/bsd-user/aarch64/signal.c
@@ -81,3 +81,17 @@ abi_long get_mcontext(CPUARMState *regs, target_mcontext_t 
*mcp, int flags)
 
 return err;
 }
+
+/*
+ * Compare to arm64/arm64/exec_machdep.c sendsig()
+ * Assumes that the memory is locked if frame points to user memory.
+ */
+abi_long setup_sigframe_arch(CPUARMState *env, abi_ulong frame_addr,
+ struct target_sigframe *frame, int flags)
+{
+target_mcontext_t *mcp = &frame->sf_uc.uc_mcontext;
+
+get_mcontext(env, mcp, flags);
+return 0;
+}
+
-- 
2.45.1




[PATCH 01/14] bsd-user:Add CPU initialization and management functions

2024-07-22 Thread Warner Losh
From: Stacey Son 

Added function to initialize ARM CPU and check if it supports 64-bit mode.
Implemented CPU loop function to handle exceptions and emulate execution of 
instructions.
Added function to clone CPU state to create a new thread.
Included AArch64 specific CPU functions for bsd-user to set and receive 
thread-local-storage
value from the tpidr_el0 register.
Introduced structure for storing CPU register states for BSD-USER.

Signed-off-by: Stacey Son 
Signed-off-by: Ajeet Singh 
Co-authored-by: Kyle Evans 
Co-authored-by: Sean Bruno 
Co-authored-by: Jessica Clarke 
Reviewed-by: Warner Losh 
Reviewed-by: Richard Henderson 
Message-Id: <20240707191128.10509-2-itac...@freebsd.org>
Signed-off-by: Warner Losh 
---
 bsd-user/aarch64/target_arch_cpu.c |  31 +
 bsd-user/aarch64/target_arch_cpu.h | 192 +
 bsd-user/aarch64/target_syscall.h  |  51 
 3 files changed, 274 insertions(+)
 create mode 100644 bsd-user/aarch64/target_arch_cpu.c
 create mode 100644 bsd-user/aarch64/target_arch_cpu.h
 create mode 100644 bsd-user/aarch64/target_syscall.h

diff --git a/bsd-user/aarch64/target_arch_cpu.c 
b/bsd-user/aarch64/target_arch_cpu.c
new file mode 100644
index 000..b2fa59efaf6
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_cpu.c
@@ -0,0 +1,31 @@
+/*
+ * ARM AArch64 specific CPU for bsd-user
+ *
+ * Copyright (c) 2015 Stacey Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+#include "target_arch.h"
+
+/* See cpu_set_user_tls() in arm64/arm64/vm_machdep.c */
+void target_cpu_set_tls(CPUARMState *env, target_ulong newtls)
+{
+env->cp15.tpidr_el[0] = newtls;
+}
+
+target_ulong target_cpu_get_tls(CPUARMState *env)
+{
+return env->cp15.tpidr_el[0];
+}
diff --git a/bsd-user/aarch64/target_arch_cpu.h 
b/bsd-user/aarch64/target_arch_cpu.h
new file mode 100644
index 000..5c150bb7e9c
--- /dev/null
+++ b/bsd-user/aarch64/target_arch_cpu.h
@@ -0,0 +1,192 @@
+/*
+ *  ARM AArch64 cpu init and loop
+ *
+ * Copyright (c) 2015 Stacey Son
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TARGET_ARCH_CPU_H
+#define TARGET_ARCH_CPU_H
+
+#include "target_arch.h"
+#include "signal-common.h"
+#include "target/arm/syndrome.h"
+
+#define TARGET_DEFAULT_CPU_MODEL "any"
+
+static inline void target_cpu_init(CPUARMState *env,
+struct target_pt_regs *regs)
+{
+int i;
+
+if (!(arm_feature(env, ARM_FEATURE_AARCH64))) {
+fprintf(stderr, "The selected ARM CPU does not support 64 bit mode\n");
+exit(1);
+}
+for (i = 0; i < 31; i++) {
+env->xregs[i] = regs->regs[i];
+}
+env->pc = regs->pc;
+env->xregs[31] = regs->sp;
+}
+
+
+static inline void target_cpu_loop(CPUARMState *env)
+{
+CPUState *cs = env_cpu(env);
+int trapnr, ec, fsc, si_code, si_signo;
+uint64_t code, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
+uint32_t pstate;
+abi_long ret;
+
+for (;;) {
+cpu_exec_start(cs);
+trapnr = cpu_exec(cs);
+cpu_exec_end(cs);
+process_queued_cpu_work(cs);
+
+switch (trapnr) {
+case EXCP_SWI:
+/* See arm64/arm64/trap.c cpu_fetch_syscall_args() */
+code = env->xregs[8];
+if (code == TARGET_FREEBSD_NR_syscall ||
+code == TARGET_FREEBSD_NR___syscall) {
+code = env->xregs[0];
+arg1 = env->xregs[1];
+arg2 = env->xregs[2];
+arg3 = env->xregs[3];
+arg4 = env->xregs[4];
+arg5 = env-&g

Re: [PATCH v2 0/8] ARM AArch64 Support for BSD

2024-07-21 Thread Warner Losh
Queued for my 9.1 pull request...

Thanks! Sorry for the delay...

Warner



On Sun, Jul 7, 2024 at 1:11 PM Ajeet Singh  wrote:

> Patch 1: Previous patches 1 to 5, which were reviewed and acked, have been
> folded into this patch.Some changes that were suggested,
>  such as replacing "pstate &= ~PSTATE_C" with faster constructs
> like "env->CF = 0", have been addressed in patch 8.
> Patch 2: In this patch, patches 6 to 11 have been folded. Changes such as
> using "ROUND_DOWN" for stack pointer calculation
>  and replacing "__uint128_t" with "Int128" in the following patch
> 8 have been addressed. Also, "CPUARMState" will be
>  changed to "CPUArchState" in the upcoming version, and the issue
> with "-R" is being worked on separately.
> Patch 3: Patches 12 to 16 were reviewed and folded. The function
> "get_second_rval" will be used upstream.
> Patch 4: Patches 17 to 19 were folded, and "__uint128_t" to "Int128" has
> been fixed in patch 8. Also, the suggestion to
>  use "const_le32()" has been deferred for now.
> Patch 5: Previously patch 20, the FP suggestion is being deferred, but the
> patch is ready for review.
> Patch 6: Previously patch 21 has been reviewed.
> Patch 7: Previously patch 22, same as patch 5.
> Patch 8: Previously patch 23. For now, no changes, but issues of other
> patches have been addressed, such as updating
>  AArch64 code to use CF directly and changing FP data structures
> to use Int128 instead of __uint128_t.
>
> Stacey Son (6):
>   bsd-user:Add CPU initialization and management functions
>   bsd-user:Add AArch64 register handling and related functions
>   bsd-user:Add ARM AArch64 signal handling support
>   bsd-user:Add get_mcontext function for ARM AArch64
>   bsd-user:Add set_mcontext function for ARM AArch64
>   bsd-user:Add AArch64 improvements and signal handling functions
>
> Warner Losh (2):
>   bsd-user:Add ARM AArch64 support and capabilities
>   bsd-user:Add setup_sigframe_arch function for ARM AArch64
>
>  bsd-user/aarch64/signal.c   | 137 +
>  bsd-user/aarch64/target_arch.h  |  28 
>  bsd-user/aarch64/target_arch_cpu.c  |  31 
>  bsd-user/aarch64/target_arch_cpu.h  | 188 
>  bsd-user/aarch64/target_arch_elf.h  | 165 +
>  bsd-user/aarch64/target_arch_reg.h  |  56 +++
>  bsd-user/aarch64/target_arch_signal.h   |  80 ++
>  bsd-user/aarch64/target_arch_sigtramp.h |  48 ++
>  bsd-user/aarch64/target_arch_sysarch.h  |  42 ++
>  bsd-user/aarch64/target_arch_thread.h   |  61 
>  bsd-user/aarch64/target_arch_vmparam.h  |  74 ++
>  bsd-user/aarch64/target_syscall.h   |  51 +++
>  bsd-user/qemu.h |   3 +
>  13 files changed, 964 insertions(+)
>  create mode 100644 bsd-user/aarch64/signal.c
>  create mode 100644 bsd-user/aarch64/target_arch.h
>  create mode 100644 bsd-user/aarch64/target_arch_cpu.c
>  create mode 100644 bsd-user/aarch64/target_arch_cpu.h
>  create mode 100644 bsd-user/aarch64/target_arch_elf.h
>  create mode 100644 bsd-user/aarch64/target_arch_reg.h
>  create mode 100644 bsd-user/aarch64/target_arch_signal.h
>  create mode 100644 bsd-user/aarch64/target_arch_sigtramp.h
>  create mode 100644 bsd-user/aarch64/target_arch_sysarch.h
>  create mode 100644 bsd-user/aarch64/target_arch_thread.h
>  create mode 100644 bsd-user/aarch64/target_arch_vmparam.h
>  create mode 100644 bsd-user/aarch64/target_syscall.h
>
> --
> 2.34.1
>
>


Re: linux-user cannot allocate stack memory on riscv64 host due to non-zero guest_base

2024-06-27 Thread Warner Losh
On Thu, Jun 27, 2024, 1:54 AM Andreas Schwab  wrote:

> On Jun 26 2024, Warner Losh wrote:
>
> > On Wed, Jun 26, 2024 at 9:48 AM Richard Henderson <
> > richard.hender...@linaro.org> wrote:
> >
> >> On 6/26/24 01:23, Andreas Schwab wrote:
> >> > On Jun 25 2024, Richard Henderson wrote:
> >> >
> >> >> can always force the use of a non-zero base with -B or -R.
> >> >
> >> > $ qemu-riscv64 -d page -B 0x3ee000 hello.riscv64
> >> > host mmap_min_addr=0x1000 (fallback)
> >> > qemu-riscv64: /daten/src/test/hello.riscv64: requires virtual address
> >> space that is in use (omit the -B option or choose a different value)
> >> >
> >>
> >> Well, sure, but that obviously is where qemu-riscv64 itself is located.
> >> Still not a valid test case.
> >>
> >
> > Yea, what happens if you say -B 0x3ee00 or something else that won't
> > conflict?
>
> I didn't chose that number, qemu did.  If it doesn't work then qemu must
> be fixed.
>

And when you are diagnosing the root cause of the bug, the submitter of the
bug sometimes needs to do diagnostic tests when requested, not attack the
volunteers who are trying to help. If that's all you do, there will be no
fix. You can't talk to me like that and expect any reaction but "I have
better things to do with my time than deal with this jerk" regardless of
the merits of the original complaint.

Warner

-- 
> Andreas Schwab, SUSE Labs, sch...@suse.de
> GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
> "And now for something completely different."
>


Re: linux-user cannot allocate stack memory on riscv64 host due to non-zero guest_base

2024-06-26 Thread Warner Losh
On Wed, Jun 26, 2024 at 9:48 AM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/26/24 01:23, Andreas Schwab wrote:
> > On Jun 25 2024, Richard Henderson wrote:
> >
> >> can always force the use of a non-zero base with -B or -R.
> >
> > $ qemu-riscv64 -d page -B 0x3ee000 hello.riscv64
> > host mmap_min_addr=0x1000 (fallback)
> > qemu-riscv64: /daten/src/test/hello.riscv64: requires virtual address
> space that is in use (omit the -B option or choose a different value)
> >
>
> Well, sure, but that obviously is where qemu-riscv64 itself is located.
> Still not a valid test case.
>

Yea, what happens if you say -B 0x3ee00 or something else that won't
conflict?

Warner


Re: [PATCH 12/23] Add ability to get rval2

2024-06-23 Thread Warner Losh
On Tue, Jun 18, 2024 at 4:17 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/17/24 11:57, Ajeet Singh wrote:
> > From: Warner Losh 
> >
> > Function accesses the x1 register which holds the value
> >
> > Signed-off-by: Warner Losh 
> > Signed-off-by: Ajeet Singh 
> > ---
> >   bsd-user/aarch64/target_arch_vmparam.h | 6 ++
> >   1 file changed, 6 insertions(+)
> >
> > diff --git a/bsd-user/aarch64/target_arch_vmparam.h
> b/bsd-user/aarch64/target_arch_vmparam.h
> > index dc66e1289b..0c35491970 100644
> > --- a/bsd-user/aarch64/target_arch_vmparam.h
> > +++ b/bsd-user/aarch64/target_arch_vmparam.h
> > @@ -65,4 +65,10 @@ static inline void set_second_rval(CPUARMState
> *state, abi_ulong retval2)
> >   {
> >   state->xregs[1] = retval2; /* XXX not really used on 64-bit arch */
> >   }
> > +
> > +static inline abi_ulong get_second_rval(CPUARMState *state)
> > +{
> > +return state->xregs[1];
> > +}
>
> The other two ports only define set_second_rval, and this one only defines
> get?  This
> doesn't seem right...
>

The other two ports need to have this added. Upstream, there's some code
that uses this in the system call tracing path.

Warner


Re: [PATCH 06/23] Add Aarch64 register handling

2024-06-23 Thread Warner Losh
On Mon, Jun 17, 2024 at 10:35 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/17/24 11:57, Ajeet Singh wrote:
> > From: Stacey Son 
> >
> > Header file for managing CPU register states in
> > FreeBSD user mode
> >
> > Signed-off-by: Stacey Son 
> > Signed-off-by: Ajeet Singh 
> > ---
> >   bsd-user/aarch64/target_arch_reg.h | 56 ++
> >   1 file changed, 56 insertions(+)
> >   create mode 100644 bsd-user/aarch64/target_arch_reg.h
> >
> > diff --git a/bsd-user/aarch64/target_arch_reg.h
> b/bsd-user/aarch64/target_arch_reg.h
> > new file mode 100644
> > index 00..5c7154f0c1
> > --- /dev/null
> > +++ b/bsd-user/aarch64/target_arch_reg.h
> > @@ -0,0 +1,56 @@
> > +/*
> > + *  FreeBSD arm64 register structures
> > + *
> > + *  Copyright (c) 2015 Stacey Son
> > + *  All rights reserved.
> > + *
> > + *  This program is free software; you can redistribute it and/or modify
> > + *  it under the terms of the GNU General Public License as published by
> > + *  the Free Software Foundation; either version 2 of the License, or
> > + *  (at your option) any later version.
> > + *
> > + *  This program is distributed in the hope that it will be useful,
> > + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > + *  GNU General Public License for more details.
> > + *
> > + *  You should have received a copy of the GNU General Public License
> > + *  along with this program; if not, see  >.
> > + */
> > +
> > +#ifndef TARGET_ARCH_REG_H
> > +#define TARGET_ARCH_REG_H
> > +
> > +/* See sys/arm64/include/reg.h */
> > +typedef struct target_reg {
> > +uint64_tx[30];
> > +uint64_tlr;
> > +uint64_tsp;
> > +uint64_telr;
> > +uint64_tspsr;
> > +} target_reg_t;
> > +
> > +typedef struct target_fpreg {
> > +__uint128_t fp_q[32];
>
> I'm not keen on this, though possibly it doesn't matter for hosts that
> bsd-user is
> intended to support.  Better as either Int128 or uint64_t fp_q[32][2].
>

OK. We don't use In128 anywhere today, but it's an easy change.


> What is this structure used for within qemu?
>

Yes. target_fpreg_t is used by the generic core dump code. This reserves
the proper amount of space for it. IIRC, gdb gets cranky if you don't, but
that memory is "dim" in my head, so maybe that problem was corrected ages
ago.


> Does freebsd support SVE yet?
>

Yes. But as later patches show bsd-user does not. I need to add that
support. None of our target applications seem to care, but that might just
be because the wrong answers are produced w/o a crash and there's enough
other crashes that it hasn't percolated up to the top of anybody's queue...


> It's certainly not used with this patch, so it's hard to tell, but can we
> omit it entirely
> for now?
>

It will be necessary for the core dump stuff, though I suppose it's all 0's
right now.  And core dumps are a low-priority item, but it just has to be
SOMETHING, so I propose we just change it here (and in
target_arch_signal.h) to be Int128 and then the future set of changes that
will add FP support for signals, etc will make whatever tweaks / fixes are
needed then.

Warner


Re: [PATCH 23/23] Add get_ucontext_sigreturn function

2024-06-23 Thread Warner Losh
On Tue, Jun 18, 2024 at 4:56 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/17/24 11:58, Ajeet Singh wrote:
> > From: Stacey Son 
> >
> > Function checks the processor state to ensure that the current
> > execution mode is EL0 and no flags indicating interrupts or
> > exceptions are set
> >
> > Signed-off-by: Stacey Son 
> > Signed-off-by: Ajeet Singh 
> > ---
> >   bsd-user/aarch64/signal.c | 18 ++
> >   1 file changed, 18 insertions(+)
> >
> > diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
> > index 13faac8ce6..ad81531ec5 100644
> > --- a/bsd-user/aarch64/signal.c
> > +++ b/bsd-user/aarch64/signal.c
> > @@ -117,3 +117,21 @@ abi_long set_mcontext(CPUARMState *regs,
> target_mcontext_t *mcp, int srflag)
> >
> >   return err;
> >   }
> > +
> > +/* Compare to sys_sigreturn() in  arm64/arm64/machdep.c */
>

This is now in exec_machdep.c and the most relevant bits are set_mcontext()
and sys_sigreturn().


> > +abi_long get_ucontext_sigreturn(CPUARMState *regs, abi_ulong target_sf,
> > +abi_ulong *target_uc)
> > +{
> > +uint32_t pstate = pstate_read(regs);
> > +
> > +*target_uc = 0;
> > +
> > +if ((pstate & PSTATE_M) != PSTATE_MODE_EL0t  ||
> > +(pstate & (PSTATE_F | PSTATE_I | PSTATE_A | PSTATE_D)) != 0) {
> > +return -TARGET_EINVAL;
> > +}
> > +
> > +*target_uc = target_sf;
>
> Why delay this store?  I don't see why you're assigning 0 above.
>

I'm not sure I understand this either  We don't store anything when
there's an error in the pstate, at least in the kernel code.

Warner


Re: [PATCH 22/23] Add set_mcontext function for ARM AArch64 in bsd-user

2024-06-23 Thread Warner Losh
On Tue, Jun 18, 2024 at 4:51 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/17/24 11:58, Ajeet Singh wrote:
> > From: Stacey Son 
> >
> > The function copies register values from the provided target_mcontext_t
> > structure to the CPUARMState registers
> >
> > Signed-off-by: Stacey Son 
> > Signed-off-by: Ajeet Singh 
> > ---
> >   bsd-user/aarch64/signal.c | 22 ++
> >   1 file changed, 22 insertions(+)
> >
> > diff --git a/bsd-user/aarch64/signal.c b/bsd-user/aarch64/signal.c
> > index 43c886e603..13faac8ce6 100644
> > --- a/bsd-user/aarch64/signal.c
> > +++ b/bsd-user/aarch64/signal.c
> > @@ -95,3 +95,25 @@ abi_long setup_sigframe_arch(CPUARMState *env,
> abi_ulong frame_addr,
> >   return 0;
> >   }
> >
> > +/*
> > + * Compare to set_mcontext() in arm64/arm64/machdep.c
> > + * Assumes that the memory is locked if frame points to user memory.
> > + */
> > +abi_long set_mcontext(CPUARMState *regs, target_mcontext_t *mcp, int
> srflag)
> > +{
> > +int err = 0, i;
> > +const uint64_t *gr = mcp->mc_gpregs.gp_x;
> > +
> > +for (i = 0; i < 30; i++) {
> > +regs->xregs[i] = tswap64(gr[i]);
> > +}
> > +
> > +regs->xregs[TARGET_REG_SP] = tswap64(mcp->mc_gpregs.gp_sp);
> > +regs->xregs[TARGET_REG_LR] = tswap64(mcp->mc_gpregs.gp_lr);
> > +regs->pc = mcp->mc_gpregs.gp_elr;
>
> tswap.
>

So "regs->pc = tswap64(mcp->mc_gpregs.gp_elr)"  then?


> > +pstate_write(regs, mcp->mc_gpregs.gp_spsr);
> > +
> > +/* XXX FP? */
>
> Similarly, see target_restore_fpsimd_record.
>

Since neither this, nor the get_mcontext is written for FP on upstream yet,
can we do those as a followup commit?

Warner


>
> r~
>


Re: [PATCH 11/23] Update ARM AArch64 VM parameter definitions for bsd-user

2024-06-22 Thread Warner Losh
On Tue, Jun 18, 2024 at 4:16 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/17/24 11:57, Ajeet Singh wrote:
> > From: Stacey Son 
> >
> > Defined address spaces for FreeBSD/arm64 and added function for
> > getting stack pointer from CPU and setting a return value.
> >
> > Signed-off-by: Stacey Son 
> > Signed-off-by: Warner Losh 
> > Signed-off-by: Ajeet Singh 
> > Co-authored-by: Sean Bruno 
> > Co-authored-by: Warner Losh 
> > ---
> >   bsd-user/aarch64/target_arch_vmparam.h | 68 ++
> >   1 file changed, 68 insertions(+)
> >   create mode 100644 bsd-user/aarch64/target_arch_vmparam.h
>
> Acked-by: Richard Henderson 
>
> > +/* KERNBASE - 512 MB */
> > +#define TARGET_VM_MAXUSER_ADDRESS   (0x7f00ULL - (512 *
> MiB))
> > +#define TARGET_USRSTACK TARGET_VM_MAXUSER_ADDRESS
>
> I will note that this may conflict with -R reserved_size,
> and is an existing issue with the x86_64 port as well.
>

There are indeed existing issues with address space management. We're
working through
them right now in the blitz branch. We have finally found where the atomic
issues were
coming from and it is  not setting the flag saying we want atomic
ops when creating
the CPU structures (that's a quick summary, I'll post more on this later
when we review it).
So I'd suggest, for the moment, allowing this in and fixing it when we get
those details
ironed out. Does that sound OK?

Warner


Re: [PATCH 02/23] Added CPU loop function

2024-06-22 Thread Warner Losh
On Mon, Jun 17, 2024 at 10:24 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/17/24 11:57, Ajeet Singh wrote:
> > +/*
> > + * The carry bit is cleared for no error; set for error.
> > + * See arm64/arm64/vm_machdep.c cpu_set_syscall_retval()
> > + */
> > +pstate = pstate_read(env);
> > +if (ret >= 0) {
> > +pstate &= ~PSTATE_C;
> > +env->xregs[0] = ret;
> > +} else if (ret == -TARGET_ERESTART) {
> > +env->pc -= 4;
> > +break;
> > +} else if (ret != -TARGET_EJUSTRETURN) {
> > +pstate |= PSTATE_C;
> > +env->xregs[0] = -ret;
> > +}
> > +pstate_write(env, pstate);
>
> No need for full pstate read/write:
>
>  env->CF = {0,1};
>

If I understand what you're suggesting, the quoted code can be replaced
by the following, faster construct:

/*
 * The carry bit is cleared for no error; set for error.
 * See arm64/arm64/vm_machdep.c cpu_set_syscall_retval()
 */
if (ret >= 0) {
env->CF = 0;
env->xregs[0] = ret;
} else if (ret == -TARGET_ERESTART) {
env->pc -= 4;
break;
} else if (ret != -TARGET_EJUSTRETURN) {
env->CF = 1;
env->xregs[0] = -ret;
}
break;

Is that what you're saying?


> > +break;
> > +
> > +case EXCP_INTERRUPT:
> > +/* Just indicate that signals should be handle ASAP. */
> > +break;
> > +
> > +case EXCP_UDEF:
> > +force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->pc);
> > +break;
> > +
> > +
> > +case EXCP_PREFETCH_ABORT:
> > +case EXCP_DATA_ABORT:
> > +/* We should only arrive here with EC in {DATAABORT,
> INSNABORT}. */
> > +ec = syn_get_ec(env->exception.syndrome);
>
> Nevermind about my question about syndrome.h vs patch 1.
>

Ah, Since we have to re-roll this patch anyway, maybe moving it is a good
idea?
Honestly, I'm good either way.

Warner


> r~
>


Re: [PATCH 01/23] Add CPU initialization function

2024-06-22 Thread Warner Losh
On Mon, Jun 17, 2024 at 10:17 PM Richard Henderson <
richard.hender...@linaro.org> wrote:

> On 6/17/24 11:57, Ajeet Singh wrote:
> > From: Stacey Son 
> >
> > Addded function to initialize ARM CPU
> > and to check if it supports 64 bit mode
> >
> > Signed-off-by: Ajeet Singh 
> > Signed-off-by: Stacey Son 
> > ---
> >   bsd-user/aarch64/target_arch_cpu.h | 42 ++
> >   1 file changed, 42 insertions(+)
> >   create mode 100644 bsd-user/aarch64/target_arch_cpu.h
> >
> > diff --git a/bsd-user/aarch64/target_arch_cpu.h
> b/bsd-user/aarch64/target_arch_cpu.h
> > new file mode 100644
> > index 00..db5c7062b9
> > --- /dev/null
> > +++ b/bsd-user/aarch64/target_arch_cpu.h
> > @@ -0,0 +1,42 @@
> > +/*
> > + *  ARM AArch64 cpu init and loop
> > + *
> > + * Copyright (c) 2015 Stacey Son
> > + *
> > + * This library is free software; you can redistribute it and/or
> > + * modify it under the terms of the GNU Lesser General Public
> > + * License as published by the Free Software Foundation; either
> > + * version 2 of the License, or (at your option) any later version.
> > + *
> > + * This library is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> > + * Lesser General Public License for more details.
> > + *
> > + * You should have received a copy of the GNU Lesser General Public
> > + * License along with this library; if not, see <
> http://www.gnu.org/licenses/>.
> > + */
> > +
> > +#ifndef TARGET_ARCH_CPU_H
> > +#define TARGET_ARCH_CPU_H
> > +
> > +#include "target_arch.h"
> > +#include "target/arm/syndrome.h"
>
> Do you actually need syndrome.h?
>

It's needed, but not for this chunk. It is needed for patch 2 because we
start to use the syndrome functions there to dispatch / decode the traps.
So that should be moved to patch 2 in the next round, I think.

Also

Reviewed-by: Warner Losh 

since this looks correct and I didn't write it :)

Warner


> Otherwise,
> Reviewed-by: Richard Henderson 
>
> r~
>
> > +
> > +#define TARGET_DEFAULT_CPU_MODEL "any"
> > +
> > +static inline void target_cpu_init(CPUARMState *env,
> > +struct target_pt_regs *regs)
> > +{
> > +int i;
> > +
> > +if (!(arm_feature(env, ARM_FEATURE_AARCH64))) {
> > +fprintf(stderr, "The selected ARM CPU does not support 64 bit
> mode\n");
> > +exit(1);
> > +}
> > +for (i = 0; i < 31; i++) {
> > +env->xregs[i] = regs->regs[i];
> > +}
> > +env->pc = regs->pc;
> > +env->xregs[31] = regs->sp;
> > +}
>
>


[PULL 1/3] linux-user: Adjust comment to reflect the code.

2024-06-09 Thread Warner Losh
If the user didn't specify reserved_va, there's an else for 64-bit host
32-bit (or fewer) target to reserve 32-bits of address space. Update the
comments to reflect this, and rejustify comment to 80 columns.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 linux-user/main.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/linux-user/main.c b/linux-user/main.c
index 94e4c47f052..94c99a1366f 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -814,10 +814,10 @@ int main(int argc, char **argv, char **envp)
 thread_cpu = cpu;
 
 /*
- * Reserving too much vm space via mmap can run into problems
- * with rlimits, oom due to page table creation, etc.  We will
- * still try it, if directed by the command-line option, but
- * not by default.
+ * Reserving too much vm space via mmap can run into problems with rlimits,
+ * oom due to page table creation, etc.  We will still try it, if directed
+ * by the command-line option, but not by default. Unless we're running a
+ * target address space of 32 or fewer bits on a host with 64 bits.
  */
 max_reserved_va = MAX_RESERVED_VA(cpu);
 if (reserved_va != 0) {
-- 
2.43.0




[PULL 0/3] Bsd user misc 2024q2 patches

2024-06-09 Thread Warner Losh
The following changes since commit 3e246da2c3f85298b52f8a1154b832acf36aa656:

  Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging 
(2024-06-08 07:40:08 -0700)

are available in the Git repository at:

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

for you to fetch changes up to cb4c259052cbc5dd04c17d963c789360cb8fe340:

  bsd-user: Catch up to run-time reserved_va math (2024-06-09 10:30:25 -0600)


bsd-user: Baby Steps towards eliminating qemu_host_page_size, et al

First baby-steps towards eliminating qemu_host_page_size: tackle the reserve_va
calculation (which is easier to copy from linux-user than to fix).
-BEGIN PGP SIGNATURE-
Comment: GPGTools - https://gpgtools.org

iQIzBAABCgAdFiEEIDX4lLAKo898zeG3bBzRKH2wEQAFAmZl3pgACgkQbBzRKH2w
EQBfpg//U4YdJAA0H4okwPtowP1wIK1gpWvVd5FIN17pCXLKT4FR4efhWeEnQh8U
+dXvkCpX/MnhBkStYoGZBmYe1rNKkEAn8BPCsQqX4y3af5RzKyKWo0gZXOjN3L9e
ixmeFcg/7BTwnSbcO02xd9BOPPaRiFBDSidh28gr/1sxpXRxlbQHzIUpTBncDaN6
4w5DnF+b1RFHCz05ytrP517cj7E32Ig9S/cVMmBd1pGJiLnHiOp/peMprCL6tnI+
YNBzttCbRPNH2z0zVd9En/hDnVirGPYX+LXg0Djkw3I+stJj4jwbJTuDG+5Lzghp
YrYfiU6x7OG9ywjFJgY1/pExVT1cwkNjuGCXL+F4R49R5LfIEHq5/MlQp+tjpYYO
g5WmpiLnFpFosmXIPJmxr16zqm2sLD+P0Jr/kdIz58fTWmIQeKwi/Vu/73h4kxST
vjBbhC3eg56lQDaospc4h8+RehmI6LdSWYx0kxv2JKpXH3lQPqsDSrOcm9hEbWYS
DeV++vkyQcXrbCnwomfxG1U+dVYBlJ1L1wClxc/1WD9KxXXJIwlvGmIu3o3c2+xj
BM6eRe3evWioqdqhc2lY+XxATwbIUxiect6ml+F6E0KJxlm3Ajqy6qw49G+uhZxa
XWUEIYGDd6/xHMlBeo6FKUpe/Ez/i3eCFXr4AD4iO7AtTuukrO4=
=3EaH
-END PGP SIGNATURE-

----

Warner Losh (3):
  linux-user: Adjust comment to reflect the code.
  bsd-user: port linux-user:ff8a8bbc2ad1 for variable page sizes
  bsd-user: Catch up to run-time reserved_va math

 bsd-user/main.c   | 51 ---
 linux-user/main.c |  8 
 2 files changed, 43 insertions(+), 16 deletions(-)

-- 
2.43.0




[PULL 3/3] bsd-user: Catch up to run-time reserved_va math

2024-06-09 Thread Warner Losh
Catch up to linux-user's 8f67b9c694d0, 13c13397556a, 2f7828b57293, and
95059f9c313a by Richard Henderson which made reserved_va a run-time
calculation, defaulting to nothing except in the case of 64-bit host
32-bit target. Also include the adjustment of the comment heading that
work submitted in the same patch stream. Since this is a direct copy,
squash it into one patch rather than follow the Linux evolution since
breaking this down further at this point doesn't make sense for this
"new code".

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 bsd-user/main.c | 39 +++
 1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index d685734d087..dcad266c2c9 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -77,25 +77,16 @@ bool have_guest_base;
 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
 #  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
   (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
-#   define MAX_RESERVED_VA  0xul
+#   define MAX_RESERVED_VA(CPU)  0xul
 #  else
-#   define MAX_RESERVED_VA  ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
+#   define MAX_RESERVED_VA(CPU)  ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
 #  endif
 # else
-#  define MAX_RESERVED_VA  0
+#  define MAX_RESERVED_VA(CPU)  0
 # endif
 #endif
 
-/*
- * That said, reserving *too* much vm space via mmap can run into problems
- * with rlimits, oom due to page table creation, etc.  We will still try it,
- * if directed by the command-line option, but not by default.
- */
-#if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
-unsigned long reserved_va = MAX_RESERVED_VA;
-#else
 unsigned long reserved_va;
-#endif
 
 const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
@@ -293,6 +284,7 @@ int main(int argc, char **argv)
 envlist_t *envlist = NULL;
 char *argv0 = NULL;
 int host_page_size;
+unsigned long max_reserved_va;
 
 adjust_ssize();
 
@@ -493,6 +485,29 @@ int main(int argc, char **argv)
 cpu_reset(cpu);
 thread_cpu = cpu;
 
+/*
+ * Reserving too much vm space via mmap can run into problems with rlimits,
+ * oom due to page table creation, etc.  We will still try it, if directed
+ * by the command-line option, but not by default. Unless we're running a
+ * target address space of 32 or fewer bits on a host with 64 bits.
+ */
+max_reserved_va = MAX_RESERVED_VA(cpu);
+if (reserved_va != 0) {
+if ((reserved_va + 1) % host_page_size) {
+char *s = size_to_str(host_page_size);
+fprintf(stderr, "Reserved virtual address not aligned mod %s\n", 
s);
+g_free(s);
+exit(EXIT_FAILURE);
+}
+if (max_reserved_va && reserved_va > max_reserved_va) {
+fprintf(stderr, "Reserved virtual address too big\n");
+exit(EXIT_FAILURE);
+}
+} else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
+/* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
+reserved_va = max_reserved_va;
+}
+
 if (getenv("QEMU_STRACE")) {
 do_strace = 1;
 }
-- 
2.43.0




[PULL 2/3] bsd-user: port linux-user:ff8a8bbc2ad1 for variable page sizes

2024-06-09 Thread Warner Losh
Bring in Richard Henderson's ff8a8bbc2ad1 to finalize the page size to
allow TARGET_PAGE_BITS_VARY. bsd-user's "blitz" fork has aarch64
support, which is now variable page size. Add support for it here, even
though it's effectively a nop in upstream qemu.

Signed-off-by: Warner Losh 
Reviewed-by: Richard Henderson 
---
 bsd-user/main.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 29a629d8779..d685734d087 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -46,6 +46,7 @@
 #include "crypto/init.h"
 #include "qemu/guest-random.h"
 #include "gdbstub/user.h"
+#include "exec/page-vary.h"
 
 #include "host-os.h"
 #include "target_arch_cpu.h"
@@ -291,6 +292,7 @@ int main(int argc, char **argv)
 char **target_environ, **wrk;
 envlist_t *envlist = NULL;
 char *argv0 = NULL;
+int host_page_size;
 
 adjust_ssize();
 
@@ -476,6 +478,16 @@ int main(int argc, char **argv)
  opt_one_insn_per_tb, &error_abort);
 ac->init_machine(NULL);
 }
+
+/*
+ * Finalize page size before creating CPUs.
+ * This will do nothing if !TARGET_PAGE_BITS_VARY.
+ * The most efficient setting is to match the host.
+ */
+host_page_size = qemu_real_host_page_size();
+set_preferred_target_page_bits(ctz32(host_page_size));
+finalize_target_page_bits();
+
 cpu = cpu_create(cpu_type);
 env = cpu_env(cpu);
 cpu_reset(cpu);
-- 
2.43.0




[PATCH 1/3] linux-user: Adjust comment to reflect the code.

2024-06-06 Thread Warner Losh
If the user didn't specify a reserved_va, there's an else for 64-bit
host 32-bit (or fewer) target to reserve 32-bits of address
space. Update the comments to reflect this, and rejustify comment
to 80 columns.

Signed-off-by: Warner Losh 
---
 linux-user/main.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/linux-user/main.c b/linux-user/main.c
index 94e4c47f052..94c99a1366f 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -814,10 +814,10 @@ int main(int argc, char **argv, char **envp)
 thread_cpu = cpu;
 
 /*
- * Reserving too much vm space via mmap can run into problems
- * with rlimits, oom due to page table creation, etc.  We will
- * still try it, if directed by the command-line option, but
- * not by default.
+ * Reserving too much vm space via mmap can run into problems with rlimits,
+ * oom due to page table creation, etc.  We will still try it, if directed
+ * by the command-line option, but not by default. Unless we're running a
+ * target address space of 32 or fewer bits on a host with 64 bits.
  */
 max_reserved_va = MAX_RESERVED_VA(cpu);
 if (reserved_va != 0) {
-- 
2.43.0




[PATCH 2/3] bsd-user: port linux-user:ff8a8bbc2ad1 for variable page sizes

2024-06-06 Thread Warner Losh
Bring in Richard Henderson's ff8a8bbc2ad1 to finalize the page size to
allow TARGET_PAGE_BITS_VARY. bsd-user's "blitz" fork has aarch64
support, which is now variable page size. Add support for it here, even
though it's effectively a nop in upstream qemu.

Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index 29a629d8779..d685734d087 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -46,6 +46,7 @@
 #include "crypto/init.h"
 #include "qemu/guest-random.h"
 #include "gdbstub/user.h"
+#include "exec/page-vary.h"
 
 #include "host-os.h"
 #include "target_arch_cpu.h"
@@ -291,6 +292,7 @@ int main(int argc, char **argv)
 char **target_environ, **wrk;
 envlist_t *envlist = NULL;
 char *argv0 = NULL;
+int host_page_size;
 
 adjust_ssize();
 
@@ -476,6 +478,16 @@ int main(int argc, char **argv)
  opt_one_insn_per_tb, &error_abort);
 ac->init_machine(NULL);
 }
+
+/*
+ * Finalize page size before creating CPUs.
+ * This will do nothing if !TARGET_PAGE_BITS_VARY.
+ * The most efficient setting is to match the host.
+ */
+host_page_size = qemu_real_host_page_size();
+set_preferred_target_page_bits(ctz32(host_page_size));
+finalize_target_page_bits();
+
 cpu = cpu_create(cpu_type);
 env = cpu_env(cpu);
 cpu_reset(cpu);
-- 
2.43.0




[PATCH 3/3] bsd-user: Catch up to run-time reserved_va math

2024-06-06 Thread Warner Losh
Catch up to linux-user's 8f67b9c694d0, 13c13397556a, 2f7828b57293, and
95059f9c313a by Richard Henderson which made reserved_va a run-time
calculation, defaulting to nothing except in the case of 64-bit host
32-bit target. Also include the adjustment of the comment heading that
work submitted in the same patch stream. Since this is a direct copy,
squash it into one patch rather than follow the Linux evolution since
breaking this down further at this point doesn't make sense for this
"new code".

Signed-off-by: Warner Losh 
---
 bsd-user/main.c | 39 +++
 1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/bsd-user/main.c b/bsd-user/main.c
index d685734d087..dcad266c2c9 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -77,25 +77,16 @@ bool have_guest_base;
 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
 #  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
   (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
-#   define MAX_RESERVED_VA  0xul
+#   define MAX_RESERVED_VA(CPU)  0xul
 #  else
-#   define MAX_RESERVED_VA  ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
+#   define MAX_RESERVED_VA(CPU)  ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
 #  endif
 # else
-#  define MAX_RESERVED_VA  0
+#  define MAX_RESERVED_VA(CPU)  0
 # endif
 #endif
 
-/*
- * That said, reserving *too* much vm space via mmap can run into problems
- * with rlimits, oom due to page table creation, etc.  We will still try it,
- * if directed by the command-line option, but not by default.
- */
-#if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
-unsigned long reserved_va = MAX_RESERVED_VA;
-#else
 unsigned long reserved_va;
-#endif
 
 const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release;
@@ -293,6 +284,7 @@ int main(int argc, char **argv)
 envlist_t *envlist = NULL;
 char *argv0 = NULL;
 int host_page_size;
+unsigned long max_reserved_va;
 
 adjust_ssize();
 
@@ -493,6 +485,29 @@ int main(int argc, char **argv)
 cpu_reset(cpu);
 thread_cpu = cpu;
 
+/*
+ * Reserving too much vm space via mmap can run into problems with rlimits,
+ * oom due to page table creation, etc.  We will still try it, if directed
+ * by the command-line option, but not by default. Unless we're running a
+ * target address space of 32 or fewer bits on a host with 64 bits.
+ */
+max_reserved_va = MAX_RESERVED_VA(cpu);
+if (reserved_va != 0) {
+if ((reserved_va + 1) % host_page_size) {
+char *s = size_to_str(host_page_size);
+fprintf(stderr, "Reserved virtual address not aligned mod %s\n", 
s);
+g_free(s);
+exit(EXIT_FAILURE);
+}
+if (max_reserved_va && reserved_va > max_reserved_va) {
+fprintf(stderr, "Reserved virtual address too big\n");
+exit(EXIT_FAILURE);
+}
+} else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
+/* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
+reserved_va = max_reserved_va;
+}
+
 if (getenv("QEMU_STRACE")) {
 do_strace = 1;
 }
-- 
2.43.0




[PATCH 0/3] bsd-user: Baby Steps towards eliminating qemu_host_page_size, et al

2024-06-06 Thread Warner Losh
First baby-steps towards eliminating qemu_host_page_size: tackle the reserve_va
calculation (which is easier to copy from linux-user than to fix).

Warner Losh (3):
  linux-user: Adjust comment to reflect the code.
  bsd-user: port linux-user:ff8a8bbc2ad1 for variable page sizes
  bsd-user: Catch up to run-time reserved_va math

 bsd-user/main.c   | 51 ---
 linux-user/main.c |  8 
 2 files changed, 43 insertions(+), 16 deletions(-)

-- 
2.43.0




  1   2   3   4   5   6   7   8   9   10   >