From: Anup Patel <anup@brainfault.org>
To: Mike Rapoport <rppt@linux.ibm.com>
Cc: Palmer Dabbelt <palmer@sifive.com>,
Anup Patel <Anup.Patel@wdc.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Christoph Hellwig <hch@infradead.org>,
Atish Patra <Atish.Patra@wdc.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Paul Walmsley <paul.walmsley@sifive.com>,
"linux-riscv@lists.infradead.org"
<linux-riscv@lists.infradead.org>
Subject: Re: [PATCH v2 2/5] RISC-V: Make setup_vm() independent of GCC code model
Date: Mon, 25 Mar 2019 09:49:55 +0530 [thread overview]
Message-ID: <CAAhSdy2pgoEknshuJT3z-QdPMMtw5fwWM2tzRUaGvZ1Hjn-SGQ@mail.gmail.com> (raw)
In-Reply-To: <20190323154459.GC25149@rapoport-lnx>
On Sat, Mar 23, 2019 at 9:15 PM Mike Rapoport <rppt@linux.ibm.com> wrote:
>
> On Thu, Mar 21, 2019 at 09:47:47AM +0000, Anup Patel wrote:
> > The setup_vm() must access kernel symbols in a position independent way
> > because it will be called from head.S with MMU off.
> >
> > If we compile kernel with cmodel=medany then PC-relative addressing will
> > be used in setup_vm() to access kernel symbols so it works perfectly fine.
> >
> > Although, if we compile kernel with cmodel=medlow then either absolute
> > addressing or PC-relative addressing (based on whichever requires fewer
> > instructions) is used to access kernel symbols in setup_vm(). This can
> > break setup_vm() whenever any absolute addressing is used to access
> > kernel symbols.
> >
> > With the movement of setup_vm() from kernel/setup.c to mm/init.c, the
> > setup_vm() is now broken for cmodel=medlow but it works perfectly fine
> > for cmodel=medany.
> >
> > This patch fixes setup_vm() and makes it independent of GCC code model
> > by accessing kernel symbols relative to kernel load address instead of
> > assuming PC-relative addressing.
> >
> > Fixes: 6f1e9e946f0b ("RISC-V: Move setup_vm() to mm/init.c")
> > Signed-off-by: Anup Patel <anup.patel@wdc.com>
> > ---
> > arch/riscv/kernel/head.S | 1 +
> > arch/riscv/mm/init.c | 73 ++++++++++++++++++++++++++--------------
> > 2 files changed, 49 insertions(+), 25 deletions(-)
> >
> > diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
> > index fe884cd69abd..7966262b4f9d 100644
> > --- a/arch/riscv/kernel/head.S
> > +++ b/arch/riscv/kernel/head.S
> > @@ -62,6 +62,7 @@ clear_bss_done:
> >
> > /* Initialize page tables and relocate to virtual addresses */
> > la sp, init_thread_union + THREAD_SIZE
> > + la a0, _start
> > call setup_vm
> > call relocate
> >
> > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
> > index b379a75ac6a6..e38f8195e45b 100644
> > --- a/arch/riscv/mm/init.c
> > +++ b/arch/riscv/mm/init.c
> > @@ -172,55 +172,78 @@ void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
> > }
> > }
> >
> > -asmlinkage void __init setup_vm(void)
> > +static inline void *__load_addr(void *ptr, uintptr_t load_pa)
> > {
> > extern char _start;
> > + uintptr_t va = (uintptr_t)ptr;
> > + uintptr_t sz = (uintptr_t)(&_end) - (uintptr_t)(&_start);
> > +
> > + if (va >= PAGE_OFFSET && va <= (PAGE_OFFSET + sz))
> > + return (void *)(load_pa + (va - PAGE_OFFSET));
> > + return (void *)va;
> > +}
> > +
> > +#define __load_va(ptr, load_pa) __load_addr(ptr, load_pa)
> > +#define __load_pa(ptr, load_pa) ((uintptr_t)__load_addr(ptr, load_pa))
> > +
> > +asmlinkage void __init setup_vm(uintptr_t load_pa)
> > +{
> > uintptr_t i;
> > - uintptr_t pa = (uintptr_t) &_start;
> > +#ifndef __PAGETABLE_PMD_FOLDED
> > + pmd_t *pmdp;
> > +#endif
> > + pgd_t *pgdp;
> > + phys_addr_t map_pa;
> > + pgprot_t tableprot = __pgprot(_PAGE_TABLE);
> > pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
> >
> > - va_pa_offset = PAGE_OFFSET - pa;
> > - pfn_base = PFN_DOWN(pa);
> > + va_pa_offset = PAGE_OFFSET - load_pa;
> > + pfn_base = PFN_DOWN(load_pa);
> >
> > /* Sanity check alignment and size */
> > BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
> > - BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
> > + BUG_ON((load_pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
> >
> > #ifndef __PAGETABLE_PMD_FOLDED
> > - trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
> > - pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
> > - __pgprot(_PAGE_TABLE));
> > - trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
> > + pgdp = __load_va(trampoline_pg_dir, load_pa);
> > + map_pa = __load_pa(trampoline_pmd, load_pa);
> > + pgdp[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
>
> Can we use pgd_index(PAGE_OFFSET) here as index to PGD?
>
> > + pfn_pgd(PFN_DOWN(map_pa), tableprot);
>
> It seems that __load_pa result is always used with PFN_DOWN(), it's worth
> adding __load_pfn(). Then the last two statements become
>
> map_pfn = __load_pfn(trampoline_pmd, load_pa);
> pgdp[pgd_index(PAGE_OFFSET)] = pfn_pgd(map_pfn, tableprot);
>
> This applies to most of the mappings below as well.
Thanks for the comments.
I am going to drop this patch because we have other patch which uses
"CFLAGS_init.o := -cmodel=medany" in mm/Makefile
Regards,
Anup
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2019-03-25 4:20 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-21 9:47 [PATCH v2 0/5] Boot RISC-V kernel from any 4KB aligned address Anup Patel
2019-03-21 9:47 ` [PATCH v2 1/5] RISC-V: Add separate defconfig for 32bit systems Anup Patel
2019-03-21 9:47 ` [PATCH v2 2/5] RISC-V: Make setup_vm() independent of GCC code model Anup Patel
2019-03-23 15:45 ` Mike Rapoport
2019-03-25 4:19 ` Anup Patel [this message]
2019-03-21 9:47 ` [PATCH v2 3/5] RISC-V: Allow booting kernel from any 4KB aligned address Anup Patel
2019-03-23 15:40 ` Mike Rapoport
2019-03-23 17:24 ` Christoph Hellwig
2019-03-24 4:16 ` Anup Patel
2019-03-24 3:32 ` Anup Patel
2019-03-21 9:47 ` [PATCH v2 4/5] RISC-V: Remove redundant trampoline page table Anup Patel
2019-03-22 13:33 ` Christoph Hellwig
2019-03-25 4:17 ` Anup Patel
2019-03-21 9:47 ` [PATCH v2 5/5] RISC-V: Fix memory reservation in setup_bootmem() Anup Patel
2019-03-22 13:31 ` Christoph Hellwig
2019-03-23 15:44 ` Mike Rapoport
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAAhSdy2pgoEknshuJT3z-QdPMMtw5fwWM2tzRUaGvZ1Hjn-SGQ@mail.gmail.com \
--to=anup@brainfault.org \
--cc=Anup.Patel@wdc.com \
--cc=Atish.Patra@wdc.com \
--cc=aou@eecs.berkeley.edu \
--cc=hch@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@sifive.com \
--cc=paul.walmsley@sifive.com \
--cc=rppt@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 NNTP newsgroup(s).