Path: blob/master/arch/arm64/kvm/hyp/include/nvhe/memory.h
26532 views
/* SPDX-License-Identifier: GPL-2.0-only */1#ifndef __KVM_HYP_MEMORY_H2#define __KVM_HYP_MEMORY_H34#include <asm/kvm_mmu.h>5#include <asm/page.h>67#include <linux/types.h>89/*10* Bits 0-1 are used to encode the memory ownership state of each page from the11* point of view of a pKVM "component" (host, hyp, guest, ... see enum12* pkvm_component_id):13* 00: The page is owned and exclusively accessible by the component;14* 01: The page is owned and accessible by the component, but is also15* accessible by another component;16* 10: The page is accessible but not owned by the component;17* The storage of this state depends on the component: either in the18* hyp_vmemmap for the host and hyp states or in PTE software bits for guests.19*/20enum pkvm_page_state {21PKVM_PAGE_OWNED = 0ULL,22PKVM_PAGE_SHARED_OWNED = BIT(0),23PKVM_PAGE_SHARED_BORROWED = BIT(1),2425/*26* 'Meta-states' are not stored directly in PTE SW bits for guest27* states, but inferred from the context (e.g. invalid PTE entries).28* For the host and hyp, meta-states are stored directly in the29* struct hyp_page.30*/31PKVM_NOPAGE = BIT(0) | BIT(1),32};33#define PKVM_PAGE_STATE_MASK (BIT(0) | BIT(1))3435#define PKVM_PAGE_STATE_PROT_MASK (KVM_PGTABLE_PROT_SW0 | KVM_PGTABLE_PROT_SW1)36static inline enum kvm_pgtable_prot pkvm_mkstate(enum kvm_pgtable_prot prot,37enum pkvm_page_state state)38{39prot &= ~PKVM_PAGE_STATE_PROT_MASK;40prot |= FIELD_PREP(PKVM_PAGE_STATE_PROT_MASK, state);41return prot;42}4344static inline enum pkvm_page_state pkvm_getstate(enum kvm_pgtable_prot prot)45{46return FIELD_GET(PKVM_PAGE_STATE_PROT_MASK, prot);47}4849struct hyp_page {50u16 refcount;51u8 order;5253/* Host state. Guarded by the host stage-2 lock. */54unsigned __host_state : 4;5556/*57* Complement of the hyp state. Guarded by the hyp stage-1 lock. We use58* the complement so that the initial 0 in __hyp_state_comp (due to the59* entire vmemmap starting off zeroed) encodes PKVM_NOPAGE.60*/61unsigned __hyp_state_comp : 4;6263u32 host_share_guest_count;64};6566extern u64 __hyp_vmemmap;67#define hyp_vmemmap ((struct hyp_page *)__hyp_vmemmap)6869#define __hyp_va(phys) ((void *)((phys_addr_t)(phys) - hyp_physvirt_offset))7071static inline void *hyp_phys_to_virt(phys_addr_t phys)72{73return __hyp_va(phys);74}7576static inline phys_addr_t hyp_virt_to_phys(void *addr)77{78return __hyp_pa(addr);79}8081#define hyp_phys_to_pfn(phys) ((phys) >> PAGE_SHIFT)82#define hyp_pfn_to_phys(pfn) ((phys_addr_t)((pfn) << PAGE_SHIFT))8384static inline struct hyp_page *hyp_phys_to_page(phys_addr_t phys)85{86BUILD_BUG_ON(sizeof(struct hyp_page) != sizeof(u64));87return &hyp_vmemmap[hyp_phys_to_pfn(phys)];88}8990#define hyp_virt_to_page(virt) hyp_phys_to_page(__hyp_pa(virt))91#define hyp_virt_to_pfn(virt) hyp_phys_to_pfn(__hyp_pa(virt))9293#define hyp_page_to_pfn(page) ((struct hyp_page *)(page) - hyp_vmemmap)94#define hyp_page_to_phys(page) hyp_pfn_to_phys((hyp_page_to_pfn(page)))95#define hyp_page_to_virt(page) __hyp_va(hyp_page_to_phys(page))96#define hyp_page_to_pool(page) (((struct hyp_page *)page)->pool)9798static inline enum pkvm_page_state get_host_state(struct hyp_page *p)99{100return p->__host_state;101}102103static inline void set_host_state(struct hyp_page *p, enum pkvm_page_state state)104{105p->__host_state = state;106}107108static inline enum pkvm_page_state get_hyp_state(struct hyp_page *p)109{110return p->__hyp_state_comp ^ PKVM_PAGE_STATE_MASK;111}112113static inline void set_hyp_state(struct hyp_page *p, enum pkvm_page_state state)114{115p->__hyp_state_comp = state ^ PKVM_PAGE_STATE_MASK;116}117118/*119* Refcounting for 'struct hyp_page'.120* hyp_pool::lock must be held if atomic access to the refcount is required.121*/122static inline int hyp_page_count(void *addr)123{124struct hyp_page *p = hyp_virt_to_page(addr);125126return p->refcount;127}128129static inline void hyp_page_ref_inc(struct hyp_page *p)130{131BUG_ON(p->refcount == USHRT_MAX);132p->refcount++;133}134135static inline void hyp_page_ref_dec(struct hyp_page *p)136{137BUG_ON(!p->refcount);138p->refcount--;139}140141static inline int hyp_page_ref_dec_and_test(struct hyp_page *p)142{143hyp_page_ref_dec(p);144return (p->refcount == 0);145}146147static inline void hyp_set_page_refcounted(struct hyp_page *p)148{149BUG_ON(p->refcount);150p->refcount = 1;151}152#endif /* __KVM_HYP_MEMORY_H */153154155