Re: [RFC PATCH 2/2] objtool/powerpc: Enhance objtool to fixup alternate feature relative addresses

2024-06-03 Thread Sathvika Vasireddy

Hi Nathan,

On 4/23/24 5:58 AM, Nathan Chancellor wrote:

Hi Sathvika,

On Mon, Apr 22, 2024 at 02:52:06PM +0530, Sathvika Vasireddy wrote:

Implement build-time fixup of alternate feature relative addresses for
the out-of-line (else) patch code. Initial posting to achieve the same
using another tool can be found at [1]. Idea is to implement this using
objtool instead of introducing another tool since it already has elf
parsing and processing covered.

Introduce --ftr-fixup as an option to objtool to do feature fixup at
build-time.

Couple of issues and warnings encountered while implementing feature
fixup using objtool are as follows:

1. libelf is creating corrupted vmlinux file after writing necessary
changes to the file. Due to this, kexec is not able to load new
kernel.

It gives the following error:
 ELF Note corrupted !
 Cannot determine the file type of vmlinux

To fix this issue, after opening vmlinux file, make a call to
elf_flagelf (e, ELF_C_SET, ELF_F_LAYOUT). This instructs libelf not
to touch the segment and section layout. It informs the library
that the application will take responsibility for the layout of the
file and that the library should not insert any padding between
sections.

2. Fix can't find starting instruction warnings when run on vmlinux

Objtool throws a lot of can't find starting instruction warnings
when run on vmlinux with --ftr-fixup option.

These warnings are seen because find_insn() function looks for
instructions at offsets that are relative to the start of the section.
In case of individual object files (.o), there are no can't find
starting instruction warnings seen because the actual offset
associated with an instruction is itself a relative offset since the
sections start at offset 0x0.

However, in case of vmlinux, find_insn() function fails to find
instructions at the actual offset associated with an instruction
since the sections in vmlinux do not start at offset 0x0. Due to
this, find_insn() will look for absolute offset and not the relative
offset. This is resulting in a lot of can't find starting instruction
warnings when objtool is run on vmlinux.

To fix this, pass offset that is relative to the start of the section
to find_insn().

find_insn() is also looking for symbols of size 0. But, objtool does
not store empty STT_NOTYPE symbols in the rbtree. Due to this,
for empty symbols, objtool is throwing can't find starting
instruction warnings. Fix this by ignoring symbols that are of
size 0 since objtool does not add them to the rbtree.

3. Objtool is throwing unannotated intra-function call warnings
when run on vmlinux with --ftr-fixup option.

One such example:

vmlinux: warning: objtool: .text+0x3d94:
 unannotated intra-function call

.text + 0x3d94 = c0008000 + 3d94 = c00081d4

c00081d4: 45 24 02 48  bl c002a618


c002a610 :
c002a610:   0e 01 4c 3c addis   r2,r12,270
 c002a610: R_PPC64_REL16_HA.TOC.
c002a614:   f0 6c 42 38 addir2,r2,27888
 c002a614: R_PPC64_REL16_LO.TOC.+0x4
c002a618:   a6 02 08 7c mflrr0

This is happening because we should be looking for destination
symbols that are at absolute offsets instead of relative offsets.
After fixing dest_off to point to absolute offset, there are still
a lot of these warnings shown.

In the above example, objtool is computing the destination
offset to be c002a618, which points to a completely
different instruction. find_call_destination() is looking for this
offset and failing. Instead, we should be looking for destination
offset c002a610 which points to system_reset_exception
function.

Even after fixing the way destination offset is computed, and
after looking for dest_off - 0x8 in cases where the original offset
is not found, there are still a lot of unannotated intra-function
call warnings generated. This is due to symbols that are not
properly annotated.

So, for now, as a hack to curb these warnings, do not emit
unannotated intra-function call warnings when objtool is run
with --ftr-fixup option.

TODO:
This patch enables build time feature fixup only for powerpc little
endian configs. There are boot failures with big endian configs.
Posting this as an initial RFC to get some review comments while I work
on big endian issues.

[1]
https://lore.kernel.org/linuxppc-dev/20170521010130.13552-1-npig...@gmail.com/

Co-developed-by: Nicholas Piggin 
Signed-off-by: Nicholas Piggin 
Signed-off-by: Sathvika Vasireddy 

When I build this series with LLVM 14 [1] (due to an issue I report
below), I am getting a crash when CONFIG_FTR_FIXUP_SELFTEST is disabled.

diff --git a/arch/powerpc/configs/ppc64_defconfig 
b/arch/powerpc/configs/ppc64_defconfig
index 544a65fda77b..95d2906ec814 100644
--- a/arch/powerpc/configs/ppc64_defconfig
+++ b/arch/powerpc/configs/ppc64_defconfig
@@ -427,7 +427,6

Re: [RFC PATCH 1/2] objtool: Run objtool only if either of the config options are selected

2024-04-22 Thread Sathvika Vasireddy

Hi Masahiro, thanks for reviewing.

On 4/22/24 5:39 PM, Masahiro Yamada wrote:

On Mon, Apr 22, 2024 at 6:25 PM Sathvika Vasireddy  wrote:

Currently, when objtool is enabled and none of the supported options
are triggered, kernel build errors out with the below error:
error: objtool: At least one command required.


Then, I think CONFIG_OBJTOOL should be disabled.
A subsequent patch introduces --ftr-fixup as an option to objtool to do 
feature fixup at build-time via CONFIG_HAVE_OBJTOOL_FTR_FIXUP option. If 
CONFIG_OBJTOOL is not selected, then objtool cannot be used to pass 
--ftr-fixup option.


In cases where none of the supported options (like --mcount on powerpc 
for example) is triggered, but still require --ftr-fixup option to be 
passed to objtool, we see "error: objtool: At least one command 
required" errors. So, to address this, run only when either of the 
config options are selected.


Thanks,
Sathvika

[RFC PATCH 2/2] objtool/powerpc: Enhance objtool to fixup alternate feature relative addresses

2024-04-22 Thread Sathvika Vasireddy
Implement build-time fixup of alternate feature relative addresses for
the out-of-line (else) patch code. Initial posting to achieve the same
using another tool can be found at [1]. Idea is to implement this using
objtool instead of introducing another tool since it already has elf
parsing and processing covered.

Introduce --ftr-fixup as an option to objtool to do feature fixup at
build-time.

Couple of issues and warnings encountered while implementing feature
fixup using objtool are as follows:

1. libelf is creating corrupted vmlinux file after writing necessary
changes to the file. Due to this, kexec is not able to load new
kernel.

It gives the following error:
ELF Note corrupted !
Cannot determine the file type of vmlinux

To fix this issue, after opening vmlinux file, make a call to
elf_flagelf (e, ELF_C_SET, ELF_F_LAYOUT). This instructs libelf not
to touch the segment and section layout. It informs the library
that the application will take responsibility for the layout of the
file and that the library should not insert any padding between
sections.

2. Fix can't find starting instruction warnings when run on vmlinux

Objtool throws a lot of can't find starting instruction warnings
when run on vmlinux with --ftr-fixup option.

These warnings are seen because find_insn() function looks for
instructions at offsets that are relative to the start of the section.
In case of individual object files (.o), there are no can't find
starting instruction warnings seen because the actual offset
associated with an instruction is itself a relative offset since the
sections start at offset 0x0.

However, in case of vmlinux, find_insn() function fails to find
instructions at the actual offset associated with an instruction
since the sections in vmlinux do not start at offset 0x0. Due to
this, find_insn() will look for absolute offset and not the relative
offset. This is resulting in a lot of can't find starting instruction
warnings when objtool is run on vmlinux.

To fix this, pass offset that is relative to the start of the section
to find_insn().

find_insn() is also looking for symbols of size 0. But, objtool does
not store empty STT_NOTYPE symbols in the rbtree. Due to this,
for empty symbols, objtool is throwing can't find starting
instruction warnings. Fix this by ignoring symbols that are of
size 0 since objtool does not add them to the rbtree.

3. Objtool is throwing unannotated intra-function call warnings
when run on vmlinux with --ftr-fixup option.

One such example:

vmlinux: warning: objtool: .text+0x3d94:
unannotated intra-function call

.text + 0x3d94 = c0008000 + 3d94 = c00081d4

c00081d4: 45 24 02 48  bl c002a618


c002a610 :
c002a610:   0e 01 4c 3c addis   r2,r12,270
c002a610: R_PPC64_REL16_HA.TOC.
c002a614:   f0 6c 42 38 addir2,r2,27888
c002a614: R_PPC64_REL16_LO.TOC.+0x4
c002a618:   a6 02 08 7c mflrr0

This is happening because we should be looking for destination
symbols that are at absolute offsets instead of relative offsets.
After fixing dest_off to point to absolute offset, there are still
a lot of these warnings shown.

In the above example, objtool is computing the destination
offset to be c002a618, which points to a completely
different instruction. find_call_destination() is looking for this
offset and failing. Instead, we should be looking for destination
offset c002a610 which points to system_reset_exception
function.

Even after fixing the way destination offset is computed, and
after looking for dest_off - 0x8 in cases where the original offset
is not found, there are still a lot of unannotated intra-function
call warnings generated. This is due to symbols that are not
properly annotated.

So, for now, as a hack to curb these warnings, do not emit
unannotated intra-function call warnings when objtool is run
with --ftr-fixup option.

TODO:
This patch enables build time feature fixup only for powerpc little
endian configs. There are boot failures with big endian configs.
Posting this as an initial RFC to get some review comments while I work
on big endian issues.

[1]
https://lore.kernel.org/linuxppc-dev/20170521010130.13552-1-npig...@gmail.com/

Co-developed-by: Nicholas Piggin 
Signed-off-by: Nicholas Piggin 
Signed-off-by: Sathvika Vasireddy 
---
 arch/Kconfig  |   3 +
 arch/powerpc/Kconfig  |   5 +
 arch/powerpc/Makefile |   5 +
 arch/powerpc/include/asm/feature-fixups.h |  11 +-
 arch/powerpc/kernel/vmlinux.lds.S |  14 +-
 arch/powerpc/lib/feature-fixups.c |  13 +
 scripts/Makefile.lib  |   7 +
 scripts/Makefile.vmlinux  |  15 +-
 tools/objtool/arch/powerpc/special.c  | 329 ++
 tools/objtool/arch/x86/special.c

[RFC PATCH 1/2] objtool: Run objtool only if either of the config options are selected

2024-04-22 Thread Sathvika Vasireddy
Currently, when objtool is enabled and none of the supported options
are triggered, kernel build errors out with the below error:
error: objtool: At least one command required.

To address this, ensure that objtool is run only when either of the
config options are selected.

Signed-off-by: Sathvika Vasireddy 
---
 scripts/Makefile.lib | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3179747cbd2c..c65bb0fbd136 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -286,7 +286,10 @@ objtool-args = $(objtool-args-y)   
\
 
 delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
 
+ifneq ($(objtool-args-y),)
 cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool-args) $@)
+endif
+
 cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard 
$(objtool))' ; } >> $(dot-target).cmd)
 
 endif # CONFIG_OBJTOOL
-- 
2.34.1



[RFC PATCH] powerpc/Kconfig: Select FUNCTION_ALIGNMENT_4B

2023-03-06 Thread Sathvika Vasireddy
Commit d49a0626216b95 ("arch: Introduce CONFIG_FUNCTION_ALIGNMENT")
introduced a generic function-alignment infrastructure. Move to using
FUNCTION_ALIGNMENT_4B on powerpc, to use the same alignment as that of the
existing _GLOBAL macro.

Signed-off-by: Sathvika Vasireddy 

Note:
Given that alignment beyond the minimum requirement may be desirable,
should we instead select FUNCTION_ALIGNMENT_16B for ppc64 and select
FUNCTION_ALIGNMENT_4B/8B for ppc32?

>From vmlinux (pseries_le_defconfig) symbol offsets, it looks like most of
the ppc64 symbols are being aligned to a 16B boundary, but there are a few
which are not.

Currently, size of vmlinux (built with pseries_le_defconfig) is 47090kB.
With FUNCTION_ALIGNMENT_4B selected, size of vmlinux is 47152kB.
With FUNCTION_ALIGNMENT_16B selected, size of vmlinux is 47152kB.

Currently, size of vmlinux (built with powernv_defconfig) is 42852kB.
With FUNCTION_ALIGNMENT_4B selected, size of vmlinux is 42911kB.
With FUNCTION_ALIGNMENT_16B selected, size of vmlinux is 42977kB.

I am wondering if we should use the same alignment as that of
_GLOBAL macro or have alignment set to a 8B/16B boundary.
Please let me know your thoughts on the same. Thanks!
---
 arch/powerpc/Kconfig   | 1 +
 arch/powerpc/include/asm/linkage.h | 3 ---
 2 files changed, 1 insertion(+), 3 deletions(-)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index a6c4407d3ec8..ac3f80c0db36 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -174,6 +174,7 @@ config PPC
select DYNAMIC_FTRACE   if FUNCTION_TRACER
select EDAC_ATOMIC_SCRUB
select EDAC_SUPPORT
+   select FUNCTION_ALIGNMENT_4B
select GENERIC_ATOMIC64 if PPC32
select GENERIC_CLOCKEVENTS_BROADCASTif SMP
select GENERIC_CMOS_UPDATE
diff --git a/arch/powerpc/include/asm/linkage.h 
b/arch/powerpc/include/asm/linkage.h
index b88d1d2cf304..b71b9582e754 100644
--- a/arch/powerpc/include/asm/linkage.h
+++ b/arch/powerpc/include/asm/linkage.h
@@ -4,9 +4,6 @@
 
 #include 
 
-#define __ALIGN.align 2
-#define __ALIGN_STR".align 2"
-
 #ifdef CONFIG_PPC64_ELF_ABI_V1
 #define cond_syscall(x) \
