1 /******************************************************************************
2 * arch/x86/hvm/grant_table.c
3 *
4 * Grant table interfaces for HVM guests
5 *
6 * Copyright (C) 2017 Wei Liu <wei.liu2@citrix.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <xen/types.h>
23
24 #include <public/grant_table.h>
25
26 #include <asm/p2m.h>
27
create_grant_p2m_mapping(uint64_t addr,unsigned long frame,unsigned int flags,unsigned int cache_flags)28 int create_grant_p2m_mapping(uint64_t addr, unsigned long frame,
29 unsigned int flags,
30 unsigned int cache_flags)
31 {
32 p2m_type_t p2mt;
33 int rc;
34
35 if ( cache_flags || (flags & ~GNTMAP_readonly) != GNTMAP_host_map )
36 return GNTST_general_error;
37
38 if ( flags & GNTMAP_readonly )
39 p2mt = p2m_grant_map_ro;
40 else
41 p2mt = p2m_grant_map_rw;
42 rc = guest_physmap_add_entry(current->domain,
43 _gfn(addr >> PAGE_SHIFT),
44 _mfn(frame), PAGE_ORDER_4K, p2mt);
45 if ( rc )
46 return GNTST_general_error;
47 else
48 return GNTST_okay;
49 }
50
replace_grant_p2m_mapping(uint64_t addr,unsigned long frame,uint64_t new_addr,unsigned int flags)51 int replace_grant_p2m_mapping(uint64_t addr, unsigned long frame,
52 uint64_t new_addr, unsigned int flags)
53 {
54 unsigned long gfn = (unsigned long)(addr >> PAGE_SHIFT);
55 p2m_type_t type;
56 mfn_t old_mfn;
57 struct domain *d = current->domain;
58
59 if ( new_addr != 0 || (flags & GNTMAP_contains_pte) )
60 return GNTST_general_error;
61
62 old_mfn = get_gfn(d, gfn, &type);
63 if ( !p2m_is_grant(type) || mfn_x(old_mfn) != frame )
64 {
65 put_gfn(d, gfn);
66 gdprintk(XENLOG_WARNING,
67 "old mapping invalid (type %d, mfn %" PRI_mfn ", frame %lx)\n",
68 type, mfn_x(old_mfn), frame);
69 return GNTST_general_error;
70 }
71 if ( guest_physmap_remove_page(d, _gfn(gfn), _mfn(frame), PAGE_ORDER_4K) )
72 {
73 put_gfn(d, gfn);
74 return GNTST_general_error;
75 }
76
77 put_gfn(d, gfn);
78 return GNTST_okay;
79 }
80
81 /*
82 * Local variables:
83 * mode: C
84 * c-file-style: "BSD"
85 * c-basic-offset: 4
86 * tab-width: 4
87 * indent-tabs-mode: nil
88 * End:
89 */
90