public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101275] New: [RISCV] Document the machine constraint 'S' and make it non-internal
@ 2021-06-30 23:01 i at maskray dot me
  2021-07-02  2:34 ` [Bug target/101275] " kito at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: i at maskray dot me @ 2021-06-30 23:01 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101275

            Bug ID: 101275
           Summary: [RISCV] Document the machine constraint 'S' and make
                    it non-internal
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: i at maskray dot me
  Target Milestone: ---

An absolute symbolic operand is useful in inline asm. In aarch64 and x86-64,
there is a documented (supported) approach.

// aarch64
// 'S' is documented on
https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints
extern int var;
void *addr() { return &var; }
void *addr_via_asm() {
  void *ret;
  asm("adrp %0, %1\nadd %0, %0, :lo12:%1" : "=r"(ret) : "S"(&var));
  return ret;
}
// x86-64
// https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#x86-Operand-Modifiers
// 'P' is useful in some cases.
extern int var;
void *addr() { return &var; }
void *addr_via_asm() {
  void *ret;
  asm("movl %1, %k0" : "=r"(ret) : "i"(&var));
  return ret;
}

For riscv there is no supported approach. The following code works

// riscv
extern int var;
void *addr() { return &var; }
void *addr_via_asm() {
  void *ret;
  asm("lui %0, %%hi(%1)\naddi %0,%0,%%lo(%1)" : "=r"(ret) : "S"(&var));
  return ret;
}

but it uses the internal/undocumented constraint "S"

  // gcc/config/riscv/constraints.md
  (define_constraint "S"
    "@internal
     A constant call address."
    (match_operand 0 "absolute_symbolic_operand"))

I hope this can made supported/documented/non-internal.

---

Why do I want this?

In the Linux kernel, arch/riscv/include/asm/vdso.h defines

  /*
   * The VDSO symbols are mapped into Linux so we can just use regular symbol
   * addressing to get their offsets in userspace.  The symbols are mapped at
an
   * offset of 0, but since the linker must support setting weak undefined
   * symbols to the absolute address 0 it also happens to support other low
   * addresses even when the code model suggests those low addresses would not
   * otherwise be availiable.
   */
  #define VDSO_SYMBOL(base, name)                                              
 \
  ({                                                                           
 \
          extern const char __vdso_##name[];                                   
 \
          (void __user *)((unsigned long)(base) + __vdso_##name);              
 \
  })

arch/riscv/kernel/signal.c has

  regs->ra = (unsigned long)VDSO_SYMBOL(
          current->mm->context.vdso, rt_sigreturn);

The file is compiled with -fno-PIE -mcmodel=medany. Because of medany, auipc is
generated:

 36e:   00000697                auipc   a3,0x0
                        36e: R_RISCV_PCREL_HI20 __vdso_rt_sigreturn
                        36e: R_RISCV_RELAX      *ABS*
 372:   00068693                mv      a3,a3

At link time, auipc/mv (PC-relative) is rewritten as lui/addi (absolute):

  ffffffe000201880:       000016b7                lui     a3,0x1
  ffffffe000201884:       80068693                addi    a3,a3,-2048 # 800
<__vdso_rt_sigreturn>

The absolute address is needed for correctness. With PC-relative insns, the
runtime address will be different from its link-time address and
current->mm->context.vdso + __vdso_rt_sigreturn will be wrong.

It is fragile to rely on linker instruction rewritting (part of linker
relaxation) to do this correctness related stuff!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug target/101275] [RISCV] Document the machine constraint 'S' and make it non-internal
  2021-06-30 23:01 [Bug c/101275] New: [RISCV] Document the machine constraint 'S' and make it non-internal i at maskray dot me
@ 2021-07-02  2:34 ` kito at gcc dot gnu.org
  2021-07-13  6:10 ` cvs-commit at gcc dot gnu.org
  2021-07-13  6:12 ` kito at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: kito at gcc dot gnu.org @ 2021-07-02  2:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101275

Kito Cheng <kito at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kito at gcc dot gnu.org

--- Comment #1 from Kito Cheng <kito at gcc dot gnu.org> ---
I am happy to add it to document, here it the patch:

https://gcc.gnu.org/pipermail/gcc-patches/2021-July/574318.html

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug target/101275] [RISCV] Document the machine constraint 'S' and make it non-internal
  2021-06-30 23:01 [Bug c/101275] New: [RISCV] Document the machine constraint 'S' and make it non-internal i at maskray dot me
  2021-07-02  2:34 ` [Bug target/101275] " kito at gcc dot gnu.org
@ 2021-07-13  6:10 ` cvs-commit at gcc dot gnu.org
  2021-07-13  6:12 ` kito at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-07-13  6:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101275

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Kito Cheng <kito@gcc.gnu.org>:

https://gcc.gnu.org/g:18a463bb666cc8f3421589e7641ec617acb84741

commit r12-2268-g18a463bb666cc8f3421589e7641ec617acb84741
Author: Kito Cheng <kito.cheng@sifive.com>
Date:   Fri Jul 2 10:19:30 2021 +0800

    docs: Add 'S' to Machine Constraints for RISC-V

    It was undocument before, but it might used in linux kernel for resolve
    code model issue, so LLVM community suggest we should document that,
    so that make it become supported/documented/non-internal machine
constraints.

    gcc/ChangeLog:

            PR target/101275
            * config/riscv/constraints.md ("S"): Update description and remove
            @internal.
            * doc/md.texi (Machine Constraints): Document the 'S' constraints
            for RISC-V.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug target/101275] [RISCV] Document the machine constraint 'S' and make it non-internal
  2021-06-30 23:01 [Bug c/101275] New: [RISCV] Document the machine constraint 'S' and make it non-internal i at maskray dot me
  2021-07-02  2:34 ` [Bug target/101275] " kito at gcc dot gnu.org
  2021-07-13  6:10 ` cvs-commit at gcc dot gnu.org
@ 2021-07-13  6:12 ` kito at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: kito at gcc dot gnu.org @ 2021-07-13  6:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101275

Kito Cheng <kito at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

--- Comment #3 from Kito Cheng <kito at gcc dot gnu.org> ---
Change committed to trunk

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-13  6:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-30 23:01 [Bug c/101275] New: [RISCV] Document the machine constraint 'S' and make it non-internal i at maskray dot me
2021-07-02  2:34 ` [Bug target/101275] " kito at gcc dot gnu.org
2021-07-13  6:10 ` cvs-commit at gcc dot gnu.org
2021-07-13  6:12 ` kito at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).