2019-05-28 23:57:21 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-04-27 23:47:12 +07:00
|
|
|
/*:
|
|
|
|
* Hibernate support specific for ARM64
|
|
|
|
*
|
|
|
|
* Derived from work on ARM hibernation support by:
|
|
|
|
*
|
|
|
|
* Ubuntu project, hibernation support for mach-dove
|
|
|
|
* Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu)
|
|
|
|
* Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.)
|
|
|
|
* https://lkml.org/lkml/2010/6/18/4
|
|
|
|
* https://lists.linux-foundation.org/pipermail/linux-pm/2010-June/027422.html
|
|
|
|
* https://patchwork.kernel.org/patch/96442/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
|
|
|
|
*/
|
|
|
|
#define pr_fmt(x) "hibernate: " x
|
2016-08-17 19:50:26 +07:00
|
|
|
#include <linux/cpu.h>
|
2016-04-27 23:47:12 +07:00
|
|
|
#include <linux/kvm_host.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/suspend.h>
|
|
|
|
#include <linux/utsname.h>
|
|
|
|
#include <linux/version.h>
|
|
|
|
|
|
|
|
#include <asm/barrier.h>
|
|
|
|
#include <asm/cacheflush.h>
|
2016-08-17 19:50:26 +07:00
|
|
|
#include <asm/cputype.h>
|
2017-11-02 19:12:34 +07:00
|
|
|
#include <asm/daifflags.h>
|
2016-04-27 23:47:12 +07:00
|
|
|
#include <asm/irqflags.h>
|
2017-04-03 09:24:35 +07:00
|
|
|
#include <asm/kexec.h>
|
2016-04-27 23:47:12 +07:00
|
|
|
#include <asm/memory.h>
|
|
|
|
#include <asm/mmu_context.h>
|
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/pgtable-hwdef.h>
|
|
|
|
#include <asm/sections.h>
|
2016-06-22 16:06:13 +07:00
|
|
|
#include <asm/smp.h>
|
2016-08-17 19:50:26 +07:00
|
|
|
#include <asm/smp_plat.h>
|
2016-04-27 23:47:12 +07:00
|
|
|
#include <asm/suspend.h>
|
arm64: hibernate: avoid potential TLB conflict
In create_safe_exec_page we install a set of global mappings in TTBR0,
then subsequently invalidate TLBs. While TTBR0 points at the zero page,
and the TLBs should be free of stale global entries, we may have stale
ASID-tagged entries (e.g. from the EFI runtime services mappings) for
the same VAs. Per the ARM ARM these ASID-tagged entries may conflict
with newly-allocated global entries, and we must follow a
Break-Before-Make approach to avoid issues resulting from this.
This patch reworks create_safe_exec_page to invalidate TLBs while the
zero page is still in place, ensuring that there are no potential
conflicts when the new TTBR0 value is installed. As a single CPU is
online while this code executes, we do not need to perform broadcast TLB
maintenance, and can call local_flush_tlb_all(), which also subsumes
some barriers. The remaining assembly is converted to use write_sysreg()
and isb().
Other than this, we safely manipulate TTBRs in the hibernate dance. The
code we install as part of the new TTBR0 mapping (the hibernated
kernel's swsusp_arch_suspend_exit) installs a zero page into TTBR1,
invalidates TLBs, then installs its preferred value. Upon being restored
to the middle of swsusp_arch_suspend, the new image will call
__cpu_suspend_exit, which will call cpu_uninstall_idmap, installing the
zero page in TTBR0 and invalidating all TLB entries.
Fixes: 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: James Morse <james.morse@arm.com>
Tested-by: James Morse <james.morse@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: <stable@vger.kernel.org> # 4.7+
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-08-11 20:11:05 +07:00
|
|
|
#include <asm/sysreg.h>
|
2016-04-27 23:47:12 +07:00
|
|
|
#include <asm/virt.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hibernate core relies on this value being 0 on resume, and marks it
|
|
|
|
* __nosavedata assuming it will keep the resume kernel's '0' value. This
|
|
|
|
* doesn't happen with either KASLR.
|
|
|
|
*
|
|
|
|
* defined as "__visible int in_suspend __nosavedata" in
|
|
|
|
* kernel/power/hibernate.c
|
|
|
|
*/
|
|
|
|
extern int in_suspend;
|
|
|
|
|
|
|
|
/* Do we need to reset el2? */
|
|
|
|
#define el2_reset_needed() (is_hyp_mode_available() && !is_kernel_in_hyp_mode())
|
|
|
|
|
|
|
|
/* temporary el2 vectors in the __hibernate_exit_text section. */
|
|
|
|
extern char hibernate_el2_vectors[];
|
|
|
|
|
|
|
|
/* hyp-stub vectors, used to restore el2 during resume from hibernate. */
|
|
|
|
extern char __hyp_stub_vectors[];
|
|
|
|
|
2016-08-17 19:50:26 +07:00
|
|
|
/*
|
|
|
|
* The logical cpu number we should resume on, initialised to a non-cpu
|
|
|
|
* number.
|
|
|
|
*/
|
|
|
|
static int sleep_cpu = -EINVAL;
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
/*
|
|
|
|
* Values that may not change over hibernate/resume. We put the build number
|
|
|
|
* and date in here so that we guarantee not to resume with a different
|
|
|
|
* kernel.
|
|
|
|
*/
|
|
|
|
struct arch_hibernate_hdr_invariants {
|
|
|
|
char uts_version[__NEW_UTS_LEN + 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* These values need to be know across a hibernate/restore. */
|
|
|
|
static struct arch_hibernate_hdr {
|
|
|
|
struct arch_hibernate_hdr_invariants invariants;
|
|
|
|
|
|
|
|
/* These are needed to find the relocated kernel if built with kaslr */
|
|
|
|
phys_addr_t ttbr1_el1;
|
|
|
|
void (*reenter_kernel)(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to know where the __hyp_stub_vectors are after restore to
|
|
|
|
* re-configure el2.
|
|
|
|
*/
|
|
|
|
phys_addr_t __hyp_stub_vectors;
|
2016-08-17 19:50:26 +07:00
|
|
|
|
|
|
|
u64 sleep_cpu_mpidr;
|
2016-04-27 23:47:12 +07:00
|
|
|
} resume_hdr;
|
|
|
|
|
|
|
|
static inline void arch_hdr_invariants(struct arch_hibernate_hdr_invariants *i)
|
|
|
|
{
|
|
|
|
memset(i, 0, sizeof(*i));
|
|
|
|
memcpy(i->uts_version, init_utsname()->version, sizeof(i->uts_version));
|
|
|
|
}
|
|
|
|
|
|
|
|
int pfn_is_nosave(unsigned long pfn)
|
|
|
|
{
|
2017-01-11 04:35:49 +07:00
|
|
|
unsigned long nosave_begin_pfn = sym_to_pfn(&__nosave_begin);
|
|
|
|
unsigned long nosave_end_pfn = sym_to_pfn(&__nosave_end - 1);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2017-04-03 09:24:35 +07:00
|
|
|
return ((pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn)) ||
|
|
|
|
crash_is_nosave(pfn);
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
void notrace save_processor_state(void)
|
|
|
|
{
|
|
|
|
WARN_ON(num_online_cpus() != 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void notrace restore_processor_state(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int arch_hibernation_header_save(void *addr, unsigned int max_size)
|
|
|
|
{
|
|
|
|
struct arch_hibernate_hdr *hdr = addr;
|
|
|
|
|
|
|
|
if (max_size < sizeof(*hdr))
|
|
|
|
return -EOVERFLOW;
|
|
|
|
|
|
|
|
arch_hdr_invariants(&hdr->invariants);
|
2017-01-11 04:35:49 +07:00
|
|
|
hdr->ttbr1_el1 = __pa_symbol(swapper_pg_dir);
|
2016-04-27 23:47:12 +07:00
|
|
|
hdr->reenter_kernel = _cpu_resume;
|
|
|
|
|
|
|
|
/* We can't use __hyp_get_vectors() because kvm may still be loaded */
|
|
|
|
if (el2_reset_needed())
|
2017-01-11 04:35:49 +07:00
|
|
|
hdr->__hyp_stub_vectors = __pa_symbol(__hyp_stub_vectors);
|
2016-04-27 23:47:12 +07:00
|
|
|
else
|
|
|
|
hdr->__hyp_stub_vectors = 0;
|
|
|
|
|
2016-08-17 19:50:26 +07:00
|
|
|
/* Save the mpidr of the cpu we called cpu_suspend() on... */
|
|
|
|
if (sleep_cpu < 0) {
|
2016-09-17 21:44:17 +07:00
|
|
|
pr_err("Failing to hibernate on an unknown CPU.\n");
|
2016-08-17 19:50:26 +07:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
hdr->sleep_cpu_mpidr = cpu_logical_map(sleep_cpu);
|
|
|
|
pr_info("Hibernating on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
|
|
|
|
hdr->sleep_cpu_mpidr);
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(arch_hibernation_header_save);
|
|
|
|
|
|
|
|
int arch_hibernation_header_restore(void *addr)
|
|
|
|
{
|
2016-08-17 19:50:26 +07:00
|
|
|
int ret;
|
2016-04-27 23:47:12 +07:00
|
|
|
struct arch_hibernate_hdr_invariants invariants;
|
|
|
|
struct arch_hibernate_hdr *hdr = addr;
|
|
|
|
|
|
|
|
arch_hdr_invariants(&invariants);
|
|
|
|
if (memcmp(&hdr->invariants, &invariants, sizeof(invariants))) {
|
|
|
|
pr_crit("Hibernate image not generated by this kernel!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-08-17 19:50:26 +07:00
|
|
|
sleep_cpu = get_logical_index(hdr->sleep_cpu_mpidr);
|
|
|
|
pr_info("Hibernated on CPU %d [mpidr:0x%llx]\n", sleep_cpu,
|
|
|
|
hdr->sleep_cpu_mpidr);
|
|
|
|
if (sleep_cpu < 0) {
|
|
|
|
pr_crit("Hibernated on a CPU not known to this kernel!\n");
|
|
|
|
sleep_cpu = -EINVAL;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (!cpu_online(sleep_cpu)) {
|
|
|
|
pr_info("Hibernated on a CPU that is offline! Bringing CPU up.\n");
|
|
|
|
ret = cpu_up(sleep_cpu);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("Failed to bring hibernate-CPU up!\n");
|
|
|
|
sleep_cpu = -EINVAL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
resume_hdr = *hdr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(arch_hibernation_header_restore);
|
|
|
|
|
2019-12-04 22:59:23 +07:00
|
|
|
static int trans_pgd_map_page(pgd_t *trans_pgd, void *page,
|
|
|
|
unsigned long dst_addr,
|
|
|
|
pgprot_t pgprot)
|
2016-04-27 23:47:12 +07:00
|
|
|
{
|
2018-02-15 18:14:56 +07:00
|
|
|
pgd_t *pgdp;
|
|
|
|
pud_t *pudp;
|
|
|
|
pmd_t *pmdp;
|
|
|
|
pte_t *ptep;
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2019-10-14 21:48:24 +07:00
|
|
|
pgdp = pgd_offset_raw(trans_pgd, dst_addr);
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pgd_none(READ_ONCE(*pgdp))) {
|
2019-12-04 22:59:19 +07:00
|
|
|
pudp = (void *)get_safe_page(GFP_ATOMIC);
|
2019-12-04 22:59:20 +07:00
|
|
|
if (!pudp)
|
|
|
|
return -ENOMEM;
|
2018-02-15 18:14:56 +07:00
|
|
|
pgd_populate(&init_mm, pgdp, pudp);
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
pudp = pud_offset(pgdp, dst_addr);
|
|
|
|
if (pud_none(READ_ONCE(*pudp))) {
|
2019-12-04 22:59:19 +07:00
|
|
|
pmdp = (void *)get_safe_page(GFP_ATOMIC);
|
2019-12-04 22:59:20 +07:00
|
|
|
if (!pmdp)
|
|
|
|
return -ENOMEM;
|
2018-02-15 18:14:56 +07:00
|
|
|
pud_populate(&init_mm, pudp, pmdp);
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
pmdp = pmd_offset(pudp, dst_addr);
|
|
|
|
if (pmd_none(READ_ONCE(*pmdp))) {
|
2019-12-04 22:59:19 +07:00
|
|
|
ptep = (void *)get_safe_page(GFP_ATOMIC);
|
2019-12-04 22:59:20 +07:00
|
|
|
if (!ptep)
|
|
|
|
return -ENOMEM;
|
2018-02-15 18:14:56 +07:00
|
|
|
pmd_populate_kernel(&init_mm, pmdp, ptep);
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
ptep = pte_offset_kernel(pmdp, dst_addr);
|
2019-12-04 22:59:21 +07:00
|
|
|
set_pte(ptep, pfn_pte(virt_to_pfn(page), PAGE_KERNEL_EXEC));
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2019-12-04 22:59:23 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies length bytes, starting at src_start into an new page,
|
|
|
|
* perform cache maintenance, then maps it at the specified address low
|
|
|
|
* address as executable.
|
|
|
|
*
|
|
|
|
* This is used by hibernate to copy the code it needs to execute when
|
|
|
|
* overwriting the kernel text. This function generates a new set of page
|
|
|
|
* tables, which it loads into ttbr0.
|
|
|
|
*
|
|
|
|
* Length is provided as we probably only want 4K of data, even on a 64K
|
|
|
|
* page system.
|
|
|
|
*/
|
|
|
|
static int create_safe_exec_page(void *src_start, size_t length,
|
|
|
|
unsigned long dst_addr,
|
|
|
|
phys_addr_t *phys_dst_addr)
|
|
|
|
{
|
|
|
|
void *page = (void *)get_safe_page(GFP_ATOMIC);
|
|
|
|
pgd_t *trans_pgd;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!page)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(page, src_start, length);
|
|
|
|
__flush_icache_range((unsigned long)page, (unsigned long)page + length);
|
|
|
|
|
|
|
|
trans_pgd = (void *)get_safe_page(GFP_ATOMIC);
|
|
|
|
if (!trans_pgd)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
rc = trans_pgd_map_page(trans_pgd, page, dst_addr,
|
|
|
|
PAGE_KERNEL_EXEC);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
arm64: hibernate: avoid potential TLB conflict
In create_safe_exec_page we install a set of global mappings in TTBR0,
then subsequently invalidate TLBs. While TTBR0 points at the zero page,
and the TLBs should be free of stale global entries, we may have stale
ASID-tagged entries (e.g. from the EFI runtime services mappings) for
the same VAs. Per the ARM ARM these ASID-tagged entries may conflict
with newly-allocated global entries, and we must follow a
Break-Before-Make approach to avoid issues resulting from this.
This patch reworks create_safe_exec_page to invalidate TLBs while the
zero page is still in place, ensuring that there are no potential
conflicts when the new TTBR0 value is installed. As a single CPU is
online while this code executes, we do not need to perform broadcast TLB
maintenance, and can call local_flush_tlb_all(), which also subsumes
some barriers. The remaining assembly is converted to use write_sysreg()
and isb().
Other than this, we safely manipulate TTBRs in the hibernate dance. The
code we install as part of the new TTBR0 mapping (the hibernated
kernel's swsusp_arch_suspend_exit) installs a zero page into TTBR1,
invalidates TLBs, then installs its preferred value. Upon being restored
to the middle of swsusp_arch_suspend, the new image will call
__cpu_suspend_exit, which will call cpu_uninstall_idmap, installing the
zero page in TTBR0 and invalidating all TLB entries.
Fixes: 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: James Morse <james.morse@arm.com>
Tested-by: James Morse <james.morse@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: <stable@vger.kernel.org> # 4.7+
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-08-11 20:11:05 +07:00
|
|
|
/*
|
|
|
|
* Load our new page tables. A strict BBM approach requires that we
|
|
|
|
* ensure that TLBs are free of any entries that may overlap with the
|
|
|
|
* global mappings we are about to install.
|
|
|
|
*
|
|
|
|
* For a real hibernate/resume cycle TTBR0 currently points to a zero
|
|
|
|
* page, but TLBs may contain stale ASID-tagged entries (e.g. for EFI
|
|
|
|
* runtime services), while for a userspace-driven test_resume cycle it
|
|
|
|
* points to userspace page tables (and we must point it at a zero page
|
|
|
|
* ourselves). Elsewhere we only (un)install the idmap with preemption
|
|
|
|
* disabled, so T0SZ should be as required regardless.
|
|
|
|
*/
|
|
|
|
cpu_set_reserved_ttbr0();
|
|
|
|
local_flush_tlb_all();
|
2019-12-04 22:59:18 +07:00
|
|
|
write_sysreg(phys_to_ttbr(virt_to_phys(trans_pgd)), ttbr0_el1);
|
arm64: hibernate: avoid potential TLB conflict
In create_safe_exec_page we install a set of global mappings in TTBR0,
then subsequently invalidate TLBs. While TTBR0 points at the zero page,
and the TLBs should be free of stale global entries, we may have stale
ASID-tagged entries (e.g. from the EFI runtime services mappings) for
the same VAs. Per the ARM ARM these ASID-tagged entries may conflict
with newly-allocated global entries, and we must follow a
Break-Before-Make approach to avoid issues resulting from this.
This patch reworks create_safe_exec_page to invalidate TLBs while the
zero page is still in place, ensuring that there are no potential
conflicts when the new TTBR0 value is installed. As a single CPU is
online while this code executes, we do not need to perform broadcast TLB
maintenance, and can call local_flush_tlb_all(), which also subsumes
some barriers. The remaining assembly is converted to use write_sysreg()
and isb().
Other than this, we safely manipulate TTBRs in the hibernate dance. The
code we install as part of the new TTBR0 mapping (the hibernated
kernel's swsusp_arch_suspend_exit) installs a zero page into TTBR1,
invalidates TLBs, then installs its preferred value. Upon being restored
to the middle of swsusp_arch_suspend, the new image will call
__cpu_suspend_exit, which will call cpu_uninstall_idmap, installing the
zero page in TTBR0 and invalidating all TLB entries.
Fixes: 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: James Morse <james.morse@arm.com>
Tested-by: James Morse <james.morse@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: <stable@vger.kernel.org> # 4.7+
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-08-11 20:11:05 +07:00
|
|
|
isb();
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2019-12-04 22:59:21 +07:00
|
|
|
*phys_dst_addr = virt_to_phys(page);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2019-12-04 22:59:20 +07:00
|
|
|
return 0;
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
|
|
|
|
2016-08-25 00:27:30 +07:00
|
|
|
#define dcache_clean_range(start, end) __flush_dcache_area(start, (end - start))
|
2016-04-27 23:47:12 +07:00
|
|
|
|
|
|
|
int swsusp_arch_suspend(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
struct sleep_stack_data state;
|
|
|
|
|
2016-06-22 16:06:13 +07:00
|
|
|
if (cpus_are_stuck_in_kernel()) {
|
|
|
|
pr_err("Can't hibernate: no mechanism to offline secondary CPUs.\n");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:12:34 +07:00
|
|
|
flags = local_daif_save();
|
2016-04-27 23:47:12 +07:00
|
|
|
|
|
|
|
if (__cpu_suspend_enter(&state)) {
|
2017-04-03 09:24:35 +07:00
|
|
|
/* make the crash dump kernel image visible/saveable */
|
|
|
|
crash_prepare_suspend();
|
|
|
|
|
2016-08-17 19:50:26 +07:00
|
|
|
sleep_cpu = smp_processor_id();
|
2016-04-27 23:47:12 +07:00
|
|
|
ret = swsusp_save();
|
|
|
|
} else {
|
2016-08-25 00:27:30 +07:00
|
|
|
/* Clean kernel core startup/idle code to PoC*/
|
|
|
|
dcache_clean_range(__mmuoff_data_start, __mmuoff_data_end);
|
|
|
|
dcache_clean_range(__idmap_text_start, __idmap_text_end);
|
|
|
|
|
|
|
|
/* Clean kvm setup code to PoC? */
|
2019-01-24 23:32:57 +07:00
|
|
|
if (el2_reset_needed()) {
|
2016-08-25 00:27:30 +07:00
|
|
|
dcache_clean_range(__hyp_idmap_text_start, __hyp_idmap_text_end);
|
2019-01-24 23:32:57 +07:00
|
|
|
dcache_clean_range(__hyp_text_start, __hyp_text_end);
|
|
|
|
}
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2017-04-03 09:24:35 +07:00
|
|
|
/* make the crash dump kernel image protected again */
|
|
|
|
crash_post_resume();
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
/*
|
|
|
|
* Tell the hibernation core that we've just restored
|
|
|
|
* the memory
|
|
|
|
*/
|
|
|
|
in_suspend = 0;
|
|
|
|
|
2016-08-17 19:50:26 +07:00
|
|
|
sleep_cpu = -EINVAL;
|
2016-04-27 23:47:12 +07:00
|
|
|
__cpu_suspend_exit();
|
2018-05-29 19:11:12 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Just in case the boot kernel did turn the SSBD
|
|
|
|
* mitigation off behind our back, let's set the state
|
|
|
|
* to what we expect it to be.
|
|
|
|
*/
|
|
|
|
switch (arm64_get_ssbd_state()) {
|
|
|
|
case ARM64_SSBD_FORCE_ENABLE:
|
|
|
|
case ARM64_SSBD_KERNEL:
|
|
|
|
arm64_set_ssbd_mitigation(true);
|
|
|
|
}
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
|
|
|
|
2017-11-02 19:12:34 +07:00
|
|
|
local_daif_restore(flags);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr)
|
2016-08-25 00:27:30 +07:00
|
|
|
{
|
2018-02-15 18:14:56 +07:00
|
|
|
pte_t pte = READ_ONCE(*src_ptep);
|
2016-08-25 00:27:30 +07:00
|
|
|
|
|
|
|
if (pte_valid(pte)) {
|
|
|
|
/*
|
|
|
|
* Resume will overwrite areas that may be marked
|
|
|
|
* read only (code, rodata). Clear the RDONLY bit from
|
|
|
|
* the temporary mappings we use during restore.
|
|
|
|
*/
|
2018-02-15 18:14:56 +07:00
|
|
|
set_pte(dst_ptep, pte_mkwrite(pte));
|
2016-08-25 00:27:30 +07:00
|
|
|
} else if (debug_pagealloc_enabled() && !pte_none(pte)) {
|
|
|
|
/*
|
|
|
|
* debug_pagealloc will removed the PTE_VALID bit if
|
|
|
|
* the page isn't in use by the resume kernel. It may have
|
|
|
|
* been in use by the original kernel, in which case we need
|
|
|
|
* to put it back in our copy to do the restore.
|
|
|
|
*
|
|
|
|
* Before marking this entry valid, check the pfn should
|
|
|
|
* be mapped.
|
|
|
|
*/
|
|
|
|
BUG_ON(!pfn_valid(pte_pfn(pte)));
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
set_pte(dst_ptep, pte_mkpresent(pte_mkwrite(pte)));
|
2016-08-25 00:27:30 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
static int copy_pte(pmd_t *dst_pmdp, pmd_t *src_pmdp, unsigned long start,
|
2016-04-27 23:47:12 +07:00
|
|
|
unsigned long end)
|
|
|
|
{
|
2018-02-15 18:14:56 +07:00
|
|
|
pte_t *src_ptep;
|
|
|
|
pte_t *dst_ptep;
|
2016-04-27 23:47:12 +07:00
|
|
|
unsigned long addr = start;
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
dst_ptep = (pte_t *)get_safe_page(GFP_ATOMIC);
|
|
|
|
if (!dst_ptep)
|
2016-04-27 23:47:12 +07:00
|
|
|
return -ENOMEM;
|
2018-02-15 18:14:56 +07:00
|
|
|
pmd_populate_kernel(&init_mm, dst_pmdp, dst_ptep);
|
|
|
|
dst_ptep = pte_offset_kernel(dst_pmdp, start);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
src_ptep = pte_offset_kernel(src_pmdp, start);
|
2016-04-27 23:47:12 +07:00
|
|
|
do {
|
2018-02-15 18:14:56 +07:00
|
|
|
_copy_pte(dst_ptep, src_ptep, addr);
|
|
|
|
} while (dst_ptep++, src_ptep++, addr += PAGE_SIZE, addr != end);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
static int copy_pmd(pud_t *dst_pudp, pud_t *src_pudp, unsigned long start,
|
2016-04-27 23:47:12 +07:00
|
|
|
unsigned long end)
|
|
|
|
{
|
2018-02-15 18:14:56 +07:00
|
|
|
pmd_t *src_pmdp;
|
|
|
|
pmd_t *dst_pmdp;
|
2016-04-27 23:47:12 +07:00
|
|
|
unsigned long next;
|
|
|
|
unsigned long addr = start;
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pud_none(READ_ONCE(*dst_pudp))) {
|
|
|
|
dst_pmdp = (pmd_t *)get_safe_page(GFP_ATOMIC);
|
|
|
|
if (!dst_pmdp)
|
2016-04-27 23:47:12 +07:00
|
|
|
return -ENOMEM;
|
2018-02-15 18:14:56 +07:00
|
|
|
pud_populate(&init_mm, dst_pudp, dst_pmdp);
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
2018-02-15 18:14:56 +07:00
|
|
|
dst_pmdp = pmd_offset(dst_pudp, start);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
src_pmdp = pmd_offset(src_pudp, start);
|
2016-04-27 23:47:12 +07:00
|
|
|
do {
|
2018-02-15 18:14:56 +07:00
|
|
|
pmd_t pmd = READ_ONCE(*src_pmdp);
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
next = pmd_addr_end(addr, end);
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pmd_none(pmd))
|
2016-04-27 23:47:12 +07:00
|
|
|
continue;
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pmd_table(pmd)) {
|
|
|
|
if (copy_pte(dst_pmdp, src_pmdp, addr, next))
|
2016-04-27 23:47:12 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
2018-02-15 18:14:56 +07:00
|
|
|
set_pmd(dst_pmdp,
|
|
|
|
__pmd(pmd_val(pmd) & ~PMD_SECT_RDONLY));
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
2018-02-15 18:14:56 +07:00
|
|
|
} while (dst_pmdp++, src_pmdp++, addr = next, addr != end);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
static int copy_pud(pgd_t *dst_pgdp, pgd_t *src_pgdp, unsigned long start,
|
2016-04-27 23:47:12 +07:00
|
|
|
unsigned long end)
|
|
|
|
{
|
2018-02-15 18:14:56 +07:00
|
|
|
pud_t *dst_pudp;
|
|
|
|
pud_t *src_pudp;
|
2016-04-27 23:47:12 +07:00
|
|
|
unsigned long next;
|
|
|
|
unsigned long addr = start;
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pgd_none(READ_ONCE(*dst_pgdp))) {
|
|
|
|
dst_pudp = (pud_t *)get_safe_page(GFP_ATOMIC);
|
|
|
|
if (!dst_pudp)
|
2016-04-27 23:47:12 +07:00
|
|
|
return -ENOMEM;
|
2018-02-15 18:14:56 +07:00
|
|
|
pgd_populate(&init_mm, dst_pgdp, dst_pudp);
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
2018-02-15 18:14:56 +07:00
|
|
|
dst_pudp = pud_offset(dst_pgdp, start);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
src_pudp = pud_offset(src_pgdp, start);
|
2016-04-27 23:47:12 +07:00
|
|
|
do {
|
2018-02-15 18:14:56 +07:00
|
|
|
pud_t pud = READ_ONCE(*src_pudp);
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
next = pud_addr_end(addr, end);
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pud_none(pud))
|
2016-04-27 23:47:12 +07:00
|
|
|
continue;
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pud_table(pud)) {
|
|
|
|
if (copy_pmd(dst_pudp, src_pudp, addr, next))
|
2016-04-27 23:47:12 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
} else {
|
2018-02-15 18:14:56 +07:00
|
|
|
set_pud(dst_pudp,
|
2019-12-04 22:59:22 +07:00
|
|
|
__pud(pud_val(pud) & ~PUD_SECT_RDONLY));
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
2018-02-15 18:14:56 +07:00
|
|
|
} while (dst_pudp++, src_pudp++, addr = next, addr != end);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
static int copy_page_tables(pgd_t *dst_pgdp, unsigned long start,
|
2016-04-27 23:47:12 +07:00
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
unsigned long addr = start;
|
2018-02-15 18:14:56 +07:00
|
|
|
pgd_t *src_pgdp = pgd_offset_k(start);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
2018-02-15 18:14:56 +07:00
|
|
|
dst_pgdp = pgd_offset_raw(dst_pgdp, start);
|
2016-04-27 23:47:12 +07:00
|
|
|
do {
|
|
|
|
next = pgd_addr_end(addr, end);
|
2018-02-15 18:14:56 +07:00
|
|
|
if (pgd_none(READ_ONCE(*src_pgdp)))
|
2016-04-27 23:47:12 +07:00
|
|
|
continue;
|
2018-02-15 18:14:56 +07:00
|
|
|
if (copy_pud(dst_pgdp, src_pgdp, addr, next))
|
2016-04-27 23:47:12 +07:00
|
|
|
return -ENOMEM;
|
2018-02-15 18:14:56 +07:00
|
|
|
} while (dst_pgdp++, src_pgdp++, addr = next, addr != end);
|
2016-04-27 23:47:12 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-04 22:59:23 +07:00
|
|
|
static int trans_pgd_create_copy(pgd_t **dst_pgdp, unsigned long start,
|
|
|
|
unsigned long end)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
pgd_t *trans_pgd = (pgd_t *)get_safe_page(GFP_ATOMIC);
|
|
|
|
|
|
|
|
if (!trans_pgd) {
|
|
|
|
pr_err("Failed to allocate memory for temporary page tables.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = copy_page_tables(trans_pgd, start, end);
|
|
|
|
if (!rc)
|
|
|
|
*dst_pgdp = trans_pgd;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
/*
|
|
|
|
* Setup then Resume from the hibernate image using swsusp_arch_suspend_exit().
|
|
|
|
*
|
|
|
|
* Memory allocated by get_safe_page() will be dealt with by the hibernate code,
|
|
|
|
* we don't need to free it here.
|
|
|
|
*/
|
|
|
|
int swsusp_arch_resume(void)
|
|
|
|
{
|
2019-12-04 22:59:20 +07:00
|
|
|
int rc;
|
2016-04-27 23:47:12 +07:00
|
|
|
void *zero_page;
|
|
|
|
size_t exit_size;
|
|
|
|
pgd_t *tmp_pg_dir;
|
|
|
|
phys_addr_t phys_hibernate_exit;
|
|
|
|
void __noreturn (*hibernate_exit)(phys_addr_t, phys_addr_t, void *,
|
|
|
|
void *, phys_addr_t, phys_addr_t);
|
|
|
|
|
arm64: hibernate: handle allocation failures
In create_safe_exec_page(), we create a copy of the hibernate exit text,
along with some page tables to map this via TTBR0. We then install the
new tables in TTBR0.
In swsusp_arch_resume() we call create_safe_exec_page() before trying a
number of operations which may fail (e.g. copying the linear map page
tables). If these fail, we bail out of swsusp_arch_resume() and return
an error code, but leave TTBR0 as-is. Subsequently, the core hibernate
code will call free_basic_memory_bitmaps(), which will free all of the
memory allocations we made, including the page tables installed in
TTBR0.
Thus, we may have TTBR0 pointing at dangling freed memory for some
period of time. If the hibernate attempt was triggered by a user
requesting a hibernate test via the reboot syscall, we may return to
userspace with the clobbered TTBR0 value.
Avoid these issues by reorganising swsusp_arch_resume() such that we
have no failure paths after create_safe_exec_page(). We also add a check
that the zero page allocation succeeded, matching what we have for other
allocations.
Fixes: 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: James Morse <james.morse@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: <stable@vger.kernel.org> # 4.7+
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-08-11 20:11:06 +07:00
|
|
|
/*
|
|
|
|
* Restoring the memory image will overwrite the ttbr1 page tables.
|
|
|
|
* Create a second copy of just the linear map, and use this when
|
|
|
|
* restoring.
|
|
|
|
*/
|
2019-12-04 22:59:23 +07:00
|
|
|
rc = trans_pgd_create_copy(&tmp_pg_dir, PAGE_OFFSET, PAGE_END);
|
arm64: hibernate: handle allocation failures
In create_safe_exec_page(), we create a copy of the hibernate exit text,
along with some page tables to map this via TTBR0. We then install the
new tables in TTBR0.
In swsusp_arch_resume() we call create_safe_exec_page() before trying a
number of operations which may fail (e.g. copying the linear map page
tables). If these fail, we bail out of swsusp_arch_resume() and return
an error code, but leave TTBR0 as-is. Subsequently, the core hibernate
code will call free_basic_memory_bitmaps(), which will free all of the
memory allocations we made, including the page tables installed in
TTBR0.
Thus, we may have TTBR0 pointing at dangling freed memory for some
period of time. If the hibernate attempt was triggered by a user
requesting a hibernate test via the reboot syscall, we may return to
userspace with the clobbered TTBR0 value.
Avoid these issues by reorganising swsusp_arch_resume() such that we
have no failure paths after create_safe_exec_page(). We also add a check
that the zero page allocation succeeded, matching what we have for other
allocations.
Fixes: 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: James Morse <james.morse@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: <stable@vger.kernel.org> # 4.7+
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-08-11 20:11:06 +07:00
|
|
|
if (rc)
|
2019-12-04 22:59:20 +07:00
|
|
|
return rc;
|
arm64: hibernate: handle allocation failures
In create_safe_exec_page(), we create a copy of the hibernate exit text,
along with some page tables to map this via TTBR0. We then install the
new tables in TTBR0.
In swsusp_arch_resume() we call create_safe_exec_page() before trying a
number of operations which may fail (e.g. copying the linear map page
tables). If these fail, we bail out of swsusp_arch_resume() and return
an error code, but leave TTBR0 as-is. Subsequently, the core hibernate
code will call free_basic_memory_bitmaps(), which will free all of the
memory allocations we made, including the page tables installed in
TTBR0.
Thus, we may have TTBR0 pointing at dangling freed memory for some
period of time. If the hibernate attempt was triggered by a user
requesting a hibernate test via the reboot syscall, we may return to
userspace with the clobbered TTBR0 value.
Avoid these issues by reorganising swsusp_arch_resume() such that we
have no failure paths after create_safe_exec_page(). We also add a check
that the zero page allocation succeeded, matching what we have for other
allocations.
Fixes: 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: James Morse <james.morse@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: <stable@vger.kernel.org> # 4.7+
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-08-11 20:11:06 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need a zero page that is zero before & after resume in order to
|
|
|
|
* to break before make on the ttbr1 page tables.
|
|
|
|
*/
|
|
|
|
zero_page = (void *)get_safe_page(GFP_ATOMIC);
|
|
|
|
if (!zero_page) {
|
2017-01-09 21:13:36 +07:00
|
|
|
pr_err("Failed to allocate zero page.\n");
|
2019-12-04 22:59:20 +07:00
|
|
|
return -ENOMEM;
|
arm64: hibernate: handle allocation failures
In create_safe_exec_page(), we create a copy of the hibernate exit text,
along with some page tables to map this via TTBR0. We then install the
new tables in TTBR0.
In swsusp_arch_resume() we call create_safe_exec_page() before trying a
number of operations which may fail (e.g. copying the linear map page
tables). If these fail, we bail out of swsusp_arch_resume() and return
an error code, but leave TTBR0 as-is. Subsequently, the core hibernate
code will call free_basic_memory_bitmaps(), which will free all of the
memory allocations we made, including the page tables installed in
TTBR0.
Thus, we may have TTBR0 pointing at dangling freed memory for some
period of time. If the hibernate attempt was triggered by a user
requesting a hibernate test via the reboot syscall, we may return to
userspace with the clobbered TTBR0 value.
Avoid these issues by reorganising swsusp_arch_resume() such that we
have no failure paths after create_safe_exec_page(). We also add a check
that the zero page allocation succeeded, matching what we have for other
allocations.
Fixes: 82869ac57b5d ("arm64: kernel: Add support for hibernate/suspend-to-disk")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Acked-by: James Morse <james.morse@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: <stable@vger.kernel.org> # 4.7+
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
2016-08-11 20:11:06 +07:00
|
|
|
}
|
|
|
|
|
2016-04-27 23:47:12 +07:00
|
|
|
/*
|
|
|
|
* Locate the exit code in the bottom-but-one page, so that *NULL
|
|
|
|
* still has disastrous affects.
|
|
|
|
*/
|
|
|
|
hibernate_exit = (void *)PAGE_SIZE;
|
|
|
|
exit_size = __hibernate_exit_text_end - __hibernate_exit_text_start;
|
|
|
|
/*
|
|
|
|
* Copy swsusp_arch_suspend_exit() to a safe page. This will generate
|
|
|
|
* a new set of ttbr0 page tables and load them.
|
|
|
|
*/
|
|
|
|
rc = create_safe_exec_page(__hibernate_exit_text_start, exit_size,
|
|
|
|
(unsigned long)hibernate_exit,
|
2019-12-04 22:59:19 +07:00
|
|
|
&phys_hibernate_exit);
|
2016-04-27 23:47:12 +07:00
|
|
|
if (rc) {
|
2017-01-09 21:13:36 +07:00
|
|
|
pr_err("Failed to create safe executable page for hibernate_exit code.\n");
|
2019-12-04 22:59:20 +07:00
|
|
|
return rc;
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The hibernate exit text contains a set of el2 vectors, that will
|
|
|
|
* be executed at el2 with the mmu off in order to reload hyp-stub.
|
|
|
|
*/
|
|
|
|
__flush_dcache_area(hibernate_exit, exit_size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* KASLR will cause the el2 vectors to be in a different location in
|
|
|
|
* the resumed kernel. Load hibernate's temporary copy into el2.
|
|
|
|
*
|
|
|
|
* We can skip this step if we booted at EL1, or are running with VHE.
|
|
|
|
*/
|
|
|
|
if (el2_reset_needed()) {
|
|
|
|
phys_addr_t el2_vectors = phys_hibernate_exit; /* base */
|
|
|
|
el2_vectors += hibernate_el2_vectors -
|
|
|
|
__hibernate_exit_text_start; /* offset */
|
|
|
|
|
|
|
|
__hyp_set_vectors(el2_vectors);
|
|
|
|
}
|
|
|
|
|
|
|
|
hibernate_exit(virt_to_phys(tmp_pg_dir), resume_hdr.ttbr1_el1,
|
2017-01-11 04:35:49 +07:00
|
|
|
resume_hdr.reenter_kernel, restore_pblist,
|
2016-04-27 23:47:12 +07:00
|
|
|
resume_hdr.__hyp_stub_vectors, virt_to_phys(zero_page));
|
|
|
|
|
2019-12-04 22:59:20 +07:00
|
|
|
return 0;
|
2016-04-27 23:47:12 +07:00
|
|
|
}
|
2016-04-27 23:47:13 +07:00
|
|
|
|
2016-08-17 19:50:26 +07:00
|
|
|
int hibernate_resume_nonboot_cpu_disable(void)
|
|
|
|
{
|
|
|
|
if (sleep_cpu < 0) {
|
2016-09-17 21:44:17 +07:00
|
|
|
pr_err("Failing to resume from hibernate on an unknown CPU.\n");
|
2016-08-17 19:50:26 +07:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return freeze_secondary_cpus(sleep_cpu);
|
|
|
|
}
|