162306a36Sopenharmony_ci/* SPDX-License-Identifier: MIT */
262306a36Sopenharmony_ci/******************************************************************************
362306a36Sopenharmony_ci * memory.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Memory reservation and information.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (c) 2005, Keir Fraser <keir@xensource.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef __XEN_PUBLIC_MEMORY_H__
1162306a36Sopenharmony_ci#define __XEN_PUBLIC_MEMORY_H__
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/spinlock.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * Increase or decrease the specified domain's memory reservation. Returns a
1762306a36Sopenharmony_ci * -ve errcode on failure, or the # extents successfully allocated or freed.
1862306a36Sopenharmony_ci * arg == addr of struct xen_memory_reservation.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci#define XENMEM_increase_reservation 0
2162306a36Sopenharmony_ci#define XENMEM_decrease_reservation 1
2262306a36Sopenharmony_ci#define XENMEM_populate_physmap     6
2362306a36Sopenharmony_cistruct xen_memory_reservation {
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci    /*
2662306a36Sopenharmony_ci     * XENMEM_increase_reservation:
2762306a36Sopenharmony_ci     *   OUT: MFN (*not* GMFN) bases of extents that were allocated
2862306a36Sopenharmony_ci     * XENMEM_decrease_reservation:
2962306a36Sopenharmony_ci     *   IN:  GMFN bases of extents to free
3062306a36Sopenharmony_ci     * XENMEM_populate_physmap:
3162306a36Sopenharmony_ci     *   IN:  GPFN bases of extents to populate with memory
3262306a36Sopenharmony_ci     *   OUT: GMFN bases of extents that were allocated
3362306a36Sopenharmony_ci     *   (NB. This command also updates the mach_to_phys translation table)
3462306a36Sopenharmony_ci     */
3562306a36Sopenharmony_ci    GUEST_HANDLE(xen_pfn_t) extent_start;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci    /* Number of extents, and size/alignment of each (2^extent_order pages). */
3862306a36Sopenharmony_ci    xen_ulong_t  nr_extents;
3962306a36Sopenharmony_ci    unsigned int   extent_order;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci    /*
4262306a36Sopenharmony_ci     * Maximum # bits addressable by the user of the allocated region (e.g.,
4362306a36Sopenharmony_ci     * I/O devices often have a 32-bit limitation even in 64-bit systems). If
4462306a36Sopenharmony_ci     * zero then the user has no addressing restriction.
4562306a36Sopenharmony_ci     * This field is not used by XENMEM_decrease_reservation.
4662306a36Sopenharmony_ci     */
4762306a36Sopenharmony_ci    unsigned int   address_bits;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci    /*
5062306a36Sopenharmony_ci     * Domain whose reservation is being changed.
5162306a36Sopenharmony_ci     * Unprivileged domains can specify only DOMID_SELF.
5262306a36Sopenharmony_ci     */
5362306a36Sopenharmony_ci    domid_t        domid;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_memory_reservation);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/*
5962306a36Sopenharmony_ci * An atomic exchange of memory pages. If return code is zero then
6062306a36Sopenharmony_ci * @out.extent_list provides GMFNs of the newly-allocated memory.
6162306a36Sopenharmony_ci * Returns zero on complete success, otherwise a negative error code.
6262306a36Sopenharmony_ci * On complete success then always @nr_exchanged == @in.nr_extents.
6362306a36Sopenharmony_ci * On partial success @nr_exchanged indicates how much work was done.
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_ci#define XENMEM_exchange             11
6662306a36Sopenharmony_cistruct xen_memory_exchange {
6762306a36Sopenharmony_ci    /*
6862306a36Sopenharmony_ci     * [IN] Details of memory extents to be exchanged (GMFN bases).
6962306a36Sopenharmony_ci     * Note that @in.address_bits is ignored and unused.
7062306a36Sopenharmony_ci     */
7162306a36Sopenharmony_ci    struct xen_memory_reservation in;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci    /*
7462306a36Sopenharmony_ci     * [IN/OUT] Details of new memory extents.
7562306a36Sopenharmony_ci     * We require that:
7662306a36Sopenharmony_ci     *  1. @in.domid == @out.domid
7762306a36Sopenharmony_ci     *  2. @in.nr_extents  << @in.extent_order ==
7862306a36Sopenharmony_ci     *     @out.nr_extents << @out.extent_order
7962306a36Sopenharmony_ci     *  3. @in.extent_start and @out.extent_start lists must not overlap
8062306a36Sopenharmony_ci     *  4. @out.extent_start lists GPFN bases to be populated
8162306a36Sopenharmony_ci     *  5. @out.extent_start is overwritten with allocated GMFN bases
8262306a36Sopenharmony_ci     */
8362306a36Sopenharmony_ci    struct xen_memory_reservation out;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci    /*
8662306a36Sopenharmony_ci     * [OUT] Number of input extents that were successfully exchanged:
8762306a36Sopenharmony_ci     *  1. The first @nr_exchanged input extents were successfully
8862306a36Sopenharmony_ci     *     deallocated.
8962306a36Sopenharmony_ci     *  2. The corresponding first entries in the output extent list correctly
9062306a36Sopenharmony_ci     *     indicate the GMFNs that were successfully exchanged.
9162306a36Sopenharmony_ci     *  3. All other input and output extents are untouched.
9262306a36Sopenharmony_ci     *  4. If not all input exents are exchanged then the return code of this
9362306a36Sopenharmony_ci     *     command will be non-zero.
9462306a36Sopenharmony_ci     *  5. THIS FIELD MUST BE INITIALISED TO ZERO BY THE CALLER!
9562306a36Sopenharmony_ci     */
9662306a36Sopenharmony_ci    xen_ulong_t nr_exchanged;
9762306a36Sopenharmony_ci};
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_memory_exchange);
10062306a36Sopenharmony_ci/*
10162306a36Sopenharmony_ci * Returns the maximum machine frame number of mapped RAM in this system.
10262306a36Sopenharmony_ci * This command always succeeds (it never returns an error code).
10362306a36Sopenharmony_ci * arg == NULL.
10462306a36Sopenharmony_ci */
10562306a36Sopenharmony_ci#define XENMEM_maximum_ram_page     2
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci/*
10862306a36Sopenharmony_ci * Returns the current or maximum memory reservation, in pages, of the
10962306a36Sopenharmony_ci * specified domain (may be DOMID_SELF). Returns -ve errcode on failure.
11062306a36Sopenharmony_ci * arg == addr of domid_t.
11162306a36Sopenharmony_ci */
11262306a36Sopenharmony_ci#define XENMEM_current_reservation  3
11362306a36Sopenharmony_ci#define XENMEM_maximum_reservation  4
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci/*
11662306a36Sopenharmony_ci * Returns a list of MFN bases of 2MB extents comprising the machine_to_phys
11762306a36Sopenharmony_ci * mapping table. Architectures which do not have a m2p table do not implement
11862306a36Sopenharmony_ci * this command.
11962306a36Sopenharmony_ci * arg == addr of xen_machphys_mfn_list_t.
12062306a36Sopenharmony_ci */
12162306a36Sopenharmony_ci#define XENMEM_machphys_mfn_list    5
12262306a36Sopenharmony_cistruct xen_machphys_mfn_list {
12362306a36Sopenharmony_ci    /*
12462306a36Sopenharmony_ci     * Size of the 'extent_start' array. Fewer entries will be filled if the
12562306a36Sopenharmony_ci     * machphys table is smaller than max_extents * 2MB.
12662306a36Sopenharmony_ci     */
12762306a36Sopenharmony_ci    unsigned int max_extents;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci    /*
13062306a36Sopenharmony_ci     * Pointer to buffer to fill with list of extent starts. If there are
13162306a36Sopenharmony_ci     * any large discontiguities in the machine address space, 2MB gaps in
13262306a36Sopenharmony_ci     * the machphys table will be represented by an MFN base of zero.
13362306a36Sopenharmony_ci     */
13462306a36Sopenharmony_ci    GUEST_HANDLE(xen_pfn_t) extent_start;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci    /*
13762306a36Sopenharmony_ci     * Number of extents written to the above array. This will be smaller
13862306a36Sopenharmony_ci     * than 'max_extents' if the machphys table is smaller than max_e * 2MB.
13962306a36Sopenharmony_ci     */
14062306a36Sopenharmony_ci    unsigned int nr_extents;
14162306a36Sopenharmony_ci};
14262306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_machphys_mfn_list);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/*
14562306a36Sopenharmony_ci * Returns the location in virtual address space of the machine_to_phys
14662306a36Sopenharmony_ci * mapping table. Architectures which do not have a m2p table, or which do not
14762306a36Sopenharmony_ci * map it by default into guest address space, do not implement this command.
14862306a36Sopenharmony_ci * arg == addr of xen_machphys_mapping_t.
14962306a36Sopenharmony_ci */
15062306a36Sopenharmony_ci#define XENMEM_machphys_mapping     12
15162306a36Sopenharmony_cistruct xen_machphys_mapping {
15262306a36Sopenharmony_ci    xen_ulong_t v_start, v_end; /* Start and end virtual addresses.   */
15362306a36Sopenharmony_ci    xen_ulong_t max_mfn;        /* Maximum MFN that can be looked up. */
15462306a36Sopenharmony_ci};
15562306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_machphys_mapping_t);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#define XENMAPSPACE_shared_info  0 /* shared info page */
15862306a36Sopenharmony_ci#define XENMAPSPACE_grant_table  1 /* grant table page */
15962306a36Sopenharmony_ci#define XENMAPSPACE_gmfn         2 /* GMFN */
16062306a36Sopenharmony_ci#define XENMAPSPACE_gmfn_range   3 /* GMFN range, XENMEM_add_to_physmap only. */
16162306a36Sopenharmony_ci#define XENMAPSPACE_gmfn_foreign 4 /* GMFN from another dom,
16262306a36Sopenharmony_ci				    * XENMEM_add_to_physmap_range only.
16362306a36Sopenharmony_ci				    */
16462306a36Sopenharmony_ci#define XENMAPSPACE_dev_mmio     5 /* device mmio region */
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci/*
16762306a36Sopenharmony_ci * Sets the GPFN at which a particular page appears in the specified guest's
16862306a36Sopenharmony_ci * pseudophysical address space.
16962306a36Sopenharmony_ci * arg == addr of xen_add_to_physmap_t.
17062306a36Sopenharmony_ci */
17162306a36Sopenharmony_ci#define XENMEM_add_to_physmap      7
17262306a36Sopenharmony_cistruct xen_add_to_physmap {
17362306a36Sopenharmony_ci    /* Which domain to change the mapping for. */
17462306a36Sopenharmony_ci    domid_t domid;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci    /* Number of pages to go through for gmfn_range */
17762306a36Sopenharmony_ci    uint16_t    size;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci    /* Source mapping space. */
18062306a36Sopenharmony_ci    unsigned int space;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci    /* Index into source mapping space. */
18362306a36Sopenharmony_ci    xen_ulong_t idx;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci    /* GPFN where the source mapping page should appear. */
18662306a36Sopenharmony_ci    xen_pfn_t gpfn;
18762306a36Sopenharmony_ci};
18862306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_add_to_physmap);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci/*** REMOVED ***/
19162306a36Sopenharmony_ci/*#define XENMEM_translate_gpfn_list  8*/
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci#define XENMEM_add_to_physmap_range 23
19462306a36Sopenharmony_cistruct xen_add_to_physmap_range {
19562306a36Sopenharmony_ci    /* IN */
19662306a36Sopenharmony_ci    /* Which domain to change the mapping for. */
19762306a36Sopenharmony_ci    domid_t domid;
19862306a36Sopenharmony_ci    uint16_t space; /* => enum phys_map_space */
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci    /* Number of pages to go through */
20162306a36Sopenharmony_ci    uint16_t size;
20262306a36Sopenharmony_ci    domid_t foreign_domid; /* IFF gmfn_foreign */
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci    /* Indexes into space being mapped. */
20562306a36Sopenharmony_ci    GUEST_HANDLE(xen_ulong_t) idxs;
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci    /* GPFN in domid where the source mapping page should appear. */
20862306a36Sopenharmony_ci    GUEST_HANDLE(xen_pfn_t) gpfns;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci    /* OUT */
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci    /* Per index error code. */
21362306a36Sopenharmony_ci    GUEST_HANDLE(int) errs;
21462306a36Sopenharmony_ci};
21562306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_add_to_physmap_range);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci/*
21862306a36Sopenharmony_ci * Returns the pseudo-physical memory map as it was when the domain
21962306a36Sopenharmony_ci * was started (specified by XENMEM_set_memory_map).
22062306a36Sopenharmony_ci * arg == addr of struct xen_memory_map.
22162306a36Sopenharmony_ci */
22262306a36Sopenharmony_ci#define XENMEM_memory_map           9
22362306a36Sopenharmony_cistruct xen_memory_map {
22462306a36Sopenharmony_ci    /*
22562306a36Sopenharmony_ci     * On call the number of entries which can be stored in buffer. On
22662306a36Sopenharmony_ci     * return the number of entries which have been stored in
22762306a36Sopenharmony_ci     * buffer.
22862306a36Sopenharmony_ci     */
22962306a36Sopenharmony_ci    unsigned int nr_entries;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci    /*
23262306a36Sopenharmony_ci     * Entries in the buffer are in the same format as returned by the
23362306a36Sopenharmony_ci     * BIOS INT 0x15 EAX=0xE820 call.
23462306a36Sopenharmony_ci     */
23562306a36Sopenharmony_ci    GUEST_HANDLE(void) buffer;
23662306a36Sopenharmony_ci};
23762306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_memory_map);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci/*
24062306a36Sopenharmony_ci * Returns the real physical memory map. Passes the same structure as
24162306a36Sopenharmony_ci * XENMEM_memory_map.
24262306a36Sopenharmony_ci * arg == addr of struct xen_memory_map.
24362306a36Sopenharmony_ci */
24462306a36Sopenharmony_ci#define XENMEM_machine_memory_map   10
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci/*
24862306a36Sopenharmony_ci * Unmaps the page appearing at a particular GPFN from the specified guest's
24962306a36Sopenharmony_ci * pseudophysical address space.
25062306a36Sopenharmony_ci * arg == addr of xen_remove_from_physmap_t.
25162306a36Sopenharmony_ci */
25262306a36Sopenharmony_ci#define XENMEM_remove_from_physmap      15
25362306a36Sopenharmony_cistruct xen_remove_from_physmap {
25462306a36Sopenharmony_ci    /* Which domain to change the mapping for. */
25562306a36Sopenharmony_ci    domid_t domid;
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci    /* GPFN of the current mapping of the page. */
25862306a36Sopenharmony_ci    xen_pfn_t gpfn;
25962306a36Sopenharmony_ci};
26062306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_remove_from_physmap);
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci/*
26362306a36Sopenharmony_ci * Get the pages for a particular guest resource, so that they can be
26462306a36Sopenharmony_ci * mapped directly by a tools domain.
26562306a36Sopenharmony_ci */
26662306a36Sopenharmony_ci#define XENMEM_acquire_resource 28
26762306a36Sopenharmony_cistruct xen_mem_acquire_resource {
26862306a36Sopenharmony_ci    /* IN - The domain whose resource is to be mapped */
26962306a36Sopenharmony_ci    domid_t domid;
27062306a36Sopenharmony_ci    /* IN - the type of resource */
27162306a36Sopenharmony_ci    uint16_t type;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci#define XENMEM_resource_ioreq_server 0
27462306a36Sopenharmony_ci#define XENMEM_resource_grant_table 1
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci    /*
27762306a36Sopenharmony_ci     * IN - a type-specific resource identifier, which must be zero
27862306a36Sopenharmony_ci     *      unless stated otherwise.
27962306a36Sopenharmony_ci     *
28062306a36Sopenharmony_ci     * type == XENMEM_resource_ioreq_server -> id == ioreq server id
28162306a36Sopenharmony_ci     * type == XENMEM_resource_grant_table -> id defined below
28262306a36Sopenharmony_ci     */
28362306a36Sopenharmony_ci    uint32_t id;
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci#define XENMEM_resource_grant_table_id_shared 0
28662306a36Sopenharmony_ci#define XENMEM_resource_grant_table_id_status 1
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci    /* IN/OUT - As an IN parameter number of frames of the resource
28962306a36Sopenharmony_ci     *          to be mapped. However, if the specified value is 0 and
29062306a36Sopenharmony_ci     *          frame_list is NULL then this field will be set to the
29162306a36Sopenharmony_ci     *          maximum value supported by the implementation on return.
29262306a36Sopenharmony_ci     */
29362306a36Sopenharmony_ci    uint32_t nr_frames;
29462306a36Sopenharmony_ci    /*
29562306a36Sopenharmony_ci     * OUT - Must be zero on entry. On return this may contain a bitwise
29662306a36Sopenharmony_ci     *       OR of the following values.
29762306a36Sopenharmony_ci     */
29862306a36Sopenharmony_ci    uint32_t flags;
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci    /* The resource pages have been assigned to the calling domain */
30162306a36Sopenharmony_ci#define _XENMEM_rsrc_acq_caller_owned 0
30262306a36Sopenharmony_ci#define XENMEM_rsrc_acq_caller_owned (1u << _XENMEM_rsrc_acq_caller_owned)
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci    /*
30562306a36Sopenharmony_ci     * IN - the index of the initial frame to be mapped. This parameter
30662306a36Sopenharmony_ci     *      is ignored if nr_frames is 0.
30762306a36Sopenharmony_ci     */
30862306a36Sopenharmony_ci    uint64_t frame;
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci#define XENMEM_resource_ioreq_server_frame_bufioreq 0
31162306a36Sopenharmony_ci#define XENMEM_resource_ioreq_server_frame_ioreq(n) (1 + (n))
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci    /*
31462306a36Sopenharmony_ci     * IN/OUT - If the tools domain is PV then, upon return, frame_list
31562306a36Sopenharmony_ci     *          will be populated with the MFNs of the resource.
31662306a36Sopenharmony_ci     *          If the tools domain is HVM then it is expected that, on
31762306a36Sopenharmony_ci     *          entry, frame_list will be populated with a list of GFNs
31862306a36Sopenharmony_ci     *          that will be mapped to the MFNs of the resource.
31962306a36Sopenharmony_ci     *          If -EIO is returned then the frame_list has only been
32062306a36Sopenharmony_ci     *          partially mapped and it is up to the caller to unmap all
32162306a36Sopenharmony_ci     *          the GFNs.
32262306a36Sopenharmony_ci     *          This parameter may be NULL if nr_frames is 0.
32362306a36Sopenharmony_ci     */
32462306a36Sopenharmony_ci    GUEST_HANDLE(xen_pfn_t) frame_list;
32562306a36Sopenharmony_ci};
32662306a36Sopenharmony_ciDEFINE_GUEST_HANDLE_STRUCT(xen_mem_acquire_resource);
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci#endif /* __XEN_PUBLIC_MEMORY_H__ */
329