18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  KVM guest address space mapping code
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *    Copyright IBM Corp. 2007, 2016
68c2ecf20Sopenharmony_ci *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef _ASM_S390_GMAP_H
108c2ecf20Sopenharmony_ci#define _ASM_S390_GMAP_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/radix-tree.h>
138c2ecf20Sopenharmony_ci#include <linux/refcount.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* Generic bits for GMAP notification on DAT table entry changes. */
168c2ecf20Sopenharmony_ci#define GMAP_NOTIFY_SHADOW	0x2
178c2ecf20Sopenharmony_ci#define GMAP_NOTIFY_MPROT	0x1
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Status bits only for huge segment entries */
208c2ecf20Sopenharmony_ci#define _SEGMENT_ENTRY_GMAP_IN		0x8000	/* invalidation notify bit */
218c2ecf20Sopenharmony_ci#define _SEGMENT_ENTRY_GMAP_UC		0x4000	/* dirty (migration) */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/**
248c2ecf20Sopenharmony_ci * struct gmap_struct - guest address space
258c2ecf20Sopenharmony_ci * @list: list head for the mm->context gmap list
268c2ecf20Sopenharmony_ci * @crst_list: list of all crst tables used in the guest address space
278c2ecf20Sopenharmony_ci * @mm: pointer to the parent mm_struct
288c2ecf20Sopenharmony_ci * @guest_to_host: radix tree with guest to host address translation
298c2ecf20Sopenharmony_ci * @host_to_guest: radix tree with pointer to segment table entries
308c2ecf20Sopenharmony_ci * @guest_table_lock: spinlock to protect all entries in the guest page table
318c2ecf20Sopenharmony_ci * @ref_count: reference counter for the gmap structure
328c2ecf20Sopenharmony_ci * @table: pointer to the page directory
338c2ecf20Sopenharmony_ci * @asce: address space control element for gmap page table
348c2ecf20Sopenharmony_ci * @pfault_enabled: defines if pfaults are applicable for the guest
358c2ecf20Sopenharmony_ci * @guest_handle: protected virtual machine handle for the ultravisor
368c2ecf20Sopenharmony_ci * @host_to_rmap: radix tree with gmap_rmap lists
378c2ecf20Sopenharmony_ci * @children: list of shadow gmap structures
388c2ecf20Sopenharmony_ci * @pt_list: list of all page tables used in the shadow guest address space
398c2ecf20Sopenharmony_ci * @shadow_lock: spinlock to protect the shadow gmap list
408c2ecf20Sopenharmony_ci * @parent: pointer to the parent gmap for shadow guest address spaces
418c2ecf20Sopenharmony_ci * @orig_asce: ASCE for which the shadow page table has been created
428c2ecf20Sopenharmony_ci * @edat_level: edat level to be used for the shadow translation
438c2ecf20Sopenharmony_ci * @removed: flag to indicate if a shadow guest address space has been removed
448c2ecf20Sopenharmony_ci * @initialized: flag to indicate if a shadow guest address space can be used
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cistruct gmap {
478c2ecf20Sopenharmony_ci	struct list_head list;
488c2ecf20Sopenharmony_ci	struct list_head crst_list;
498c2ecf20Sopenharmony_ci	struct mm_struct *mm;
508c2ecf20Sopenharmony_ci	struct radix_tree_root guest_to_host;
518c2ecf20Sopenharmony_ci	struct radix_tree_root host_to_guest;
528c2ecf20Sopenharmony_ci	spinlock_t guest_table_lock;
538c2ecf20Sopenharmony_ci	refcount_t ref_count;
548c2ecf20Sopenharmony_ci	unsigned long *table;
558c2ecf20Sopenharmony_ci	unsigned long asce;
568c2ecf20Sopenharmony_ci	unsigned long asce_end;
578c2ecf20Sopenharmony_ci	void *private;
588c2ecf20Sopenharmony_ci	bool pfault_enabled;
598c2ecf20Sopenharmony_ci	/* only set for protected virtual machines */
608c2ecf20Sopenharmony_ci	unsigned long guest_handle;
618c2ecf20Sopenharmony_ci	/* Additional data for shadow guest address spaces */
628c2ecf20Sopenharmony_ci	struct radix_tree_root host_to_rmap;
638c2ecf20Sopenharmony_ci	struct list_head children;
648c2ecf20Sopenharmony_ci	struct list_head pt_list;
658c2ecf20Sopenharmony_ci	spinlock_t shadow_lock;
668c2ecf20Sopenharmony_ci	struct gmap *parent;
678c2ecf20Sopenharmony_ci	unsigned long orig_asce;
688c2ecf20Sopenharmony_ci	int edat_level;
698c2ecf20Sopenharmony_ci	bool removed;
708c2ecf20Sopenharmony_ci	bool initialized;
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/**
748c2ecf20Sopenharmony_ci * struct gmap_rmap - reverse mapping for shadow page table entries
758c2ecf20Sopenharmony_ci * @next: pointer to next rmap in the list
768c2ecf20Sopenharmony_ci * @raddr: virtual rmap address in the shadow guest address space
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_cistruct gmap_rmap {
798c2ecf20Sopenharmony_ci	struct gmap_rmap *next;
808c2ecf20Sopenharmony_ci	unsigned long raddr;
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define gmap_for_each_rmap(pos, head) \
848c2ecf20Sopenharmony_ci	for (pos = (head); pos; pos = pos->next)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define gmap_for_each_rmap_safe(pos, n, head) \
878c2ecf20Sopenharmony_ci	for (pos = (head); n = pos ? pos->next : NULL, pos; pos = n)
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/**
908c2ecf20Sopenharmony_ci * struct gmap_notifier - notify function block for page invalidation
918c2ecf20Sopenharmony_ci * @notifier_call: address of callback function
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_cistruct gmap_notifier {
948c2ecf20Sopenharmony_ci	struct list_head list;
958c2ecf20Sopenharmony_ci	struct rcu_head rcu;
968c2ecf20Sopenharmony_ci	void (*notifier_call)(struct gmap *gmap, unsigned long start,
978c2ecf20Sopenharmony_ci			      unsigned long end);
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic inline int gmap_is_shadow(struct gmap *gmap)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	return !!gmap->parent;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistruct gmap *gmap_create(struct mm_struct *mm, unsigned long limit);
1068c2ecf20Sopenharmony_civoid gmap_remove(struct gmap *gmap);
1078c2ecf20Sopenharmony_cistruct gmap *gmap_get(struct gmap *gmap);
1088c2ecf20Sopenharmony_civoid gmap_put(struct gmap *gmap);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_civoid gmap_enable(struct gmap *gmap);
1118c2ecf20Sopenharmony_civoid gmap_disable(struct gmap *gmap);
1128c2ecf20Sopenharmony_cistruct gmap *gmap_get_enabled(void);
1138c2ecf20Sopenharmony_ciint gmap_map_segment(struct gmap *gmap, unsigned long from,
1148c2ecf20Sopenharmony_ci		     unsigned long to, unsigned long len);
1158c2ecf20Sopenharmony_ciint gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len);
1168c2ecf20Sopenharmony_ciunsigned long __gmap_translate(struct gmap *, unsigned long gaddr);
1178c2ecf20Sopenharmony_ciunsigned long gmap_translate(struct gmap *, unsigned long gaddr);
1188c2ecf20Sopenharmony_ciint __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr);
1198c2ecf20Sopenharmony_ciint gmap_fault(struct gmap *, unsigned long gaddr, unsigned int fault_flags);
1208c2ecf20Sopenharmony_civoid gmap_discard(struct gmap *, unsigned long from, unsigned long to);
1218c2ecf20Sopenharmony_civoid __gmap_zap(struct gmap *, unsigned long gaddr);
1228c2ecf20Sopenharmony_civoid gmap_unlink(struct mm_struct *, unsigned long *table, unsigned long vmaddr);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciint gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistruct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1278c2ecf20Sopenharmony_ci			 int edat_level);
1288c2ecf20Sopenharmony_ciint gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level);
1298c2ecf20Sopenharmony_ciint gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1308c2ecf20Sopenharmony_ci		    int fake);
1318c2ecf20Sopenharmony_ciint gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1328c2ecf20Sopenharmony_ci		    int fake);
1338c2ecf20Sopenharmony_ciint gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1348c2ecf20Sopenharmony_ci		    int fake);
1358c2ecf20Sopenharmony_ciint gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
1368c2ecf20Sopenharmony_ci		    int fake);
1378c2ecf20Sopenharmony_ciint gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
1388c2ecf20Sopenharmony_ci			   unsigned long *pgt, int *dat_protection, int *fake);
1398c2ecf20Sopenharmony_ciint gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_civoid gmap_register_pte_notifier(struct gmap_notifier *);
1428c2ecf20Sopenharmony_civoid gmap_unregister_pte_notifier(struct gmap_notifier *);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciint gmap_mprotect_notify(struct gmap *, unsigned long start,
1458c2ecf20Sopenharmony_ci			 unsigned long len, int prot);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_civoid gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long dirty_bitmap[4],
1488c2ecf20Sopenharmony_ci			     unsigned long gaddr, unsigned long vmaddr);
1498c2ecf20Sopenharmony_ciint gmap_mark_unmergeable(void);
1508c2ecf20Sopenharmony_civoid s390_reset_acc(struct mm_struct *mm);
1518c2ecf20Sopenharmony_civoid s390_unlist_old_asce(struct gmap *gmap);
1528c2ecf20Sopenharmony_ciint s390_replace_asce(struct gmap *gmap);
1538c2ecf20Sopenharmony_ci#endif /* _ASM_S390_GMAP_H */
154