summaryrefslogtreecommitdiff
path: root/xen/arch/x86/guest/hyperv/tlb.c
blob: ef930efa954ba2e462c038c6fdaa5e60d1390075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* SPDX-License-Identifier: GPL-2.0-or-later */
/******************************************************************************
 * arch/x86/guest/hyperv/tlb.c
 *
 * Support for TLB management using hypercalls
 *
 * Copyright (c) 2020 Microsoft.
 */

#include <xen/cpu.h>
#include <xen/cpumask.h>
#include <xen/errno.h>

#include <asm/guest/hyperv.h>
#include <asm/guest/hyperv-hcall.h>
#include <asm/guest/hyperv-tlfs.h>

#include "private.h"

/*
 * It is possible to encode up to 4096 pages using the lower 12 bits
 * in an element of gva_list
 */
#define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)

static unsigned int fill_gva_list(uint64_t *gva_list, const void *va,
                                  unsigned int order)
{
    unsigned long cur = (unsigned long)va;
    /* end is 1 past the range to be flushed */
    unsigned long end = cur + (PAGE_SIZE << order);
    unsigned int n = 0;

    do {
        unsigned long diff = end - cur;

        gva_list[n] = cur & PAGE_MASK;

        /*
         * Use lower 12 bits to encode the number of additional pages
         * to flush
         */
        if ( diff >= HV_TLB_FLUSH_UNIT )
        {
            gva_list[n] |= ~PAGE_MASK;
            cur += HV_TLB_FLUSH_UNIT;
        }
        else
        {
            gva_list[n] |= (diff - 1) >> PAGE_SHIFT;
            cur = end;
        }

        n++;
    } while ( cur < end );

    return n;
}

static uint64_t flush_tlb_ex(const cpumask_t *mask, const void *va,
                             unsigned int flags)
{
    struct hv_tlb_flush_ex *flush = this_cpu(hv_input_page);
    int nr_banks;
    unsigned int max_gvas, order = (flags - 1) & FLUSH_ORDER_MASK;
    uint64_t *gva_list;

    if ( !flush || local_irq_is_enabled() )
    {
        ASSERT_UNREACHABLE();
        return ~0ULL;
    }

    if ( !(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED) )
        return ~0ULL;

    flush->address_space = 0;
    flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
    if ( !(flags & FLUSH_TLB_GLOBAL) )
        flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;

    nr_banks = cpumask_to_vpset(&flush->hv_vp_set, mask);
    if ( nr_banks < 0 )
        return ~0ULL;

    max_gvas =
        (PAGE_SIZE - sizeof(*flush) - nr_banks *
         sizeof(flush->hv_vp_set.bank_contents[0])) /
        sizeof(uint64_t);       /* gva is represented as uint64_t */

    /*
     * Flush the entire address space if va is NULL or if there is not
     * enough space for gva_list.
     */
    if ( !va || (PAGE_SIZE << order) / HV_TLB_FLUSH_UNIT > max_gvas )
        return hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX, 0,
                                   nr_banks, virt_to_maddr(flush), 0);

    /*
     * The calculation of gva_list address requires the structure to
     * be 64 bits aligned.
     */
    BUILD_BUG_ON(sizeof(*flush) % sizeof(uint64_t));
    gva_list = (uint64_t *)flush + sizeof(*flush) / sizeof(uint64_t) + nr_banks;

    return hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX,
                               fill_gva_list(gva_list, va, order),
                               nr_banks, virt_to_maddr(flush), 0);
}

/* Maximum number of gvas for hv_tlb_flush */
#define MAX_GVAS ((PAGE_SIZE - sizeof(struct hv_tlb_flush)) / sizeof(uint64_t))

int hyperv_flush_tlb(const cpumask_t *mask, const void *va,
                     unsigned int flags)
{
    unsigned long irq_flags;
    struct hv_tlb_flush *flush = this_cpu(hv_input_page);
    unsigned int order = (flags - 1) & FLUSH_ORDER_MASK;
    uint64_t ret;

    if ( !flush || cpumask_empty(mask) )
    {
        ASSERT_UNREACHABLE();
        return -EINVAL;
    }

    /* TODO: may need to check if in #NMI or #MC and fallback to native path */

    local_irq_save(irq_flags);

    flush->address_space = 0;
    flush->flags = HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES;
    flush->processor_mask = 0;
    if ( !(flags & FLUSH_TLB_GLOBAL) )
        flush->flags |= HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY;

    if ( cpumask_equal(mask, &cpu_online_map) )
        flush->flags |= HV_FLUSH_ALL_PROCESSORS;
    else
    {
        unsigned int cpu;

        /*
         * Normally VP indices are in ascending order and match Xen's
         * idea of CPU ids. Check the last index to see if VP index is
         * >= 64. If so, we can skip setting up parameters for
         * non-applicable hypercalls without looking further.
         */
        if ( hv_vp_index(cpumask_last(mask)) >= 64 )
            goto do_ex_hypercall;

        for_each_cpu ( cpu, mask )
        {
            unsigned int vpid = hv_vp_index(cpu);

            if ( vpid > hv_max_vp_index )
            {
                local_irq_restore(irq_flags);
                return -ENXIO;
            }

            if ( vpid >= 64 )
                goto do_ex_hypercall;

            __set_bit(vpid, &flush->processor_mask);
        }
    }

    /*
     * Flush the entire address space if va is NULL or if there is not
     * enough space for gva_list.
     */
    if ( !va || (PAGE_SIZE << order) / HV_TLB_FLUSH_UNIT > MAX_GVAS )
        ret = hv_do_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE,
                              virt_to_maddr(flush), 0);
    else
        ret = hv_do_rep_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST,
                                  fill_gva_list(flush->gva_list, va, order),
                                  0, virt_to_maddr(flush), 0);
    goto done;

 do_ex_hypercall:
    ret = flush_tlb_ex(mask, va, flags);

 done:
    local_irq_restore(irq_flags);

    return ret & HV_HYPERCALL_RESULT_MASK ? -ENXIO : 0;
}

#undef MAX_GVAS

/*
 * Local variables:
 * mode: C
 * c-file-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */