ifconfig(8): always print the mtu, don't hide it on "bridges"

2022-02-21 Thread David Gwynne
this lets ifconfig show the MTU on interfaces like nvgre, vxlan, etc.
they currently don't show it because they also implement a bridge ioctl,
so ifconfig thinks they're a bridge.

why ifconfig hides the mtu on bridges looks to be a hold over from when
brconfig was merged into ifconfig. if we dont want bridge(4) to report
an mtu, then i can make bridge(4) itself hide the mtu or stop setting
the mtu.

found by jason tubnor.

ok?

Index: ifconfig.c
===
RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v
retrieving revision 1.451
diff -u -p -r1.451 ifconfig.c
--- ifconfig.c  23 Nov 2021 19:13:45 -  1.451
+++ ifconfig.c  22 Feb 2022 05:38:48 -
@@ -1027,11 +1027,7 @@ getinfo(struct ifreq *ifr, int create)
metric = 0;
else
metric = ifr->ifr_metric;
-#ifdef SMALL
if (ioctl(sock, SIOCGIFMTU, (caddr_t)ifr) == -1)
-#else
-   if (is_bridge() || ioctl(sock, SIOCGIFMTU, (caddr_t)ifr) == -1)
-#endif
mtu = 0;
else
mtu = ifr->ifr_mtu;



Re: Import seq(1) from FreeBSD

2022-02-21 Thread Greg Steuck
"Todd C. Miller"  writes:

> On Sun, 13 Feb 2022 12:07:31 -0800, Greg Steuck wrote:
>
>> I noticed that despite the OKs the code didn't submitted. Should we
>> revive this diff now and continue in the tree?
>>
>> I have vested interest: I don't want to fix up lang/ghc test suite which
>> is full of seq(1).
>
> Here's an updated diff that removes the -t option which is not
> present in either the GNU or Plan9 versions.  I've added -h and -v
> flag support to be more compatible with the GNU version.
>
> Like the GNU version, this seq always uses a newline terminator and
> only displays the separator between numbers (not at the end).  This
> is different from the NetBSD version.

OK gnezdo@

Thank you very much!

>
>  - todd
>
> Index: usr.bin/seq/Makefile
> ===
> RCS file: usr.bin/seq/Makefile
> diff -N usr.bin/seq/Makefile
> --- /dev/null 1 Jan 1970 00:00:00 -
> +++ usr.bin/seq/Makefile  16 Nov 2020 03:52:57 -
> @@ -0,0 +1,8 @@
> +#$OpenBSD$
> +
> +PROG=seq
> +CFLAGS+= -Wall
> +LDADD+=  -lm
> +DPADD+=  ${LIBM}
> +
> +.include 
> Index: usr.bin/seq/seq.1
> ===
> RCS file: usr.bin/seq/seq.1
> diff -N usr.bin/seq/seq.1
> --- /dev/null 1 Jan 1970 00:00:00 -
> +++ usr.bin/seq/seq.1 21 Feb 2022 18:05:31 -
> @@ -0,0 +1,197 @@
> +.\"  $OpenBSD$
> +.\"
> +.\" Copyright (c) 2005 The NetBSD Foundation, Inc.
> +.\" All rights reserved.
> +.\"
> +.\" This code is derived from software contributed to The NetBSD Foundation
> +.\" by Brian Ginsbach.
> +.\"
> +.\" Redistribution and use in source and binary forms, with or without
> +.\" modification, are permitted provided that the following conditions
> +.\" are met:
> +.\" 1. Redistributions of source code must retain the above copyright
> +.\"notice, this list of conditions and the following disclaimer.
> +.\" 2. Redistributions in binary form must reproduce the above copyright
> +.\"notice, this list of conditions and the following disclaimer in the
> +.\"documentation and/or other materials provided with the distribution.
> +.\"
> +.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
> +.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
> LIMITED
> +.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
> PARTICULAR
> +.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
> +.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> +.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> +.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> +.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> +.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> +.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
> THE
> +.\" POSSIBILITY OF SUCH DAMAGE.
> +.\"
> +.Dd $Mdocdate$
> +.Dt SEQ 1
> +.Os
> +.Sh NAME
> +.Nm seq
> +.Nd print sequences of numbers
> +.Sh SYNOPSIS
> +.Nm
> +.Op Fl hvw
> +.Op Fl f Ar format
> +.Op Fl s Ar string
> +.Op Ar first Op Ar incr
> +.Ar last
> +.Sh DESCRIPTION
> +The
> +.Nm
> +utility prints a sequence of numbers, one per line
> +.Pq default ,
> +from
> +.Ar first
> +.Pq default 1 ,
> +to near
> +.Ar last
> +as possible, in increments of
> +.Ar incr
> +.Pq default 1 .
> +When
> +.Ar first
> +is larger than
> +.Ar last ,
> +the default
> +.Ar incr
> +is -1.
> +.Pp
> +All numbers are interpreted as floating point.
> +.Pp
> +Normally integer values are printed as decimal integers.
> +.Pp
> +The
> +.Nm
> +utility accepts the following options:
> +.Bl -tag -width Ar
> +.It Fl f Ar format , Fl -format Ar format
> +Use a
> +.Xr printf 3
> +style
> +.Ar format
> +to print each number.
> +Only the
> +.Cm A ,
> +.Cm a ,
> +.Cm E ,
> +.Cm e ,
> +.Cm F ,
> +.Cm f ,
> +.Cm G ,
> +.Cm g ,
> +and
> +.Cm %
> +conversion characters are valid, along with any optional
> +flags and an optional numeric minimum field width or precision.
> +The
> +.Ar format
> +can contain character escape sequences in backslash notation as
> +defined in
> +.St -ansiC .
> +The default is
> +.Cm %g .
> +.It Fl h , Fl -help
> +Display the program usage and exit.
> +.It Fl s Ar string , Fl -separator Ar string
> +Use
> +.Ar string
> +to separate numbers.
> +The
> +.Ar string
> +can contain character escape sequences in backslash notation as
> +defined in
> +.St -ansiC .
> +The default is
> +.Cm \en .
> +.It Fl v , Fl -version
> +Display the verion number and exit.
> +.It Fl w , Fl -fixed-width
> +Equalize the widths of all numbers by padding with zeros as necessary.
> +This option has no effect with the
> +.Fl f
> +option.
> +If any sequence numbers will be printed in exponential notation,
> +the default conversion is changed to
> +.Cm %e .
> +.El
> +.Sh EXIT STATUS
> +.Ex -std
> +.Sh EXAMPLES
> 

Re: Improve ddb's stack trace printing on riscv64

2022-02-21 Thread Mark Kettenis
> Date: Mon, 21 Feb 2022 17:34:14 +
> From: Visa Hankala 
> 
> On riscv64, ddb's stack unwinder performs poorly. The main problem is
> that the exception handlers use a frame structure (trapframe) that
> differs from the typical call frame.
> 
> The following patch does several adjustments, including:
> 
> * Detect and handle exception frames. (Alternatively, the relevant part
>   of struct trapframe could be adjusted to match with callframe, as has
>   been done on amd64 and i386.)
> 
> * Don't offset ra with -4 when printing function addresses. It is safer
>   to print the original address and let the user decipher the meaning,
>   in part because the fixed offset is wrong with compressed instructions.
> 
> * Stop if frame pointer is badly misaligned.
> 
> * Print the frame address where unwinding stopped.
> 
> OK?

looks reasonable to me

ok kettenis@


> Index: arch/riscv64/riscv64/db_trace.c
> ===
> RCS file: src/sys/arch/riscv64/riscv64/db_trace.c,v
> retrieving revision 1.4
> diff -u -p -r1.4 db_trace.c
> --- arch/riscv64/riscv64/db_trace.c   9 Jul 2021 20:59:51 -   1.4
> +++ arch/riscv64/riscv64/db_trace.c   21 Feb 2022 17:22:43 -
> @@ -43,6 +43,9 @@
>  #include 
>  #include 
>  
> +extern unsigned char cpu_exception_handler_supervisor[];
> +extern unsigned char cpu_exception_handler_user[];
> +
>  db_regs_t ddb_regs;
>  
>  #define INKERNEL(va) (((vaddr_t)(va)) & (1ULL << 63))
> @@ -51,7 +54,7 @@ void
>  db_stack_trace_print(db_expr_t addr, int have_addr, db_expr_t count,
>  char *modif, int (*pr)(const char *, ...))
>  {
> - vaddr_t frame, lastframe, ra, lastra, sp;
> + vaddr_t frame, lastframe, ra, sp, subr;
>   charc, *cp = modif;
>   db_expr_t   offset;
>   Elf_Sym *   sym;
> @@ -68,60 +71,73 @@ db_stack_trace_print(db_expr_t addr, int
>   }
>  
>   if (!have_addr) {
> - sp = ddb_regs.tf_sp;
>   ra = ddb_regs.tf_ra;
> - lastra = ddb_regs.tf_ra;
>   frame = ddb_regs.tf_s[0];
>   } else {
>   sp = addr;
>   db_read_bytes(sp - 16, sizeof(vaddr_t), (char *));
>   db_read_bytes(sp - 8, sizeof(vaddr_t), (char *));
> - lastra = 0;
>   }
>  
> - while (count-- && frame != 0) {
> - lastframe = frame;
> -
> - sym = db_search_symbol(lastra, DB_STGY_ANY, );
> - db_symbol_values(sym, , NULL);
> + while (count != 0 && frame != 0) {
> + if (INKERNEL(frame)) {
> + sym = db_search_symbol(ra, DB_STGY_ANY, );
> + db_symbol_values(sym, , NULL);
> + } else {
> + sym = NULL;
> + name = NULL;
> + }
>  
>   if (name == NULL || strcmp(name, "end") == 0) {
> - (*pr)("%llx at 0x%lx", lastra, ra - 4);
> + (*pr)("%llx() at 0x%lx", ra, ra);
>   } else {
>   (*pr)("%s() at ", name);
> - db_printsym(ra - 4, DB_STGY_PROC, pr);
> + db_printsym(ra, DB_STGY_PROC, pr);
>   }
>   (*pr)("\n");
>  
> - // can we detect traps ?
> - db_read_bytes(frame - 16, sizeof(vaddr_t), (char *));
> - if (frame == 0)
> + if ((frame & 0x7) != 0) {
> + (*pr)("bad frame pointer: 0x%lx\n", frame);
>   break;
> - lastra = ra;
> - db_read_bytes(frame - 8, sizeof(vaddr_t), (char *));
> + }
> +
> + subr = 0;
> + if (sym != NULL)
> + subr = ra - (vaddr_t)offset;
>  
> -#if 0
> - if (name != NULL) {
> - if ((strcmp (name, "handle_el0_irq") == 0) ||
> - (strcmp (name, "handle_el1_irq") == 0)) {
> - (*pr)("--- interrupt ---\n");
> - } else if (
> - (strcmp (name, "handle_el0_sync") == 0) ||
> - (strcmp (name, "handle_el1_sync") == 0)) {
> - (*pr)("--- trap ---\n");
> + lastframe = frame;
> + if (subr == (vaddr_t)cpu_exception_handler_supervisor ||
> + subr == (vaddr_t)cpu_exception_handler_user) {
> + struct trapframe *tf = (struct trapframe *)frame;
> +
> + db_read_bytes((vaddr_t)>tf_ra, sizeof(ra),
> + (char *));
> + db_read_bytes((vaddr_t)>tf_s[0], sizeof(frame),
> + (char *));
> + } else {
> + db_read_bytes(frame - 16, sizeof(frame),
> + (char *));
> + if (frame == 0)
> + break;
> + if ((frame & 

OpenBSD Errata: February 21, 2022 (slaacd)

2022-02-21 Thread Alexander Bluhm
Errata patches for slaacd have been released for OpenBSD 6.9 and 7.0.

Binary updates for the amd64, i386 and arm64 platform are available
via the syspatch utility.  Source code patches can be found on the
respective errata page:

  https://www.openbsd.org/errata69.html
  https://www.openbsd.org/errata70.html



Re: Zero frame pointer in cpu_fork() on riscv64

2022-02-21 Thread Mark Kettenis
> Date: Mon, 21 Feb 2022 17:37:22 +
> From: Visa Hankala 
> 
> When setting up the stack for a (kernel) thread on riscv64, zero the
> frame pointer in switchframe so that the frame chain terminates cleanly.
> At the moment, the unwinding of kernel thread stacks stops to "bad frame
> pointer" error.
> 
> OK?

s2 is callee-saved just like s0/s1, so that should indeed work

ok kettenis@

> Index: arch/riscv64/riscv64/cpuswitch.S
> ===
> RCS file: src/sys/arch/riscv64/riscv64/cpuswitch.S,v
> retrieving revision 1.5
> diff -u -p -r1.5 cpuswitch.S
> --- arch/riscv64/riscv64/cpuswitch.S  2 Jul 2021 14:58:33 -   1.5
> +++ arch/riscv64/riscv64/cpuswitch.S  21 Feb 2022 17:22:43 -
> @@ -100,8 +100,8 @@ ENTRY(proc_trampoline)
>   li  a0, IPL_NONE
>   la  t0, spllower
>   jalrt0
> - mv  a0, s1
> - jalrs0
> + mv  a0, s2
> + jalrs1
>   la  t0, syscall_return
>   jr  t0
>  END(cpu_switch)
> Index: arch/riscv64/riscv64/vm_machdep.c
> ===
> RCS file: src/sys/arch/riscv64/riscv64/vm_machdep.c,v
> retrieving revision 1.7
> diff -u -p -r1.7 vm_machdep.c
> --- arch/riscv64/riscv64/vm_machdep.c 30 Jun 2021 22:20:56 -  1.7
> +++ arch/riscv64/riscv64/vm_machdep.c 21 Feb 2022 17:22:43 -
> @@ -92,8 +92,9 @@ cpu_fork(struct proc *p1, struct proc *p
>   tf->tf_sstatus &= ~(SSTATUS_SPP); /* Enter user mode. */
>  
>   sf = (struct switchframe *)tf - 1;
> - sf->sf_s[0] = (uint64_t)func;
> - sf->sf_s[1] = (uint64_t)arg;
> + sf->sf_s[0] = 0;/* Terminate chain of call frames. */
> + sf->sf_s[1] = (uint64_t)func;
> + sf->sf_s[2] = (uint64_t)arg;
>   sf->sf_ra = (u_int64_t)_trampoline;
>   pcb->pcb_sp = (uint64_t)sf;
>  }
> 
> 



Re: Import seq(1) from FreeBSD

2022-02-21 Thread Todd C . Miller
On Sun, 13 Feb 2022 12:07:31 -0800, Greg Steuck wrote:

> I noticed that despite the OKs the code didn't submitted. Should we
> revive this diff now and continue in the tree?
>
> I have vested interest: I don't want to fix up lang/ghc test suite which
> is full of seq(1).

Here's an updated diff that removes the -t option which is not
present in either the GNU or Plan9 versions.  I've added -h and -v
flag support to be more compatible with the GNU version.

Like the GNU version, this seq always uses a newline terminator and
only displays the separator between numbers (not at the end).  This
is different from the NetBSD version.

 - todd

Index: usr.bin/seq/Makefile
===
RCS file: usr.bin/seq/Makefile
diff -N usr.bin/seq/Makefile
--- /dev/null   1 Jan 1970 00:00:00 -
+++ usr.bin/seq/Makefile16 Nov 2020 03:52:57 -
@@ -0,0 +1,8 @@
+#  $OpenBSD$
+
+PROG=  seq
+CFLAGS+= -Wall
+LDADD+=-lm
+DPADD+=${LIBM}
+
+.include 
Index: usr.bin/seq/seq.1
===
RCS file: usr.bin/seq/seq.1
diff -N usr.bin/seq/seq.1
--- /dev/null   1 Jan 1970 00:00:00 -
+++ usr.bin/seq/seq.1   21 Feb 2022 18:05:31 -
@@ -0,0 +1,197 @@
+.\"$OpenBSD$
+.\"
+.\" Copyright (c) 2005 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Brian Ginsbach.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"notice, this list of conditions and the following disclaimer in the
+.\"documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd $Mdocdate$
+.Dt SEQ 1
+.Os
+.Sh NAME
+.Nm seq
+.Nd print sequences of numbers
+.Sh SYNOPSIS
+.Nm
+.Op Fl hvw
+.Op Fl f Ar format
+.Op Fl s Ar string
+.Op Ar first Op Ar incr
+.Ar last
+.Sh DESCRIPTION
+The
+.Nm
+utility prints a sequence of numbers, one per line
+.Pq default ,
+from
+.Ar first
+.Pq default 1 ,
+to near
+.Ar last
+as possible, in increments of
+.Ar incr
+.Pq default 1 .
+When
+.Ar first
+is larger than
+.Ar last ,
+the default
+.Ar incr
+is -1.
+.Pp
+All numbers are interpreted as floating point.
+.Pp
+Normally integer values are printed as decimal integers.
+.Pp
+The
+.Nm
+utility accepts the following options:
+.Bl -tag -width Ar
+.It Fl f Ar format , Fl -format Ar format
+Use a
+.Xr printf 3
+style
+.Ar format
+to print each number.
+Only the
+.Cm A ,
+.Cm a ,
+.Cm E ,
+.Cm e ,
+.Cm F ,
+.Cm f ,
+.Cm G ,
+.Cm g ,
+and
+.Cm %
+conversion characters are valid, along with any optional
+flags and an optional numeric minimum field width or precision.
+The
+.Ar format
+can contain character escape sequences in backslash notation as
+defined in
+.St -ansiC .
+The default is
+.Cm %g .
+.It Fl h , Fl -help
+Display the program usage and exit.
+.It Fl s Ar string , Fl -separator Ar string
+Use
+.Ar string
+to separate numbers.
+The
+.Ar string
+can contain character escape sequences in backslash notation as
+defined in
+.St -ansiC .
+The default is
+.Cm \en .
+.It Fl v , Fl -version
+Display the verion number and exit.
+.It Fl w , Fl -fixed-width
+Equalize the widths of all numbers by padding with zeros as necessary.
+This option has no effect with the
+.Fl f
+option.
+If any sequence numbers will be printed in exponential notation,
+the default conversion is changed to
+.Cm %e .
+.El
+.Sh EXIT STATUS
+.Ex -std
+.Sh EXAMPLES
+Generate a sequence from 1 to 3 (inclusive) with a default increment of 1:
+.Bd -literal -offset indent
+# seq 1 3
+1
+2
+3
+.Ed
+.Pp
+Generate a sequence from 3 to 1 (inclusive) with a default increment of -1:
+.Bd -literal -offset indent
+# seq 3 1
+3
+2
+1
+.Ed
+.Pp
+Generate a sequence from 0 to 0.1 (inclusive) with an increment of 0.05 and 
padding
+with leading zeroes.
+.Bd 

Zero frame pointer in cpu_fork() on riscv64

2022-02-21 Thread Visa Hankala
When setting up the stack for a (kernel) thread on riscv64, zero the
frame pointer in switchframe so that the frame chain terminates cleanly.
At the moment, the unwinding of kernel thread stacks stops to "bad frame
pointer" error.

OK?

Index: arch/riscv64/riscv64/cpuswitch.S
===
RCS file: src/sys/arch/riscv64/riscv64/cpuswitch.S,v
retrieving revision 1.5
diff -u -p -r1.5 cpuswitch.S
--- arch/riscv64/riscv64/cpuswitch.S2 Jul 2021 14:58:33 -   1.5
+++ arch/riscv64/riscv64/cpuswitch.S21 Feb 2022 17:22:43 -
@@ -100,8 +100,8 @@ ENTRY(proc_trampoline)
li  a0, IPL_NONE
la  t0, spllower
jalrt0
-   mv  a0, s1
-   jalrs0
+   mv  a0, s2
+   jalrs1
la  t0, syscall_return
jr  t0
 END(cpu_switch)
Index: arch/riscv64/riscv64/vm_machdep.c
===
RCS file: src/sys/arch/riscv64/riscv64/vm_machdep.c,v
retrieving revision 1.7
diff -u -p -r1.7 vm_machdep.c
--- arch/riscv64/riscv64/vm_machdep.c   30 Jun 2021 22:20:56 -  1.7
+++ arch/riscv64/riscv64/vm_machdep.c   21 Feb 2022 17:22:43 -
@@ -92,8 +92,9 @@ cpu_fork(struct proc *p1, struct proc *p
tf->tf_sstatus &= ~(SSTATUS_SPP); /* Enter user mode. */
 
sf = (struct switchframe *)tf - 1;
-   sf->sf_s[0] = (uint64_t)func;
-   sf->sf_s[1] = (uint64_t)arg;
+   sf->sf_s[0] = 0;/* Terminate chain of call frames. */
+   sf->sf_s[1] = (uint64_t)func;
+   sf->sf_s[2] = (uint64_t)arg;
sf->sf_ra = (u_int64_t)_trampoline;
pcb->pcb_sp = (uint64_t)sf;
 }



Improve ddb's stack trace printing on riscv64

2022-02-21 Thread Visa Hankala
On riscv64, ddb's stack unwinder performs poorly. The main problem is
that the exception handlers use a frame structure (trapframe) that
differs from the typical call frame.

The following patch does several adjustments, including:

* Detect and handle exception frames. (Alternatively, the relevant part
  of struct trapframe could be adjusted to match with callframe, as has
  been done on amd64 and i386.)

* Don't offset ra with -4 when printing function addresses. It is safer
  to print the original address and let the user decipher the meaning,
  in part because the fixed offset is wrong with compressed instructions.

* Stop if frame pointer is badly misaligned.

* Print the frame address where unwinding stopped.

OK?

Index: arch/riscv64/riscv64/db_trace.c
===
RCS file: src/sys/arch/riscv64/riscv64/db_trace.c,v
retrieving revision 1.4
diff -u -p -r1.4 db_trace.c
--- arch/riscv64/riscv64/db_trace.c 9 Jul 2021 20:59:51 -   1.4
+++ arch/riscv64/riscv64/db_trace.c 21 Feb 2022 17:22:43 -
@@ -43,6 +43,9 @@
 #include 
 #include 
 
+extern unsigned char   cpu_exception_handler_supervisor[];
+extern unsigned char   cpu_exception_handler_user[];
+
 db_regs_t ddb_regs;
 
 #define INKERNEL(va)   (((vaddr_t)(va)) & (1ULL << 63))
@@ -51,7 +54,7 @@ void
 db_stack_trace_print(db_expr_t addr, int have_addr, db_expr_t count,
 char *modif, int (*pr)(const char *, ...))
 {
-   vaddr_t frame, lastframe, ra, lastra, sp;
+   vaddr_t frame, lastframe, ra, sp, subr;
charc, *cp = modif;
db_expr_t   offset;
Elf_Sym *   sym;
@@ -68,60 +71,73 @@ db_stack_trace_print(db_expr_t addr, int
}
 
if (!have_addr) {
-   sp = ddb_regs.tf_sp;
ra = ddb_regs.tf_ra;
-   lastra = ddb_regs.tf_ra;
frame = ddb_regs.tf_s[0];
} else {
sp = addr;
db_read_bytes(sp - 16, sizeof(vaddr_t), (char *));
db_read_bytes(sp - 8, sizeof(vaddr_t), (char *));
-   lastra = 0;
}
 
-   while (count-- && frame != 0) {
-   lastframe = frame;
-
-   sym = db_search_symbol(lastra, DB_STGY_ANY, );
-   db_symbol_values(sym, , NULL);
+   while (count != 0 && frame != 0) {
+   if (INKERNEL(frame)) {
+   sym = db_search_symbol(ra, DB_STGY_ANY, );
+   db_symbol_values(sym, , NULL);
+   } else {
+   sym = NULL;
+   name = NULL;
+   }
 
if (name == NULL || strcmp(name, "end") == 0) {
-   (*pr)("%llx at 0x%lx", lastra, ra - 4);
+   (*pr)("%llx() at 0x%lx", ra, ra);
} else {
(*pr)("%s() at ", name);
-   db_printsym(ra - 4, DB_STGY_PROC, pr);
+   db_printsym(ra, DB_STGY_PROC, pr);
}
(*pr)("\n");
 
-   // can we detect traps ?
-   db_read_bytes(frame - 16, sizeof(vaddr_t), (char *));
-   if (frame == 0)
+   if ((frame & 0x7) != 0) {
+   (*pr)("bad frame pointer: 0x%lx\n", frame);
break;
-   lastra = ra;
-   db_read_bytes(frame - 8, sizeof(vaddr_t), (char *));
+   }
+
+   subr = 0;
+   if (sym != NULL)
+   subr = ra - (vaddr_t)offset;
 
-#if 0
-   if (name != NULL) {
-   if ((strcmp (name, "handle_el0_irq") == 0) ||
-   (strcmp (name, "handle_el1_irq") == 0)) {
-   (*pr)("--- interrupt ---\n");
-   } else if (
-   (strcmp (name, "handle_el0_sync") == 0) ||
-   (strcmp (name, "handle_el1_sync") == 0)) {
-   (*pr)("--- trap ---\n");
+   lastframe = frame;
+   if (subr == (vaddr_t)cpu_exception_handler_supervisor ||
+   subr == (vaddr_t)cpu_exception_handler_user) {
+   struct trapframe *tf = (struct trapframe *)frame;
+
+   db_read_bytes((vaddr_t)>tf_ra, sizeof(ra),
+   (char *));
+   db_read_bytes((vaddr_t)>tf_s[0], sizeof(frame),
+   (char *));
+   } else {
+   db_read_bytes(frame - 16, sizeof(frame),
+   (char *));
+   if (frame == 0)
+   break;
+   if ((frame & 0x7) != 0) {
+   (*pr)("bad frame pointer: 0x%lx\n", frame);
+   break;
}
+   

Re: A program compiled with '-pg' option always gets SEGV on its execution.

2022-02-21 Thread Theo de Raadt
Stefan Sperling  wrote:

> Pledge and unveil interfering with profiling is a separate issue which
> is more obvious when it occurs and can easily be worked around by the
> developer.

Basically any privsep method or technology will harm accounting, because
accounting file access makes a poor path choice.



Re: A program compiled with '-pg' option always gets SEGV on its execution.

2022-02-21 Thread Stefan Sperling
On Mon, Feb 21, 2022 at 10:20:17AM +0100, Marc Espie wrote:
> On Mon, Feb 21, 2022 at 05:36:16PM +0900, Yuichiro NAITO wrote:
> > Of course, all programs compiled without '-pg' work fine for me.
> > I found this issue when I profile my application with gprof(1).
> > For example, following example C source code fails to execute on OpenBSD 
> > 7.0.
> 
> Profile is partly broken and has been for a while.
> 
> Compiling with -static and removing any pledge() call allow profiling to work.

Yes, and the proposed patch effectively enables -static if -pg is used.
I don't know if this the best fix. But this issue keeps popping up and
it would be nice to have something that works out of the box.
I would not mind this patch going in.

Pledge and unveil interfering with profiling is a separate issue which
is more obvious when it occurs and can easily be worked around by the
developer.



Re: A program compiled with '-pg' option always gets SEGV on its execution.

2022-02-21 Thread Marc Espie
On Mon, Feb 21, 2022 at 05:36:16PM +0900, Yuichiro NAITO wrote:
> Of course, all programs compiled without '-pg' work fine for me.
> I found this issue when I profile my application with gprof(1).
> For example, following example C source code fails to execute on OpenBSD 7.0.

Profile is partly broken and has been for a while.

Compiling with -static and removing any pledge() call allow profiling to work.



A program compiled with '-pg' option always gets SEGV on its execution.

2022-02-21 Thread Yuichiro NAITO

Of course, all programs compiled without '-pg' work fine for me.
I found this issue when I profile my application with gprof(1).
For example, following example C source code fails to execute on OpenBSD 7.0.

```
$ cat helloworld.c
#include 
int main(int argc, char *argv[])
{
printf("Hello World!\n");
return 0;
}
$ cc helloworld.c
$ ./a.out
Hello World!
$ cc -pg helloworld.c
$ ./a.out
Segmentation fault (core dumped)
$ readelf -h -l -d -u ./a.out
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class: ELF64
  Data:  2's complement, little endian
  Version:   1 (current)
  OS/ABI:UNIX - System V
  ABI Version:   0
  Type:  EXEC (Executable file)
  Machine:   Advanced Micro Devices X86-64
  Version:   0x1
  Entry point address:   0x2045f0
  Start of program headers:  64 (bytes into file)
  Start of section headers:  419600 (bytes into file)
  Flags: 0x0
  Size of this header:   64 (bytes)
  Size of program headers:   56 (bytes)
  Number of program headers: 11
  Size of section headers:   64 (bytes)
  Number of section headers: 29
  Section header string table index: 27

Program Headers:
  Type   Offset VirtAddr   PhysAddr
 FileSizMemSiz  Flags  Align
  PHDR   0x0040 0x00200040 0x00200040
 0x0268 0x0268  R  8
  INTERP 0x02a8 0x002002a8 0x002002a8
 0x0013 0x0013  R  1
  [Requesting program interpreter: /usr/libexec/ld.so]
  LOAD   0x 0x0020 0x0020
 0x35ec 0x35ec  R  1000
  LOAD   0x35f0 0x002045f0 0x002045f0
 0x0001563e 0x0001563e  R E1000
  LOAD   0x00018c30 0x0021ac30 0x0021ac30
 0x0700 0x0700  RW 1000
  LOAD   0x00019330 0x0021c330 0x0021c330
 0x1028 0x6a38  RW 1000
  GNU_RELRO  0x00018c30 0x0021ac30 0x0021ac30
 0x0700 0x13d0  R  1
  GNU_EH_FRAME   0x17a8 0x002017a8 0x002017a8
 0x04bc 0x04bc  R  4
  OPENBSD_RANDOM 0x00018c30 0x0021ac30 0x0021ac30
 0x0068 0x0068  RW 8
  GNU_STACK  0x 0x 0x
 0x 0x  RW 0
  NOTE   0x02bc 0x002002bc 0x002002bc
 0x0018 0x0018  R  4

 Section to Segment mapping:
  Segment Sections...
   00
   01 .interp
   02 .interp .note.openbsd.ident .rodata .eh_frame_hdr .eh_frame
   03 .text .init .fini
   04 .openbsd.randomdata .jcr .ctors .dtors .preinit_array .data.rel.ro 
.got
   05 .data .bss
   06 .openbsd.randomdata .jcr .ctors .dtors .preinit_array .data.rel.ro 
.got
   07 .eh_frame_hdr
   08 .openbsd.randomdata
   09
   10 .note.openbsd.ident

There is no dynamic section in this file.

There are no unwind sections in this file.
```

I can see the compiled program is statically linked (Type: EXEC),
and has an INTERP section (/usr/libexec/ld.so), and no dynamic sections.

If a elf binary has an INTERP section, the kernel invokes dynamic linker 
(ld.so).
Ld.so tries to link dynamic link libraries, but it fails because no dynamic
sections in the elf binary.

In 'src/libexec/ld.so/loader.c', `_dl_boot` function reads a dynamic section and
initialize `exe_obj` pointer. If there is no dynamic sections, `exe_obj` is 
still `NULL`.
After reading header sections, `exe_obj` is referred to store parsed values,
and it fails by NULL pointer access.

```
501exe_obj = NULL;

(snip)

511for (loop = 0; loop < dl_data[AUX_phnum]; loop++) {
512switch (phdp->p_type) {
513case PT_PHDR:
514exe_loff = (Elf_Addr)dl_data[AUX_phdr] - 
phdp->p_vaddr;
515us += exe_loff;
516DL_DEB(("exe load offset:  0x%lx\n", exe_loff));
517break;
518case PT_DYNAMIC:
519minva = TRUNC_PG(minva);
520maxva = ROUND_PG(maxva);
521exe_obj = _dl_finalize_object(argv[0] ? argv[0] : "",
522