asm ("\t.weak " #x "\n\t.set " #x ", sys_ni_syscall\n"  \
-- 
2.31.1



Re: [PATCH 1/2] arch/powerpc/kvm: kvmppc_hv_entry: remove .global scope

2023-02-24 Thread Sathvika Vasireddy

On 23/02/23 10:39, Kautuk Consul wrote:


Hi Sathvika,

Just one question though. Went through the code again and I think
that this place shouldn't be proper to insert a SYM_FUNC_END
because we haven't entered the guest at this point and the name
of the function is kvmppc_hv_entry which  I think implies that
this SYM_FUNC_END should be at some place after the HRFI_TO_GUEST.

What do you think ?

Any updates on this ? Is there any other way to avoid this warning ?
Hmm, to mark the end of the kvmppc_hv_entry function, I think 
SYM_FUNC_END(kvmppc_hv_entry) should be placed before the next symbol, 
which is kvmppc_got_guest() in this case.


However, if you think it needs to be put at a different place, then it 
does not make sense to have any other symbols before that. You may want 
to consider checking if other macros like SYM_INNER_LABEL() can be used.


- Sathvika



Re: [PATCH 1/2] arch/powerpc/kvm: kvmppc_hv_entry: remove .global scope

2023-02-20 Thread Sathvika Vasireddy



On 20/02/23 12:58, Kautuk Consul wrote:

Hi Sathvika,

(Sorry didn't include list in earlier email.)

On Mon, Feb 20, 2023 at 12:35:09PM +0530, Sathvika Vasireddy wrote:

Hi Kautuk,

On 20/02/23 10:53, Kautuk Consul wrote:

kvmppc_hv_entry isn't called from anywhere other than
book3s_hv_rmhandlers.S itself. Removing .global scope for
this function.

Signed-off-by: Kautuk Consul 
---
   arch/powerpc/kvm/book3s_hv_rmhandlers.S | 1 -
   1 file changed, 1 deletion(-)

diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S 
b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index acf80915f406..7e063fde7adc 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -502,7 +502,6 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
*   
 *

*/
-.global kvmppc_hv_entry
   kvmppc_hv_entry:
/* Required state:

I see the following objtool warning with this patch applied.
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x48:
unannotated intra-function call

Annotating kvmppc_hv_entry symbol with SYM_FUNC_START_LOCAL and SYM_FUNC_END
macros should help fix this warning.

Not sure where to put the SYM_FUNC_END annotation.
Will the following do:

 ld  r0, VCPU_GPR(R0)(r4)
 ld  r2, VCPU_GPR(R2)(r4)
 ld  r3, VCPU_GPR(R3)(r4)
 ld  r4, VCPU_GPR(R4)(r4)
 HRFI_TO_GUEST
 b   .

SYM_FUNC_END(kvmppc_hv_entry)

secondary_too_late:
 li  r12, 0

?

Thanks.


Placing SYM_FUNC_END(kvmppc_hv_entry) before kvmppc_got_guest() should do:

@@ -502,12 +500,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
* *
*/

-.global kvmppc_hv_entry
-kvmppc_hv_entry:
+SYM_FUNC_START_LOCAL(kvmppc_hv_entry)

    /* Required state:
 *
-    * R4 = vcpu pointer (or NULL)
 * MSR = ~IR|DR
 * R13 = PACA
 * R1 = host R1
@@ -525,6 +521,8 @@ kvmppc_hv_entry:
    li  r6, KVM_GUEST_MODE_HOST_HV
    stb r6, HSTATE_IN_GUEST(r13)

+   ld  r4, HSTATE_KVM_VCPU(r13)
+
 #ifdef CONFIG_KVM_BOOK3S_HV_P8_TIMING
    /* Store initial timestamp */
    cmpdi   r4, 0
@@ -619,6 +617,8 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
    /* Do we have a guest vcpu to run? */
 10:    cmpdi   r4, 0
    beq kvmppc_primary_no_guest
+SYM_FUNC_END(kvmppc_hv_entry)
+
 kvmppc_got_guest:
    /* Increment yield count if they have a VPA */
    ld  r3, VCPU_VPA(r4)


Thanks,
Sathvika


Re: [PATCH 1/2] arch/powerpc/kvm: kvmppc_hv_entry: remove .global scope

2023-02-19 Thread Sathvika Vasireddy

Hi Kautuk,

On 20/02/23 10:53, Kautuk Consul wrote:

kvmppc_hv_entry isn't called from anywhere other than
book3s_hv_rmhandlers.S itself. Removing .global scope for
this function.

Signed-off-by: Kautuk Consul 
---
  arch/powerpc/kvm/book3s_hv_rmhandlers.S | 1 -
  1 file changed, 1 deletion(-)

diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S 
b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index acf80915f406..7e063fde7adc 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -502,7 +502,6 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
   *
*
   
*/
  
-.global kvmppc_hv_entry

  kvmppc_hv_entry:
  
  	/* Required state:

I see the following objtool warning with this patch applied.
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x48: 
unannotated intra-function call


Annotating kvmppc_hv_entry symbol with SYM_FUNC_START_LOCAL and 
SYM_FUNC_END macros should help fix this warning.


Thanks,
Sathvika




[PATCH] powerpc/64: Fix unannotated intra-function call warning

2023-02-16 Thread Sathvika Vasireddy
objtool throws the following warning:
  arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x6128:
  unannotated intra-function call

Fix the warning by annotating start_initialization_book3s symbol with the
SYM_FUNC_START_LOCAL and SYM_FUNC_END macros.

Reported-by: Stephen Rothwell 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/head_64.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index 3a7266fa8a18..1febb56ebaeb 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -472,7 +472,7 @@ SYM_FUNC_START_LOCAL(__mmu_off)
b   .   /* prevent speculative execution */
 SYM_FUNC_END(__mmu_off)
 
-start_initialization_book3s:
+SYM_FUNC_START_LOCAL(start_initialization_book3s)
mflrr25
 
/* Setup some critical 970 SPRs before switching MMU off */
@@ -494,6 +494,7 @@ start_initialization_book3s:
 
mtlrr25
blr
+SYM_FUNC_END(start_initialization_book3s)
 #endif
 
 /*
-- 
2.31.1



Re: [PATCH AUTOSEL 6.1 18/38] powerpc/kvm: Fix unannotated intra-function call warning

2023-02-10 Thread Sathvika Vasireddy

Hi Sasha,

On 09/02/23 16:44, Sasha Levin wrote:

From: Sathvika Vasireddy 

[ Upstream commit fe6de81b610e5d0b9d2231acff2de74a35482e7d ]

objtool throws the following warning:
   arch/powerpc/kvm/booke.o: warning: objtool: kvmppc_fill_pt_regs+0x30:
   unannotated intra-function call

Fix the warning by setting the value of 'nip' using the _THIS_IP_ macro,
without using an assembly bl/mflr sequence to save the instruction
pointer.

Reported-by: kernel test robot 
Suggested-by: Michael Ellerman 
Signed-off-by: Sathvika Vasireddy 
Signed-off-by: Michael Ellerman 
Link: https://lore.kernel.org/r/20230128124158.1066251-1...@linux.ibm.com
Signed-off-by: Sasha Levin 
---
  arch/powerpc/kvm/booke.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/kvm/booke.c b/arch/powerpc/kvm/booke.c
index 7b4920e9fd263..3852209989f04 100644
--- a/arch/powerpc/kvm/booke.c
+++ b/arch/powerpc/kvm/booke.c
@@ -912,16 +912,15 @@ static int kvmppc_handle_debug(struct kvm_vcpu *vcpu)
  
  static void kvmppc_fill_pt_regs(struct pt_regs *regs)

  {
-   ulong r1, ip, msr, lr;
+   ulong r1, msr, lr;
  
  	asm("mr %0, 1" : "=r"(r1));

asm("mflr %0" : "=r"(lr));
asm("mfmsr %0" : "=r"(msr));
-   asm("bl 1f; 1: mflr %0" : "=r"(ip));
  
  	memset(regs, 0, sizeof(*regs));

regs->gpr[1] = r1;
-   regs->nip = ip;
+   regs->nip = _THIS_IP_;
regs->msr = msr;
regs->link = lr;
  }


Please drop this patch because objtool enablement patches for powerpc 
are not a part of kernel v6.1



Thanks,
Sathvika



Re: [PATCH AUTOSEL 6.1 17/38] powerpc/85xx: Fix unannotated intra-function call warning

2023-02-10 Thread Sathvika Vasireddy

Hi Sasha,

On 09/02/23 16:44, Sasha Levin wrote:

From: Sathvika Vasireddy 

[ Upstream commit 8afffce6aa3bddc940ac1909627ff1e772b6cbf1 ]

objtool throws the following warning:
   arch/powerpc/kernel/head_85xx.o: warning: objtool: .head.text+0x1a6c:
   unannotated intra-function call

Fix the warning by annotating KernelSPE symbol with SYM_FUNC_START_LOCAL
and SYM_FUNC_END macros.

Reported-by: kernel test robot 
Signed-off-by: Sathvika Vasireddy 
Signed-off-by: Michael Ellerman 
Link: https://lore.kernel.org/r/20230128124138.1066176-1...@linux.ibm.com
Signed-off-by: Sasha Levin 
---
  arch/powerpc/kernel/head_85xx.S | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/head_85xx.S b/arch/powerpc/kernel/head_85xx.S
index 52c0ab416326a..d3939849f4550 100644
--- a/arch/powerpc/kernel/head_85xx.S
+++ b/arch/powerpc/kernel/head_85xx.S
@@ -862,7 +862,7 @@ _GLOBAL(load_up_spe)
   * SPE unavailable trap from kernel - print a message, but let
   * the task use SPE in the kernel until it returns to user mode.
   */
-KernelSPE:
+SYM_FUNC_START_LOCAL(KernelSPE)
lwz r3,_MSR(r1)
orisr3,r3,MSR_SPE@h
stw r3,_MSR(r1) /* enable use of SPE after return */
@@ -879,6 +879,7 @@ KernelSPE:
  #endif
.align  4,0
  
+SYM_FUNC_END(KernelSPE)

  #endif /* CONFIG_SPE */
  
  /*


Please drop this patch because objtool enablement patches for powerpc 
are not a part of kernel v6.1.


Thanks,
Sathvika


[PATCH] powerpc/kvm: Fix objtool warning for unannotated intra-function call in booke.o

2023-01-28 Thread Sathvika Vasireddy
Objtool throws the following warning:
arch/powerpc/kvm/booke.o: warning: objtool: kvmppc_fill_pt_regs+0x30: 
unannotated intra-function call

Fix this warning by allowing the function to set the value of 'nip' field
using _THIS_IP_ macro, without having to use an additional assembly
instruction to save the instruction pointer.

Reported-by: kernel test robot 
Suggested-by: Michael Ellerman 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kvm/booke.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/kvm/booke.c b/arch/powerpc/kvm/booke.c
index 0dce93ccaadf..e89281d3ba28 100644
--- a/arch/powerpc/kvm/booke.c
+++ b/arch/powerpc/kvm/booke.c
@@ -912,16 +912,15 @@ static int kvmppc_handle_debug(struct kvm_vcpu *vcpu)
 
 static void kvmppc_fill_pt_regs(struct pt_regs *regs)
 {
-   ulong r1, ip, msr, lr;
+   ulong r1, msr, lr;
 
asm("mr %0, 1" : "=r"(r1));
asm("mflr %0" : "=r"(lr));
asm("mfmsr %0" : "=r"(msr));
-   asm("bl 1f; 1: mflr %0" : "=r"(ip));
 
memset(regs, 0, sizeof(*regs));
regs->gpr[1] = r1;
-   regs->nip = ip;
+   regs->nip = _THIS_IP_;
regs->msr = msr;
regs->link = lr;
 }
-- 
2.31.1



[PATCH] powerpc: Fix objtool warning for unannotated intra-function call in head_85xx.o

2023-01-28 Thread Sathvika Vasireddy
Objtool throws the following warning:
arch/powerpc/kernel/head_85xx.o: warning: objtool: .head.text+0x1a6c:
unannotated intra-function call

Fix this warning by annotating KernelSPE symbol with SYM_FUNC_START_LOCAL
and SYM_FUNC_END macros.

Reported-by: kernel test robot 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/head_85xx.S | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/head_85xx.S b/arch/powerpc/kernel/head_85xx.S
index d438ca74e96c..fdbee1093e2b 100644
--- a/arch/powerpc/kernel/head_85xx.S
+++ b/arch/powerpc/kernel/head_85xx.S
@@ -864,7 +864,7 @@ _GLOBAL(load_up_spe)
  * SPE unavailable trap from kernel - print a message, but let
  * the task use SPE in the kernel until it returns to user mode.
  */
-KernelSPE:
+SYM_FUNC_START_LOCAL(KernelSPE)
lwz r3,_MSR(r1)
orisr3,r3,MSR_SPE@h
stw r3,_MSR(r1) /* enable use of SPE after return */
@@ -881,6 +881,7 @@ KernelSPE:
 #endif
.align  4,0
 
+SYM_FUNC_END(KernelSPE)
 #endif /* CONFIG_SPE */
 
 /*
-- 
2.31.1



Re: crypto: p10-aes-gcm - Add asm markings necessary for kernel code

2023-01-18 Thread Sathvika Vasireddy

Hi Stephen,

On 18/01/23 08:34, Stephen Rothwell wrote:

Hi Herbert,

On Tue, 17 Jan 2023 15:26:24 +0800 Herbert Xu  
wrote:

On Tue, Jan 17, 2023 at 02:47:47PM +1100, Stephen Rothwell wrote:

Hi all,

After merging the crypto tree, today's linux-next build (powerpc
pseries_le_defconfig) failed like this:

arch/powerpc/crypto/p10_aes_gcm.o: warning: objtool: .text+0x884: unannotated 
intra-function call
arch/powerpc/crypto/aesp8-ppc.o: warning: objtool: aes_p8_set_encrypt_key+0x44: 
unannotated intra-function call
ld: arch/powerpc/crypto/p10_aes_gcm.o: ABI version 1 is not compatible with ABI 
version 2 output
ld: failed to merge target specific data of file 
arch/powerpc/crypto/p10_aes_gcm.o

Caused by commit

   ca68a96c37eb ("crypto: p10-aes-gcm - An accelerated AES/GCM stitched 
implementation")

I have applied the following hack for today.

Thanks Stephen, I'm going to update the previous fix as follows:

I still get:

arch/powerpc/crypto/aesp8-ppc.o: warning: objtool: aes_p8_set_encrypt_key+0x44: 
unannotated intra-function call

from the powerpc pseries_le_defconfig build (which is otherwise ok).


Warnings [1], [2], and [3] are seen with pseries_le_defconfig.

[1] - arch/powerpc/crypto/aesp8-ppc.o: warning: objtool: 
aes_p8_set_encrypt_key+0x44: unannotated intra-function call
[2] - arch/powerpc/crypto/aesp8-ppc.o: warning: objtool: .text+0x2448: 
unannotated intra-function call
[3] - arch/powerpc/crypto/aesp8-ppc.o: warning: objtool: .text+0x2d68: 
unannotated intra-function call

Given that there are no calls to _mcount, one way to address this warning, is 
by skipping objtool from running on arch/powerpc/crypto/aesp8-ppc.o file.
The below diff works for me.

=
diff --git a/arch/powerpc/crypto/Makefile b/arch/powerpc/crypto/Makefile
index 5b8252013abd..d00664c8d761 100644
--- a/arch/powerpc/crypto/Makefile
+++ b/arch/powerpc/crypto/Makefile
@@ -31,3 +31,5 @@ targets += aesp8-ppc.S ghashp8-ppc.S
 
 $(obj)/aesp8-ppc.S $(obj)/ghashp8-ppc.S: $(obj)/%.S: $(src)/%.pl FORCE

$(call if_changed,perl)
+
+OBJECT_FILES_NON_STANDARD_aesp8-ppc.o := y
=


The other way to fix these warnings is by using ANNOTATE_INTRA_FUNCTION_CALL 
macro to indicate that the branch target is valid. And, by annotating symbols 
with SYM_FUNC_START_LOCAL and SYM_FUNC_END macros.
The below diff works for me:

=
diff --git a/arch/powerpc/crypto/aesp8-ppc.pl b/arch/powerpc/crypto/aesp8-ppc.pl
index cdbcf6e13efc..355e0036869a 100644
--- a/arch/powerpc/crypto/aesp8-ppc.pl
+++ b/arch/powerpc/crypto/aesp8-ppc.pl
@@ -80,6 +80,9 @@
 # POWER8[le]   3.96/0.72   0.741.1
 # POWER8[be]   3.75/0.65   0.661.0
 
+print "#include \n";

+print "#include \n";
+
 $flavour = shift;
 
 if ($flavour =~ /64/) {

@@ -185,7 +188,8 @@ Lset_encrypt_key:
lis r0,0xfff0
mfspr   $vrsave,256
mtspr   256,r0
-
+
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  Lconsts
mtlrr11
 
@@ -3039,7 +3043,7 @@ Lxts_enc6x_ret:

.long   0
 
 .align 5

-_aesp8_xts_enc5x:
+SYM_FUNC_START_LOCAL(_aesp8_xts_enc5x)
vcipher $out0,$out0,v24
vcipher $out1,$out1,v24
vcipher $out2,$out2,v24
@@ -3121,6 +3125,7 @@ _aesp8_xts_enc5x:
blr
 .long  0
 .byte  0,12,0x14,0,0,0,0,0
+SYM_FUNC_END(_aesp8_xts_enc5x)
 
 .align 5

 _aesp8_xts_decrypt6x:
@@ -3727,7 +3732,7 @@ Lxts_dec6x_ret:
.long   0
 
 .align 5

-_aesp8_xts_dec5x:
+SYM_FUNC_START_LOCAL(_aesp8_xts_dec5x)
vncipher$out0,$out0,v24
vncipher$out1,$out1,v24
vncipher$out2,$out2,v24
@@ -3809,6 +3814,7 @@ _aesp8_xts_dec5x:
blr
 .long  0
 .byte  0,12,0x14,0,0,0,0,0
+SYM_FUNC_END(_aesp8_xts_dec5x)
 ___
 }} }}}

=


Thanks,
Sathvika



Re: [PATCH] objtool: continue if find_insn() fails in decode_instructions()

2023-01-09 Thread Sathvika Vasireddy



On 09/01/23 22:23, Ingo Molnar wrote:

* Sathvika Vasireddy  wrote:


Hi Ingo, Happy New Year!

Happy New Year to you too! :-)


On 07/01/23 15:51, Ingo Molnar wrote:

* Sathvika Vasireddy  wrote:


Currently, decode_instructions() is failing if it is not able to find
instruction, and this is happening since commit dbcdbdfdf137b4
("objtool: Rework instruction -> symbol mapping") because it is
expecting instruction for STT_NOTYPE symbols.

Due to this, the following objtool warnings are seen:
   [1] arch/powerpc/kernel/optprobes_head.o: warning: objtool: 
optprobe_template_end(): can't find starting instruction
   [2] arch/powerpc/kernel/kvm_emul.o: warning: objtool: kvm_template_end(): 
can't find starting instruction
   [3] arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B(): can't 
find starting instruction

The warnings are thrown because find_insn() is failing for symbols that
are at the end of the file, or at the end of the section. Given how
STT_NOTYPE symbols are currently handled in decode_instructions(),
continue if the instruction is not found, instead of throwing warning
and returning.

Signed-off-by: Naveen N. Rao 
Signed-off-by: Sathvika Vasireddy 

The SOB chain doesn't look valid: is Naveen N. Rao, the first SOB line, the
author of the patch? If yes then a matching From: line is needed.

Or if two people developed the patch, then Co-developed-by should be used:

  Co-developed-by: First Co-Author 
  Signed-off-by: First Co-Author 
  Co-developed-by: Second Co-Author 
  Signed-off-by: Second Co-Author 

[ In this SOB sequence "Second Co-Author" is the one who submits the patch. ]

[ Please only use Co-developed-by if actual lines of code were written by
the co-author that created copyrightable material - it's not a courtesy
tag. Reviewed-by/Acked-by/Tested-by can be used to credit non-code
contributions. ]

Thank you for the clarification, and for bringing these points to my
attention. I'll keep these things in mind. In this case, since both Naveen
N. Rao and I developed the patch, the below tags
are applicable.

     Co-developed-by: First Co-Author 
     Signed-off-by: First Co-Author 
     Co-developed-by: Second Co-Author 
     Signed-off-by: Second Co-Author 

... while filling in your real names & email addresses I suppose. ;-)

Indeed :-)



However, I would be dropping this particular patch, since I think Nick's
patch [1] is better to fix the objtool issue.

[1] - 
https://lore.kernel.org/linuxppc-dev/20221220101323.3119939-1-npig...@gmail.com/

Ok, I'll pick up Nick's fix, with these tags added for the PowerPC
regression aspect and your review:

   Reported-by: Naveen N. Rao 
   Reported-by: Sathvika Vasireddy 
   Acked-by: Sathvika Vasireddy 

To document & credit the efforts of your patch.


Sure, thank you!

- Sathvika



Re: [PATCH] objtool: continue if find_insn() fails in decode_instructions()

2023-01-09 Thread Sathvika Vasireddy

Hi Ingo, Happy New Year!

On 07/01/23 15:51, Ingo Molnar wrote:

* Sathvika Vasireddy  wrote:


Currently, decode_instructions() is failing if it is not able to find
instruction, and this is happening since commit dbcdbdfdf137b4
("objtool: Rework instruction -> symbol mapping") because it is
expecting instruction for STT_NOTYPE symbols.

Due to this, the following objtool warnings are seen:
  [1] arch/powerpc/kernel/optprobes_head.o: warning: objtool: 
optprobe_template_end(): can't find starting instruction
  [2] arch/powerpc/kernel/kvm_emul.o: warning: objtool: kvm_template_end(): 
can't find starting instruction
  [3] arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B(): can't 
find starting instruction

The warnings are thrown because find_insn() is failing for symbols that
are at the end of the file, or at the end of the section. Given how
STT_NOTYPE symbols are currently handled in decode_instructions(),
continue if the instruction is not found, instead of throwing warning
and returning.

Signed-off-by: Naveen N. Rao 
Signed-off-by: Sathvika Vasireddy 

The SOB chain doesn't look valid: is Naveen N. Rao, the first SOB line, the
author of the patch? If yes then a matching From: line is needed.

Or if two people developed the patch, then Co-developed-by should be used:

 Co-developed-by: First Co-Author 
 Signed-off-by: First Co-Author 
 Co-developed-by: Second Co-Author 
 Signed-off-by: Second Co-Author 

[ In this SOB sequence "Second Co-Author" is the one who submits the patch. ]

[ Please only use Co-developed-by if actual lines of code were written by
   the co-author that created copyrightable material - it's not a courtesy
   tag. Reviewed-by/Acked-by/Tested-by can be used to credit non-code
   contributions. ]
Thank you for the clarification, and for bringing these points to my 
attention. I'll keep these things in mind. In this case, since both 
Naveen N. Rao and I developed the patch, the below tags

are applicable.

    Co-developed-by: First Co-Author 
    Signed-off-by: First Co-Author 
    Co-developed-by: Second Co-Author 
    Signed-off-by: Second Co-Author 

However, I would be dropping this particular patch, since I think Nick's 
patch [1] is better to fix the objtool issue.


[1] - 
https://lore.kernel.org/linuxppc-dev/20221220101323.3119939-1-npig...@gmail.com/ 




Thanks for reviewing!

- Sathvika



[PATCH] powerpc/32: Curb objtool unannotated intra-function call warning

2022-12-15 Thread Sathvika Vasireddy
objtool throws the following warning:
arch/powerpc/kexec/relocate_32.o: warning: objtool: .text+0x2bc: unannotated 
intra-function call

Fix this warning by annotating intra-function call, using
ANNOTATE_INTRA_FUNCTION_CALL macro, to indicate that the branch target
is valid.

Reported-by: kernel test robot 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kexec/relocate_32.S | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/powerpc/kexec/relocate_32.S b/arch/powerpc/kexec/relocate_32.S
index d9f0dd9b34ff..104c9911f406 100644
--- a/arch/powerpc/kexec/relocate_32.S
+++ b/arch/powerpc/kexec/relocate_32.S
@@ -8,6 +8,7 @@
  * Author: Suzuki Poulose 
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -349,6 +350,7 @@ write_utlb:
cmpwi   r10, PPC47x_TLB0_4K
bne 0f
li  r10, 0x1000 /* r10 = 4k */
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  1f
 
 0:
-- 
2.31.1



[PATCH] objtool: continue if find_insn() fails in decode_instructions()

2022-12-07 Thread Sathvika Vasireddy
Currently, decode_instructions() is failing if it is not able to find
instruction, and this is happening since commit dbcdbdfdf137b4
("objtool: Rework instruction -> symbol mapping") because it is
expecting instruction for STT_NOTYPE symbols.

Due to this, the following objtool warnings are seen:
 [1] arch/powerpc/kernel/optprobes_head.o: warning: objtool: 
optprobe_template_end(): can't find starting instruction
 [2] arch/powerpc/kernel/kvm_emul.o: warning: objtool: kvm_template_end(): 
can't find starting instruction
 [3] arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B(): can't 
find starting instruction

The warnings are thrown because find_insn() is failing for symbols that
are at the end of the file, or at the end of the section. Given how
STT_NOTYPE symbols are currently handled in decode_instructions(),
continue if the instruction is not found, instead of throwing warning
and returning.

Signed-off-by: Naveen N. Rao 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c | 7 ++-
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 4350be739f4f..bce2be5ebf36 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -430,11 +430,8 @@ static int decode_instructions(struct objtool_file *file)
if (func->return_thunk || func->alias != func)
continue;
 
-   if (!find_insn(file, sec, func->offset)) {
-   WARN("%s(): can't find starting instruction",
-func->name);
-   return -1;
-   }
+   if (!find_insn(file, sec, func->offset))
+   continue;
 
sym_for_each_insn(file, func, insn) {
insn->sym = func;
-- 
2.31.1



Re: linux-next: build warnings after merge of the powerpc-objtool tree

2022-11-30 Thread Sathvika Vasireddy



On 29/11/22 20:58, Christophe Leroy wrote:


Le 29/11/2022 à 16:13, Sathvika Vasireddy a écrit :

Hi all,

On 25/11/22 09:00, Stephen Rothwell wrote:

Hi all,

After merging the powerpc-objtool tree, today's linux-next build (powerpc
pseries_le_defconfig) produced these warnings:

arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B():
can't find starting instruction
arch/powerpc/kernel/optprobes_head.o: warning: objtool:
optprobe_template_end(): can't find starting instruction

I have no idea what started this (they may have been there yesterday).

I was able to recreate the above mentioned warnings with
pseries_le_defconfig and powernv_defconfig. The regression report also
mentions a warning
(https://lore.kernel.org/oe-kbuild-all/202211282102.qur7hhrw-...@intel.com/) 
seen with arch/powerpc/kernel/kvm_emul.S assembly file.

   [1] arch/powerpc/kernel/optprobes_head.o: warning: objtool:
optprobe_template_end(): can't find starting instruction
   [2] arch/powerpc/kernel/kvm_emul.o: warning: objtool:
kvm_template_end(): can't find starting instruction
   [3] arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B():
can't find starting instruction

The warnings [1] and [2] go away after adding 'nop' instruction. Below
diff fixes it for me:

You have to add NOPs just because those labels are at the end of the
files. That's a bit odd.
I think either we are missing some kind of flagging for the symbols, or
objtool has a bug. In both cases, I'm not sure adding an artificial
'nop' is the solution. At least there should be a big hammer warning
explaining why.


I don't see these warnings with powerpc/topic/objtool branch. However, 
they are seen with linux-next master branch.
Commit dbcdbdfdf137b49144204571f1a5e5dc01b8aaad objtool: Rework 
instruction -> symbol mapping in linux-next is resulting in objtool 
can't find starting instruction warnings on powerpc.


Reverting this particular hunk (pasted below), resolves it and we don't 
see the problem anymore.


@@ -427,7 +427,10 @@ static int decode_instructions(struct objtool_file 
*file)

    }

    list_for_each_entry(func, >symbol_list, list) {
-   if (func->type != STT_FUNC || func->alias != func)
+   if (func->type != STT_NOTYPE && func->type != 
STT_FUNC)

+   continue;
+
+   if (func->return_thunk || func->alias != func)
    continue;

    if (!find_insn(file, sec, func->offset)) {

Peterz, can we ignore STT_NOTYPE symbols?


diff --git a/arch/powerpc/kernel/optprobes_head.S
b/arch/powerpc/kernel/optprobes_head.S
index cd4e7bc32609..ea4e3bd82f4f 100644
--- a/arch/powerpc/kernel/optprobes_head.S
+++ b/arch/powerpc/kernel/optprobes_head.S
@@ -134,3 +134,4 @@ optprobe_template_ret:

      .global optprobe_template_end
   optprobe_template_end:
+   nop

diff --git a/arch/powerpc/kernel/kvm_emul.S
b/arch/powerpc/kernel/kvm_emul.S
index 7af6f8b50c5d..41fd664e3ba0 100644
--- a/arch/powerpc/kernel/kvm_emul.S
+++ b/arch/powerpc/kernel/kvm_emul.S
@@ -352,3 +352,4 @@ kvm_tmp_end:

   .global kvm_template_end
   kvm_template_end:
+   nop

For warning [3], objtool is throwing can't find starting instruction
warning because it finds that the symbol (end_first_256B) is zero sized,
and such symbols are not added to the rbtree. I tried to fix it by
adding a 'nop' instruction (pasted diff below), but that resulted in a
kernel build failure.

What's the failure ?



diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index 874efd25cc45..d48850fe159f 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -192,6 +192,7 @@ __secondary_hold:
      EMIT_BUG_ENTRY 0b, __FILE__, __LINE__, 0
   #endif
   CLOSE_FIXED_SECTION(first_256B)
+nop

   /*
    * On server, we include the exception vectors code here as it

diff --git a/arch/powerpc/kernel/exceptions-64s.S
b/arch/powerpc/kernel/exceptions-64s.S
index 26f8fef53c72..f7517d443e9b 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -3104,9 +3104,13 @@ __end_interrupts:
   DEFINE_FIXED_SYMBOL(__end_interrupts, virt_trampolines)

   CLOSE_FIXED_SECTION(real_vectors);
+nop
   CLOSE_FIXED_SECTION(real_trampolines);
+nop
   CLOSE_FIXED_SECTION(virt_vectors);
+nop
   CLOSE_FIXED_SECTION(virt_trampolines);
+nop

What are the NOPs after the CLOSE_FIXED_SECTION() ? You don't explain
them, and I can't see any related warning in the warnings you show.


After fixing arch/powerpc/kernel/head_64.o: warning: objtool: 
end_first_256B(): can't find starting instruction warning, objtool 
started showing more warnings in the same file.

Below is the list of warnings:
 arch/powerpc/kernel/head_64.o: warning: objtool: end_real_vectors(): 
can't find starting instruction
 arch/powerpc/kernel/head_64.o: warning: objtool: 
end_real

Re: linux-next: build warnings after merge of the powerpc-objtool tree

2022-11-29 Thread Sathvika Vasireddy

Hi all,

On 25/11/22 09:00, Stephen Rothwell wrote:

Hi all,

After merging the powerpc-objtool tree, today's linux-next build (powerpc
pseries_le_defconfig) produced these warnings:

arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B(): can't find 
starting instruction
arch/powerpc/kernel/optprobes_head.o: warning: objtool: 
optprobe_template_end(): can't find starting instruction

I have no idea what started this (they may have been there yesterday).
I was able to recreate the above mentioned warnings with 
pseries_le_defconfig and powernv_defconfig. The regression report also 
mentions a warning 
(https://lore.kernel.org/oe-kbuild-all/202211282102.qur7hhrw-...@intel.com/) 
seen with arch/powerpc/kernel/kvm_emul.S assembly file.


 [1] arch/powerpc/kernel/optprobes_head.o: warning: objtool: 
optprobe_template_end(): can't find starting instruction
 [2] arch/powerpc/kernel/kvm_emul.o: warning: objtool: 
kvm_template_end(): can't find starting instruction
 [3] arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B(): 
can't find starting instruction


The warnings [1] and [2] go away after adding 'nop' instruction. Below 
diff fixes it for me:


diff --git a/arch/powerpc/kernel/optprobes_head.S 
b/arch/powerpc/kernel/optprobes_head.S

index cd4e7bc32609..ea4e3bd82f4f 100644
--- a/arch/powerpc/kernel/optprobes_head.S
+++ b/arch/powerpc/kernel/optprobes_head.S
@@ -134,3 +134,4 @@ optprobe_template_ret:

    .global optprobe_template_end
 optprobe_template_end:
+   nop

diff --git a/arch/powerpc/kernel/kvm_emul.S b/arch/powerpc/kernel/kvm_emul.S
index 7af6f8b50c5d..41fd664e3ba0 100644
--- a/arch/powerpc/kernel/kvm_emul.S
+++ b/arch/powerpc/kernel/kvm_emul.S
@@ -352,3 +352,4 @@ kvm_tmp_end:

 .global kvm_template_end
 kvm_template_end:
+   nop

For warning [3], objtool is throwing can't find starting instruction 
warning because it finds that the symbol (end_first_256B) is zero sized, 
and such symbols are not added to the rbtree. I tried to fix it by 
adding a 'nop' instruction (pasted diff below), but that resulted in a 
kernel build failure.


diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index 874efd25cc45..d48850fe159f 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -192,6 +192,7 @@ __secondary_hold:
    EMIT_BUG_ENTRY 0b, __FILE__, __LINE__, 0
 #endif
 CLOSE_FIXED_SECTION(first_256B)
+nop

 /*
  * On server, we include the exception vectors code here as it

diff --git a/arch/powerpc/kernel/exceptions-64s.S 
b/arch/powerpc/kernel/exceptions-64s.S

index 26f8fef53c72..f7517d443e9b 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -3104,9 +3104,13 @@ __end_interrupts:
 DEFINE_FIXED_SYMBOL(__end_interrupts, virt_trampolines)

 CLOSE_FIXED_SECTION(real_vectors);
+nop
 CLOSE_FIXED_SECTION(real_trampolines);
+nop
 CLOSE_FIXED_SECTION(virt_vectors);
+nop
 CLOSE_FIXED_SECTION(virt_trampolines);
+nop

 USE_TEXT_SECTION()

I'm not very sure on how to address this particular warning 
(arch/powerpc/kernel/head_64.o: warning: objtool: end_first_256B(): 
can't find starting instruction). Given that there are no calls to 
_mcount, one workaround is to skip objtool from running on 
arch/powerpc/kernel/head_64.o file. The below diff works for me:


diff --git a/arch/powerpc/kernel/Makefile b/arch/powerpc/kernel/Makefile
index 9b6146056e48..9ef6a040d875 100644
--- a/arch/powerpc/kernel/Makefile
+++ b/arch/powerpc/kernel/Makefile
@@ -219,3 +219,5 @@ $(obj)/vdso64_wrapper.o : $(obj)/vdso/vdso64.so.dbg

 # for cleaning
 subdir- += vdso
+
+OBJECT_FILES_NON_STANDARD_head_64.o := y


Thanks,
Sathvika


[PATCH v6 16/16] objtool/powerpc: Add --mcount specific implementation

2022-11-14 Thread Sathvika Vasireddy
This patch enables objtool --mcount on powerpc, and adds implementation
specific to powerpc.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/Kconfig  |  1 +
 tools/objtool/arch/powerpc/decode.c   | 16 
 tools/objtool/arch/powerpc/include/arch/elf.h |  2 ++
 3 files changed, 19 insertions(+)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 82e3e5e175ae..170a3a354a4a 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -240,6 +240,7 @@ config PPC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
+   select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
index dcd0975cad6b..01cade98b49e 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -9,6 +9,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "_mcount");
+}
+
 unsigned long arch_dest_reloc_offset(int addend)
 {
return addend;
@@ -50,6 +55,17 @@ int arch_decode_instruction(struct objtool_file *file, const 
struct section *sec
typ = INSN_OTHER;
imm = 0;
 
+   switch (opcode) {
+   case 18: /* b[l][a] */
+   if ((insn & 3) == 1) /* bl */
+   typ = INSN_CALL;
+
+   imm = insn & 0x3fc;
+   if (imm & 0x200)
+   imm -= 0x400;
+   break;
+   }
+
if (opcode == 1)
*len = 8;
else
diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h 
b/tools/objtool/arch/powerpc/include/arch/elf.h
index 3c8ebb7d2a6b..73f9ae172fe5 100644
--- a/tools/objtool/arch/powerpc/include/arch/elf.h
+++ b/tools/objtool/arch/powerpc/include/arch/elf.h
@@ -4,5 +4,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_PPC_NONE
+#define R_ABS64 R_PPC64_ADDR64
+#define R_ABS32 R_PPC_ADDR32
 
 #endif /* _OBJTOOL_ARCH_ELF */
-- 
2.31.1



[PATCH v6 15/16] objtool/powerpc: Enable objtool to be built on ppc

2022-11-14 Thread Sathvika Vasireddy
This patch adds [stub] implementations for required functions, inorder
to enable objtool build on powerpc.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
[Christophe Leroy: powerpc: Add missing asm/asm.h for objtool,
Use local variables for type and imm in arch_decode_instruction(),
Adapt len for prefixed instructions.]
Signed-off-by: Christophe Leroy 
---
 arch/powerpc/Kconfig  |  1 +
 arch/powerpc/include/asm/asm.h|  7 ++
 tools/objtool/arch/powerpc/Build  |  2 +
 tools/objtool/arch/powerpc/decode.c   | 85 +++
 .../arch/powerpc/include/arch/cfi_regs.h  | 11 +++
 tools/objtool/arch/powerpc/include/arch/elf.h |  8 ++
 .../arch/powerpc/include/arch/special.h   | 21 +
 tools/objtool/arch/powerpc/special.c  | 19 +
 8 files changed, 154 insertions(+)
 create mode 100644 arch/powerpc/include/asm/asm.h
 create mode 100644 tools/objtool/arch/powerpc/Build
 create mode 100644 tools/objtool/arch/powerpc/decode.c
 create mode 100644 tools/objtool/arch/powerpc/include/arch/cfi_regs.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/elf.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/special.h
 create mode 100644 tools/objtool/arch/powerpc/special.c

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 2ca5418457ed..82e3e5e175ae 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -239,6 +239,7 @@ config PPC
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
+   select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/arch/powerpc/include/asm/asm.h b/arch/powerpc/include/asm/asm.h
new file mode 100644
index ..86f46b604e9a
--- /dev/null
+++ b/arch/powerpc/include/asm/asm.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_POWERPC_ASM_H
+#define _ASM_POWERPC_ASM_H
+
+#define _ASM_PTR   " .long "
+
+#endif /* _ASM_POWERPC_ASM_H */
diff --git a/tools/objtool/arch/powerpc/Build b/tools/objtool/arch/powerpc/Build
new file mode 100644
index ..d24d5636a5b8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/Build
@@ -0,0 +1,2 @@
+objtool-y += decode.o
+objtool-y += special.o
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
new file mode 100644
index ..dcd0975cad6b
--- /dev/null
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+unsigned long arch_dest_reloc_offset(int addend)
+{
+   return addend;
+}
+
+bool arch_callee_saved_reg(unsigned char reg)
+{
+   return false;
+}
+
+int arch_decode_hint_reg(u8 sp_reg, int *base)
+{
+   exit(-1);
+}
+
+const char *arch_nop_insn(int len)
+{
+   exit(-1);
+}
+
+const char *arch_ret_insn(int len)
+{
+   exit(-1);
+}
+
+int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
+   unsigned long offset, unsigned int maxlen,
+   unsigned int *len, enum insn_type *type,
+   unsigned long *immediate,
+   struct list_head *ops_list)
+{
+   unsigned int opcode;
+   enum insn_type typ;
+   unsigned long imm;
+   u32 insn;
+
+   insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
+   opcode = insn >> 26;
+   typ = INSN_OTHER;
+   imm = 0;
+
+   if (opcode == 1)
+   *len = 8;
+   else
+   *len = 4;
+
+   *type = typ;
+   *immediate = imm;
+
+   return 0;
+}
+
+unsigned long arch_jump_destination(struct instruction *insn)
+{
+   return insn->offset + insn->immediate;
+}
+
+void arch_initial_func_cfi_state(struct cfi_init_state *state)
+{
+   int i;
+
+   for (i = 0; i < CFI_NUM_REGS; i++) {
+   state->regs[i].base = CFI_UNDEFINED;
+   state->regs[i].offset = 0;
+   }
+
+   /* initial CFA (call frame address) */
+   state->cfa.base = CFI_SP;
+   state->cfa.offset = 0;
+
+   /* initial LR (return address) */
+   state->regs[CFI_RA].base = CFI_CFA;
+   state->regs[CFI_RA].offset = 0;
+}
diff --git a/tools/objtool/arch/powerpc/include/arch/cfi_regs.h 
b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
new file mode 100644
index ..59638ebeafc8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _OBJTOOL_CFI_REGS_H
+#define _OBJTOOL_CFI_REGS_H
+

[PATCH v6 14/16] objtool: Add arch specific function arch_ftrace_match()

2022-11-14 Thread Sathvika Vasireddy
Add architecture specific function to look for relocation records
pointing to architecture specific symbols.

Suggested-by: Christophe Leroy 
Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/decode.c  | 5 +
 tools/objtool/check.c| 2 +-
 tools/objtool/include/objtool/arch.h | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index 1c253b4b7ce0..af7ad09c926c 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -23,6 +23,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "__fentry__");
+}
+
 static int is_x86_64(const struct elf *elf)
 {
switch (elf->ehdr.e_machine) {
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 2d7153b5d5d1..7580c66ca5c8 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2316,7 +2316,7 @@ static int classify_symbols(struct objtool_file *file)
if (arch_is_rethunk(func))
func->return_thunk = true;
 
-   if (!strcmp(func->name, "__fentry__"))
+   if (arch_ftrace_match(func->name))
func->fentry = true;
 
if (is_profiling_func(func->name))
diff --git a/tools/objtool/include/objtool/arch.h 
b/tools/objtool/include/objtool/arch.h
index beb2f3aa94ff..5149330f400f 100644
--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -69,6 +69,8 @@ struct stack_op {
 
 struct instruction;
 
+int arch_ftrace_match(char *name);
+
 void arch_initial_func_cfi_state(struct cfi_init_state *state);
 
 int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
-- 
2.31.1



[PATCH v6 13/16] objtool: Use macros to define arch specific reloc types

2022-11-14 Thread Sathvika Vasireddy
Make relocation types architecture specific.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Peter Zijlstra (Intel) 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/include/arch/elf.h | 2 ++
 tools/objtool/check.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/include/arch/elf.h 
b/tools/objtool/arch/x86/include/arch/elf.h
index 69cc4264b28a..ac14987cf687 100644
--- a/tools/objtool/arch/x86/include/arch/elf.h
+++ b/tools/objtool/arch/x86/include/arch/elf.h
@@ -2,5 +2,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_X86_64_NONE
+#define R_ABS64 R_X86_64_64
+#define R_ABS32 R_X86_64_32
 
 #endif /* _OBJTOOL_ARCH_ELF */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 752a6ffd5c4c..2d7153b5d5d1 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -885,7 +885,7 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
memset(loc, 0, addrsize);
 
if (elf_add_reloc_to_insn(file->elf, sec, idx,
- R_X86_64_64,
+ addrsize == sizeof(u64) ? R_ABS64 : 
R_ABS32,
  insn->sec, insn->offset))
return -1;
 
-- 
2.31.1



[PATCH v6 12/16] objtool: Read special sections with alts only when specific options are selected

2022-11-14 Thread Sathvika Vasireddy
Call add_special_section_alts() only when stackval or orc or uaccess or
noinstr options are passed to objtool.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 71cf4b4ba1da..752a6ffd5c4c 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2392,9 +2392,11 @@ static int decode_sections(struct objtool_file *file)
 * Must be before add_jump_destinations(), which depends on 'func'
 * being set for alternatives, to enable proper sibling call detection.
 */
-   ret = add_special_section_alts(file);
-   if (ret)
-   return ret;
+   if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
+   ret = add_special_section_alts(file);
+   if (ret)
+   return ret;
+   }
 
ret = add_jump_destinations(file);
if (ret)
-- 
2.31.1



[PATCH v6 11/16] objtool: Add --mnop as an option to --mcount

2022-11-14 Thread Sathvika Vasireddy
Some architectures (powerpc) may not support ftrace locations being nop'ed
out at build time. Introduce CONFIG_HAVE_OBJTOOL_NOP_MCOUNT for objtool, as
a means for architectures to enable nop'ing of ftrace locations. Add --mnop
as an option to objtool --mcount, to indicate support for the same.

Also, make sure that --mnop can be passed as an option to objtool only when
--mcount is passed.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 Makefile|  4 +++-
 arch/x86/Kconfig|  1 +
 kernel/trace/Kconfig|  7 +++
 scripts/Makefile.lib|  3 +++
 tools/objtool/builtin-check.c   | 14 ++
 tools/objtool/check.c   | 19 ++-
 tools/objtool/include/objtool/builtin.h |  1 +
 7 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index ac2ec990422d..196f2795f18f 100644
--- a/Makefile
+++ b/Makefile
@@ -933,7 +933,9 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_CC
   endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
-  CC_FLAGS_USING   += -DCC_USING_NOP_MCOUNT
+  ifdef CONFIG_HAVE_OBJTOOL_NOP_MCOUNT
+CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
+  endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
   ifdef CONFIG_HAVE_C_RECORDMCOUNT
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 67745ceab0db..4be7c06a5d18 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -195,6 +195,7 @@ config X86
select HAVE_CONTEXT_TRACKING_USER_OFFSTACK  if 
HAVE_CONTEXT_TRACKING_USER
select HAVE_C_RECORDMCOUNT
select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
+   select HAVE_OBJTOOL_NOP_MCOUNT  if HAVE_OBJTOOL_MCOUNT
select HAVE_BUILDTIME_MCOUNT_SORT
select HAVE_DEBUG_KMEMLEAK
select HAVE_DMA_CONTIGUOUS
diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
index e9e95c790b8e..2b782321376a 100644
--- a/kernel/trace/Kconfig
+++ b/kernel/trace/Kconfig
@@ -82,6 +82,13 @@ config HAVE_OBJTOOL_MCOUNT
help
  Arch supports objtool --mcount
 
+config HAVE_OBJTOOL_NOP_MCOUNT
+   bool
+   help
+ Arch supports the objtool options --mcount with --mnop.
+ An architecture can select this if it wants to enable nop'ing
+ of ftrace locations.
+
 config HAVE_C_RECORDMCOUNT
bool
help
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3aa384cec76b..658f541c2782 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -256,6 +256,9 @@ objtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK) += 
--hacks=jump_label
 objtool-args-$(CONFIG_HAVE_NOINSTR_HACK)   += --hacks=noinstr
 objtool-args-$(CONFIG_X86_KERNEL_IBT)  += --ibt
 objtool-args-$(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL)   += --mcount
+ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
+objtool-args-$(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT) += --mnop
+endif
 objtool-args-$(CONFIG_UNWINDER_ORC)+= --orc
 objtool-args-$(CONFIG_RETPOLINE)   += --retpoline
 objtool-args-$(CONFIG_RETHUNK) += --rethunk
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 24fbe803a0d3..9bd347d3c244 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -82,6 +82,7 @@ const struct option check_options[] = {
OPT_BOOLEAN(0, "dry-run", , "don't write modifications"),
OPT_BOOLEAN(0, "link", , "object is a linked object"),
OPT_BOOLEAN(0, "module", , "object is part of a kernel 
module"),
+   OPT_BOOLEAN(0, "mnop", , "nop out mcount call sites"),
OPT_BOOLEAN(0, "no-unreachable", _unreachable, "skip 
'unreachable instruction' warnings"),
OPT_BOOLEAN(0, "sec-address", _address, "print section 
addresses in warnings"),
OPT_BOOLEAN(0, "stats", , "print statistics"),
@@ -150,6 +151,16 @@ static bool opts_valid(void)
return false;
 }
 
+static bool mnop_opts_valid(void)
+{
+   if (opts.mnop && !opts.mcount) {
+   ERROR("--mnop requires --mcount");
+   return false;
+   }
+
+   return true;
+}
+
 static bool link_opts_valid(struct objtool_file *file)
 {
if (opts.link)
@@ -198,6 +209,9 @@ int objtool_run(int argc, const char **argv)
if (!file)
return 1;
 
+   if (!mnop_opts_valid())
+   return 1;
+
if (!link_opts_valid(file))
return 1;
 
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index b64518c7c7b4..71cf4b4ba1da 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1256,17 +1256,18 @@ static void annotate_call_site(struct objtool_f

[PATCH v6 10/16] objtool: Use target file class size instead of a compiled constant

2022-11-14 Thread Sathvika Vasireddy
From: Christophe Leroy 

In order to allow using objtool on cross-built kernels,
determine size of long from elf data instead of using
sizeof(long) at build time.

For the time being this covers only mcount.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Rename variable "size" to "addrsize" and function
"elf_class_size()" to "elf_class_addrsize()", and modify
create_mcount_loc_sections() function to follow reverse christmas tree
format to order local variable declarations.]
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c   | 18 ++
 tools/objtool/elf.c |  8 ++--
 tools/objtool/include/objtool/elf.h |  8 
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index ad5dab175701..b64518c7c7b4 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -852,9 +852,9 @@ static int create_ibt_endbr_seal_sections(struct 
objtool_file *file)
 
 static int create_mcount_loc_sections(struct objtool_file *file)
 {
-   struct section *sec;
-   unsigned long *loc;
+   int addrsize = elf_class_addrsize(file->elf);
struct instruction *insn;
+   struct section *sec;
int idx;
 
sec = find_section_by_name(file->elf, "__mcount_loc");
@@ -871,23 +871,25 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
list_for_each_entry(insn, >mcount_loc_list, call_node)
idx++;
 
-   sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned 
long), idx);
+   sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx);
if (!sec)
return -1;
 
+   sec->sh.sh_addralign = addrsize;
+
idx = 0;
list_for_each_entry(insn, >mcount_loc_list, call_node) {
+   void *loc;
 
-   loc = (unsigned long *)sec->data->d_buf + idx;
-   memset(loc, 0, sizeof(unsigned long));
+   loc = sec->data->d_buf + idx;
+   memset(loc, 0, addrsize);
 
-   if (elf_add_reloc_to_insn(file->elf, sec,
- idx * sizeof(unsigned long),
+   if (elf_add_reloc_to_insn(file->elf, sec, idx,
  R_X86_64_64,
  insn->sec, insn->offset))
return -1;
 
-   idx++;
+   idx += addrsize;
}
 
return 0;
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 7e24b09b1163..33739865735b 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1129,6 +1129,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
 {
char *relocname;
struct section *sec;
+   int addrsize = elf_class_addrsize(elf);
 
relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
if (!relocname) {
@@ -1138,7 +1139,10 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
strcpy(relocname, ".rela");
strcat(relocname, base->name);
 
-   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
+   if (addrsize == sizeof(u32))
+   sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 
0);
+   else
+   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 
0);
free(relocname);
if (!sec)
return NULL;
@@ -1147,7 +1151,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
sec->base = base;
 
sec->sh.sh_type = SHT_RELA;
-   sec->sh.sh_addralign = 8;
+   sec->sh.sh_addralign = addrsize;
sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
sec->sh.sh_info = base->idx;
sec->sh.sh_flags = SHF_INFO_LINK;
diff --git a/tools/objtool/include/objtool/elf.h 
b/tools/objtool/include/objtool/elf.h
index 16f4067b82ae..78b3aa2e546d 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -142,6 +142,14 @@ static inline bool has_multiple_files(struct elf *elf)
return elf->num_files > 1;
 }
 
+static inline int elf_class_addrsize(struct elf *elf)
+{
+   if (elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+   return sizeof(u32);
+   else
+   return sizeof(u64);
+}
+
 struct elf *elf_open_read(const char *name, int flags);
 struct section *elf_create_section(struct elf *elf, const char *name, unsigned 
int sh_flags, size_t entsize, int nr);
 
-- 
2.31.1



[PATCH v6 09/16] objtool: Use target file endianness instead of a compiled constant

2022-11-14 Thread Sathvika Vasireddy
From: Christophe Leroy 

Some architectures like powerpc support both endianness, it's
therefore not possible to fix the endianness via arch/endianness.h
because there is no easy way to get the target endianness at
build time.

Use the endianness recorded in the file objtool is working on.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 .../arch/x86/include/arch/endianness.h|  9 --
 tools/objtool/check.c |  2 +-
 tools/objtool/include/objtool/endianness.h| 32 +--
 tools/objtool/orc_dump.c  | 11 +--
 tools/objtool/orc_gen.c   |  4 +--
 tools/objtool/special.c   |  3 +-
 6 files changed, 30 insertions(+), 31 deletions(-)
 delete mode 100644 tools/objtool/arch/x86/include/arch/endianness.h

diff --git a/tools/objtool/arch/x86/include/arch/endianness.h 
b/tools/objtool/arch/x86/include/arch/endianness.h
deleted file mode 100644
index 7c362527da20..
--- a/tools/objtool/arch/x86/include/arch/endianness.h
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-#ifndef _ARCH_ENDIANNESS_H
-#define _ARCH_ENDIANNESS_H
-
-#include 
-
-#define __TARGET_BYTE_ORDER __LITTLE_ENDIAN
-
-#endif /* _ARCH_ENDIANNESS_H */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 8427af808221..ad5dab175701 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2100,7 +2100,7 @@ static int read_unwind_hints(struct objtool_file *file)
return -1;
}
 
-   cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
+   cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
cfi.type = hint->type;
cfi.end = hint->end;
 
diff --git a/tools/objtool/include/objtool/endianness.h 
b/tools/objtool/include/objtool/endianness.h
index 10241341eff3..4d2aa9b0fe2f 100644
--- a/tools/objtool/include/objtool/endianness.h
+++ b/tools/objtool/include/objtool/endianness.h
@@ -2,33 +2,33 @@
 #ifndef _OBJTOOL_ENDIANNESS_H
 #define _OBJTOOL_ENDIANNESS_H
 
-#include 
 #include 
 #include 
-
-#ifndef __TARGET_BYTE_ORDER
-#error undefined arch __TARGET_BYTE_ORDER
-#endif
-
-#if __BYTE_ORDER != __TARGET_BYTE_ORDER
-#define __NEED_BSWAP 1
-#else
-#define __NEED_BSWAP 0
-#endif
+#include 
 
 /*
- * Does a byte swap if target endianness doesn't match the host, i.e. cross
+ * Does a byte swap if target file endianness doesn't match the host, i.e. 
cross
  * compilation for little endian on big endian and vice versa.
  * To be used for multi-byte values conversion, which are read from / about
  * to be written to a target native endianness ELF file.
  */
-#define bswap_if_needed(val)   \
+static inline bool need_bswap(struct elf *elf)
+{
+   return (__BYTE_ORDER == __LITTLE_ENDIAN) ^
+  (elf->ehdr.e_ident[EI_DATA] == ELFDATA2LSB);
+}
+
+#define bswap_if_needed(elf, val)  \
 ({ \
__typeof__(val) __ret;  \
+   bool __need_bswap = need_bswap(elf);\
switch (sizeof(val)) {  \
-   case 8: __ret = __NEED_BSWAP ? bswap_64(val) : (val); break;\
-   case 4: __ret = __NEED_BSWAP ? bswap_32(val) : (val); break;\
-   case 2: __ret = __NEED_BSWAP ? bswap_16(val) : (val); break;\
+   case 8: \
+   __ret = __need_bswap ? bswap_64(val) : (val); break;\
+   case 4: \
+   __ret = __need_bswap ? bswap_32(val) : (val); break;\
+   case 2: \
+   __ret = __need_bswap ? bswap_16(val) : (val); break;\
default:\
BUILD_BUG(); break; \
}   \
diff --git a/tools/objtool/orc_dump.c b/tools/objtool/orc_dump.c
index f5a8508c42d6..4f1211fec82c 100644
--- a/tools/objtool/orc_dump.c
+++ b/tools/objtool/orc_dump.c
@@ -76,6 +76,7 @@ int orc_dump(const char *_objname)
GElf_Rela rela;
GElf_Sym sym;
Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
+   struct elf dummy_elf = {};
 
 
objname = _objname;
@@ -94,6 +95,12 @@ int orc_dump(const char *_objname)
return -1;
}
 
+   if (!elf64_getehdr(elf)) {
+   WARN_ELF("elf64_getehdr");
+   return -1;
+   }
+   memcpy(_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
+
if 

[PATCH v6 08/16] objtool: Fix SEGFAULT

2022-11-14 Thread Sathvika Vasireddy
From: Christophe Leroy 

find_insn() will return NULL in case of failure. Check insn in order
to avoid a kernel Oops for NULL pointer dereference.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 43ec14c29a60..8427af808221 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -207,7 +207,7 @@ static bool __dead_end_function(struct objtool_file *file, 
struct symbol *func,
return false;
 
insn = find_insn(file, func->sec, func->offset);
-   if (!insn->func)
+   if (!insn || !insn->func)
return false;
 
func_for_each_insn(file, func, insn) {
-- 
2.31.1



[PATCH v6 07/16] powerpc: Skip objtool from running on VDSO files

2022-11-14 Thread Sathvika Vasireddy
Do not run objtool on VDSO files, by using OBJECT_FILES_NON_STANDARD.

Suggested-by: Christophe Leroy 
Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/vdso/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/powerpc/kernel/vdso/Makefile 
b/arch/powerpc/kernel/vdso/Makefile
index a2e7b0ce5b19..6a977b0d8ffc 100644
--- a/arch/powerpc/kernel/vdso/Makefile
+++ b/arch/powerpc/kernel/vdso/Makefile
@@ -102,3 +102,5 @@ quiet_cmd_vdso64ld_and_check = VDSO64L $@
   cmd_vdso64ld_and_check = $(VDSOCC) $(c_flags) $(CC64FLAGS) -o $@ 
-Wl,-T$(filter %.lds,$^) $(filter %.o,$^) -z noexecstack ; $(cmd_vdso_check)
 quiet_cmd_vdso64as = VDSO64A $@
   cmd_vdso64as = $(VDSOCC) $(a_flags) $(CC64FLAGS) $(AS64FLAGS) -c -o $@ $<
+
+OBJECT_FILES_NON_STANDARD := y
-- 
2.31.1



[PATCH v6 06/16] powerpc: Fix objtool unannotated intra-function call warnings on PPC32

2022-11-14 Thread Sathvika Vasireddy
From: Christophe Leroy 

Fix several annotations in assembly files on PPC32.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Changed subject line from "objtool/powerpc: Activate
objtool on PPC32" to "powerpc: Fix objtool unannotated intra-function call
warnings on PPC32", and removed Kconfig change to enable objtool, as it
is a part of "objtool/powerpc: Enable objtool to be built on ppc" patch in
this series.]
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/cpu_setup_6xx.S  | 26 --
 arch/powerpc/kernel/cpu_setup_e500.S |  8 --
 arch/powerpc/kernel/entry_32.S   |  9 --
 arch/powerpc/kernel/head_40x.S   |  5 +++-
 arch/powerpc/kernel/head_85xx.S  |  5 +++-
 arch/powerpc/kernel/head_8xx.S   |  5 +++-
 arch/powerpc/kernel/head_book3s_32.S | 29 ++--
 arch/powerpc/kernel/swsusp_32.S  |  5 +++-
 arch/powerpc/kvm/fpu.S   | 17 
 arch/powerpc/platforms/52xx/lite5200_sleep.S | 15 +++---
 10 files changed, 89 insertions(+), 35 deletions(-)

diff --git a/arch/powerpc/kernel/cpu_setup_6xx.S 
b/arch/powerpc/kernel/cpu_setup_6xx.S
index f8b5ff64b604..f29ce3dd6140 100644
--- a/arch/powerpc/kernel/cpu_setup_6xx.S
+++ b/arch/powerpc/kernel/cpu_setup_6xx.S
@@ -4,6 +4,8 @@
  *Copyright (C) 2003 Benjamin Herrenschmidt (b...@kernel.crashing.org)
  */
 
+#include 
+
 #include 
 #include 
 #include 
@@ -81,7 +83,7 @@ _GLOBAL(__setup_cpu_745x)
blr
 
 /* Enable caches for 603's, 604, 750 & 7400 */
-setup_common_caches:
+SYM_FUNC_START_LOCAL(setup_common_caches)
mfspr   r11,SPRN_HID0
andi.   r0,r11,HID0_DCE
ori r11,r11,HID0_ICE|HID0_DCE
@@ -95,11 +97,12 @@ setup_common_caches:
sync
isync
blr
+SYM_FUNC_END(setup_common_caches)
 
 /* 604, 604e, 604ev, ...
  * Enable superscalar execution & branch history table
  */
-setup_604_hid0:
+SYM_FUNC_START_LOCAL(setup_604_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SIED|HID0_BHTE
ori r8,r11,HID0_BTCD
@@ -110,6 +113,7 @@ setup_604_hid0:
sync
isync
blr
+SYM_FUNC_END(setup_604_hid0)
 
 /* 7400 <= rev 2.7 and 7410 rev = 1.0 suffer from some
  * erratas we work around here.
@@ -125,13 +129,14 @@ setup_604_hid0:
  * needed once we have applied workaround #5 (though it's
  * not set by Apple's firmware at least).
  */
-setup_7400_workarounds:
+SYM_FUNC_START_LOCAL(setup_7400_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0207
ble 1f
blr
-setup_7410_workarounds:
+SYM_FUNC_END(setup_7400_workarounds)
+SYM_FUNC_START_LOCAL(setup_7410_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0100
@@ -151,6 +156,7 @@ setup_7410_workarounds:
sync
isync
blr
+SYM_FUNC_END(setup_7410_workarounds)
 
 /* 740/750/7400/7410
  * Enable Store Gathering (SGE), Address Broadcast (ABE),
@@ -158,7 +164,7 @@ setup_7410_workarounds:
  * Dynamic Power Management (DPM), Speculative (SPD)
  * Clear Instruction cache throttling (ICTC)
  */
-setup_750_7400_hid0:
+SYM_FUNC_START_LOCAL(setup_750_7400_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SGE | HID0_ABE | HID0_BHTE | HID0_BTIC
orisr11,r11,HID0_DPM@h
@@ -177,12 +183,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_750_7400_hid0)
 
 /* 750cx specific
  * Looks like we have to disable NAP feature for some PLL settings...
  * (waiting for confirmation)
  */
-setup_750cx:
+SYM_FUNC_START_LOCAL(setup_750cx)
mfspr   r10, SPRN_HID1
rlwinm  r10,r10,4,28,31
cmpwi   cr0,r10,7
@@ -196,11 +203,13 @@ setup_750cx:
andcr6,r6,r7
stw r6,CPU_SPEC_FEATURES(r4)
blr
+SYM_FUNC_END(setup_750cx)
 
 /* 750fx specific
  */
-setup_750fx:
+SYM_FUNC_START_LOCAL(setup_750fx)
blr
+SYM_FUNC_END(setup_750fx)
 
 /* MPC 745x
  * Enable Store Gathering (SGE), Branch Folding (FOLD)
@@ -212,7 +221,7 @@ setup_750fx:
  * Clear Instruction cache throttling (ICTC)
  * Enable L2 HW prefetch
  */
-setup_745x_specifics:
+SYM_FUNC_START_LOCAL(setup_745x_specifics)
/* We check for the presence of an L3 cache setup by
 * the firmware. If any, we disable NAP capability as
 * it's known to be bogus on rev 2.1 and earlier
@@ -270,6 +279,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_745x_specifics)
 
 /*
  * Initialize the FPU registers. This is needed to work around an errata
diff --git a/arch/powerpc/kernel/cpu_setup_e500.S 
b/arch/powerpc/kernel/cpu_setup_e500.S
index 2ab25161b0ad..077cfccc3461 100644
--- a/arch/powerpc/kernel/cpu_setup_e500.S
+++ b/arch/powerpc/kernel/cpu_

[PATCH v6 05/16] powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o

2022-11-14 Thread Sathvika Vasireddy
With objtool enabled, below warnings are seen when trying to build:
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: aes_p8_set_encrypt_key+0x44: 
unannotated intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2448: unannotated 
intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2d68: unannotated 
intra-function call

Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o file for the
following reasons:

- Since this file comes from OpenSSL, and since it is a perl file which
  generates a .S file, it may not be the best choice to make too many
  code changes to such files, unless absolutely necessary.

- As far as the objtool --mcount functionality is concerned, we do not
  have to run objtool on this file because there are no calls to
  _mcount.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 drivers/crypto/vmx/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/crypto/vmx/Makefile b/drivers/crypto/vmx/Makefile
index 2560cfea1dec..7b41f0da6807 100644
--- a/drivers/crypto/vmx/Makefile
+++ b/drivers/crypto/vmx/Makefile
@@ -9,3 +9,5 @@ targets += aesp8-ppc.S ghashp8-ppc.S
 
 $(obj)/aesp8-ppc.S $(obj)/ghashp8-ppc.S: $(obj)/%.S: $(src)/%.pl FORCE
$(call if_changed,perl)
+
+OBJECT_FILES_NON_STANDARD_aesp8-ppc.o := y
-- 
2.31.1



[PATCH v6 04/16] powerpc: Curb objtool unannotated intra-function call warnings

2022-11-14 Thread Sathvika Vasireddy
objtool throws the following unannotated intra-function call warnings:
arch/powerpc/kernel/entry_64.o: warning: objtool: .text+0x4: unannotated 
intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xe64: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xee4: 
unannotated intra-function call

Fix these warnings by annotating intra-function calls, using
ANNOTATE_INTRA_FUNCTION_CALL macro, to indicate that the branch targets
are valid.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/entry_64.S  | 2 ++
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
index 3e2e37e6ecab..1bf1121e17f1 100644
--- a/arch/powerpc/kernel/entry_64.S
+++ b/arch/powerpc/kernel/entry_64.S
@@ -14,6 +14,7 @@
  *  code, and exception/interrupt return code for PowerPC.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -73,6 +74,7 @@ flush_branch_caches:
 
// Flush the link stack
.rept 64
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
b   1f
diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S 
b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index a69d36cbf43b..96b65b530156 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -11,6 +11,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1523,12 +1524,14 @@ kvm_flush_link_stack:
 
/* Flush the link stack. On Power8 it's up to 32 entries in size. */
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 
/* And on Power9 it's up to 64. */
 BEGIN_FTR_SECTION
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
-- 
2.31.1



[PATCH v6 03/16] powerpc: Fix objtool unannotated intra-function call warnings

2022-11-14 Thread Sathvika Vasireddy
Objtool throws unannotated intra-function call warnings in the following
assembly files:

arch/powerpc/kernel/vector.o: warning: objtool: .text+0x53c: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x60: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x124: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5d4: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5dc: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xcb8: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xd0c: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x1030: 
unannotated intra-function call

arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x358: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x728: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4d94: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4ec4: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_interrupts.o: warning: objtool: .text+0x6c: 
unannotated intra-function call
arch/powerpc/kernel/misc_64.o: warning: objtool: .text+0x64: unannotated 
intra-function call

Objtool does not add STT_NOTYPE symbols with size 0 to the rbtree, which
is why find_call_destination() function is not able to find the
destination symbol for 'bl' instruction. For such symbols, objtool is
throwing unannotated intra-function call warnings in assembly files. Fix
these warnings by annotating those symbols with SYM_FUNC_START_LOCAL and
SYM_FUNC_END macros, inorder to set symbol type to STT_FUNC and symbol
size accordingly.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/exceptions-64s.S|  4 +++-
 arch/powerpc/kernel/head_64.S   |  7 +--
 arch/powerpc/kernel/misc_64.S   |  4 +++-
 arch/powerpc/kernel/vector.S|  4 +++-
 arch/powerpc/kvm/book3s_hv_interrupts.S |  4 +++-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 22 +++---
 6 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/arch/powerpc/kernel/exceptions-64s.S 
b/arch/powerpc/kernel/exceptions-64s.S
index 651c36b056bd..26f8fef53c72 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -13,6 +13,7 @@
  *
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -3124,7 +3125,7 @@ _GLOBAL(enable_machine_check)
blr
 
 /* MSR[RI] should be clear because this uses SRR[01] */
-disable_machine_check:
+SYM_FUNC_START_LOCAL(disable_machine_check)
mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3137,3 +3138,4 @@ disable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(disable_machine_check)
diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index dedcc6fe2263..874efd25cc45 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -18,6 +18,7 @@
  *  variants.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -462,7 +463,7 @@ generic_secondary_common_init:
  * Assumes we're mapped EA == RA if the MMU is on.
  */
 #ifdef CONFIG_PPC_BOOK3S
-__mmu_off:
+SYM_FUNC_START_LOCAL(__mmu_off)
mfmsr   r3
andi.   r0,r3,MSR_IR|MSR_DR
beqlr
@@ -473,6 +474,7 @@ __mmu_off:
sync
rfid
b   .   /* prevent speculative execution */
+SYM_FUNC_END(__mmu_off)
 #endif
 
 
@@ -869,7 +871,7 @@ _GLOBAL(start_secondary_resume)
 /*
  * This subroutine clobbers r11 and r12
  */
-enable_64b_mode:
+SYM_FUNC_START_LOCAL(enable_64b_mode)
mfmsr   r11 /* grab the current MSR */
 #ifdef CONFIG_PPC_BOOK3E_64
orisr11,r11,0x8000  /* CM bit set, we'll set ICM later */
@@ -881,6 +883,7 @@ enable_64b_mode:
isync
 #endif
blr
+SYM_FUNC_END(enable_64b_mode)
 
 /*
  * This puts the TOC pointer into r2, offset by 0x8000 (as expected
diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
index 36184cada00b..c61a7ba446a8 100644
--- a/arch/powerpc/kernel/misc_64.S
+++ b/arch/powerpc/kernel/misc_64.S
@@ -9,6 +9,7 @@
  * PPC64 updates by Dave Engebretsen (engeb...@us.ibm.com)
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -353,7 +354,7 @@ _GLOBAL(kexec_smp_wait)
  *
  * don't overwrite r3 here, it is live for kexec_wait above.
  */
-real_mode: /* assume normal blr return */
+SYM_FUNC_START_LOCAL(real_mode)/* assume normal blr return */
 #ifdef CONFIG_PPC_BOOK3E_64
/* Create an identity mapping. */
b   kexec_create_tlb

[PATCH v6 02/16] powerpc: Override __ALIGN and __ALIGN_STR macros

2022-11-14 Thread Sathvika Vasireddy
In a subsequent patch, we would want to annotate powerpc assembly functions
with SYM_FUNC_START_LOCAL macro. This macro depends on __ALIGN macro.

The default expansion of __ALIGN macro is:
#define __ALIGN  .align 4,0x90

So, override __ALIGN and __ALIGN_STR macros to use the same alignment as
that of the existing _GLOBAL macro. Also, do not pad with 0x90, because
repeated 0x90s are not a nop or trap on powerpc.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/linkage.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/powerpc/include/asm/linkage.h 
b/arch/powerpc/include/asm/linkage.h
index b71b9582e754..b88d1d2cf304 100644
--- a/arch/powerpc/include/asm/linkage.h
+++ b/arch/powerpc/include/asm/linkage.h
@@ -4,6 +4,9 @@
 
 #include 
 
+#define __ALIGN.align 2
+#define __ALIGN_STR".align 2"
+
 #ifdef CONFIG_PPC64_ELF_ABI_V1
 #define cond_syscall(x) \
asm ("\t.weak " #x "\n\t.set " #x ", sys_ni_syscall\n"  \
-- 
2.31.1



[PATCH v6 01/16] powerpc: Fix __WARN_FLAGS() for use with Objtool

2022-11-14 Thread Sathvika Vasireddy
Commit 1e688dd2a3d675 ("powerpc/bug: Provide better flexibility to
WARN_ON/__WARN_FLAGS() with asm goto") updated __WARN_FLAGS() to use asm
goto, and added a call to 'unreachable()' after the asm goto for optimal
code generation. With CONFIG_OBJTOOL enabled, 'annotate_unreachable()'
statement in 'unreachable()' tries to note down the location of the
subsequent instruction in a separate elf section to aid code flow
analysis. However, on powerpc, this results in gcc emitting a call to a
symbol of size 0. This results in objtool complaining of "unannotated
intra-function call" since the target symbol is not a valid function
call destination.

Objtool wants this annotation for code flow analysis, which we are not
yet enabling on powerpc. As such, expand the call to 'unreachable()' in
__WARN_FLAGS() without annotate_unreachable():
barrier_before_unreachable();
__builtin_unreachable();

This still results in optimal code generation for __WARN_FLAGS(), while
getting rid of the objtool warning.

We still need barrier_before_unreachable() to work around gcc bugs 82365
and 106751:
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106751

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/bug.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 61a4736355c2..ef42adb44aa3 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -99,7 +99,8 @@
__label__ __label_warn_on;  \
\
WARN_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags), __label_warn_on); 
\
-   unreachable();  \
+   barrier_before_unreachable();   \
+   __builtin_unreachable();\
\
 __label_warn_on:   \
break;  \
-- 
2.31.1



[PATCH v6 00/16] objtool: Enable and implement --mcount option on powerpc

2022-11-14 Thread Sathvika Vasireddy
This patchset enables and implements objtool --mcount
option on powerpc. This applies atop powerpc/merge branch.

Changelog:


v6:

* Patch 06/16 - Add annotations to assembly files
arch/powerpc/kernel/cpu_setup_e500.S and 
arch/powerpc/kernel/head_85xx.S

v5:

* Patch 02/16 - Add Reviewed-by tag from Christophe Leroy

* Patch 03/16 - Fix merge conflicts with latest powerpc/merge branch

* Patch 06/16 - Files arch/powerpc/kernel/cpu_setup_fsl_booke.S and
arch/powerpc/kernel/head_fsl_booke.S are not present
today. Removed annotations in those assembly files.

* Patch 11/16 - Add Reviewed-by tag from Christophe Leroy
  - Changed scripts/Makefile.lib file to make
CONFIG_HAVE_OBJTOOL_NOP_MCOUNT depend on
CONFIG_FTRACE_MCOUNT_USE_OBJTOOL.

* Patch 12/16 - Add Reviewed-by tag from Christophe Leroy

* Patch 16/16 - Add Reviewed-by tag from Christophe Leroy 

* For this series - Add Acked-by tag from Josh Poimboeuf
  - Add Tested-by tag from Naveen N. Rao
  - Add Reviewed-by tag from Naveen N. Rao

v4:

* Patch 11/16 - Introduce a new config option
CONFIG_HAVE_OBJTOOL_NOP_MCOUNT as a means for
architectures to enable nop'ing ftrace locations.

  - Remove Acked-by tag from Peter Zijlstra (Intel),
and Reviewed-by tag from Christophe Leroy.
[This is done because I reworked the patch to add
a new config option to objtool. Please let me know
if you want me to retain the tags. Thanks!]

* Patch 16/16 - Rework the patch to handle only 'bl' instruction
decoding.


v3:

* Patch 01/16 - Rework patch subject.
  - Rework changelog.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 02/16 - Rework changelog to update details based on feedback
from Nicholas Piggin and Michael Ellerman.
  - Use quotes instead of __stringify macro, based on
suggestion from Christophe Leroy.

* Patch 03/16 - Add Reviewed-by tag from Christophe Leroy.
  - Based on Christophe's suggestion, keep all 
before .
  - Rework changelog.

* Patch 04/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 05/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 06/16 - No change.

* Patch 07/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 08/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 09/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 10/16 - Reorder local variable declarations to use reverse
xmas tree format.
  - Add Signed-off-by tag from Sathvika Vasireddy indicating
changes done.
  - Add Acked-by tag from Peter Zijlstra.

* Patch 11/16 - Update changelog to indicate that powerpc kernel does
not support nop'ed out ftrace locations.
  - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 12/16 - Per Christophe's comment, rework changelog.

* Patch 13/16 - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 14/16 - Simplify arch_ftrace_match() function, based on
Christophe's suggestion.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 15/16 - Include code from Christophe Leroy to use local vars for
type and imm, and to adapt len for prefixed
instructions.

* Patch 16/16 - Based on suggestion from Christophe Leroy, setup
immediate value calculation outside the check for
specific instruction under case 18.
  - Set instruction type to INSN_CALL for 'bla'
instruction as well.


v2:

* Change subject of patch 01/16
* As suggested by Christophe Leroy, add barrier_before_unreachable()
before __builtin_unreachable() to work around a gcc problem.
* Fix issues reported by Kernel Test Robot.
* Include suggestions from Christophe Leroy, and change commit
messages for patches 01/16, 02/16, 03/16, 05/16.



Christophe Leroy (4):
  objtool: Fix SEGFAULT
  objtool: Use target file endianness instead of a compiled constant
  objtool: Use target file class size instead of a compiled constant
  powerpc: Fix objtool unannotated intra-function call warnings on PPC32

Sathvika Vasireddy (12):
  powerpc: Fix __WARN_FLAGS() for use with Objtool
  powerpc: Override __ALIGN and __ALIGN_STR macros
  powerpc: Fix objtool unannotated intra-function call warnings
  powerpc: Curb objtool unannotated intra-function warnings
  powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o
  powerpc: Skip objtool from running on VDSO files
  objtool: Add --mnop as an option to --mcount
  objtool: Read special sections with alts only when specific options are 
selected

Re: [PATCH v5 02/16] powerpc: Override __ALIGN and __ALIGN_STR macros

2022-11-13 Thread Sathvika Vasireddy

Hi Peter,

On 03/11/22 14:18, Peter Zijlstra wrote:

On Wed, Nov 02, 2022 at 12:35:07PM +, Christophe Leroy wrote:


Le 28/10/2022 à 16:33, Sathvika Vasireddy a écrit :

In a subsequent patch, we would want to annotate powerpc assembly functions
with SYM_FUNC_START_LOCAL macro. This macro depends on __ALIGN macro.

The default expansion of __ALIGN macro is:
  #define __ALIGN  .align 4,0x90

So, override __ALIGN and __ALIGN_STR macros to use the same alignment as
that of the existing _GLOBAL macro. Also, do not pad with 0x90, because
repeated 0x90s are not a nop or trap on powerpc.

By the way, do we know what the instruction 0x90909090 is on powerpc ?
Is that something valid or not ?

Please also look at the version that's in tip/x86/core (and next). This
stuff should be gone now.

include/linux/linkage.h now reads like:

#ifndef __ALIGN
#define __ALIGN .balign CONFIG_FUNCTION_ALIGNMENT
#define __ALIGN_STR __stringify(__ALIGN)
#endif


Since the above mentioned changes are not a part of powerpc/merge branch 
yet, I am retaining this patch for this merge cycle and will post a 
cleanup patch (to move to using FUNCTION_ALIGNMENT_4B) after the next -rc1.


Thanks,
Sathvika


[PATCH v5 16/16] objtool/powerpc: Add --mcount specific implementation

2022-10-28 Thread Sathvika Vasireddy
This patch enables objtool --mcount on powerpc, and adds implementation
specific to powerpc.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/Kconfig  |  1 +
 tools/objtool/arch/powerpc/decode.c   | 16 
 tools/objtool/arch/powerpc/include/arch/elf.h |  2 ++
 3 files changed, 19 insertions(+)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 12e6c16be54e..9c07068ba5e5 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -239,6 +239,7 @@ config PPC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
+   select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
index dcd0975cad6b..01cade98b49e 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -9,6 +9,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "_mcount");
+}
+
 unsigned long arch_dest_reloc_offset(int addend)
 {
return addend;
@@ -50,6 +55,17 @@ int arch_decode_instruction(struct objtool_file *file, const 
struct section *sec
typ = INSN_OTHER;
imm = 0;
 
+   switch (opcode) {
+   case 18: /* b[l][a] */
+   if ((insn & 3) == 1) /* bl */
+   typ = INSN_CALL;
+
+   imm = insn & 0x3fc;
+   if (imm & 0x200)
+   imm -= 0x400;
+   break;
+   }
+
if (opcode == 1)
*len = 8;
else
diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h 
b/tools/objtool/arch/powerpc/include/arch/elf.h
index 3c8ebb7d2a6b..73f9ae172fe5 100644
--- a/tools/objtool/arch/powerpc/include/arch/elf.h
+++ b/tools/objtool/arch/powerpc/include/arch/elf.h
@@ -4,5 +4,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_PPC_NONE
+#define R_ABS64 R_PPC64_ADDR64
+#define R_ABS32 R_PPC_ADDR32
 
 #endif /* _OBJTOOL_ARCH_ELF */
-- 
2.31.1



[PATCH v5 15/16] objtool/powerpc: Enable objtool to be built on ppc

2022-10-28 Thread Sathvika Vasireddy
This patch adds [stub] implementations for required functions, inorder
to enable objtool build on powerpc.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
[Christophe Leroy: powerpc: Add missing asm/asm.h for objtool,
Use local variables for type and imm in arch_decode_instruction(),
Adapt len for prefixed instructions.]
Signed-off-by: Christophe Leroy 
---
 arch/powerpc/Kconfig  |  1 +
 arch/powerpc/include/asm/asm.h|  7 ++
 tools/objtool/arch/powerpc/Build  |  2 +
 tools/objtool/arch/powerpc/decode.c   | 85 +++
 .../arch/powerpc/include/arch/cfi_regs.h  | 11 +++
 tools/objtool/arch/powerpc/include/arch/elf.h |  8 ++
 .../arch/powerpc/include/arch/special.h   | 21 +
 tools/objtool/arch/powerpc/special.c  | 19 +
 8 files changed, 154 insertions(+)
 create mode 100644 arch/powerpc/include/asm/asm.h
 create mode 100644 tools/objtool/arch/powerpc/Build
 create mode 100644 tools/objtool/arch/powerpc/decode.c
 create mode 100644 tools/objtool/arch/powerpc/include/arch/cfi_regs.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/elf.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/special.h
 create mode 100644 tools/objtool/arch/powerpc/special.c

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 699df27b0e2f..12e6c16be54e 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -238,6 +238,7 @@ config PPC
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
+   select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/arch/powerpc/include/asm/asm.h b/arch/powerpc/include/asm/asm.h
new file mode 100644
index ..86f46b604e9a
--- /dev/null
+++ b/arch/powerpc/include/asm/asm.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_POWERPC_ASM_H
+#define _ASM_POWERPC_ASM_H
+
+#define _ASM_PTR   " .long "
+
+#endif /* _ASM_POWERPC_ASM_H */
diff --git a/tools/objtool/arch/powerpc/Build b/tools/objtool/arch/powerpc/Build
new file mode 100644
index ..d24d5636a5b8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/Build
@@ -0,0 +1,2 @@
+objtool-y += decode.o
+objtool-y += special.o
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
new file mode 100644
index ..dcd0975cad6b
--- /dev/null
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+unsigned long arch_dest_reloc_offset(int addend)
+{
+   return addend;
+}
+
+bool arch_callee_saved_reg(unsigned char reg)
+{
+   return false;
+}
+
+int arch_decode_hint_reg(u8 sp_reg, int *base)
+{
+   exit(-1);
+}
+
+const char *arch_nop_insn(int len)
+{
+   exit(-1);
+}
+
+const char *arch_ret_insn(int len)
+{
+   exit(-1);
+}
+
+int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
+   unsigned long offset, unsigned int maxlen,
+   unsigned int *len, enum insn_type *type,
+   unsigned long *immediate,
+   struct list_head *ops_list)
+{
+   unsigned int opcode;
+   enum insn_type typ;
+   unsigned long imm;
+   u32 insn;
+
+   insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
+   opcode = insn >> 26;
+   typ = INSN_OTHER;
+   imm = 0;
+
+   if (opcode == 1)
+   *len = 8;
+   else
+   *len = 4;
+
+   *type = typ;
+   *immediate = imm;
+
+   return 0;
+}
+
+unsigned long arch_jump_destination(struct instruction *insn)
+{
+   return insn->offset + insn->immediate;
+}
+
+void arch_initial_func_cfi_state(struct cfi_init_state *state)
+{
+   int i;
+
+   for (i = 0; i < CFI_NUM_REGS; i++) {
+   state->regs[i].base = CFI_UNDEFINED;
+   state->regs[i].offset = 0;
+   }
+
+   /* initial CFA (call frame address) */
+   state->cfa.base = CFI_SP;
+   state->cfa.offset = 0;
+
+   /* initial LR (return address) */
+   state->regs[CFI_RA].base = CFI_CFA;
+   state->regs[CFI_RA].offset = 0;
+}
diff --git a/tools/objtool/arch/powerpc/include/arch/cfi_regs.h 
b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
new file mode 100644
index ..59638ebeafc8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _OBJTOOL_CFI_REGS_H
+#define _OBJTOOL_CFI_REGS_H
+

[PATCH v5 14/16] objtool: Add arch specific function arch_ftrace_match()

2022-10-28 Thread Sathvika Vasireddy
Add architecture specific function to look for relocation records
pointing to architecture specific symbols.

Suggested-by: Christophe Leroy 
Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/decode.c  | 5 +
 tools/objtool/check.c| 2 +-
 tools/objtool/include/objtool/arch.h | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index 1c253b4b7ce0..af7ad09c926c 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -23,6 +23,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "__fentry__");
+}
+
 static int is_x86_64(const struct elf *elf)
 {
switch (elf->ehdr.e_machine) {
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 2d7153b5d5d1..7580c66ca5c8 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2316,7 +2316,7 @@ static int classify_symbols(struct objtool_file *file)
if (arch_is_rethunk(func))
func->return_thunk = true;
 
-   if (!strcmp(func->name, "__fentry__"))
+   if (arch_ftrace_match(func->name))
func->fentry = true;
 
if (is_profiling_func(func->name))
diff --git a/tools/objtool/include/objtool/arch.h 
b/tools/objtool/include/objtool/arch.h
index beb2f3aa94ff..5149330f400f 100644
--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -69,6 +69,8 @@ struct stack_op {
 
 struct instruction;
 
+int arch_ftrace_match(char *name);
+
 void arch_initial_func_cfi_state(struct cfi_init_state *state);
 
 int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
-- 
2.31.1



[PATCH v5 13/16] objtool: Use macros to define arch specific reloc types

2022-10-28 Thread Sathvika Vasireddy
Make relocation types architecture specific.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Peter Zijlstra (Intel) 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/include/arch/elf.h | 2 ++
 tools/objtool/check.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/include/arch/elf.h 
b/tools/objtool/arch/x86/include/arch/elf.h
index 69cc4264b28a..ac14987cf687 100644
--- a/tools/objtool/arch/x86/include/arch/elf.h
+++ b/tools/objtool/arch/x86/include/arch/elf.h
@@ -2,5 +2,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_X86_64_NONE
+#define R_ABS64 R_X86_64_64
+#define R_ABS32 R_X86_64_32
 
 #endif /* _OBJTOOL_ARCH_ELF */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 752a6ffd5c4c..2d7153b5d5d1 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -885,7 +885,7 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
memset(loc, 0, addrsize);
 
if (elf_add_reloc_to_insn(file->elf, sec, idx,
- R_X86_64_64,
+ addrsize == sizeof(u64) ? R_ABS64 : 
R_ABS32,
  insn->sec, insn->offset))
return -1;
 
-- 
2.31.1



[PATCH v5 12/16] objtool: Read special sections with alts only when specific options are selected

2022-10-28 Thread Sathvika Vasireddy
Call add_special_section_alts() only when stackval or orc or uaccess or
noinstr options are passed to objtool.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 71cf4b4ba1da..752a6ffd5c4c 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2392,9 +2392,11 @@ static int decode_sections(struct objtool_file *file)
 * Must be before add_jump_destinations(), which depends on 'func'
 * being set for alternatives, to enable proper sibling call detection.
 */
-   ret = add_special_section_alts(file);
-   if (ret)
-   return ret;
+   if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
+   ret = add_special_section_alts(file);
+   if (ret)
+   return ret;
+   }
 
ret = add_jump_destinations(file);
if (ret)
-- 
2.31.1



[PATCH v5 11/16] objtool: Add --mnop as an option to --mcount

2022-10-28 Thread Sathvika Vasireddy
Some architectures (powerpc) may not support ftrace locations being nop'ed
out at build time. Introduce CONFIG_HAVE_OBJTOOL_NOP_MCOUNT for objtool, as
a means for architectures to enable nop'ing of ftrace locations. Add --mnop
as an option to objtool --mcount, to indicate support for the same.

Also, make sure that --mnop can be passed as an option to objtool only when
--mcount is passed.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 Makefile|  4 +++-
 arch/x86/Kconfig|  1 +
 kernel/trace/Kconfig|  7 +++
 scripts/Makefile.lib|  3 +++
 tools/objtool/builtin-check.c   | 14 ++
 tools/objtool/check.c   | 19 ++-
 tools/objtool/include/objtool/builtin.h |  1 +
 7 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index d148a55bfd0f..53c2b715d0bf 100644
--- a/Makefile
+++ b/Makefile
@@ -933,7 +933,9 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_CC
   endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
-  CC_FLAGS_USING   += -DCC_USING_NOP_MCOUNT
+  ifdef CONFIG_HAVE_OBJTOOL_NOP_MCOUNT
+CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
+  endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
   ifdef CONFIG_HAVE_C_RECORDMCOUNT
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 67745ceab0db..4be7c06a5d18 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -195,6 +195,7 @@ config X86
select HAVE_CONTEXT_TRACKING_USER_OFFSTACK  if 
HAVE_CONTEXT_TRACKING_USER
select HAVE_C_RECORDMCOUNT
select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
+   select HAVE_OBJTOOL_NOP_MCOUNT  if HAVE_OBJTOOL_MCOUNT
select HAVE_BUILDTIME_MCOUNT_SORT
select HAVE_DEBUG_KMEMLEAK
select HAVE_DMA_CONTIGUOUS
diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
index e9e95c790b8e..2b782321376a 100644
--- a/kernel/trace/Kconfig
+++ b/kernel/trace/Kconfig
@@ -82,6 +82,13 @@ config HAVE_OBJTOOL_MCOUNT
help
  Arch supports objtool --mcount
 
+config HAVE_OBJTOOL_NOP_MCOUNT
+   bool
+   help
+ Arch supports the objtool options --mcount with --mnop.
+ An architecture can select this if it wants to enable nop'ing
+ of ftrace locations.
+
 config HAVE_C_RECORDMCOUNT
bool
help
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3aa384cec76b..658f541c2782 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -256,6 +256,9 @@ objtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK) += 
--hacks=jump_label
 objtool-args-$(CONFIG_HAVE_NOINSTR_HACK)   += --hacks=noinstr
 objtool-args-$(CONFIG_X86_KERNEL_IBT)  += --ibt
 objtool-args-$(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL)   += --mcount
+ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
+objtool-args-$(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT) += --mnop
+endif
 objtool-args-$(CONFIG_UNWINDER_ORC)+= --orc
 objtool-args-$(CONFIG_RETPOLINE)   += --retpoline
 objtool-args-$(CONFIG_RETHUNK) += --rethunk
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 24fbe803a0d3..9bd347d3c244 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -82,6 +82,7 @@ const struct option check_options[] = {
OPT_BOOLEAN(0, "dry-run", , "don't write modifications"),
OPT_BOOLEAN(0, "link", , "object is a linked object"),
OPT_BOOLEAN(0, "module", , "object is part of a kernel 
module"),
+   OPT_BOOLEAN(0, "mnop", , "nop out mcount call sites"),
OPT_BOOLEAN(0, "no-unreachable", _unreachable, "skip 
'unreachable instruction' warnings"),
OPT_BOOLEAN(0, "sec-address", _address, "print section 
addresses in warnings"),
OPT_BOOLEAN(0, "stats", , "print statistics"),
@@ -150,6 +151,16 @@ static bool opts_valid(void)
return false;
 }
 
+static bool mnop_opts_valid(void)
+{
+   if (opts.mnop && !opts.mcount) {
+   ERROR("--mnop requires --mcount");
+   return false;
+   }
+
+   return true;
+}
+
 static bool link_opts_valid(struct objtool_file *file)
 {
if (opts.link)
@@ -198,6 +209,9 @@ int objtool_run(int argc, const char **argv)
if (!file)
return 1;
 
+   if (!mnop_opts_valid())
+   return 1;
+
if (!link_opts_valid(file))
return 1;
 
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index b64518c7c7b4..71cf4b4ba1da 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1256,17 +1256,18 @@ static void annotate_call_site(struct objtool_f

[PATCH v5 10/16] objtool: Use target file class size instead of a compiled constant

2022-10-28 Thread Sathvika Vasireddy
From: Christophe Leroy 

In order to allow using objtool on cross-built kernels,
determine size of long from elf data instead of using
sizeof(long) at build time.

For the time being this covers only mcount.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Rename variable "size" to "addrsize" and function
"elf_class_size()" to "elf_class_addrsize()", and modify
create_mcount_loc_sections() function to follow reverse christmas tree
format to order local variable declarations.]
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c   | 18 ++
 tools/objtool/elf.c |  8 ++--
 tools/objtool/include/objtool/elf.h |  8 
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index ad5dab175701..b64518c7c7b4 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -852,9 +852,9 @@ static int create_ibt_endbr_seal_sections(struct 
objtool_file *file)
 
 static int create_mcount_loc_sections(struct objtool_file *file)
 {
-   struct section *sec;
-   unsigned long *loc;
+   int addrsize = elf_class_addrsize(file->elf);
struct instruction *insn;
+   struct section *sec;
int idx;
 
sec = find_section_by_name(file->elf, "__mcount_loc");
@@ -871,23 +871,25 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
list_for_each_entry(insn, >mcount_loc_list, call_node)
idx++;
 
-   sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned 
long), idx);
+   sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx);
if (!sec)
return -1;
 
+   sec->sh.sh_addralign = addrsize;
+
idx = 0;
list_for_each_entry(insn, >mcount_loc_list, call_node) {
+   void *loc;
 
-   loc = (unsigned long *)sec->data->d_buf + idx;
-   memset(loc, 0, sizeof(unsigned long));
+   loc = sec->data->d_buf + idx;
+   memset(loc, 0, addrsize);
 
-   if (elf_add_reloc_to_insn(file->elf, sec,
- idx * sizeof(unsigned long),
+   if (elf_add_reloc_to_insn(file->elf, sec, idx,
  R_X86_64_64,
  insn->sec, insn->offset))
return -1;
 
-   idx++;
+   idx += addrsize;
}
 
return 0;
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 7e24b09b1163..33739865735b 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1129,6 +1129,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
 {
char *relocname;
struct section *sec;
+   int addrsize = elf_class_addrsize(elf);
 
relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
if (!relocname) {
@@ -1138,7 +1139,10 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
strcpy(relocname, ".rela");
strcat(relocname, base->name);
 
-   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
+   if (addrsize == sizeof(u32))
+   sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 
0);
+   else
+   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 
0);
free(relocname);
if (!sec)
return NULL;
@@ -1147,7 +1151,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
sec->base = base;
 
sec->sh.sh_type = SHT_RELA;
-   sec->sh.sh_addralign = 8;
+   sec->sh.sh_addralign = addrsize;
sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
sec->sh.sh_info = base->idx;
sec->sh.sh_flags = SHF_INFO_LINK;
diff --git a/tools/objtool/include/objtool/elf.h 
b/tools/objtool/include/objtool/elf.h
index 16f4067b82ae..78b3aa2e546d 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -142,6 +142,14 @@ static inline bool has_multiple_files(struct elf *elf)
return elf->num_files > 1;
 }
 
+static inline int elf_class_addrsize(struct elf *elf)
+{
+   if (elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+   return sizeof(u32);
+   else
+   return sizeof(u64);
+}
+
 struct elf *elf_open_read(const char *name, int flags);
 struct section *elf_create_section(struct elf *elf, const char *name, unsigned 
int sh_flags, size_t entsize, int nr);
 
-- 
2.31.1



[PATCH v5 09/16] objtool: Use target file endianness instead of a compiled constant

2022-10-28 Thread Sathvika Vasireddy
From: Christophe Leroy 

Some architectures like powerpc support both endianness, it's
therefore not possible to fix the endianness via arch/endianness.h
because there is no easy way to get the target endianness at
build time.

Use the endianness recorded in the file objtool is working on.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 .../arch/x86/include/arch/endianness.h|  9 --
 tools/objtool/check.c |  2 +-
 tools/objtool/include/objtool/endianness.h| 32 +--
 tools/objtool/orc_dump.c  | 11 +--
 tools/objtool/orc_gen.c   |  4 +--
 tools/objtool/special.c   |  3 +-
 6 files changed, 30 insertions(+), 31 deletions(-)
 delete mode 100644 tools/objtool/arch/x86/include/arch/endianness.h

diff --git a/tools/objtool/arch/x86/include/arch/endianness.h 
b/tools/objtool/arch/x86/include/arch/endianness.h
deleted file mode 100644
index 7c362527da20..
--- a/tools/objtool/arch/x86/include/arch/endianness.h
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-#ifndef _ARCH_ENDIANNESS_H
-#define _ARCH_ENDIANNESS_H
-
-#include 
-
-#define __TARGET_BYTE_ORDER __LITTLE_ENDIAN
-
-#endif /* _ARCH_ENDIANNESS_H */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 8427af808221..ad5dab175701 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2100,7 +2100,7 @@ static int read_unwind_hints(struct objtool_file *file)
return -1;
}
 
-   cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
+   cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
cfi.type = hint->type;
cfi.end = hint->end;
 
diff --git a/tools/objtool/include/objtool/endianness.h 
b/tools/objtool/include/objtool/endianness.h
index 10241341eff3..4d2aa9b0fe2f 100644
--- a/tools/objtool/include/objtool/endianness.h
+++ b/tools/objtool/include/objtool/endianness.h
@@ -2,33 +2,33 @@
 #ifndef _OBJTOOL_ENDIANNESS_H
 #define _OBJTOOL_ENDIANNESS_H
 
-#include 
 #include 
 #include 
-
-#ifndef __TARGET_BYTE_ORDER
-#error undefined arch __TARGET_BYTE_ORDER
-#endif
-
-#if __BYTE_ORDER != __TARGET_BYTE_ORDER
-#define __NEED_BSWAP 1
-#else
-#define __NEED_BSWAP 0
-#endif
+#include 
 
 /*
- * Does a byte swap if target endianness doesn't match the host, i.e. cross
+ * Does a byte swap if target file endianness doesn't match the host, i.e. 
cross
  * compilation for little endian on big endian and vice versa.
  * To be used for multi-byte values conversion, which are read from / about
  * to be written to a target native endianness ELF file.
  */
-#define bswap_if_needed(val)   \
+static inline bool need_bswap(struct elf *elf)
+{
+   return (__BYTE_ORDER == __LITTLE_ENDIAN) ^
+  (elf->ehdr.e_ident[EI_DATA] == ELFDATA2LSB);
+}
+
+#define bswap_if_needed(elf, val)  \
 ({ \
__typeof__(val) __ret;  \
+   bool __need_bswap = need_bswap(elf);\
switch (sizeof(val)) {  \
-   case 8: __ret = __NEED_BSWAP ? bswap_64(val) : (val); break;\
-   case 4: __ret = __NEED_BSWAP ? bswap_32(val) : (val); break;\
-   case 2: __ret = __NEED_BSWAP ? bswap_16(val) : (val); break;\
+   case 8: \
+   __ret = __need_bswap ? bswap_64(val) : (val); break;\
+   case 4: \
+   __ret = __need_bswap ? bswap_32(val) : (val); break;\
+   case 2: \
+   __ret = __need_bswap ? bswap_16(val) : (val); break;\
default:\
BUILD_BUG(); break; \
}   \
diff --git a/tools/objtool/orc_dump.c b/tools/objtool/orc_dump.c
index f5a8508c42d6..4f1211fec82c 100644
--- a/tools/objtool/orc_dump.c
+++ b/tools/objtool/orc_dump.c
@@ -76,6 +76,7 @@ int orc_dump(const char *_objname)
GElf_Rela rela;
GElf_Sym sym;
Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
+   struct elf dummy_elf = {};
 
 
objname = _objname;
@@ -94,6 +95,12 @@ int orc_dump(const char *_objname)
return -1;
}
 
+   if (!elf64_getehdr(elf)) {
+   WARN_ELF("elf64_getehdr");
+   return -1;
+   }
+   memcpy(_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
+
if 

[PATCH v5 08/16] objtool: Fix SEGFAULT

2022-10-28 Thread Sathvika Vasireddy
From: Christophe Leroy 

find_insn() will return NULL in case of failure. Check insn in order
to avoid a kernel Oops for NULL pointer dereference.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 43ec14c29a60..8427af808221 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -207,7 +207,7 @@ static bool __dead_end_function(struct objtool_file *file, 
struct symbol *func,
return false;
 
insn = find_insn(file, func->sec, func->offset);
-   if (!insn->func)
+   if (!insn || !insn->func)
return false;
 
func_for_each_insn(file, func, insn) {
-- 
2.31.1



[PATCH v5 07/16] powerpc: Skip objtool from running on VDSO files

2022-10-28 Thread Sathvika Vasireddy
Do not run objtool on VDSO files, by using OBJECT_FILES_NON_STANDARD.

Suggested-by: Christophe Leroy 
Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/vdso/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/powerpc/kernel/vdso/Makefile 
b/arch/powerpc/kernel/vdso/Makefile
index a2e7b0ce5b19..6a977b0d8ffc 100644
--- a/arch/powerpc/kernel/vdso/Makefile
+++ b/arch/powerpc/kernel/vdso/Makefile
@@ -102,3 +102,5 @@ quiet_cmd_vdso64ld_and_check = VDSO64L $@
   cmd_vdso64ld_and_check = $(VDSOCC) $(c_flags) $(CC64FLAGS) -o $@ 
-Wl,-T$(filter %.lds,$^) $(filter %.o,$^) -z noexecstack ; $(cmd_vdso_check)
 quiet_cmd_vdso64as = VDSO64A $@
   cmd_vdso64as = $(VDSOCC) $(a_flags) $(CC64FLAGS) $(AS64FLAGS) -c -o $@ $<
+
+OBJECT_FILES_NON_STANDARD := y
-- 
2.31.1



[PATCH v5 06/16] powerpc: Fix objtool unannotated intra-function call warnings on PPC32

2022-10-28 Thread Sathvika Vasireddy
From: Christophe Leroy 

Fix several annotations in assembly files on PPC32.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Acked-by: Josh Poimboeuf 
Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Changed subject line from "objtool/powerpc: Activate
objtool on PPC32" to "powerpc: Fix objtool unannotated intra-function call
warnings on PPC32", and removed Kconfig change to enable objtool, as it
is a part of "objtool/powerpc: Enable objtool to be built on ppc" patch in
this series.]
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/cpu_setup_6xx.S  | 26 --
 arch/powerpc/kernel/entry_32.S   |  9 --
 arch/powerpc/kernel/head_40x.S   |  5 +++-
 arch/powerpc/kernel/head_8xx.S   |  5 +++-
 arch/powerpc/kernel/head_book3s_32.S | 29 ++--
 arch/powerpc/kernel/swsusp_32.S  |  5 +++-
 arch/powerpc/kvm/fpu.S   | 17 
 arch/powerpc/platforms/52xx/lite5200_sleep.S | 15 +++---
 8 files changed, 79 insertions(+), 32 deletions(-)

diff --git a/arch/powerpc/kernel/cpu_setup_6xx.S 
b/arch/powerpc/kernel/cpu_setup_6xx.S
index f8b5ff64b604..f29ce3dd6140 100644
--- a/arch/powerpc/kernel/cpu_setup_6xx.S
+++ b/arch/powerpc/kernel/cpu_setup_6xx.S
@@ -4,6 +4,8 @@
  *Copyright (C) 2003 Benjamin Herrenschmidt (b...@kernel.crashing.org)
  */
 
+#include 
+
 #include 
 #include 
 #include 
@@ -81,7 +83,7 @@ _GLOBAL(__setup_cpu_745x)
blr
 
 /* Enable caches for 603's, 604, 750 & 7400 */
-setup_common_caches:
+SYM_FUNC_START_LOCAL(setup_common_caches)
mfspr   r11,SPRN_HID0
andi.   r0,r11,HID0_DCE
ori r11,r11,HID0_ICE|HID0_DCE
@@ -95,11 +97,12 @@ setup_common_caches:
sync
isync
blr
+SYM_FUNC_END(setup_common_caches)
 
 /* 604, 604e, 604ev, ...
  * Enable superscalar execution & branch history table
  */
-setup_604_hid0:
+SYM_FUNC_START_LOCAL(setup_604_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SIED|HID0_BHTE
ori r8,r11,HID0_BTCD
@@ -110,6 +113,7 @@ setup_604_hid0:
sync
isync
blr
+SYM_FUNC_END(setup_604_hid0)
 
 /* 7400 <= rev 2.7 and 7410 rev = 1.0 suffer from some
  * erratas we work around here.
@@ -125,13 +129,14 @@ setup_604_hid0:
  * needed once we have applied workaround #5 (though it's
  * not set by Apple's firmware at least).
  */
-setup_7400_workarounds:
+SYM_FUNC_START_LOCAL(setup_7400_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0207
ble 1f
blr
-setup_7410_workarounds:
+SYM_FUNC_END(setup_7400_workarounds)
+SYM_FUNC_START_LOCAL(setup_7410_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0100
@@ -151,6 +156,7 @@ setup_7410_workarounds:
sync
isync
blr
+SYM_FUNC_END(setup_7410_workarounds)
 
 /* 740/750/7400/7410
  * Enable Store Gathering (SGE), Address Broadcast (ABE),
@@ -158,7 +164,7 @@ setup_7410_workarounds:
  * Dynamic Power Management (DPM), Speculative (SPD)
  * Clear Instruction cache throttling (ICTC)
  */
-setup_750_7400_hid0:
+SYM_FUNC_START_LOCAL(setup_750_7400_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SGE | HID0_ABE | HID0_BHTE | HID0_BTIC
orisr11,r11,HID0_DPM@h
@@ -177,12 +183,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_750_7400_hid0)
 
 /* 750cx specific
  * Looks like we have to disable NAP feature for some PLL settings...
  * (waiting for confirmation)
  */
-setup_750cx:
+SYM_FUNC_START_LOCAL(setup_750cx)
mfspr   r10, SPRN_HID1
rlwinm  r10,r10,4,28,31
cmpwi   cr0,r10,7
@@ -196,11 +203,13 @@ setup_750cx:
andcr6,r6,r7
stw r6,CPU_SPEC_FEATURES(r4)
blr
+SYM_FUNC_END(setup_750cx)
 
 /* 750fx specific
  */
-setup_750fx:
+SYM_FUNC_START_LOCAL(setup_750fx)
blr
+SYM_FUNC_END(setup_750fx)
 
 /* MPC 745x
  * Enable Store Gathering (SGE), Branch Folding (FOLD)
@@ -212,7 +221,7 @@ setup_750fx:
  * Clear Instruction cache throttling (ICTC)
  * Enable L2 HW prefetch
  */
-setup_745x_specifics:
+SYM_FUNC_START_LOCAL(setup_745x_specifics)
/* We check for the presence of an L3 cache setup by
 * the firmware. If any, we disable NAP capability as
 * it's known to be bogus on rev 2.1 and earlier
@@ -270,6 +279,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_745x_specifics)
 
 /*
  * Initialize the FPU registers. This is needed to work around an errata
diff --git a/arch/powerpc/kernel/entry_32.S b/arch/powerpc/kernel/entry_32.S
index 3fc7c9886bb7..5e0763be1549 100644
--- a/arch/powerpc/kernel/entry_32.S
+++ b/arch/powerpc/kernel/entry_32.S
@@ -18,6 +18,8 @@
 #include 
 #include 
 #include 
+#include 
+
 #include 
 #include 
 #include 
@@ -74,17 +76,18 @@ _ASM_NO

[PATCH v5 05/16] powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o

2022-10-28 Thread Sathvika Vasireddy
With objtool enabled, below warnings are seen when trying to build:
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: aes_p8_set_encrypt_key+0x44: 
unannotated intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2448: unannotated 
intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2d68: unannotated 
intra-function call

Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o file for the
following reasons:

- Since this file comes from OpenSSL, and since it is a perl file which
  generates a .S file, it may not be the best choice to make too many
  code changes to such files, unless absolutely necessary.

- As far as the objtool --mcount functionality is concerned, we do not
  have to run objtool on this file because there are no calls to
  _mcount.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 drivers/crypto/vmx/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/crypto/vmx/Makefile b/drivers/crypto/vmx/Makefile
index 2560cfea1dec..7b41f0da6807 100644
--- a/drivers/crypto/vmx/Makefile
+++ b/drivers/crypto/vmx/Makefile
@@ -9,3 +9,5 @@ targets += aesp8-ppc.S ghashp8-ppc.S
 
 $(obj)/aesp8-ppc.S $(obj)/ghashp8-ppc.S: $(obj)/%.S: $(src)/%.pl FORCE
$(call if_changed,perl)
+
+OBJECT_FILES_NON_STANDARD_aesp8-ppc.o := y
-- 
2.31.1



[PATCH v5 04/16] powerpc: Curb objtool unannotated intra-function warnings

2022-10-28 Thread Sathvika Vasireddy
objtool throws the following unannotated intra-function call warnings:
arch/powerpc/kernel/entry_64.o: warning: objtool: .text+0x4: unannotated 
intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xe64: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xee4: 
unannotated intra-function call

Fix these warnings by annotating intra-function calls, using
ANNOTATE_INTRA_FUNCTION_CALL macro, to indicate that the branch targets
are valid.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/entry_64.S  | 2 ++
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
index 3e2e37e6ecab..1bf1121e17f1 100644
--- a/arch/powerpc/kernel/entry_64.S
+++ b/arch/powerpc/kernel/entry_64.S
@@ -14,6 +14,7 @@
  *  code, and exception/interrupt return code for PowerPC.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -73,6 +74,7 @@ flush_branch_caches:
 
// Flush the link stack
.rept 64
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
b   1f
diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S 
b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index a69d36cbf43b..96b65b530156 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -11,6 +11,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1523,12 +1524,14 @@ kvm_flush_link_stack:
 
/* Flush the link stack. On Power8 it's up to 32 entries in size. */
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 
/* And on Power9 it's up to 64. */
 BEGIN_FTR_SECTION
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
-- 
2.31.1



[PATCH v5 03/16] powerpc: Fix objtool unannotated intra-function call warnings

2022-10-28 Thread Sathvika Vasireddy
Objtool throws unannotated intra-function call warnings in the following
assembly files:

arch/powerpc/kernel/vector.o: warning: objtool: .text+0x53c: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x60: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x124: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5d4: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5dc: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xcb8: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xd0c: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x1030: 
unannotated intra-function call

arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x358: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x728: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4d94: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4ec4: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_interrupts.o: warning: objtool: .text+0x6c: 
unannotated intra-function call
arch/powerpc/kernel/misc_64.o: warning: objtool: .text+0x64: unannotated 
intra-function call

Objtool does not add STT_NOTYPE symbols with size 0 to the rbtree, which
is why find_call_destination() function is not able to find the
destination symbol for 'bl' instruction. For such symbols, objtool is
throwing unannotated intra-function call warnings in assembly files. Fix
these warnings by annotating those symbols with SYM_FUNC_START_LOCAL and
SYM_FUNC_END macros, inorder to set symbol type to STT_FUNC and symbol
size accordingly.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/exceptions-64s.S|  4 +++-
 arch/powerpc/kernel/head_64.S   |  7 +--
 arch/powerpc/kernel/misc_64.S   |  4 +++-
 arch/powerpc/kernel/vector.S|  4 +++-
 arch/powerpc/kvm/book3s_hv_interrupts.S |  4 +++-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 22 +++---
 6 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/arch/powerpc/kernel/exceptions-64s.S 
b/arch/powerpc/kernel/exceptions-64s.S
index 5381a43e50fe..77201ad9f329 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -13,6 +13,7 @@
  *
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -3112,7 +3113,7 @@ _GLOBAL(enable_machine_check)
blr
 
 /* MSR[RI] should be clear because this uses SRR[01] */
-disable_machine_check:
+SYM_FUNC_START_LOCAL(disable_machine_check)
mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3125,3 +3126,4 @@ disable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(disable_machine_check)
diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index dedcc6fe2263..874efd25cc45 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -18,6 +18,7 @@
  *  variants.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -462,7 +463,7 @@ generic_secondary_common_init:
  * Assumes we're mapped EA == RA if the MMU is on.
  */
 #ifdef CONFIG_PPC_BOOK3S
-__mmu_off:
+SYM_FUNC_START_LOCAL(__mmu_off)
mfmsr   r3
andi.   r0,r3,MSR_IR|MSR_DR
beqlr
@@ -473,6 +474,7 @@ __mmu_off:
sync
rfid
b   .   /* prevent speculative execution */
+SYM_FUNC_END(__mmu_off)
 #endif
 
 
@@ -869,7 +871,7 @@ _GLOBAL(start_secondary_resume)
 /*
  * This subroutine clobbers r11 and r12
  */
-enable_64b_mode:
+SYM_FUNC_START_LOCAL(enable_64b_mode)
mfmsr   r11 /* grab the current MSR */
 #ifdef CONFIG_PPC_BOOK3E_64
orisr11,r11,0x8000  /* CM bit set, we'll set ICM later */
@@ -881,6 +883,7 @@ enable_64b_mode:
isync
 #endif
blr
+SYM_FUNC_END(enable_64b_mode)
 
 /*
  * This puts the TOC pointer into r2, offset by 0x8000 (as expected
diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
index 36184cada00b..c61a7ba446a8 100644
--- a/arch/powerpc/kernel/misc_64.S
+++ b/arch/powerpc/kernel/misc_64.S
@@ -9,6 +9,7 @@
  * PPC64 updates by Dave Engebretsen (engeb...@us.ibm.com)
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -353,7 +354,7 @@ _GLOBAL(kexec_smp_wait)
  *
  * don't overwrite r3 here, it is live for kexec_wait above.
  */
-real_mode: /* assume normal blr return */
+SYM_FUNC_START_LOCAL(real_mode)/* assume normal blr return */
 #ifdef CONFIG_PPC_BOOK3E_64
/* Create an identity mapping. */
b   kexec_create_tlb

[PATCH v5 02/16] powerpc: Override __ALIGN and __ALIGN_STR macros

2022-10-28 Thread Sathvika Vasireddy
In a subsequent patch, we would want to annotate powerpc assembly functions
with SYM_FUNC_START_LOCAL macro. This macro depends on __ALIGN macro.

The default expansion of __ALIGN macro is:
#define __ALIGN  .align 4,0x90

So, override __ALIGN and __ALIGN_STR macros to use the same alignment as
that of the existing _GLOBAL macro. Also, do not pad with 0x90, because
repeated 0x90s are not a nop or trap on powerpc.

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/linkage.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/powerpc/include/asm/linkage.h 
b/arch/powerpc/include/asm/linkage.h
index b71b9582e754..b88d1d2cf304 100644
--- a/arch/powerpc/include/asm/linkage.h
+++ b/arch/powerpc/include/asm/linkage.h
@@ -4,6 +4,9 @@
 
 #include 
 
+#define __ALIGN.align 2
+#define __ALIGN_STR".align 2"
+
 #ifdef CONFIG_PPC64_ELF_ABI_V1
 #define cond_syscall(x) \
asm ("\t.weak " #x "\n\t.set " #x ", sys_ni_syscall\n"  \
-- 
2.31.1



[PATCH v5 00/16] objtool: Enable and implement --mcount option on powerpc

2022-10-28 Thread Sathvika Vasireddy
This patchset enables and implements objtool --mcount
option on powerpc. This applies atop powerpc/merge branch.

Changelog:


v5:

* Patch 02/16 - Add Reviewed-by tag from Christophe Leroy

* Patch 03/16 - Fix merge conflicts with latest powerpc/merge branch

* Patch 06/16 - Files arch/powerpc/kernel/cpu_setup_fsl_booke.S and
arch/powerpc/kernel/head_fsl_booke.S are not present
today. Removed annotations in those assembly files.

* Patch 11/16 - Add Reviewed-by tag from Christophe Leroy
  - Changed scripts/Makefile.lib file to make
CONFIG_HAVE_OBJTOOL_NOP_MCOUNT depend on
CONFIG_FTRACE_MCOUNT_USE_OBJTOOL.

* Patch 12/16 - Add Reviewed-by tag from Christophe Leroy

* Patch 16/16 - Add Reviewed-by tag from Christophe Leroy 

* For this series - Add Acked-by tag from Josh Poimboeuf
  - Add Tested-by tag from Naveen N. Rao
  - Add Reviewed-by tag from Naveen N. Rao

v4:

* Patch 11/16 - Introduce a new config option
CONFIG_HAVE_OBJTOOL_NOP_MCOUNT as a means for
architectures to enable nop'ing ftrace locations.

  - Remove Acked-by tag from Peter Zijlstra (Intel),
and Reviewed-by tag from Christophe Leroy.
[This is done because I reworked the patch to add
a new config option to objtool. Please let me know
if you want me to retain the tags. Thanks!]

* Patch 16/16 - Rework the patch to handle only 'bl' instruction
decoding.


v3:

* Patch 01/16 - Rework patch subject.
  - Rework changelog.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 02/16 - Rework changelog to update details based on feedback
from Nicholas Piggin and Michael Ellerman.
  - Use quotes instead of __stringify macro, based on
suggestion from Christophe Leroy.

* Patch 03/16 - Add Reviewed-by tag from Christophe Leroy.
  - Based on Christophe's suggestion, keep all 
before .
  - Rework changelog.

* Patch 04/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 05/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 06/16 - No change.

* Patch 07/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 08/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 09/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 10/16 - Reorder local variable declarations to use reverse
xmas tree format.
  - Add Signed-off-by tag from Sathvika Vasireddy indicating
changes done.
  - Add Acked-by tag from Peter Zijlstra.

* Patch 11/16 - Update changelog to indicate that powerpc kernel does
not support nop'ed out ftrace locations.
  - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 12/16 - Per Christophe's comment, rework changelog.

* Patch 13/16 - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 14/16 - Simplify arch_ftrace_match() function, based on
Christophe's suggestion.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 15/16 - Include code from Christophe Leroy to use local vars for
type and imm, and to adapt len for prefixed
instructions.

* Patch 16/16 - Based on suggestion from Christophe Leroy, setup
immediate value calculation outside the check for
specific instruction under case 18.
  - Set instruction type to INSN_CALL for 'bla'
instruction as well.


v2:

* Change subject of patch 01/16
* As suggested by Christophe Leroy, add barrier_before_unreachable()
before __builtin_unreachable() to work around a gcc problem.
* Fix issues reported by Kernel Test Robot.
* Include suggestions from Christophe Leroy, and change commit
messages for patches 01/16, 02/16, 03/16, 05/16.



Christophe Leroy (4):
  objtool: Fix SEGFAULT
  objtool: Use target file endianness instead of a compiled constant
  objtool: Use target file class size instead of a compiled constant
  powerpc: Fix objtool unannotated intra-function call warnings on PPC32

Sathvika Vasireddy (12):
  powerpc: Fix __WARN_FLAGS() for use with Objtool
  powerpc: Override __ALIGN and __ALIGN_STR macros
  powerpc: Fix objtool unannotated intra-function call warnings
  powerpc: Curb objtool unannotated intra-function warnings
  powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o
  powerpc: Skip objtool from running on VDSO files
  objtool: Add --mnop as an option to --mcount
  objtool: Read special sections with alts only when specific options are 
selected
  objtool: Use macros to define arch specific reloc types
  objtool: Add arch specific function arch_ftrace_match()
  objtool/powerpc: Enable objtool

[PATCH v5 01/16] powerpc: Fix __WARN_FLAGS() for use with Objtool

2022-10-28 Thread Sathvika Vasireddy
Commit 1e688dd2a3d675 ("powerpc/bug: Provide better flexibility to
WARN_ON/__WARN_FLAGS() with asm goto") updated __WARN_FLAGS() to use asm
goto, and added a call to 'unreachable()' after the asm goto for optimal
code generation. With CONFIG_OBJTOOL enabled, 'annotate_unreachable()'
statement in 'unreachable()' tries to note down the location of the
subsequent instruction in a separate elf section to aid code flow
analysis. However, on powerpc, this results in gcc emitting a call to a
symbol of size 0. This results in objtool complaining of "unannotated
intra-function call" since the target symbol is not a valid function
call destination.

Objtool wants this annotation for code flow analysis, which we are not
yet enabling on powerpc. As such, expand the call to 'unreachable()' in
__WARN_FLAGS() without annotate_unreachable():
barrier_before_unreachable();
__builtin_unreachable();

This still results in optimal code generation for __WARN_FLAGS(), while
getting rid of the objtool warning.

We still need barrier_before_unreachable() to work around gcc bugs 82365
and 106751:
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106751

Tested-by: Naveen N. Rao 
Reviewed-by: Naveen N. Rao 
Reviewed-by: Christophe Leroy 
Acked-by: Josh Poimboeuf 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/bug.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 61a4736355c2..ef42adb44aa3 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -99,7 +99,8 @@
__label__ __label_warn_on;  \
\
WARN_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags), __label_warn_on); 
\
-   unreachable();  \
+   barrier_before_unreachable();   \
+   __builtin_unreachable();\
\
 __label_warn_on:   \
break;  \
-- 
2.31.1



[PATCH v4 16/16] objtool/powerpc: Add --mcount specific implementation

2022-10-02 Thread Sathvika Vasireddy
This patch enables objtool --mcount on powerpc, and adds implementation
specific to powerpc.

Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/Kconfig  |  1 +
 tools/objtool/arch/powerpc/decode.c   | 16 
 tools/objtool/arch/powerpc/include/arch/elf.h |  2 ++
 3 files changed, 19 insertions(+)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index dc05cd23c233..6be2e68fa9eb 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -238,6 +238,7 @@ config PPC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
+   select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
index dcd0975cad6b..01cade98b49e 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -9,6 +9,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "_mcount");
+}
+
 unsigned long arch_dest_reloc_offset(int addend)
 {
return addend;
@@ -50,6 +55,17 @@ int arch_decode_instruction(struct objtool_file *file, const 
struct section *sec
typ = INSN_OTHER;
imm = 0;
 
+   switch (opcode) {
+   case 18: /* b[l][a] */
+   if ((insn & 3) == 1) /* bl */
+   typ = INSN_CALL;
+
+   imm = insn & 0x3fc;
+   if (imm & 0x200)
+   imm -= 0x400;
+   break;
+   }
+
if (opcode == 1)
*len = 8;
else
diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h 
b/tools/objtool/arch/powerpc/include/arch/elf.h
index 3c8ebb7d2a6b..73f9ae172fe5 100644
--- a/tools/objtool/arch/powerpc/include/arch/elf.h
+++ b/tools/objtool/arch/powerpc/include/arch/elf.h
@@ -4,5 +4,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_PPC_NONE
+#define R_ABS64 R_PPC64_ADDR64
+#define R_ABS32 R_PPC_ADDR32
 
 #endif /* _OBJTOOL_ARCH_ELF */
-- 
2.31.1



[PATCH v4 15/16] objtool/powerpc: Enable objtool to be built on ppc

2022-10-02 Thread Sathvika Vasireddy
This patch adds [stub] implementations for required functions, inorder
to enable objtool build on powerpc.

Signed-off-by: Sathvika Vasireddy 
[Christophe Leroy: powerpc: Add missing asm/asm.h for objtool,
Use local variables for type and imm in arch_decode_instruction(),
Adapt len for prefixed instructions.]
Signed-off-by: Christophe Leroy 
---
 arch/powerpc/Kconfig  |  1 +
 arch/powerpc/include/asm/asm.h|  7 ++
 tools/objtool/arch/powerpc/Build  |  2 +
 tools/objtool/arch/powerpc/decode.c   | 85 +++
 .../arch/powerpc/include/arch/cfi_regs.h  | 11 +++
 tools/objtool/arch/powerpc/include/arch/elf.h |  8 ++
 .../arch/powerpc/include/arch/special.h   | 21 +
 tools/objtool/arch/powerpc/special.c  | 19 +
 8 files changed, 154 insertions(+)
 create mode 100644 arch/powerpc/include/asm/asm.h
 create mode 100644 tools/objtool/arch/powerpc/Build
 create mode 100644 tools/objtool/arch/powerpc/decode.c
 create mode 100644 tools/objtool/arch/powerpc/include/arch/cfi_regs.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/elf.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/special.h
 create mode 100644 tools/objtool/arch/powerpc/special.c

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 4c466acdc70d..dc05cd23c233 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -237,6 +237,7 @@ config PPC
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
+   select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/arch/powerpc/include/asm/asm.h b/arch/powerpc/include/asm/asm.h
new file mode 100644
index ..86f46b604e9a
--- /dev/null
+++ b/arch/powerpc/include/asm/asm.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_POWERPC_ASM_H
+#define _ASM_POWERPC_ASM_H
+
+#define _ASM_PTR   " .long "
+
+#endif /* _ASM_POWERPC_ASM_H */
diff --git a/tools/objtool/arch/powerpc/Build b/tools/objtool/arch/powerpc/Build
new file mode 100644
index ..d24d5636a5b8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/Build
@@ -0,0 +1,2 @@
+objtool-y += decode.o
+objtool-y += special.o
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
new file mode 100644
index ..dcd0975cad6b
--- /dev/null
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+unsigned long arch_dest_reloc_offset(int addend)
+{
+   return addend;
+}
+
+bool arch_callee_saved_reg(unsigned char reg)
+{
+   return false;
+}
+
+int arch_decode_hint_reg(u8 sp_reg, int *base)
+{
+   exit(-1);
+}
+
+const char *arch_nop_insn(int len)
+{
+   exit(-1);
+}
+
+const char *arch_ret_insn(int len)
+{
+   exit(-1);
+}
+
+int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
+   unsigned long offset, unsigned int maxlen,
+   unsigned int *len, enum insn_type *type,
+   unsigned long *immediate,
+   struct list_head *ops_list)
+{
+   unsigned int opcode;
+   enum insn_type typ;
+   unsigned long imm;
+   u32 insn;
+
+   insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
+   opcode = insn >> 26;
+   typ = INSN_OTHER;
+   imm = 0;
+
+   if (opcode == 1)
+   *len = 8;
+   else
+   *len = 4;
+
+   *type = typ;
+   *immediate = imm;
+
+   return 0;
+}
+
+unsigned long arch_jump_destination(struct instruction *insn)
+{
+   return insn->offset + insn->immediate;
+}
+
+void arch_initial_func_cfi_state(struct cfi_init_state *state)
+{
+   int i;
+
+   for (i = 0; i < CFI_NUM_REGS; i++) {
+   state->regs[i].base = CFI_UNDEFINED;
+   state->regs[i].offset = 0;
+   }
+
+   /* initial CFA (call frame address) */
+   state->cfa.base = CFI_SP;
+   state->cfa.offset = 0;
+
+   /* initial LR (return address) */
+   state->regs[CFI_RA].base = CFI_CFA;
+   state->regs[CFI_RA].offset = 0;
+}
diff --git a/tools/objtool/arch/powerpc/include/arch/cfi_regs.h 
b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
new file mode 100644
index ..59638ebeafc8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _OBJTOOL_CFI_REGS_H
+#define _OBJTOOL_CFI_REGS_H
+
+#define CFI_BP 1
+#define CFI_SP CFI_BP
+#define CFI_RA 32
+#define CFI_N

[PATCH v4 14/16] objtool: Add arch specific function arch_ftrace_match()

2022-10-02 Thread Sathvika Vasireddy
Add architecture specific function to look for relocation records
pointing to architecture specific symbols.

Suggested-by: Christophe Leroy 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/decode.c  | 5 +
 tools/objtool/check.c| 2 +-
 tools/objtool/include/objtool/arch.h | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index c260006106be..28263e231c58 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -23,6 +23,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "__fentry__");
+}
+
 static int is_x86_64(const struct elf *elf)
 {
switch (elf->ehdr.e_machine) {
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 35bcd4f90acd..856ea80b1cfc 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2296,7 +2296,7 @@ static int classify_symbols(struct objtool_file *file)
if (arch_is_rethunk(func))
func->return_thunk = true;
 
-   if (!strcmp(func->name, "__fentry__"))
+   if (arch_ftrace_match(func->name))
func->fentry = true;
 
if (is_profiling_func(func->name))
diff --git a/tools/objtool/include/objtool/arch.h 
b/tools/objtool/include/objtool/arch.h
index beb2f3aa94ff..5149330f400f 100644
--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -69,6 +69,8 @@ struct stack_op {
 
 struct instruction;
 
+int arch_ftrace_match(char *name);
+
 void arch_initial_func_cfi_state(struct cfi_init_state *state);
 
 int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
-- 
2.31.1



[PATCH v4 13/16] objtool: Use macros to define arch specific reloc types

2022-10-02 Thread Sathvika Vasireddy
Make relocation types architecture specific.

Acked-by: Peter Zijlstra (Intel) 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/include/arch/elf.h | 2 ++
 tools/objtool/check.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/include/arch/elf.h 
b/tools/objtool/arch/x86/include/arch/elf.h
index 69cc4264b28a..ac14987cf687 100644
--- a/tools/objtool/arch/x86/include/arch/elf.h
+++ b/tools/objtool/arch/x86/include/arch/elf.h
@@ -2,5 +2,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_X86_64_NONE
+#define R_ABS64 R_X86_64_64
+#define R_ABS32 R_X86_64_32
 
 #endif /* _OBJTOOL_ARCH_ELF */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index c6457dac6d58..35bcd4f90acd 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -885,7 +885,7 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
memset(loc, 0, addrsize);
 
if (elf_add_reloc_to_insn(file->elf, sec, idx,
- R_X86_64_64,
+ addrsize == sizeof(u64) ? R_ABS64 : 
R_ABS32,
  insn->sec, insn->offset))
return -1;
 
-- 
2.31.1



[PATCH v4 12/16] objtool: Read special sections with alts only when specific options are selected

2022-10-02 Thread Sathvika Vasireddy
Call add_special_section_alts() only when stackval or orc or uaccess or
noinstr options are passed to objtool.

Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 35827e6c6df9..c6457dac6d58 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2372,9 +2372,11 @@ static int decode_sections(struct objtool_file *file)
 * Must be before add_jump_destinations(), which depends on 'func'
 * being set for alternatives, to enable proper sibling call detection.
 */
-   ret = add_special_section_alts(file);
-   if (ret)
-   return ret;
+   if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
+   ret = add_special_section_alts(file);
+   if (ret)
+   return ret;
+   }
 
ret = add_jump_destinations(file);
if (ret)
-- 
2.31.1



[PATCH v4 11/16] objtool: Add --mnop as an option to --mcount

2022-10-02 Thread Sathvika Vasireddy
Some architectures (powerpc) may not support ftrace locations being nop'ed
out at build time. Introduce CONFIG_HAVE_OBJTOOL_NOP_MCOUNT for objtool, as
a means for architectures to enable nop'ing of ftrace locations. Add --mnop
as an option to objtool --mcount, to indicate support for the same.

Also, make sure that --mnop can be passed as an option to objtool only when
--mcount is passed.

Signed-off-by: Sathvika Vasireddy 
---
 Makefile|  4 +++-
 arch/x86/Kconfig|  1 +
 kernel/trace/Kconfig|  7 +++
 scripts/Makefile.lib|  1 +
 tools/objtool/builtin-check.c   | 14 ++
 tools/objtool/check.c   | 19 ++-
 tools/objtool/include/objtool/builtin.h |  1 +
 7 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index a5e9d9388649..b2230ad14748 100644
--- a/Makefile
+++ b/Makefile
@@ -857,7 +857,9 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_CC
   endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
-  CC_FLAGS_USING   += -DCC_USING_NOP_MCOUNT
+  ifdef CONFIG_HAVE_OBJTOOL_NOP_MCOUNT
+CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
+  endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
   ifdef CONFIG_HAVE_C_RECORDMCOUNT
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index f9920f1341c8..2a79a05c4402 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -189,6 +189,7 @@ config X86
select HAVE_CONTEXT_TRACKING_USER_OFFSTACK  if 
HAVE_CONTEXT_TRACKING_USER
select HAVE_C_RECORDMCOUNT
select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
+   select HAVE_OBJTOOL_NOP_MCOUNT  if HAVE_OBJTOOL_MCOUNT
select HAVE_BUILDTIME_MCOUNT_SORT
select HAVE_DEBUG_KMEMLEAK
select HAVE_DMA_CONTIGUOUS
diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
index 1052126bdca2..9c696cb24756 100644
--- a/kernel/trace/Kconfig
+++ b/kernel/trace/Kconfig
@@ -76,6 +76,13 @@ config HAVE_OBJTOOL_MCOUNT
help
  Arch supports objtool --mcount
 
+config HAVE_OBJTOOL_NOP_MCOUNT
+   bool
+   help
+ Arch supports the objtool options --mcount with --mnop.
+ An architecture can select this if it wants to enable nop'ing
+ of ftrace locations.
+
 config HAVE_C_RECORDMCOUNT
bool
help
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3fb6a99e78c4..ce14e3b8577f 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -234,6 +234,7 @@ objtool_args =  
\
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr)  \
$(if $(CONFIG_X86_KERNEL_IBT), --ibt)   \
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
+   $(if $(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT), --mnop) \
$(if $(CONFIG_UNWINDER_ORC), --orc) \
$(if $(CONFIG_RETPOLINE), --retpoline)  \
$(if $(CONFIG_RETHUNK), --rethunk)  \
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 24fbe803a0d3..9bd347d3c244 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -82,6 +82,7 @@ const struct option check_options[] = {
OPT_BOOLEAN(0, "dry-run", , "don't write modifications"),
OPT_BOOLEAN(0, "link", , "object is a linked object"),
OPT_BOOLEAN(0, "module", , "object is part of a kernel 
module"),
+   OPT_BOOLEAN(0, "mnop", , "nop out mcount call sites"),
OPT_BOOLEAN(0, "no-unreachable", _unreachable, "skip 
'unreachable instruction' warnings"),
OPT_BOOLEAN(0, "sec-address", _address, "print section 
addresses in warnings"),
OPT_BOOLEAN(0, "stats", , "print statistics"),
@@ -150,6 +151,16 @@ static bool opts_valid(void)
return false;
 }
 
+static bool mnop_opts_valid(void)
+{
+   if (opts.mnop && !opts.mcount) {
+   ERROR("--mnop requires --mcount");
+   return false;
+   }
+
+   return true;
+}
+
 static bool link_opts_valid(struct objtool_file *file)
 {
if (opts.link)
@@ -198,6 +209,9 @@ int objtool_run(int argc, const char **argv)
if (!file)
return 1;
 
+   if (!mnop_opts_valid())
+   return 1;
+
if (!link_opts_valid(file))
return 1;
 
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 738de23cb9e8..35827e6c6df9 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1233,17 +1233,18 @@ static void annotate_call_site(struct objtool_file 
*file,
if (opts.mcount && sym->fentry) {
if 

[PATCH v4 10/16] objtool: Use target file class size instead of a compiled constant

2022-10-02 Thread Sathvika Vasireddy
From: Christophe Leroy 

In order to allow using objtool on cross-built kernels,
determine size of long from elf data instead of using
sizeof(long) at build time.

For the time being this covers only mcount.

Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Rename variable "size" to "addrsize" and function
"elf_class_size()" to "elf_class_addrsize()", and modify
create_mcount_loc_sections() function to follow reverse christmas tree
format to order local variable declarations.]
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c   | 18 ++
 tools/objtool/elf.c |  8 ++--
 tools/objtool/include/objtool/elf.h |  8 
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index c36e7a020d80..738de23cb9e8 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -852,9 +852,9 @@ static int create_ibt_endbr_seal_sections(struct 
objtool_file *file)
 
 static int create_mcount_loc_sections(struct objtool_file *file)
 {
-   struct section *sec;
-   unsigned long *loc;
+   int addrsize = elf_class_addrsize(file->elf);
struct instruction *insn;
+   struct section *sec;
int idx;
 
sec = find_section_by_name(file->elf, "__mcount_loc");
@@ -871,23 +871,25 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
list_for_each_entry(insn, >mcount_loc_list, call_node)
idx++;
 
-   sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned 
long), idx);
+   sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx);
if (!sec)
return -1;
 
+   sec->sh.sh_addralign = addrsize;
+
idx = 0;
list_for_each_entry(insn, >mcount_loc_list, call_node) {
+   void *loc;
 
-   loc = (unsigned long *)sec->data->d_buf + idx;
-   memset(loc, 0, sizeof(unsigned long));
+   loc = sec->data->d_buf + idx;
+   memset(loc, 0, addrsize);
 
-   if (elf_add_reloc_to_insn(file->elf, sec,
- idx * sizeof(unsigned long),
+   if (elf_add_reloc_to_insn(file->elf, sec, idx,
  R_X86_64_64,
  insn->sec, insn->offset))
return -1;
 
-   idx++;
+   idx += addrsize;
}
 
return 0;
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index c25e957c1e52..40c6d53b081f 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1124,6 +1124,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
 {
char *relocname;
struct section *sec;
+   int addrsize = elf_class_addrsize(elf);
 
relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
if (!relocname) {
@@ -1133,7 +1134,10 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
strcpy(relocname, ".rela");
strcat(relocname, base->name);
 
-   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
+   if (addrsize == sizeof(u32))
+   sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 
0);
+   else
+   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 
0);
free(relocname);
if (!sec)
return NULL;
@@ -1142,7 +1146,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
sec->base = base;
 
sec->sh.sh_type = SHT_RELA;
-   sec->sh.sh_addralign = 8;
+   sec->sh.sh_addralign = addrsize;
sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
sec->sh.sh_info = base->idx;
sec->sh.sh_flags = SHF_INFO_LINK;
diff --git a/tools/objtool/include/objtool/elf.h 
b/tools/objtool/include/objtool/elf.h
index 16f4067b82ae..78b3aa2e546d 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -142,6 +142,14 @@ static inline bool has_multiple_files(struct elf *elf)
return elf->num_files > 1;
 }
 
+static inline int elf_class_addrsize(struct elf *elf)
+{
+   if (elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+   return sizeof(u32);
+   else
+   return sizeof(u64);
+}
+
 struct elf *elf_open_read(const char *name, int flags);
 struct section *elf_create_section(struct elf *elf, const char *name, unsigned 
int sh_flags, size_t entsize, int nr);
 
-- 
2.31.1



[PATCH v4 09/16] objtool: Use target file endianness instead of a compiled constant

2022-10-02 Thread Sathvika Vasireddy
From: Christophe Leroy 

Some architectures like powerpc support both endianness, it's
therefore not possible to fix the endianness via arch/endianness.h
because there is no easy way to get the target endianness at
build time.

Use the endianness recorded in the file objtool is working on.

Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 .../arch/x86/include/arch/endianness.h|  9 --
 tools/objtool/check.c |  2 +-
 tools/objtool/include/objtool/endianness.h| 32 +--
 tools/objtool/orc_dump.c  | 11 +--
 tools/objtool/orc_gen.c   |  4 +--
 tools/objtool/special.c   |  3 +-
 6 files changed, 30 insertions(+), 31 deletions(-)
 delete mode 100644 tools/objtool/arch/x86/include/arch/endianness.h

diff --git a/tools/objtool/arch/x86/include/arch/endianness.h 
b/tools/objtool/arch/x86/include/arch/endianness.h
deleted file mode 100644
index 7c362527da20..
--- a/tools/objtool/arch/x86/include/arch/endianness.h
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-#ifndef _ARCH_ENDIANNESS_H
-#define _ARCH_ENDIANNESS_H
-
-#include 
-
-#define __TARGET_BYTE_ORDER __LITTLE_ENDIAN
-
-#endif /* _ARCH_ENDIANNESS_H */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index fe935f19447b..c36e7a020d80 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2077,7 +2077,7 @@ static int read_unwind_hints(struct objtool_file *file)
return -1;
}
 
-   cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
+   cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
cfi.type = hint->type;
cfi.end = hint->end;
 
diff --git a/tools/objtool/include/objtool/endianness.h 
b/tools/objtool/include/objtool/endianness.h
index 10241341eff3..4d2aa9b0fe2f 100644
--- a/tools/objtool/include/objtool/endianness.h
+++ b/tools/objtool/include/objtool/endianness.h
@@ -2,33 +2,33 @@
 #ifndef _OBJTOOL_ENDIANNESS_H
 #define _OBJTOOL_ENDIANNESS_H
 
-#include 
 #include 
 #include 
-
-#ifndef __TARGET_BYTE_ORDER
-#error undefined arch __TARGET_BYTE_ORDER
-#endif
-
-#if __BYTE_ORDER != __TARGET_BYTE_ORDER
-#define __NEED_BSWAP 1
-#else
-#define __NEED_BSWAP 0
-#endif
+#include 
 
 /*
- * Does a byte swap if target endianness doesn't match the host, i.e. cross
+ * Does a byte swap if target file endianness doesn't match the host, i.e. 
cross
  * compilation for little endian on big endian and vice versa.
  * To be used for multi-byte values conversion, which are read from / about
  * to be written to a target native endianness ELF file.
  */
-#define bswap_if_needed(val)   \
+static inline bool need_bswap(struct elf *elf)
+{
+   return (__BYTE_ORDER == __LITTLE_ENDIAN) ^
+  (elf->ehdr.e_ident[EI_DATA] == ELFDATA2LSB);
+}
+
+#define bswap_if_needed(elf, val)  \
 ({ \
__typeof__(val) __ret;  \
+   bool __need_bswap = need_bswap(elf);\
switch (sizeof(val)) {  \
-   case 8: __ret = __NEED_BSWAP ? bswap_64(val) : (val); break;\
-   case 4: __ret = __NEED_BSWAP ? bswap_32(val) : (val); break;\
-   case 2: __ret = __NEED_BSWAP ? bswap_16(val) : (val); break;\
+   case 8: \
+   __ret = __need_bswap ? bswap_64(val) : (val); break;\
+   case 4: \
+   __ret = __need_bswap ? bswap_32(val) : (val); break;\
+   case 2: \
+   __ret = __need_bswap ? bswap_16(val) : (val); break;\
default:\
BUILD_BUG(); break; \
}   \
diff --git a/tools/objtool/orc_dump.c b/tools/objtool/orc_dump.c
index f5a8508c42d6..4f1211fec82c 100644
--- a/tools/objtool/orc_dump.c
+++ b/tools/objtool/orc_dump.c
@@ -76,6 +76,7 @@ int orc_dump(const char *_objname)
GElf_Rela rela;
GElf_Sym sym;
Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
+   struct elf dummy_elf = {};
 
 
objname = _objname;
@@ -94,6 +95,12 @@ int orc_dump(const char *_objname)
return -1;
}
 
+   if (!elf64_getehdr(elf)) {
+   WARN_ELF("elf64_getehdr");
+   return -1;
+   }
+   memcpy(_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
+
if (elf_getshdrnum(elf, _sections)) {
WARN_ELF("elf_getshdrnum");
 

[PATCH v4 08/16] objtool: Fix SEGFAULT

2022-10-02 Thread Sathvika Vasireddy
From: Christophe Leroy 

find_insn() will return NULL in case of failure. Check insn in order
to avoid a kernel Oops for NULL pointer dereference.

Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index e55fdf952a3a..fe935f19447b 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -207,7 +207,7 @@ static bool __dead_end_function(struct objtool_file *file, 
struct symbol *func,
return false;
 
insn = find_insn(file, func->sec, func->offset);
-   if (!insn->func)
+   if (!insn || !insn->func)
return false;
 
func_for_each_insn(file, func, insn) {
-- 
2.31.1



[PATCH v4 07/16] powerpc: Skip objtool from running on VDSO files

2022-10-02 Thread Sathvika Vasireddy
Do not run objtool on VDSO files, by using OBJECT_FILES_NON_STANDARD.

Suggested-by: Christophe Leroy 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/vdso/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/powerpc/kernel/vdso/Makefile 
b/arch/powerpc/kernel/vdso/Makefile
index a2e7b0ce5b19..6a977b0d8ffc 100644
--- a/arch/powerpc/kernel/vdso/Makefile
+++ b/arch/powerpc/kernel/vdso/Makefile
@@ -102,3 +102,5 @@ quiet_cmd_vdso64ld_and_check = VDSO64L $@
   cmd_vdso64ld_and_check = $(VDSOCC) $(c_flags) $(CC64FLAGS) -o $@ 
-Wl,-T$(filter %.lds,$^) $(filter %.o,$^) -z noexecstack ; $(cmd_vdso_check)
 quiet_cmd_vdso64as = VDSO64A $@
   cmd_vdso64as = $(VDSOCC) $(a_flags) $(CC64FLAGS) $(AS64FLAGS) -c -o $@ $<
+
+OBJECT_FILES_NON_STANDARD := y
-- 
2.31.1



[PATCH v4 06/16] powerpc: Fix objtool unannotated intra-function call warnings on PPC32

2022-10-02 Thread Sathvika Vasireddy
From: Christophe Leroy 

Fix several annotations in assembly files on PPC32.

Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Changed subject line from "objtool/powerpc: Activate
objtool on PPC32" to "powerpc: Fix objtool unannotated intra-function call
warnings on PPC32", and removed Kconfig change to enable objtool, as it
is a part of "objtool/powerpc: Enable objtool to be built on ppc" patch in
this series.]
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/cpu_setup_6xx.S  | 26 --
 arch/powerpc/kernel/cpu_setup_fsl_booke.S|  8 --
 arch/powerpc/kernel/entry_32.S   |  9 --
 arch/powerpc/kernel/head_40x.S   |  5 +++-
 arch/powerpc/kernel/head_8xx.S   |  5 +++-
 arch/powerpc/kernel/head_book3s_32.S | 29 ++--
 arch/powerpc/kernel/head_fsl_booke.S |  5 +++-
 arch/powerpc/kernel/swsusp_32.S  |  5 +++-
 arch/powerpc/kvm/fpu.S   | 17 
 arch/powerpc/platforms/52xx/lite5200_sleep.S | 15 +++---
 10 files changed, 89 insertions(+), 35 deletions(-)

diff --git a/arch/powerpc/kernel/cpu_setup_6xx.S 
b/arch/powerpc/kernel/cpu_setup_6xx.S
index f8b5ff64b604..f29ce3dd6140 100644
--- a/arch/powerpc/kernel/cpu_setup_6xx.S
+++ b/arch/powerpc/kernel/cpu_setup_6xx.S
@@ -4,6 +4,8 @@
  *Copyright (C) 2003 Benjamin Herrenschmidt (b...@kernel.crashing.org)
  */
 
+#include 
+
 #include 
 #include 
 #include 
@@ -81,7 +83,7 @@ _GLOBAL(__setup_cpu_745x)
blr
 
 /* Enable caches for 603's, 604, 750 & 7400 */
-setup_common_caches:
+SYM_FUNC_START_LOCAL(setup_common_caches)
mfspr   r11,SPRN_HID0
andi.   r0,r11,HID0_DCE
ori r11,r11,HID0_ICE|HID0_DCE
@@ -95,11 +97,12 @@ setup_common_caches:
sync
isync
blr
+SYM_FUNC_END(setup_common_caches)
 
 /* 604, 604e, 604ev, ...
  * Enable superscalar execution & branch history table
  */
-setup_604_hid0:
+SYM_FUNC_START_LOCAL(setup_604_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SIED|HID0_BHTE
ori r8,r11,HID0_BTCD
@@ -110,6 +113,7 @@ setup_604_hid0:
sync
isync
blr
+SYM_FUNC_END(setup_604_hid0)
 
 /* 7400 <= rev 2.7 and 7410 rev = 1.0 suffer from some
  * erratas we work around here.
@@ -125,13 +129,14 @@ setup_604_hid0:
  * needed once we have applied workaround #5 (though it's
  * not set by Apple's firmware at least).
  */
-setup_7400_workarounds:
+SYM_FUNC_START_LOCAL(setup_7400_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0207
ble 1f
blr
-setup_7410_workarounds:
+SYM_FUNC_END(setup_7400_workarounds)
+SYM_FUNC_START_LOCAL(setup_7410_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0100
@@ -151,6 +156,7 @@ setup_7410_workarounds:
sync
isync
blr
+SYM_FUNC_END(setup_7410_workarounds)
 
 /* 740/750/7400/7410
  * Enable Store Gathering (SGE), Address Broadcast (ABE),
@@ -158,7 +164,7 @@ setup_7410_workarounds:
  * Dynamic Power Management (DPM), Speculative (SPD)
  * Clear Instruction cache throttling (ICTC)
  */
-setup_750_7400_hid0:
+SYM_FUNC_START_LOCAL(setup_750_7400_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SGE | HID0_ABE | HID0_BHTE | HID0_BTIC
orisr11,r11,HID0_DPM@h
@@ -177,12 +183,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_750_7400_hid0)
 
 /* 750cx specific
  * Looks like we have to disable NAP feature for some PLL settings...
  * (waiting for confirmation)
  */
-setup_750cx:
+SYM_FUNC_START_LOCAL(setup_750cx)
mfspr   r10, SPRN_HID1
rlwinm  r10,r10,4,28,31
cmpwi   cr0,r10,7
@@ -196,11 +203,13 @@ setup_750cx:
andcr6,r6,r7
stw r6,CPU_SPEC_FEATURES(r4)
blr
+SYM_FUNC_END(setup_750cx)
 
 /* 750fx specific
  */
-setup_750fx:
+SYM_FUNC_START_LOCAL(setup_750fx)
blr
+SYM_FUNC_END(setup_750fx)
 
 /* MPC 745x
  * Enable Store Gathering (SGE), Branch Folding (FOLD)
@@ -212,7 +221,7 @@ setup_750fx:
  * Clear Instruction cache throttling (ICTC)
  * Enable L2 HW prefetch
  */
-setup_745x_specifics:
+SYM_FUNC_START_LOCAL(setup_745x_specifics)
/* We check for the presence of an L3 cache setup by
 * the firmware. If any, we disable NAP capability as
 * it's known to be bogus on rev 2.1 and earlier
@@ -270,6 +279,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_745x_specifics)
 
 /*
  * Initialize the FPU registers. This is needed to work around an errata
diff --git a/arch/powerpc/kernel/cpu_setup_fsl_booke.S 
b/arch/powerpc/kernel/cpu_setup_fsl_booke.S
index 4bf33f1b4193..f573a4f3bbe6 100644
--- a/arch/powerpc/kernel/cpu_setup_fsl_booke.S
+++ b/arch/powerpc/kernel/cpu_setup_fsl_booke.S
@@ -8,6 +8,8 @@
  * Benjamin Herrenschmidt 

[PATCH v4 05/16] powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o

2022-10-02 Thread Sathvika Vasireddy
With objtool enabled, below warnings are seen when trying to build:
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: aes_p8_set_encrypt_key+0x44: 
unannotated intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2448: unannotated 
intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2d68: unannotated 
intra-function call

Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o file for the
following reasons:

- Since this file comes from OpenSSL, and since it is a perl file which
  generates a .S file, it may not be the best choice to make too many
  code changes to such files, unless absolutely necessary.

- As far as the objtool --mcount functionality is concerned, we do not
  have to run objtool on this file because there are no calls to
  _mcount.

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 drivers/crypto/vmx/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/crypto/vmx/Makefile b/drivers/crypto/vmx/Makefile
index 2560cfea1dec..7b41f0da6807 100644
--- a/drivers/crypto/vmx/Makefile
+++ b/drivers/crypto/vmx/Makefile
@@ -9,3 +9,5 @@ targets += aesp8-ppc.S ghashp8-ppc.S
 
 $(obj)/aesp8-ppc.S $(obj)/ghashp8-ppc.S: $(obj)/%.S: $(src)/%.pl FORCE
$(call if_changed,perl)
+
+OBJECT_FILES_NON_STANDARD_aesp8-ppc.o := y
-- 
2.31.1



[PATCH v4 04/16] powerpc: Curb objtool unannotated intra-function warnings

2022-10-02 Thread Sathvika Vasireddy
objtool throws the following unannotated intra-function call warnings:
arch/powerpc/kernel/entry_64.o: warning: objtool: .text+0x4: unannotated 
intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xe64: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xee4: 
unannotated intra-function call

Fix these warnings by annotating intra-function calls, using
ANNOTATE_INTRA_FUNCTION_CALL macro, to indicate that the branch targets
are valid.

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/entry_64.S  | 2 ++
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
index 01ace4c56104..fb444bc64f3f 100644
--- a/arch/powerpc/kernel/entry_64.S
+++ b/arch/powerpc/kernel/entry_64.S
@@ -14,6 +14,7 @@
  *  code, and exception/interrupt return code for PowerPC.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -73,6 +74,7 @@ flush_branch_caches:
 
// Flush the link stack
.rept 64
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
b   1f
diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S 
b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index bc187bb216ac..20212eb30764 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -11,6 +11,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1523,12 +1524,14 @@ kvm_flush_link_stack:
 
/* Flush the link stack. On Power8 it's up to 32 entries in size. */
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 
/* And on Power9 it's up to 64. */
 BEGIN_FTR_SECTION
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
-- 
2.31.1



[PATCH v4 03/16] powerpc: Fix objtool unannotated intra-function call warnings

2022-10-02 Thread Sathvika Vasireddy
Objtool throws unannotated intra-function call warnings in the following
assembly files:

arch/powerpc/kernel/vector.o: warning: objtool: .text+0x53c: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x60: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x124: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5d4: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5dc: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xcb8: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xd0c: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x1030: 
unannotated intra-function call

arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x358: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x728: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4d94: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4ec4: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_interrupts.o: warning: objtool: .text+0x6c: 
unannotated intra-function call
arch/powerpc/kernel/misc_64.o: warning: objtool: .text+0x64: unannotated 
intra-function call

Objtool does not add STT_NOTYPE symbols with size 0 to the rbtree, which
is why find_call_destination() function is not able to find the
destination symbol for 'bl' instruction. For such symbols, objtool is
throwing unannotated intra-function call warnings in assembly files. Fix
these warnings by annotating those symbols with SYM_FUNC_START_LOCAL and
SYM_FUNC_END macros, inorder to set symbol type to STT_FUNC and symbol
size accordingly.

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/exceptions-64s.S|  7 +--
 arch/powerpc/kernel/head_64.S   |  7 +--
 arch/powerpc/kernel/misc_64.S   |  4 +++-
 arch/powerpc/kernel/vector.S|  4 +++-
 arch/powerpc/kvm/book3s_hv_interrupts.S |  4 +++-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 22 +++---
 6 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/arch/powerpc/kernel/exceptions-64s.S 
b/arch/powerpc/kernel/exceptions-64s.S
index 3d0dc133a9ae..56a31424c8b0 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -13,6 +13,7 @@
  *
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -3075,7 +3076,7 @@ CLOSE_FIXED_SECTION(virt_trampolines);
 USE_TEXT_SECTION()
 
 /* MSR[RI] should be clear because this uses SRR[01] */
-enable_machine_check:
+SYM_FUNC_START_LOCAL(enable_machine_check)
mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3087,9 +3088,10 @@ enable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(enable_machine_check)
 
 /* MSR[RI] should be clear because this uses SRR[01] */
-disable_machine_check:
+SYM_FUNC_START_LOCAL(disable_machine_check)
mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3102,3 +3104,4 @@ disable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(disable_machine_check)
diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index cf2c08902c05..10e2d43420d0 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -18,6 +18,7 @@
  *  variants.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -465,7 +466,7 @@ generic_secondary_common_init:
  * Assumes we're mapped EA == RA if the MMU is on.
  */
 #ifdef CONFIG_PPC_BOOK3S
-__mmu_off:
+SYM_FUNC_START_LOCAL(__mmu_off)
mfmsr   r3
andi.   r0,r3,MSR_IR|MSR_DR
beqlr
@@ -476,6 +477,7 @@ __mmu_off:
sync
rfid
b   .   /* prevent speculative execution */
+SYM_FUNC_END(__mmu_off)
 #endif
 
 
@@ -869,7 +871,7 @@ _GLOBAL(start_secondary_resume)
 /*
  * This subroutine clobbers r11 and r12
  */
-enable_64b_mode:
+SYM_FUNC_START_LOCAL(enable_64b_mode)
mfmsr   r11 /* grab the current MSR */
 #ifdef CONFIG_PPC_BOOK3E
orisr11,r11,0x8000  /* CM bit set, we'll set ICM later */
@@ -881,6 +883,7 @@ enable_64b_mode:
isync
 #endif
blr
+SYM_FUNC_END(enable_64b_mode)
 
 /*
  * This puts the TOC pointer into r2, offset by 0x8000 (as expected
diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
index fd6d8d3a548e..b36fb89ff718 100644
--- a/arch/powerpc/kernel/misc_64.S
+++ b/arch/powerpc/kernel/misc_64.S
@@ -9,6 +9,7 @@
  * PPC64 updates by Dave Engebretsen (engeb...@us.ibm.com)
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -353,7 +354,7 @@ _GLOBAL(kexec_smp_wait)
  *
  * don't overwrite r3 here

[PATCH v4 02/16] powerpc: Override __ALIGN and __ALIGN_STR macros

2022-10-02 Thread Sathvika Vasireddy
In a subsequent patch, we would want to annotate powerpc assembly functions
with SYM_FUNC_START_LOCAL macro. This macro depends on __ALIGN macro.

The default expansion of __ALIGN macro is:
#define __ALIGN  .align 4,0x90

So, override __ALIGN and __ALIGN_STR macros to use the same alignment as
that of the existing _GLOBAL macro. Also, do not pad with 0x90, because
repeated 0x90s are not a nop or trap on powerpc.

Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/linkage.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/powerpc/include/asm/linkage.h 
b/arch/powerpc/include/asm/linkage.h
index b71b9582e754..b88d1d2cf304 100644
--- a/arch/powerpc/include/asm/linkage.h
+++ b/arch/powerpc/include/asm/linkage.h
@@ -4,6 +4,9 @@
 
 #include 
 
+#define __ALIGN.align 2
+#define __ALIGN_STR".align 2"
+
 #ifdef CONFIG_PPC64_ELF_ABI_V1
 #define cond_syscall(x) \
asm ("\t.weak " #x "\n\t.set " #x ", sys_ni_syscall\n"  \
-- 
2.31.1



[PATCH v4 00/16] objtool: Enable and implement --mcount option on powerpc

2022-10-02 Thread Sathvika Vasireddy
This patchset enables and implements objtool --mcount
option on powerpc. This applies atop powerpc/merge branch.

Changelog:


v4:

* Patch 11/16 - Introduce a new config option
CONFIG_HAVE_OBJTOOL_NOP_MCOUNT as a means for
architectures to enable nop'ing ftrace locations.

  - Remove Acked-by tag from Peter Zijlstra (Intel),
and Reviewed-by tag from Christophe Leroy. 
[This is done because I reworked the patch to add
a new config option to objtool. Please let me know
if you want me to retain the tags. Thanks!]

* Patch 16/16 - Rework the patch to handle only 'bl' instruction
decoding. 


v3:

* Patch 01/16 - Rework patch subject.
  - Rework changelog.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 02/16 - Rework changelog to update details based on feedback
from Nicholas Piggin and Michael Ellerman.
  - Use quotes instead of __stringify macro, based on
suggestion from Christophe Leroy.

* Patch 03/16 - Add Reviewed-by tag from Christophe Leroy.
  - Based on Christophe's suggestion, keep all 
before .
  - Rework changelog.

* Patch 04/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 05/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 06/16 - No change.

* Patch 07/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 08/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 09/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 10/16 - Reorder local variable declarations to use reverse
xmas tree format.
  - Add Signed-off-by tag from Sathvika Vasireddy indicating
changes done.
  - Add Acked-by tag from Peter Zijlstra.

* Patch 11/16 - Update changelog to indicate that powerpc kernel does
not support nop'ed out ftrace locations.
  - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 12/16 - Per Christophe's comment, rework changelog.

* Patch 13/16 - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 14/16 - Simplify arch_ftrace_match() function, based on
Christophe's suggestion.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 15/16 - Include code from Christophe Leroy to use local vars for
type and imm, and to adapt len for prefixed
instructions.

* Patch 16/16 - Based on suggestion from Christophe Leroy, setup
immediate value calculation outside the check for
specific instruction under case 18.
  - Set instruction type to INSN_CALL for 'bla'
instruction as well.


v2:

* Change subject of patch 01/16
* As suggested by Christophe Leroy, add barrier_before_unreachable()
before __builtin_unreachable() to work around a gcc problem.
* Fix issues reported by Kernel Test Robot.
* Include suggestions from Christophe Leroy, and change commit
messages for patches 01/16, 02/16, 03/16, 05/16.



Christophe Leroy (4):
  objtool: Fix SEGFAULT
  objtool: Use target file endianness instead of a compiled constant
  objtool: Use target file class size instead of a compiled constant
  powerpc: Fix objtool unannotated intra-function call warnings on PPC32

Sathvika Vasireddy (12):
  powerpc: Fix __WARN_FLAGS() for use with Objtool
  powerpc: Override __ALIGN and __ALIGN_STR macros
  powerpc: Fix objtool unannotated intra-function call warnings
  powerpc: Curb objtool unannotated intra-function warnings
  powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o
  powerpc: Skip objtool from running on VDSO files
  objtool: Add --mnop as an option to --mcount
  objtool: Read special sections with alts only when specific options are 
selected
  objtool: Use macros to define arch specific reloc types
  objtool: Add arch specific function arch_ftrace_match()
  objtool/powerpc: Enable objtool to be built on ppc
  objtool/powerpc: Add --mcount specific implementation


 Makefile  |   4 +-
 arch/powerpc/Kconfig  |   2 +
 arch/powerpc/include/asm/asm.h|   7 ++
 arch/powerpc/include/asm/bug.h|   3 +-
 arch/powerpc/include/asm/linkage.h|   3 +
 arch/powerpc/kernel/cpu_setup_6xx.S   |  26 +++--
 arch/powerpc/kernel/cpu_setup_fsl_booke.S |   8 +-
 arch/powerpc/kernel/entry_32.S|   9 +-
 arch/powerpc/kernel/entry_64.S|   2 +
 arch/powerpc/kernel/exceptions-64s.S  |   7 +-
 arch/powerpc/kernel/head_40x.S|   5 +-
 arch/powerpc/kernel/head_64.S |   7 +-
 arch/powerpc/kernel/head_8xx.S|   5 +-
 arch/powerpc/kernel/head_book3s_32.S  |  29

[PATCH v4 01/16] powerpc: Fix __WARN_FLAGS() for use with Objtool

2022-10-02 Thread Sathvika Vasireddy
Commit 1e688dd2a3d675 ("powerpc/bug: Provide better flexibility to
WARN_ON/__WARN_FLAGS() with asm goto") updated __WARN_FLAGS() to use asm
goto, and added a call to 'unreachable()' after the asm goto for optimal
code generation. With CONFIG_OBJTOOL enabled, 'annotate_unreachable()'
statement in 'unreachable()' tries to note down the location of the
subsequent instruction in a separate elf section to aid code flow
analysis. However, on powerpc, this results in gcc emitting a call to a
symbol of size 0. This results in objtool complaining of "unannotated
intra-function call" since the target symbol is not a valid function
call destination.

Objtool wants this annotation for code flow analysis, which we are not
yet enabling on powerpc. As such, expand the call to 'unreachable()' in
__WARN_FLAGS() without annotate_unreachable():
barrier_before_unreachable();
__builtin_unreachable();

This still results in optimal code generation for __WARN_FLAGS(), while
getting rid of the objtool warning.

We still need barrier_before_unreachable() to work around gcc bugs 82365
and 106751:
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106751

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/bug.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 61a4736355c2..ef42adb44aa3 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -99,7 +99,8 @@
__label__ __label_warn_on;  \
\
WARN_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags), __label_warn_on); 
\
-   unreachable();  \
+   barrier_before_unreachable();   \
+   __builtin_unreachable();\
\
 __label_warn_on:   \
break;  \
-- 
2.31.1



Re: [PATCH v3 00/16] objtool: Enable and implement --mcount option on powerpc

2022-09-21 Thread Sathvika Vasireddy

Hi Josh,

On 14/09/22 05:45, Josh Poimboeuf wrote:


On Tue, Sep 13, 2022 at 04:13:52PM +0200, Peter Zijlstra wrote:

On Mon, Sep 12, 2022 at 01:50:04PM +0530, Sathvika Vasireddy wrote:

Christophe Leroy (4):
   objtool: Fix SEGFAULT
   objtool: Use target file endianness instead of a compiled constant
   objtool: Use target file class size instead of a compiled constant
Sathvika Vasireddy (12):
   objtool: Add --mnop as an option to --mcount
   objtool: Read special sections with alts only when specific options are 
selected
   objtool: Use macros to define arch specific reloc types
   objtool: Add arch specific function arch_ftrace_match()
   objtool/powerpc: Enable objtool to be built on ppc
   objtool/powerpc: Add --mcount specific implementation
  tools/objtool/arch/powerpc/Build  |   2 +
  tools/objtool/arch/powerpc/decode.c   | 101 ++
  .../arch/powerpc/include/arch/cfi_regs.h  |  11 ++
  tools/objtool/arch/powerpc/include/arch/elf.h |  10 ++
  .../arch/powerpc/include/arch/special.h   |  21 
  tools/objtool/arch/powerpc/special.c  |  19 
  tools/objtool/arch/x86/decode.c   |   5 +
  tools/objtool/arch/x86/include/arch/elf.h |   2 +
  .../arch/x86/include/arch/endianness.h|   9 --
  tools/objtool/builtin-check.c |  14 +++
  tools/objtool/check.c |  53 -
  tools/objtool/elf.c   |   8 +-
  tools/objtool/include/objtool/arch.h  |   2 +
  tools/objtool/include/objtool/builtin.h   |   1 +
  tools/objtool/include/objtool/elf.h   |   8 ++
  tools/objtool/include/objtool/endianness.h|  32 +++---
  tools/objtool/orc_dump.c  |  11 +-
  tools/objtool/orc_gen.c   |   4 +-
  tools/objtool/special.c   |   3 +-

This seems to painlessly merge with the objtool changes I have in
queue.git/call-depth-tracking. After that all I need is the below little
patch to make it to build ppc44x_defconfig + CONFIG_DYNAMIC_FTRACE=y.

So I think these patches can go through the powerpc tree if Michael
wants. Josh you okay with that, or should we do something complicated?

I'm all for avoiding complicated, but let me try to give it a proper
review first.


Did you get a chance to review this patch set?

- Sathvika



[PATCH v3 07/16] powerpc: Skip objtool from running on VDSO files

2022-09-12 Thread Sathvika Vasireddy
Do not run objtool on VDSO files, by using OBJECT_FILES_NON_STANDARD.

Suggested-by: Christophe Leroy 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/vdso/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/powerpc/kernel/vdso/Makefile 
b/arch/powerpc/kernel/vdso/Makefile
index 096b0bf1335f..a49a0d6a1c53 100644
--- a/arch/powerpc/kernel/vdso/Makefile
+++ b/arch/powerpc/kernel/vdso/Makefile
@@ -102,3 +102,5 @@ quiet_cmd_vdso64ld_and_check = VDSO64L $@
   cmd_vdso64ld_and_check = $(VDSOCC) $(c_flags) $(CC64FLAGS) -o $@ 
-Wl,-T$(filter %.lds,$^) $(filter %.o,$^) ; $(cmd_vdso_check)
 quiet_cmd_vdso64as = VDSO64A $@
   cmd_vdso64as = $(VDSOCC) $(a_flags) $(CC64FLAGS) $(AS64FLAGS) -c -o $@ $<
+
+OBJECT_FILES_NON_STANDARD := y
-- 
2.31.1



[PATCH v3 15/16] objtool/powerpc: Enable objtool to be built on ppc

2022-09-12 Thread Sathvika Vasireddy
This patch adds [stub] implementations for required functions, inorder
to enable objtool build on powerpc.

Signed-off-by: Sathvika Vasireddy 
[Christophe Leroy: powerpc: Add missing asm/asm.h for objtool,
Use local variables for type and imm in arch_decode_instruction(),
Adapt len for prefixed instructions.]
Signed-off-by: Christophe Leroy 
---
 arch/powerpc/Kconfig  |  1 +
 arch/powerpc/include/asm/asm.h|  7 ++
 tools/objtool/arch/powerpc/Build  |  2 +
 tools/objtool/arch/powerpc/decode.c   | 85 +++
 .../arch/powerpc/include/arch/cfi_regs.h  | 11 +++
 tools/objtool/arch/powerpc/include/arch/elf.h |  8 ++
 .../arch/powerpc/include/arch/special.h   | 21 +
 tools/objtool/arch/powerpc/special.c  | 19 +
 8 files changed, 154 insertions(+)
 create mode 100644 arch/powerpc/include/asm/asm.h
 create mode 100644 tools/objtool/arch/powerpc/Build
 create mode 100644 tools/objtool/arch/powerpc/decode.c
 create mode 100644 tools/objtool/arch/powerpc/include/arch/cfi_regs.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/elf.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/special.h
 create mode 100644 tools/objtool/arch/powerpc/special.c

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 4c466acdc70d..dc05cd23c233 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -237,6 +237,7 @@ config PPC
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
+   select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/arch/powerpc/include/asm/asm.h b/arch/powerpc/include/asm/asm.h
new file mode 100644
index ..86f46b604e9a
--- /dev/null
+++ b/arch/powerpc/include/asm/asm.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_POWERPC_ASM_H
+#define _ASM_POWERPC_ASM_H
+
+#define _ASM_PTR   " .long "
+
+#endif /* _ASM_POWERPC_ASM_H */
diff --git a/tools/objtool/arch/powerpc/Build b/tools/objtool/arch/powerpc/Build
new file mode 100644
index ..d24d5636a5b8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/Build
@@ -0,0 +1,2 @@
+objtool-y += decode.o
+objtool-y += special.o
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
new file mode 100644
index ..dcd0975cad6b
--- /dev/null
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+unsigned long arch_dest_reloc_offset(int addend)
+{
+   return addend;
+}
+
+bool arch_callee_saved_reg(unsigned char reg)
+{
+   return false;
+}
+
+int arch_decode_hint_reg(u8 sp_reg, int *base)
+{
+   exit(-1);
+}
+
+const char *arch_nop_insn(int len)
+{
+   exit(-1);
+}
+
+const char *arch_ret_insn(int len)
+{
+   exit(-1);
+}
+
+int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
+   unsigned long offset, unsigned int maxlen,
+   unsigned int *len, enum insn_type *type,
+   unsigned long *immediate,
+   struct list_head *ops_list)
+{
+   unsigned int opcode;
+   enum insn_type typ;
+   unsigned long imm;
+   u32 insn;
+
+   insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
+   opcode = insn >> 26;
+   typ = INSN_OTHER;
+   imm = 0;
+
+   if (opcode == 1)
+   *len = 8;
+   else
+   *len = 4;
+
+   *type = typ;
+   *immediate = imm;
+
+   return 0;
+}
+
+unsigned long arch_jump_destination(struct instruction *insn)
+{
+   return insn->offset + insn->immediate;
+}
+
+void arch_initial_func_cfi_state(struct cfi_init_state *state)
+{
+   int i;
+
+   for (i = 0; i < CFI_NUM_REGS; i++) {
+   state->regs[i].base = CFI_UNDEFINED;
+   state->regs[i].offset = 0;
+   }
+
+   /* initial CFA (call frame address) */
+   state->cfa.base = CFI_SP;
+   state->cfa.offset = 0;
+
+   /* initial LR (return address) */
+   state->regs[CFI_RA].base = CFI_CFA;
+   state->regs[CFI_RA].offset = 0;
+}
diff --git a/tools/objtool/arch/powerpc/include/arch/cfi_regs.h 
b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
new file mode 100644
index ..59638ebeafc8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _OBJTOOL_CFI_REGS_H
+#define _OBJTOOL_CFI_REGS_H
+
+#define CFI_BP 1
+#define CFI_SP CFI_BP
+#define CFI_RA 32
+#define CFI_N

[PATCH v3 16/16] objtool/powerpc: Add --mcount specific implementation

2022-09-12 Thread Sathvika Vasireddy
This patch enables objtool --mcount on powerpc, and adds implementation
specific to powerpc.

Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/Kconfig  |  1 +
 tools/objtool/arch/powerpc/decode.c   | 16 
 tools/objtool/arch/powerpc/include/arch/elf.h |  2 ++
 3 files changed, 19 insertions(+)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index dc05cd23c233..6be2e68fa9eb 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -238,6 +238,7 @@ config PPC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
+   select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
index dcd0975cad6b..ea2b1968f0ee 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -9,6 +9,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "_mcount");
+}
+
 unsigned long arch_dest_reloc_offset(int addend)
 {
return addend;
@@ -50,6 +55,17 @@ int arch_decode_instruction(struct objtool_file *file, const 
struct section *sec
typ = INSN_OTHER;
imm = 0;
 
+   switch (opcode) {
+   case 18: /* b[l][a] */
+   if (insn & 1)  /* bl[a] */
+   typ = INSN_CALL;
+
+   imm = insn & 0x3fc;
+   if (imm & 0x200)
+   imm -= 0x400;
+   break;
+   }
+
if (opcode == 1)
*len = 8;
else
diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h 
b/tools/objtool/arch/powerpc/include/arch/elf.h
index 3c8ebb7d2a6b..73f9ae172fe5 100644
--- a/tools/objtool/arch/powerpc/include/arch/elf.h
+++ b/tools/objtool/arch/powerpc/include/arch/elf.h
@@ -4,5 +4,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_PPC_NONE
+#define R_ABS64 R_PPC64_ADDR64
+#define R_ABS32 R_PPC_ADDR32
 
 #endif /* _OBJTOOL_ARCH_ELF */
-- 
2.31.1



[PATCH v3 13/16] objtool: Use macros to define arch specific reloc types

2022-09-12 Thread Sathvika Vasireddy
Make relocation types architecture specific.

Acked-by: Peter Zijlstra (Intel) 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/include/arch/elf.h | 2 ++
 tools/objtool/check.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/include/arch/elf.h 
b/tools/objtool/arch/x86/include/arch/elf.h
index 69cc4264b28a..ac14987cf687 100644
--- a/tools/objtool/arch/x86/include/arch/elf.h
+++ b/tools/objtool/arch/x86/include/arch/elf.h
@@ -2,5 +2,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_X86_64_NONE
+#define R_ABS64 R_X86_64_64
+#define R_ABS32 R_X86_64_32
 
 #endif /* _OBJTOOL_ARCH_ELF */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index c6457dac6d58..35bcd4f90acd 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -885,7 +885,7 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
memset(loc, 0, addrsize);
 
if (elf_add_reloc_to_insn(file->elf, sec, idx,
- R_X86_64_64,
+ addrsize == sizeof(u64) ? R_ABS64 : 
R_ABS32,
  insn->sec, insn->offset))
return -1;
 
-- 
2.31.1



[PATCH v3 14/16] objtool: Add arch specific function arch_ftrace_match()

2022-09-12 Thread Sathvika Vasireddy
Add architecture specific function to look for relocation records
pointing to architecture specific symbols.

Suggested-by: Christophe Leroy 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/decode.c  | 5 +
 tools/objtool/check.c| 2 +-
 tools/objtool/include/objtool/arch.h | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index c260006106be..28263e231c58 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -23,6 +23,11 @@
 #include 
 #include 
 
+int arch_ftrace_match(char *name)
+{
+   return !strcmp(name, "__fentry__");
+}
+
 static int is_x86_64(const struct elf *elf)
 {
switch (elf->ehdr.e_machine) {
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 35bcd4f90acd..856ea80b1cfc 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2296,7 +2296,7 @@ static int classify_symbols(struct objtool_file *file)
if (arch_is_rethunk(func))
func->return_thunk = true;
 
-   if (!strcmp(func->name, "__fentry__"))
+   if (arch_ftrace_match(func->name))
func->fentry = true;
 
if (is_profiling_func(func->name))
diff --git a/tools/objtool/include/objtool/arch.h 
b/tools/objtool/include/objtool/arch.h
index beb2f3aa94ff..5149330f400f 100644
--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -69,6 +69,8 @@ struct stack_op {
 
 struct instruction;
 
+int arch_ftrace_match(char *name);
+
 void arch_initial_func_cfi_state(struct cfi_init_state *state);
 
 int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
-- 
2.31.1



[PATCH v3 12/16] objtool: Read special sections with alts only when specific options are selected

2022-09-12 Thread Sathvika Vasireddy
Call add_special_section_alts() only when stackval or orc or uaccess or
noinstr options are passed to objtool.

Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 35827e6c6df9..c6457dac6d58 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2372,9 +2372,11 @@ static int decode_sections(struct objtool_file *file)
 * Must be before add_jump_destinations(), which depends on 'func'
 * being set for alternatives, to enable proper sibling call detection.
 */
-   ret = add_special_section_alts(file);
-   if (ret)
-   return ret;
+   if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
+   ret = add_special_section_alts(file);
+   if (ret)
+   return ret;
+   }
 
ret = add_jump_destinations(file);
if (ret)
-- 
2.31.1



[PATCH v3 11/16] objtool: Add --mnop as an option to --mcount

2022-09-12 Thread Sathvika Vasireddy
Some architectures (powerpc) may not support ftrace locations being
nop'ed out at build time. Introduce --mnop as an option to objtool for
enabling nop'ing of ftrace locations. Re-purpose CONFIG_HAVE_NOP_MCOUNT
as a means for architectures to indicate support for the same.

Also, make sure that --mnop can be passed as an option to objtool only
when --mcount is passed.

Acked-by: Peter Zijlstra (Intel) 
Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 Makefile|  4 +++-
 arch/x86/Kconfig|  1 +
 scripts/Makefile.lib|  1 +
 tools/objtool/builtin-check.c   | 14 ++
 tools/objtool/check.c   | 19 ++-
 tools/objtool/include/objtool/builtin.h |  1 +
 6 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index a4f71076cacb..9c1f4b038f65 100644
--- a/Makefile
+++ b/Makefile
@@ -857,7 +857,9 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_CC
   endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
-  CC_FLAGS_USING   += -DCC_USING_NOP_MCOUNT
+  ifdef CONFIG_HAVE_NOP_MCOUNT
+CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
+  endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
   ifdef CONFIG_HAVE_C_RECORDMCOUNT
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index f9920f1341c8..a8dd138df637 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -189,6 +189,7 @@ config X86
select HAVE_CONTEXT_TRACKING_USER_OFFSTACK  if 
HAVE_CONTEXT_TRACKING_USER
select HAVE_C_RECORDMCOUNT
select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
+   select HAVE_NOP_MCOUNT  if HAVE_OBJTOOL_MCOUNT
select HAVE_BUILDTIME_MCOUNT_SORT
select HAVE_DEBUG_KMEMLEAK
select HAVE_DMA_CONTIGUOUS
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3fb6a99e78c4..0610078e057a 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -234,6 +234,7 @@ objtool_args =  
\
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr)  \
$(if $(CONFIG_X86_KERNEL_IBT), --ibt)   \
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
+   $(if $(CONFIG_HAVE_NOP_MCOUNT), --mnop) \
$(if $(CONFIG_UNWINDER_ORC), --orc) \
$(if $(CONFIG_RETPOLINE), --retpoline)  \
$(if $(CONFIG_RETHUNK), --rethunk)  \
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 24fbe803a0d3..9bd347d3c244 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -82,6 +82,7 @@ const struct option check_options[] = {
OPT_BOOLEAN(0, "dry-run", , "don't write modifications"),
OPT_BOOLEAN(0, "link", , "object is a linked object"),
OPT_BOOLEAN(0, "module", , "object is part of a kernel 
module"),
+   OPT_BOOLEAN(0, "mnop", , "nop out mcount call sites"),
OPT_BOOLEAN(0, "no-unreachable", _unreachable, "skip 
'unreachable instruction' warnings"),
OPT_BOOLEAN(0, "sec-address", _address, "print section 
addresses in warnings"),
OPT_BOOLEAN(0, "stats", , "print statistics"),
@@ -150,6 +151,16 @@ static bool opts_valid(void)
return false;
 }
 
+static bool mnop_opts_valid(void)
+{
+   if (opts.mnop && !opts.mcount) {
+   ERROR("--mnop requires --mcount");
+   return false;
+   }
+
+   return true;
+}
+
 static bool link_opts_valid(struct objtool_file *file)
 {
if (opts.link)
@@ -198,6 +209,9 @@ int objtool_run(int argc, const char **argv)
if (!file)
return 1;
 
+   if (!mnop_opts_valid())
+   return 1;
+
if (!link_opts_valid(file))
return 1;
 
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 738de23cb9e8..35827e6c6df9 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1233,17 +1233,18 @@ static void annotate_call_site(struct objtool_file 
*file,
if (opts.mcount && sym->fentry) {
if (sibling)
WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, 
insn->offset);
+   if (opts.mnop) {
+   if (reloc) {
+   reloc->type = R_NONE;
+   elf_write_reloc(file->elf, reloc);
+   }
 
-   if (reloc) {
-   reloc->type = R_NONE;
-   elf_write_reloc(file->elf, reloc);
-   }
-
-   elf_write_insn(file->elf, insn->sec,
-  

[PATCH v3 06/16] powerpc: Fix objtool unannotated intra-function call warnings on PPC32

2022-09-12 Thread Sathvika Vasireddy
From: Christophe Leroy 

Fix several annotations in assembly files on PPC32.

Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Changed subject line from "objtool/powerpc: Activate
objtool on PPC32" to "powerpc: Fix objtool unannotated intra-function call
warnings on PPC32", and removed Kconfig change to enable objtool, as it
is a part of "objtool/powerpc: Enable objtool to be built on ppc" patch in
this series.]
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/cpu_setup_6xx.S  | 26 --
 arch/powerpc/kernel/cpu_setup_fsl_booke.S|  8 --
 arch/powerpc/kernel/entry_32.S   |  9 --
 arch/powerpc/kernel/head_40x.S   |  5 +++-
 arch/powerpc/kernel/head_8xx.S   |  5 +++-
 arch/powerpc/kernel/head_book3s_32.S | 29 ++--
 arch/powerpc/kernel/head_fsl_booke.S |  5 +++-
 arch/powerpc/kernel/swsusp_32.S  |  5 +++-
 arch/powerpc/kvm/fpu.S   | 17 
 arch/powerpc/platforms/52xx/lite5200_sleep.S | 15 +++---
 10 files changed, 89 insertions(+), 35 deletions(-)

diff --git a/arch/powerpc/kernel/cpu_setup_6xx.S 
b/arch/powerpc/kernel/cpu_setup_6xx.S
index f8b5ff64b604..f29ce3dd6140 100644
--- a/arch/powerpc/kernel/cpu_setup_6xx.S
+++ b/arch/powerpc/kernel/cpu_setup_6xx.S
@@ -4,6 +4,8 @@
  *Copyright (C) 2003 Benjamin Herrenschmidt (b...@kernel.crashing.org)
  */
 
+#include 
+
 #include 
 #include 
 #include 
@@ -81,7 +83,7 @@ _GLOBAL(__setup_cpu_745x)
blr
 
 /* Enable caches for 603's, 604, 750 & 7400 */
-setup_common_caches:
+SYM_FUNC_START_LOCAL(setup_common_caches)
mfspr   r11,SPRN_HID0
andi.   r0,r11,HID0_DCE
ori r11,r11,HID0_ICE|HID0_DCE
@@ -95,11 +97,12 @@ setup_common_caches:
sync
isync
blr
+SYM_FUNC_END(setup_common_caches)
 
 /* 604, 604e, 604ev, ...
  * Enable superscalar execution & branch history table
  */
-setup_604_hid0:
+SYM_FUNC_START_LOCAL(setup_604_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SIED|HID0_BHTE
ori r8,r11,HID0_BTCD
@@ -110,6 +113,7 @@ setup_604_hid0:
sync
isync
blr
+SYM_FUNC_END(setup_604_hid0)
 
 /* 7400 <= rev 2.7 and 7410 rev = 1.0 suffer from some
  * erratas we work around here.
@@ -125,13 +129,14 @@ setup_604_hid0:
  * needed once we have applied workaround #5 (though it's
  * not set by Apple's firmware at least).
  */
-setup_7400_workarounds:
+SYM_FUNC_START_LOCAL(setup_7400_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0207
ble 1f
blr
-setup_7410_workarounds:
+SYM_FUNC_END(setup_7400_workarounds)
+SYM_FUNC_START_LOCAL(setup_7410_workarounds)
mfpvr   r3
rlwinm  r3,r3,0,20,31
cmpwi   0,r3,0x0100
@@ -151,6 +156,7 @@ setup_7410_workarounds:
sync
isync
blr
+SYM_FUNC_END(setup_7410_workarounds)
 
 /* 740/750/7400/7410
  * Enable Store Gathering (SGE), Address Broadcast (ABE),
@@ -158,7 +164,7 @@ setup_7410_workarounds:
  * Dynamic Power Management (DPM), Speculative (SPD)
  * Clear Instruction cache throttling (ICTC)
  */
-setup_750_7400_hid0:
+SYM_FUNC_START_LOCAL(setup_750_7400_hid0)
mfspr   r11,SPRN_HID0
ori r11,r11,HID0_SGE | HID0_ABE | HID0_BHTE | HID0_BTIC
orisr11,r11,HID0_DPM@h
@@ -177,12 +183,13 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_750_7400_hid0)
 
 /* 750cx specific
  * Looks like we have to disable NAP feature for some PLL settings...
  * (waiting for confirmation)
  */
-setup_750cx:
+SYM_FUNC_START_LOCAL(setup_750cx)
mfspr   r10, SPRN_HID1
rlwinm  r10,r10,4,28,31
cmpwi   cr0,r10,7
@@ -196,11 +203,13 @@ setup_750cx:
andcr6,r6,r7
stw r6,CPU_SPEC_FEATURES(r4)
blr
+SYM_FUNC_END(setup_750cx)
 
 /* 750fx specific
  */
-setup_750fx:
+SYM_FUNC_START_LOCAL(setup_750fx)
blr
+SYM_FUNC_END(setup_750fx)
 
 /* MPC 745x
  * Enable Store Gathering (SGE), Branch Folding (FOLD)
@@ -212,7 +221,7 @@ setup_750fx:
  * Clear Instruction cache throttling (ICTC)
  * Enable L2 HW prefetch
  */
-setup_745x_specifics:
+SYM_FUNC_START_LOCAL(setup_745x_specifics)
/* We check for the presence of an L3 cache setup by
 * the firmware. If any, we disable NAP capability as
 * it's known to be bogus on rev 2.1 and earlier
@@ -270,6 +279,7 @@ END_FTR_SECTION_IFSET(CPU_FTR_NO_DPM)
sync
isync
blr
+SYM_FUNC_END(setup_745x_specifics)
 
 /*
  * Initialize the FPU registers. This is needed to work around an errata
diff --git a/arch/powerpc/kernel/cpu_setup_fsl_booke.S 
b/arch/powerpc/kernel/cpu_setup_fsl_booke.S
index 4bf33f1b4193..f573a4f3bbe6 100644
--- a/arch/powerpc/kernel/cpu_setup_fsl_booke.S
+++ b/arch/powerpc/kernel/cpu_setup_fsl_booke.S
@@ -8,6 +8,8 @@
  * Benjamin Herrenschmidt 

[PATCH v3 10/16] objtool: Use target file class size instead of a compiled constant

2022-09-12 Thread Sathvika Vasireddy
From: Christophe Leroy 

In order to allow using objtool on cross-built kernels,
determine size of long from elf data instead of using
sizeof(long) at build time.

For the time being this covers only mcount.

Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
[Sathvika Vasireddy: Rename variable "size" to "addrsize" and function
"elf_class_size()" to "elf_class_addrsize()", and modify
create_mcount_loc_sections() function to follow reverse christmas tree
format to order local variable declarations.]
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c   | 18 ++
 tools/objtool/elf.c |  8 ++--
 tools/objtool/include/objtool/elf.h |  8 
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index c36e7a020d80..738de23cb9e8 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -852,9 +852,9 @@ static int create_ibt_endbr_seal_sections(struct 
objtool_file *file)
 
 static int create_mcount_loc_sections(struct objtool_file *file)
 {
-   struct section *sec;
-   unsigned long *loc;
+   int addrsize = elf_class_addrsize(file->elf);
struct instruction *insn;
+   struct section *sec;
int idx;
 
sec = find_section_by_name(file->elf, "__mcount_loc");
@@ -871,23 +871,25 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
list_for_each_entry(insn, >mcount_loc_list, call_node)
idx++;
 
-   sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned 
long), idx);
+   sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx);
if (!sec)
return -1;
 
+   sec->sh.sh_addralign = addrsize;
+
idx = 0;
list_for_each_entry(insn, >mcount_loc_list, call_node) {
+   void *loc;
 
-   loc = (unsigned long *)sec->data->d_buf + idx;
-   memset(loc, 0, sizeof(unsigned long));
+   loc = sec->data->d_buf + idx;
+   memset(loc, 0, addrsize);
 
-   if (elf_add_reloc_to_insn(file->elf, sec,
- idx * sizeof(unsigned long),
+   if (elf_add_reloc_to_insn(file->elf, sec, idx,
  R_X86_64_64,
  insn->sec, insn->offset))
return -1;
 
-   idx++;
+   idx += addrsize;
}
 
return 0;
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index c25e957c1e52..40c6d53b081f 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1124,6 +1124,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
 {
char *relocname;
struct section *sec;
+   int addrsize = elf_class_addrsize(elf);
 
relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
if (!relocname) {
@@ -1133,7 +1134,10 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
strcpy(relocname, ".rela");
strcat(relocname, base->name);
 
-   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
+   if (addrsize == sizeof(u32))
+   sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 
0);
+   else
+   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 
0);
free(relocname);
if (!sec)
return NULL;
@@ -1142,7 +1146,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
sec->base = base;
 
sec->sh.sh_type = SHT_RELA;
-   sec->sh.sh_addralign = 8;
+   sec->sh.sh_addralign = addrsize;
sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
sec->sh.sh_info = base->idx;
sec->sh.sh_flags = SHF_INFO_LINK;
diff --git a/tools/objtool/include/objtool/elf.h 
b/tools/objtool/include/objtool/elf.h
index 16f4067b82ae..78b3aa2e546d 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -142,6 +142,14 @@ static inline bool has_multiple_files(struct elf *elf)
return elf->num_files > 1;
 }
 
+static inline int elf_class_addrsize(struct elf *elf)
+{
+   if (elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+   return sizeof(u32);
+   else
+   return sizeof(u64);
+}
+
 struct elf *elf_open_read(const char *name, int flags);
 struct section *elf_create_section(struct elf *elf, const char *name, unsigned 
int sh_flags, size_t entsize, int nr);
 
-- 
2.31.1



[PATCH v3 08/16] objtool: Fix SEGFAULT

2022-09-12 Thread Sathvika Vasireddy
From: Christophe Leroy 

find_insn() will return NULL in case of failure. Check insn in order
to avoid a kernel Oops for NULL pointer dereference.

Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index e55fdf952a3a..fe935f19447b 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -207,7 +207,7 @@ static bool __dead_end_function(struct objtool_file *file, 
struct symbol *func,
return false;
 
insn = find_insn(file, func->sec, func->offset);
-   if (!insn->func)
+   if (!insn || !insn->func)
return false;
 
func_for_each_insn(file, func, insn) {
-- 
2.31.1



[PATCH v3 05/16] powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o

2022-09-12 Thread Sathvika Vasireddy
With objtool enabled, below warnings are seen when trying to build:
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: aes_p8_set_encrypt_key+0x44: 
unannotated intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2448: unannotated 
intra-function call
drivers/crypto/vmx/aesp8-ppc.o: warning: objtool: .text+0x2d68: unannotated 
intra-function call

Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o file for the
following reasons:

- Since this file comes from OpenSSL, and since it is a perl file which
  generates a .S file, it may not be the best choice to make too many
  code changes to such files, unless absolutely necessary.

- As far as the objtool --mcount functionality is concerned, we do not
  have to run objtool on this file because there are no calls to
  _mcount.

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 drivers/crypto/vmx/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/crypto/vmx/Makefile b/drivers/crypto/vmx/Makefile
index 2560cfea1dec..7b41f0da6807 100644
--- a/drivers/crypto/vmx/Makefile
+++ b/drivers/crypto/vmx/Makefile
@@ -9,3 +9,5 @@ targets += aesp8-ppc.S ghashp8-ppc.S
 
 $(obj)/aesp8-ppc.S $(obj)/ghashp8-ppc.S: $(obj)/%.S: $(src)/%.pl FORCE
$(call if_changed,perl)
+
+OBJECT_FILES_NON_STANDARD_aesp8-ppc.o := y
-- 
2.31.1



[PATCH v3 09/16] objtool: Use target file endianness instead of a compiled constant

2022-09-12 Thread Sathvika Vasireddy
From: Christophe Leroy 

Some architectures like powerpc support both endianness, it's
therefore not possible to fix the endianness via arch/endianness.h
because there is no easy way to get the target endianness at
build time.

Use the endianness recorded in the file objtool is working on.

Acked-by: Peter Zijlstra (Intel) 
Signed-off-by: Christophe Leroy 
---
 .../arch/x86/include/arch/endianness.h|  9 --
 tools/objtool/check.c |  2 +-
 tools/objtool/include/objtool/endianness.h| 32 +--
 tools/objtool/orc_dump.c  | 11 +--
 tools/objtool/orc_gen.c   |  4 +--
 tools/objtool/special.c   |  3 +-
 6 files changed, 30 insertions(+), 31 deletions(-)
 delete mode 100644 tools/objtool/arch/x86/include/arch/endianness.h

diff --git a/tools/objtool/arch/x86/include/arch/endianness.h 
b/tools/objtool/arch/x86/include/arch/endianness.h
deleted file mode 100644
index 7c362527da20..
--- a/tools/objtool/arch/x86/include/arch/endianness.h
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-#ifndef _ARCH_ENDIANNESS_H
-#define _ARCH_ENDIANNESS_H
-
-#include 
-
-#define __TARGET_BYTE_ORDER __LITTLE_ENDIAN
-
-#endif /* _ARCH_ENDIANNESS_H */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index fe935f19447b..c36e7a020d80 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2077,7 +2077,7 @@ static int read_unwind_hints(struct objtool_file *file)
return -1;
}
 
-   cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
+   cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
cfi.type = hint->type;
cfi.end = hint->end;
 
diff --git a/tools/objtool/include/objtool/endianness.h 
b/tools/objtool/include/objtool/endianness.h
index 10241341eff3..4d2aa9b0fe2f 100644
--- a/tools/objtool/include/objtool/endianness.h
+++ b/tools/objtool/include/objtool/endianness.h
@@ -2,33 +2,33 @@
 #ifndef _OBJTOOL_ENDIANNESS_H
 #define _OBJTOOL_ENDIANNESS_H
 
-#include 
 #include 
 #include 
-
-#ifndef __TARGET_BYTE_ORDER
-#error undefined arch __TARGET_BYTE_ORDER
-#endif
-
-#if __BYTE_ORDER != __TARGET_BYTE_ORDER
-#define __NEED_BSWAP 1
-#else
-#define __NEED_BSWAP 0
-#endif
+#include 
 
 /*
- * Does a byte swap if target endianness doesn't match the host, i.e. cross
+ * Does a byte swap if target file endianness doesn't match the host, i.e. 
cross
  * compilation for little endian on big endian and vice versa.
  * To be used for multi-byte values conversion, which are read from / about
  * to be written to a target native endianness ELF file.
  */
-#define bswap_if_needed(val)   \
+static inline bool need_bswap(struct elf *elf)
+{
+   return (__BYTE_ORDER == __LITTLE_ENDIAN) ^
+  (elf->ehdr.e_ident[EI_DATA] == ELFDATA2LSB);
+}
+
+#define bswap_if_needed(elf, val)  \
 ({ \
__typeof__(val) __ret;  \
+   bool __need_bswap = need_bswap(elf);\
switch (sizeof(val)) {  \
-   case 8: __ret = __NEED_BSWAP ? bswap_64(val) : (val); break;\
-   case 4: __ret = __NEED_BSWAP ? bswap_32(val) : (val); break;\
-   case 2: __ret = __NEED_BSWAP ? bswap_16(val) : (val); break;\
+   case 8: \
+   __ret = __need_bswap ? bswap_64(val) : (val); break;\
+   case 4: \
+   __ret = __need_bswap ? bswap_32(val) : (val); break;\
+   case 2: \
+   __ret = __need_bswap ? bswap_16(val) : (val); break;\
default:\
BUILD_BUG(); break; \
}   \
diff --git a/tools/objtool/orc_dump.c b/tools/objtool/orc_dump.c
index f5a8508c42d6..4f1211fec82c 100644
--- a/tools/objtool/orc_dump.c
+++ b/tools/objtool/orc_dump.c
@@ -76,6 +76,7 @@ int orc_dump(const char *_objname)
GElf_Rela rela;
GElf_Sym sym;
Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
+   struct elf dummy_elf = {};
 
 
objname = _objname;
@@ -94,6 +95,12 @@ int orc_dump(const char *_objname)
return -1;
}
 
+   if (!elf64_getehdr(elf)) {
+   WARN_ELF("elf64_getehdr");
+   return -1;
+   }
+   memcpy(_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
+
if (elf_getshdrnum(elf, _sections)) {
WARN_ELF("elf_getshdrnum");
 

[PATCH v3 04/16] powerpc: Curb objtool unannotated intra-function warnings

2022-09-12 Thread Sathvika Vasireddy
objtool throws the following unannotated intra-function call warnings:
arch/powerpc/kernel/entry_64.o: warning: objtool: .text+0x4: unannotated 
intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xe64: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xee4: 
unannotated intra-function call

Fix these warnings by annotating intra-function calls, using
ANNOTATE_INTRA_FUNCTION_CALL macro, to indicate that the branch targets
are valid.

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/entry_64.S  | 2 ++
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/arch/powerpc/kernel/entry_64.S b/arch/powerpc/kernel/entry_64.S
index 01ace4c56104..fb444bc64f3f 100644
--- a/arch/powerpc/kernel/entry_64.S
+++ b/arch/powerpc/kernel/entry_64.S
@@ -14,6 +14,7 @@
  *  code, and exception/interrupt return code for PowerPC.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -73,6 +74,7 @@ flush_branch_caches:
 
// Flush the link stack
.rept 64
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
b   1f
diff --git a/arch/powerpc/kvm/book3s_hv_rmhandlers.S 
b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
index bc187bb216ac..20212eb30764 100644
--- a/arch/powerpc/kvm/book3s_hv_rmhandlers.S
+++ b/arch/powerpc/kvm/book3s_hv_rmhandlers.S
@@ -11,6 +11,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1523,12 +1524,14 @@ kvm_flush_link_stack:
 
/* Flush the link stack. On Power8 it's up to 32 entries in size. */
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 
/* And on Power9 it's up to 64. */
 BEGIN_FTR_SECTION
.rept 32
+   ANNOTATE_INTRA_FUNCTION_CALL
bl  .+4
.endr
 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
-- 
2.31.1



[PATCH v3 03/16] powerpc: Fix objtool unannotated intra-function call warnings

2022-09-12 Thread Sathvika Vasireddy
Objtool throws unannotated intra-function call warnings in the following
assembly files:

arch/powerpc/kernel/vector.o: warning: objtool: .text+0x53c: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x60: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x124: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5d4: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x5dc: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xcb8: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0xd0c: 
unannotated intra-function call
arch/powerpc/kvm/book3s_hv_rmhandlers.o: warning: objtool: .text+0x1030: 
unannotated intra-function call

arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x358: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x728: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4d94: unannotated 
intra-function call
arch/powerpc/kernel/head_64.o: warning: objtool: .text+0x4ec4: unannotated 
intra-function call

arch/powerpc/kvm/book3s_hv_interrupts.o: warning: objtool: .text+0x6c: 
unannotated intra-function call
arch/powerpc/kernel/misc_64.o: warning: objtool: .text+0x64: unannotated 
intra-function call

Objtool does not add STT_NOTYPE symbols with size 0 to the rbtree, which
is why find_call_destination() function is not able to find the
destination symbol for 'bl' instruction. For such symbols, objtool is
throwing unannotated intra-function call warnings in assembly files. Fix
these warnings by annotating those symbols with SYM_FUNC_START_LOCAL and
SYM_FUNC_END macros, which set symbol type to STT_FUNC and symbol size
accordingly.

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/kernel/exceptions-64s.S|  7 +--
 arch/powerpc/kernel/head_64.S   |  7 +--
 arch/powerpc/kernel/misc_64.S   |  4 +++-
 arch/powerpc/kernel/vector.S|  4 +++-
 arch/powerpc/kvm/book3s_hv_interrupts.S |  4 +++-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S | 22 +++---
 6 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/arch/powerpc/kernel/exceptions-64s.S 
b/arch/powerpc/kernel/exceptions-64s.S
index 3d0dc133a9ae..56a31424c8b0 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -13,6 +13,7 @@
  *
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -3075,7 +3076,7 @@ CLOSE_FIXED_SECTION(virt_trampolines);
 USE_TEXT_SECTION()
 
 /* MSR[RI] should be clear because this uses SRR[01] */
-enable_machine_check:
+SYM_FUNC_START_LOCAL(enable_machine_check)
mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3087,9 +3088,10 @@ enable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(enable_machine_check)
 
 /* MSR[RI] should be clear because this uses SRR[01] */
-disable_machine_check:
+SYM_FUNC_START_LOCAL(disable_machine_check)
mflrr0
bcl 20,31,$+4
 0: mflrr3
@@ -3102,3 +3104,4 @@ disable_machine_check:
RFI_TO_KERNEL
 1: mtlrr0
blr
+SYM_FUNC_END(disable_machine_check)
diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S
index cf2c08902c05..10e2d43420d0 100644
--- a/arch/powerpc/kernel/head_64.S
+++ b/arch/powerpc/kernel/head_64.S
@@ -18,6 +18,7 @@
  *  variants.
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -465,7 +466,7 @@ generic_secondary_common_init:
  * Assumes we're mapped EA == RA if the MMU is on.
  */
 #ifdef CONFIG_PPC_BOOK3S
-__mmu_off:
+SYM_FUNC_START_LOCAL(__mmu_off)
mfmsr   r3
andi.   r0,r3,MSR_IR|MSR_DR
beqlr
@@ -476,6 +477,7 @@ __mmu_off:
sync
rfid
b   .   /* prevent speculative execution */
+SYM_FUNC_END(__mmu_off)
 #endif
 
 
@@ -869,7 +871,7 @@ _GLOBAL(start_secondary_resume)
 /*
  * This subroutine clobbers r11 and r12
  */
-enable_64b_mode:
+SYM_FUNC_START_LOCAL(enable_64b_mode)
mfmsr   r11 /* grab the current MSR */
 #ifdef CONFIG_PPC_BOOK3E
orisr11,r11,0x8000  /* CM bit set, we'll set ICM later */
@@ -881,6 +883,7 @@ enable_64b_mode:
isync
 #endif
blr
+SYM_FUNC_END(enable_64b_mode)
 
 /*
  * This puts the TOC pointer into r2, offset by 0x8000 (as expected
diff --git a/arch/powerpc/kernel/misc_64.S b/arch/powerpc/kernel/misc_64.S
index fd6d8d3a548e..b36fb89ff718 100644
--- a/arch/powerpc/kernel/misc_64.S
+++ b/arch/powerpc/kernel/misc_64.S
@@ -9,6 +9,7 @@
  * PPC64 updates by Dave Engebretsen (engeb...@us.ibm.com)
  */
 
+#include 
 #include 
 #include 
 #include 
@@ -353,7 +354,7 @@ _GLOBAL(kexec_smp_wait)
  *
  * don't overwrite r3 here, it is live

[PATCH v3 02/16] powerpc: Override __ALIGN and __ALIGN_STR macros

2022-09-12 Thread Sathvika Vasireddy
In a subsequent patch, we would want to annotate powerpc assembly functions
with SYM_FUNC_START_LOCAL macro. This macro depends on __ALIGN macro.

The default expansion of __ALIGN macro is:
#define __ALIGN  .align 4,0x90

So, override __ALIGN and __ALIGN_STR macros to use the same alignment as
that of the existing _GLOBAL macro. Also, do not pad with 0x90, because
repeated 0x90s are not a nop or trap on powerpc.

Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/linkage.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/powerpc/include/asm/linkage.h 
b/arch/powerpc/include/asm/linkage.h
index b71b9582e754..b88d1d2cf304 100644
--- a/arch/powerpc/include/asm/linkage.h
+++ b/arch/powerpc/include/asm/linkage.h
@@ -4,6 +4,9 @@
 
 #include 
 
+#define __ALIGN.align 2
+#define __ALIGN_STR".align 2"
+
 #ifdef CONFIG_PPC64_ELF_ABI_V1
 #define cond_syscall(x) \
asm ("\t.weak " #x "\n\t.set " #x ", sys_ni_syscall\n"  \
-- 
2.31.1



[PATCH v3 01/16] powerpc: Fix __WARN_FLAGS() for use with Objtool

2022-09-12 Thread Sathvika Vasireddy
Commit 1e688dd2a3d675 ("powerpc/bug: Provide better flexibility to
WARN_ON/__WARN_FLAGS() with asm goto") updated __WARN_FLAGS() to use asm
goto, and added a call to 'unreachable()' after the asm goto for optimal
code generation. With CONFIG_OBJTOOL enabled, 'annotate_unreachable()'
statement in 'unreachable()' tries to note down the location of the
subsequent instruction in a separate elf section to aid code flow
analysis. However, on powerpc, this results in gcc emitting a call to a
symbol of size 0. This results in objtool complaining of "unannotated
intra-function call" since the target symbol is not a valid function
call destination.

Objtool wants this annotation for code flow analysis, which we are not
yet enabling on powerpc. As such, expand the call to 'unreachable()' in
__WARN_FLAGS() without annotate_unreachable():
barrier_before_unreachable();
__builtin_unreachable();

This still results in optimal code generation for __WARN_FLAGS(), while
getting rid of the objtool warning.

We still need barrier_before_unreachable() to work around gcc bugs 82365
and 106751:
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365
- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106751

Reviewed-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/include/asm/bug.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 61a4736355c2..ef42adb44aa3 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -99,7 +99,8 @@
__label__ __label_warn_on;  \
\
WARN_ENTRY("twi 31, 0, 0", BUGFLAG_WARNING | (flags), __label_warn_on); 
\
-   unreachable();  \
+   barrier_before_unreachable();   \
+   __builtin_unreachable();\
\
 __label_warn_on:   \
break;  \
-- 
2.31.1



[PATCH v3 00/16] objtool: Enable and implement --mcount option on powerpc

2022-09-12 Thread Sathvika Vasireddy
This patchset enables and implements objtool --mcount
option on powerpc. This applies atop powerpc/merge branch.

Changelog:

v3:

* Patch 01/16 - Rework patch subject.
  - Rework changelog.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 02/16 - Rework changelog to update details based on feedback
from Nicholas Piggin and Michael Ellerman.
  - Use quotes instead of __stringify macro, based on
suggestion from Christophe Leroy.

* Patch 03/16 - Add Reviewed-by tag from Christophe Leroy.
  - Based on Christophe's suggestion, keep all 
before .
  - Rework changelog.

* Patch 04/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 05/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 06/16 - No change. 

* Patch 07/16 - Add Reviewed-by tag from Christophe Leroy.

* Patch 08/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 09/16 - Add Acked-by tag from Peter Zijlstra.

* Patch 10/16 - Reorder local variable declarations to use reverse
xmas tree format. 
  - Add Signed-off-by tag from Sathvika Vasireddy indicating
changes done.
  - Add Acked-by tag from Peter Zijlstra.

* Patch 11/16 - Update changelog to indicate that powerpc kernel does
not support nop'ed out ftrace locations. 
  - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 12/16 - Per Christophe's comment, rework changelog. 

* Patch 13/16 - Add Acked-by tag from Peter Zijlstra.
  - Add Reviewed-by tag from Christophe Leroy.

* Patch 14/16 - Simplify arch_ftrace_match() function, based on
Christophe's suggestion. 
  - Add Reviewed-by tag from Christophe Leroy. 

* Patch 15/16 - Include code from Christophe Leroy to use local vars for
type and imm, and to adapt len for prefixed
instructions.

* Patch 16/16 - Based on suggestion from Christophe Leroy, setup
immediate value calculation outside the check for
specific instruction under case 18. 
  - Set instruction type to INSN_CALL for 'bla'
instruction as well.

v2:

* Change subject of patch 01/16
* As suggested by Christophe Leroy, add barrier_before_unreachable()
before __builtin_unreachable() to work around a gcc problem.
* Fix issues reported by Kernel Test Robot.
* Include suggestions from Christophe Leroy, and change commit 
messages for patches 01/16, 02/16, 03/16, 05/16.

Christophe Leroy (4):
  objtool: Fix SEGFAULT
  objtool: Use target file endianness instead of a compiled constant
  objtool: Use target file class size instead of a compiled constant
  powerpc: Fix objtool unannotated intra-function call warnings on PPC32

Sathvika Vasireddy (12):
  powerpc: Fix __WARN_FLAGS() for use with Objtool
  powerpc: Override __ALIGN and __ALIGN_STR macros
  powerpc: Fix objtool unannotated intra-function call warnings
  powerpc: Curb objtool unannotated intra-function warnings
  powerpc: Skip objtool from running on drivers/crypto/vmx/aesp8-ppc.o
  powerpc: Skip objtool from running on VDSO files
  objtool: Add --mnop as an option to --mcount
  objtool: Read special sections with alts only when specific options are 
selected
  objtool: Use macros to define arch specific reloc types
  objtool: Add arch specific function arch_ftrace_match()
  objtool/powerpc: Enable objtool to be built on ppc
  objtool/powerpc: Add --mcount specific implementation

 Makefile  |   4 +-
 arch/powerpc/Kconfig  |   2 +
 arch/powerpc/include/asm/asm.h|   7 ++
 arch/powerpc/include/asm/bug.h|   3 +-
 arch/powerpc/include/asm/linkage.h|   3 +
 arch/powerpc/kernel/cpu_setup_6xx.S   |  26 +++--
 arch/powerpc/kernel/cpu_setup_fsl_booke.S |   8 +-
 arch/powerpc/kernel/entry_32.S|   9 +-
 arch/powerpc/kernel/entry_64.S|   2 +
 arch/powerpc/kernel/exceptions-64s.S  |   7 +-
 arch/powerpc/kernel/head_40x.S|   5 +-
 arch/powerpc/kernel/head_64.S |   7 +-
 arch/powerpc/kernel/head_8xx.S|   5 +-
 arch/powerpc/kernel/head_book3s_32.S  |  29 +++--
 arch/powerpc/kernel/head_fsl_booke.S  |   5 +-
 arch/powerpc/kernel/misc_64.S |   4 +-
 arch/powerpc/kernel/swsusp_32.S   |   5 +-
 arch/powerpc/kernel/vdso/Makefile |   2 +
 arch/powerpc/kernel/vector.S  |   4 +-
 arch/powerpc/kvm/book3s_hv_interrupts.S   |   4 +-
 arch/powerpc/kvm/book3s_hv_rmhandlers.S   |  25 +++--
 arch/powerpc/kvm/fpu.S|  17 ++-
 arch/powerpc/platforms/52xx/lite5200_sleep.S  |  15 ++-
 arch/x86/Kconfig  |   1 +
 drivers/crypto/vmx

Re: [PATCH v2 02/16] powerpc: override __ALIGN() and __ALIGN_STR() macros

2022-08-29 Thread Sathvika Vasireddy

Hi Christophe,

On 29/08/22 18:56, Christophe Leroy wrote:


Le 29/08/2022 à 07:52, Sathvika Vasireddy a écrit :

Powerpc instructions must be word-aligned. Currently,
there is an alignment of 16 bytes (by default), and it is
much more than what is required for powerpc (4 bytes).

The default expansion of __ALIGN() macro is:
#define __ALIGN   .align 4,0x90

Since Powerpc Linux does not require a 16 byte alignment,
override __ALIGN() and __ALIGN_STR() macros to use required
4 byte alignment.

Signed-off-by: Sathvika Vasireddy 
---
   arch/powerpc/include/asm/linkage.h | 4 
   1 file changed, 4 insertions(+)

diff --git a/arch/powerpc/include/asm/linkage.h 
b/arch/powerpc/include/asm/linkage.h
index b71b9582e754..8df88fe61438 100644
--- a/arch/powerpc/include/asm/linkage.h
+++ b/arch/powerpc/include/asm/linkage.h
@@ -2,8 +2,12 @@
   #ifndef _ASM_POWERPC_LINKAGE_H
   #define _ASM_POWERPC_LINKAGE_H
   
+#include 

   #include 
   
+#define __ALIGN			.align 2

+#define __ALIGN_STR__stringify(__ALIGN)
+

I still can't see the added value of using __stringify() macro here. In
order to use that macro you have to include linux/stringify.h . Usually
we try to minimise the amount of headers required by other headers.

Oh ok, makes sense to me. I'll wait for a day to see if there are any
other comments, and make this change as part of v3.

Thanks for reviewing!

- Sathvika


[PATCH v2 16/16] objtool/powerpc: Add --mcount specific implementation

2022-08-29 Thread Sathvika Vasireddy
This patch enables objtool --mcount on powerpc, and
adds implementation specific to powerpc.

Signed-off-by: Sathvika Vasireddy 
---
 arch/powerpc/Kconfig  |  1 +
 tools/objtool/arch/powerpc/decode.c   | 22 +++
 tools/objtool/arch/powerpc/include/arch/elf.h |  2 ++
 3 files changed, 25 insertions(+)

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index dc05cd23c233..6be2e68fa9eb 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -238,6 +238,7 @@ config PPC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
+   select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
index 8b6a14680da7..b71c265ed503 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -9,6 +9,14 @@
 #include 
 #include 
 
+bool arch_ftrace_match(char *name)
+{
+   if (!strcmp(name, "_mcount"))
+   return true;
+
+   return false;
+}
+
 unsigned long arch_dest_reloc_offset(int addend)
 {
return addend;
@@ -41,12 +49,26 @@ int arch_decode_instruction(struct objtool_file *file, 
const struct section *sec
struct list_head *ops_list)
 {
u32 insn;
+   unsigned int opcode;
 
*immediate = 0;
insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
*len = 4;
*type = INSN_OTHER;
 
+   opcode = insn >> 26;
+
+   switch (opcode) {
+   case 18: /* bl */
+   if ((insn & 3) == 1) {
+   *type = INSN_CALL;
+   *immediate = insn & 0x3fc;
+   if (*immediate & 0x200)
+   *immediate -= 0x400;
+   }
+   break;
+   }
+
return 0;
 }
 
diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h 
b/tools/objtool/arch/powerpc/include/arch/elf.h
index 3c8ebb7d2a6b..73f9ae172fe5 100644
--- a/tools/objtool/arch/powerpc/include/arch/elf.h
+++ b/tools/objtool/arch/powerpc/include/arch/elf.h
@@ -4,5 +4,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_PPC_NONE
+#define R_ABS64 R_PPC64_ADDR64
+#define R_ABS32 R_PPC_ADDR32
 
 #endif /* _OBJTOOL_ARCH_ELF */
-- 
2.31.1



[PATCH v2 15/16] objtool/powerpc: Enable objtool to be built on ppc

2022-08-29 Thread Sathvika Vasireddy
This patch adds [stub] implementations for required
functions, inorder to enable objtool build on powerpc.

Signed-off-by: Sathvika Vasireddy 
[Christophe Leroy: powerpc: Add missing asm/asm.h for objtool]
Signed-off-by: Christophe Leroy 
---
 arch/powerpc/Kconfig  |  1 +
 arch/powerpc/include/asm/asm.h|  7 ++
 tools/objtool/arch/powerpc/Build  |  2 +
 tools/objtool/arch/powerpc/decode.c   | 74 +++
 .../arch/powerpc/include/arch/cfi_regs.h  | 11 +++
 tools/objtool/arch/powerpc/include/arch/elf.h |  8 ++
 .../arch/powerpc/include/arch/special.h   | 21 ++
 tools/objtool/arch/powerpc/special.c  | 19 +
 8 files changed, 143 insertions(+)
 create mode 100644 arch/powerpc/include/asm/asm.h
 create mode 100644 tools/objtool/arch/powerpc/Build
 create mode 100644 tools/objtool/arch/powerpc/decode.c
 create mode 100644 tools/objtool/arch/powerpc/include/arch/cfi_regs.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/elf.h
 create mode 100644 tools/objtool/arch/powerpc/include/arch/special.h
 create mode 100644 tools/objtool/arch/powerpc/special.c

diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
index 4c466acdc70d..dc05cd23c233 100644
--- a/arch/powerpc/Kconfig
+++ b/arch/powerpc/Kconfig
@@ -237,6 +237,7 @@ config PPC
select HAVE_MOD_ARCH_SPECIFIC
select HAVE_NMI if PERF_EVENTS || (PPC64 && 
PPC_BOOK3S)
select HAVE_OPTPROBES
+   select HAVE_OBJTOOL if PPC32 || MPROFILE_KERNEL
select HAVE_PERF_EVENTS
select HAVE_PERF_EVENTS_NMI if PPC64
select HAVE_PERF_REGS
diff --git a/arch/powerpc/include/asm/asm.h b/arch/powerpc/include/asm/asm.h
new file mode 100644
index ..86f46b604e9a
--- /dev/null
+++ b/arch/powerpc/include/asm/asm.h
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_POWERPC_ASM_H
+#define _ASM_POWERPC_ASM_H
+
+#define _ASM_PTR   " .long "
+
+#endif /* _ASM_POWERPC_ASM_H */
diff --git a/tools/objtool/arch/powerpc/Build b/tools/objtool/arch/powerpc/Build
new file mode 100644
index ..d24d5636a5b8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/Build
@@ -0,0 +1,2 @@
+objtool-y += decode.o
+objtool-y += special.o
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
new file mode 100644
index ..8b6a14680da7
--- /dev/null
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+unsigned long arch_dest_reloc_offset(int addend)
+{
+   return addend;
+}
+
+bool arch_callee_saved_reg(unsigned char reg)
+{
+   return false;
+}
+
+int arch_decode_hint_reg(u8 sp_reg, int *base)
+{
+   exit(-1);
+}
+
+const char *arch_nop_insn(int len)
+{
+   exit(-1);
+}
+
+const char *arch_ret_insn(int len)
+{
+   exit(-1);
+}
+
+int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
+   unsigned long offset, unsigned int maxlen,
+   unsigned int *len, enum insn_type *type,
+   unsigned long *immediate,
+   struct list_head *ops_list)
+{
+   u32 insn;
+
+   *immediate = 0;
+   insn = bswap_if_needed(file->elf, *(u32 *)(sec->data->d_buf + offset));
+   *len = 4;
+   *type = INSN_OTHER;
+
+   return 0;
+}
+
+unsigned long arch_jump_destination(struct instruction *insn)
+{
+   return insn->offset +  insn->immediate;
+}
+
+void arch_initial_func_cfi_state(struct cfi_init_state *state)
+{
+   int i;
+
+   for (i = 0; i < CFI_NUM_REGS; i++) {
+   state->regs[i].base = CFI_UNDEFINED;
+   state->regs[i].offset = 0;
+   }
+
+   /* initial CFA (call frame address) */
+   state->cfa.base = CFI_SP;
+   state->cfa.offset = 0;
+
+   /* initial LR (return address) */
+   state->regs[CFI_RA].base = CFI_CFA;
+   state->regs[CFI_RA].offset = 0;
+}
diff --git a/tools/objtool/arch/powerpc/include/arch/cfi_regs.h 
b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
new file mode 100644
index ..59638ebeafc8
--- /dev/null
+++ b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _OBJTOOL_CFI_REGS_H
+#define _OBJTOOL_CFI_REGS_H
+
+#define CFI_BP 1
+#define CFI_SP CFI_BP
+#define CFI_RA 32
+#define CFI_NUM_REGS 33
+
+#endif
diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h 
b/tools/objtool/arch/powerpc/include/arch/elf.h
new file mode 100644
index ..3c8ebb7d2a6b
--- /dev/null
+++ b/tools/objtool/arch/powerpc/include/arch/elf.h
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#ifndef _OBJTOOL

[PATCH v2 14/16] objtool: Add arch specific function arch_ftrace_match()

2022-08-29 Thread Sathvika Vasireddy
Add architecture specific function to look for
relocation records pointing to arch specific
symbols.

Suggested-by: Christophe Leroy 
Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/decode.c  | 8 
 tools/objtool/check.c| 2 +-
 tools/objtool/include/objtool/arch.h | 2 ++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index c260006106be..025598b6b703 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -23,6 +23,14 @@
 #include 
 #include 
 
+bool arch_ftrace_match(char *name)
+{
+   if (!strcmp(name, "__fentry__"))
+   return true;
+
+   return false;
+}
+
 static int is_x86_64(const struct elf *elf)
 {
switch (elf->ehdr.e_machine) {
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index f1d055467926..01ff7504f18d 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2294,7 +2294,7 @@ static int classify_symbols(struct objtool_file *file)
if (arch_is_rethunk(func))
func->return_thunk = true;
 
-   if (!strcmp(func->name, "__fentry__"))
+   if (arch_ftrace_match(func->name))
func->fentry = true;
 
if (is_profiling_func(func->name))
diff --git a/tools/objtool/include/objtool/arch.h 
b/tools/objtool/include/objtool/arch.h
index beb2f3aa94ff..2ba4b9897285 100644
--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -69,6 +69,8 @@ struct stack_op {
 
 struct instruction;
 
+bool arch_ftrace_match(char *name);
+
 void arch_initial_func_cfi_state(struct cfi_init_state *state);
 
 int arch_decode_instruction(struct objtool_file *file, const struct section 
*sec,
-- 
2.31.1



[PATCH v2 13/16] objtool: Use macros to define arch specific reloc types

2022-08-29 Thread Sathvika Vasireddy
Make relocation types architecture specific.

Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/arch/x86/include/arch/elf.h | 2 ++
 tools/objtool/check.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/arch/x86/include/arch/elf.h 
b/tools/objtool/arch/x86/include/arch/elf.h
index 69cc4264b28a..ac14987cf687 100644
--- a/tools/objtool/arch/x86/include/arch/elf.h
+++ b/tools/objtool/arch/x86/include/arch/elf.h
@@ -2,5 +2,7 @@
 #define _OBJTOOL_ARCH_ELF
 
 #define R_NONE R_X86_64_NONE
+#define R_ABS64 R_X86_64_64
+#define R_ABS32 R_X86_64_32
 
 #endif /* _OBJTOOL_ARCH_ELF */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 5298a143ceac..f1d055467926 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -883,7 +883,7 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
memset(loc, 0, addrsize);
 
if (elf_add_reloc_to_insn(file->elf, sec, idx,
- R_X86_64_64,
+ addrsize == sizeof(u64) ? R_ABS64 : 
R_ABS32,
  insn->sec, insn->offset))
return -1;
 
-- 
2.31.1



[PATCH v2 12/16] objtool: Read special sections with alts only when specific options are selected

2022-08-29 Thread Sathvika Vasireddy
This patch reads special sections which have alternate
instructions, only when stackval or orc or uaccess or
noinstr options are passed to objtool.

Signed-off-by: Sathvika Vasireddy 
---
 tools/objtool/check.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 3cea58f73878..5298a143ceac 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2370,9 +2370,11 @@ static int decode_sections(struct objtool_file *file)
 * Must be before add_jump_destinations(), which depends on 'func'
 * being set for alternatives, to enable proper sibling call detection.
 */
-   ret = add_special_section_alts(file);
-   if (ret)
-   return ret;
+   if (opts.stackval || opts.orc || opts.uaccess || opts.noinstr) {
+   ret = add_special_section_alts(file);
+   if (ret)
+   return ret;
+   }
 
ret = add_jump_destinations(file);
if (ret)
-- 
2.31.1



[PATCH v2 11/16] objtool: Add --mnop as an option to --mcount

2022-08-29 Thread Sathvika Vasireddy
Architectures can select HAVE_NOP_MCOUNT if they choose
to nop out mcount call sites. If that config option is
selected, then --mnop is passed as an option to objtool,
along with --mcount.

Also, make sure that --mnop can be passed as an option
to objtool only when --mcount is passed.

Signed-off-by: Sathvika Vasireddy 
---
 Makefile|  4 +++-
 arch/x86/Kconfig|  1 +
 scripts/Makefile.lib|  1 +
 tools/objtool/builtin-check.c   | 14 ++
 tools/objtool/check.c   | 19 ++-
 tools/objtool/include/objtool/builtin.h |  1 +
 6 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index c7705f749601..99dd33d8bcfa 100644
--- a/Makefile
+++ b/Makefile
@@ -857,7 +857,9 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_CC
   endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
-  CC_FLAGS_USING   += -DCC_USING_NOP_MCOUNT
+  ifdef CONFIG_HAVE_NOP_MCOUNT
+CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
+  endif
 endif
 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
   ifdef CONFIG_HAVE_C_RECORDMCOUNT
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index f9920f1341c8..a8dd138df637 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -189,6 +189,7 @@ config X86
select HAVE_CONTEXT_TRACKING_USER_OFFSTACK  if 
HAVE_CONTEXT_TRACKING_USER
select HAVE_C_RECORDMCOUNT
select HAVE_OBJTOOL_MCOUNT  if HAVE_OBJTOOL
+   select HAVE_NOP_MCOUNT  if HAVE_OBJTOOL_MCOUNT
select HAVE_BUILDTIME_MCOUNT_SORT
select HAVE_DEBUG_KMEMLEAK
select HAVE_DMA_CONTIGUOUS
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3fb6a99e78c4..0610078e057a 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -234,6 +234,7 @@ objtool_args =  
\
$(if $(CONFIG_HAVE_NOINSTR_HACK), --hacks=noinstr)  \
$(if $(CONFIG_X86_KERNEL_IBT), --ibt)   \
$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount) \
+   $(if $(CONFIG_HAVE_NOP_MCOUNT), --mnop) \
$(if $(CONFIG_UNWINDER_ORC), --orc) \
$(if $(CONFIG_RETPOLINE), --retpoline)  \
$(if $(CONFIG_RETHUNK), --rethunk)  \
diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 24fbe803a0d3..9bd347d3c244 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -82,6 +82,7 @@ const struct option check_options[] = {
OPT_BOOLEAN(0, "dry-run", , "don't write modifications"),
OPT_BOOLEAN(0, "link", , "object is a linked object"),
OPT_BOOLEAN(0, "module", , "object is part of a kernel 
module"),
+   OPT_BOOLEAN(0, "mnop", , "nop out mcount call sites"),
OPT_BOOLEAN(0, "no-unreachable", _unreachable, "skip 
'unreachable instruction' warnings"),
OPT_BOOLEAN(0, "sec-address", _address, "print section 
addresses in warnings"),
OPT_BOOLEAN(0, "stats", , "print statistics"),
@@ -150,6 +151,16 @@ static bool opts_valid(void)
return false;
 }
 
+static bool mnop_opts_valid(void)
+{
+   if (opts.mnop && !opts.mcount) {
+   ERROR("--mnop requires --mcount");
+   return false;
+   }
+
+   return true;
+}
+
 static bool link_opts_valid(struct objtool_file *file)
 {
if (opts.link)
@@ -198,6 +209,9 @@ int objtool_run(int argc, const char **argv)
if (!file)
return 1;
 
+   if (!mnop_opts_valid())
+   return 1;
+
if (!link_opts_valid(file))
return 1;
 
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 0ecf41ee73f0..3cea58f73878 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1231,17 +1231,18 @@ static void annotate_call_site(struct objtool_file 
*file,
if (opts.mcount && sym->fentry) {
if (sibling)
WARN_FUNC("Tail call to __fentry__ !?!?", insn->sec, 
insn->offset);
+   if (opts.mnop) {
+   if (reloc) {
+   reloc->type = R_NONE;
+   elf_write_reloc(file->elf, reloc);
+   }
 
-   if (reloc) {
-   reloc->type = R_NONE;
-   elf_write_reloc(file->elf, reloc);
-   }
-
-   elf_write_insn(file->elf, insn->sec,
-  insn->offset, insn->len,
-  arch_nop_insn(insn->len));
+   elf_write_insn(file->

[PATCH v2 10/16] objtool: Use target file class size instead of a compiled constant

2022-08-28 Thread Sathvika Vasireddy
From: Christophe Leroy 

In order to allow using objtool on cross-built kernels,
determine size of long from elf data instead of using
sizeof(long) at build time.

For the time being this covers only mcount.

Signed-off-by: Christophe Leroy 
---
 tools/objtool/check.c   | 16 +---
 tools/objtool/elf.c |  8 ++--
 tools/objtool/include/objtool/elf.h |  8 
 3 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index a948b2551520..0ecf41ee73f0 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -851,9 +851,9 @@ static int create_ibt_endbr_seal_sections(struct 
objtool_file *file)
 static int create_mcount_loc_sections(struct objtool_file *file)
 {
struct section *sec;
-   unsigned long *loc;
struct instruction *insn;
int idx;
+   int addrsize = elf_class_addrsize(file->elf);
 
sec = find_section_by_name(file->elf, "__mcount_loc");
if (sec) {
@@ -869,23 +869,25 @@ static int create_mcount_loc_sections(struct objtool_file 
*file)
list_for_each_entry(insn, >mcount_loc_list, call_node)
idx++;
 
-   sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned 
long), idx);
+   sec = elf_create_section(file->elf, "__mcount_loc", 0, addrsize, idx);
if (!sec)
return -1;
 
+   sec->sh.sh_addralign = addrsize;
+
idx = 0;
list_for_each_entry(insn, >mcount_loc_list, call_node) {
+   void *loc;
 
-   loc = (unsigned long *)sec->data->d_buf + idx;
-   memset(loc, 0, sizeof(unsigned long));
+   loc = sec->data->d_buf + idx;
+   memset(loc, 0, addrsize);
 
-   if (elf_add_reloc_to_insn(file->elf, sec,
- idx * sizeof(unsigned long),
+   if (elf_add_reloc_to_insn(file->elf, sec, idx,
  R_X86_64_64,
  insn->sec, insn->offset))
return -1;
 
-   idx++;
+   idx += addrsize;
}
 
return 0;
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index c25e957c1e52..40c6d53b081f 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1124,6 +1124,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
 {
char *relocname;
struct section *sec;
+   int addrsize = elf_class_addrsize(elf);
 
relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
if (!relocname) {
@@ -1133,7 +1134,10 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
strcpy(relocname, ".rela");
strcat(relocname, base->name);
 
-   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
+   if (addrsize == sizeof(u32))
+   sec = elf_create_section(elf, relocname, 0, sizeof(Elf32_Rela), 
0);
+   else
+   sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 
0);
free(relocname);
if (!sec)
return NULL;
@@ -1142,7 +1146,7 @@ static struct section 
*elf_create_rela_reloc_section(struct elf *elf, struct sec
sec->base = base;
 
sec->sh.sh_type = SHT_RELA;
-   sec->sh.sh_addralign = 8;
+   sec->sh.sh_addralign = addrsize;
sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
sec->sh.sh_info = base->idx;
sec->sh.sh_flags = SHF_INFO_LINK;
diff --git a/tools/objtool/include/objtool/elf.h 
b/tools/objtool/include/objtool/elf.h
index 16f4067b82ae..78b3aa2e546d 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -142,6 +142,14 @@ static inline bool has_multiple_files(struct elf *elf)
return elf->num_files > 1;
 }
 
+static inline int elf_class_addrsize(struct elf *elf)
+{
+   if (elf->ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+   return sizeof(u32);
+   else
+   return sizeof(u64);
+}
+
 struct elf *elf_open_read(const char *name, int flags);
 struct section *elf_create_section(struct elf *elf, const char *name, unsigned 
int sh_flags, size_t entsize, int nr);
 
-- 
2.31.1



[PATCH v2 09/16] objtool: Use target file endianness instead of a compiled constant

2022-08-28 Thread Sathvika Vasireddy
From: Christophe Leroy 

Some architectures like powerpc support both endianness, it's
therefore not possible to fix the endianness via arch/endianness.h
because there is no easy way to get the target endianness at
build time.

Use the endianness recorded in the file objtool is working on.

Signed-off-by: Christophe Leroy 
---
 .../arch/x86/include/arch/endianness.h|  9 --
 tools/objtool/check.c |  2 +-
 tools/objtool/include/objtool/endianness.h| 32 +--
 tools/objtool/orc_dump.c  | 11 +--
 tools/objtool/orc_gen.c   |  4 +--
 tools/objtool/special.c   |  3 +-
 6 files changed, 30 insertions(+), 31 deletions(-)
 delete mode 100644 tools/objtool/arch/x86/include/arch/endianness.h

diff --git a/tools/objtool/arch/x86/include/arch/endianness.h 
b/tools/objtool/arch/x86/include/arch/endianness.h
deleted file mode 100644
index 7c362527da20..
--- a/tools/objtool/arch/x86/include/arch/endianness.h
+++ /dev/null
@@ -1,9 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-#ifndef _ARCH_ENDIANNESS_H
-#define _ARCH_ENDIANNESS_H
-
-#include 
-
-#define __TARGET_BYTE_ORDER __LITTLE_ENDIAN
-
-#endif /* _ARCH_ENDIANNESS_H */
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index ed2fdfeb1d9c..a948b2551520 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2075,7 +2075,7 @@ static int read_unwind_hints(struct objtool_file *file)
return -1;
}
 
-   cfi.cfa.offset = bswap_if_needed(hint->sp_offset);
+   cfi.cfa.offset = bswap_if_needed(file->elf, hint->sp_offset);
cfi.type = hint->type;
cfi.end = hint->end;
 
diff --git a/tools/objtool/include/objtool/endianness.h 
b/tools/objtool/include/objtool/endianness.h
index 10241341eff3..4d2aa9b0fe2f 100644
--- a/tools/objtool/include/objtool/endianness.h
+++ b/tools/objtool/include/objtool/endianness.h
@@ -2,33 +2,33 @@
 #ifndef _OBJTOOL_ENDIANNESS_H
 #define _OBJTOOL_ENDIANNESS_H
 
-#include 
 #include 
 #include 
-
-#ifndef __TARGET_BYTE_ORDER
-#error undefined arch __TARGET_BYTE_ORDER
-#endif
-
-#if __BYTE_ORDER != __TARGET_BYTE_ORDER
-#define __NEED_BSWAP 1
-#else
-#define __NEED_BSWAP 0
-#endif
+#include 
 
 /*
- * Does a byte swap if target endianness doesn't match the host, i.e. cross
+ * Does a byte swap if target file endianness doesn't match the host, i.e. 
cross
  * compilation for little endian on big endian and vice versa.
  * To be used for multi-byte values conversion, which are read from / about
  * to be written to a target native endianness ELF file.
  */
-#define bswap_if_needed(val)   \
+static inline bool need_bswap(struct elf *elf)
+{
+   return (__BYTE_ORDER == __LITTLE_ENDIAN) ^
+  (elf->ehdr.e_ident[EI_DATA] == ELFDATA2LSB);
+}
+
+#define bswap_if_needed(elf, val)  \
 ({ \
__typeof__(val) __ret;  \
+   bool __need_bswap = need_bswap(elf);\
switch (sizeof(val)) {  \
-   case 8: __ret = __NEED_BSWAP ? bswap_64(val) : (val); break;\
-   case 4: __ret = __NEED_BSWAP ? bswap_32(val) : (val); break;\
-   case 2: __ret = __NEED_BSWAP ? bswap_16(val) : (val); break;\
+   case 8: \
+   __ret = __need_bswap ? bswap_64(val) : (val); break;\
+   case 4: \
+   __ret = __need_bswap ? bswap_32(val) : (val); break;\
+   case 2: \
+   __ret = __need_bswap ? bswap_16(val) : (val); break;\
default:\
BUILD_BUG(); break; \
}   \
diff --git a/tools/objtool/orc_dump.c b/tools/objtool/orc_dump.c
index f5a8508c42d6..4f1211fec82c 100644
--- a/tools/objtool/orc_dump.c
+++ b/tools/objtool/orc_dump.c
@@ -76,6 +76,7 @@ int orc_dump(const char *_objname)
GElf_Rela rela;
GElf_Sym sym;
Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
+   struct elf dummy_elf = {};
 
 
objname = _objname;
@@ -94,6 +95,12 @@ int orc_dump(const char *_objname)
return -1;
}
 
+   if (!elf64_getehdr(elf)) {
+   WARN_ELF("elf64_getehdr");
+   return -1;
+   }
+   memcpy(_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
+
if (elf_getshdrnum(elf, _sections)) {
WARN_ELF("elf_getshdrnum");
return -1;
@@ 

[PATCH v2 08/16] objtool: Fix SEGFAULT

2022-08-28 Thread Sathvika Vasireddy
From: Christophe Leroy 

find_insn() will return NULL in case of failure. Check insn in order
to avoid a kernel Oops for NULL pointer dereference.

Signed-off-by: Christophe Leroy 
---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 91678252a9b6..ed2fdfeb1d9c 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -205,7 +205,7 @@ static bool __dead_end_function(struct objtool_file *file, 
struct symbol *func,
return false;
 
insn = find_insn(file, func->sec, func->offset);
-   if (!insn->func)
+   if (!insn || !insn->func)
return false;
 
func_for_each_insn(file, func, insn) {
-- 
2.31.1



  1   2   >