1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2024, Microsoft Corporation.
4 *
5 * This file contains functions that will be called from one or more modules.
6 * If any of these modules are configured to build, this file is built and just
7 * statically linked in.
8 *
9 * Authors: Microsoft Linux virtualization team
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <asm/mshyperv.h>
15 #include <linux/resume_user_mode.h>
16
17 #include "mshv.h"
18
19 #define HV_GET_REGISTER_BATCH_SIZE \
20 (HV_HYP_PAGE_SIZE / sizeof(union hv_register_value))
21 #define HV_SET_REGISTER_BATCH_SIZE \
22 ((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_set_vp_registers)) \
23 / sizeof(struct hv_register_assoc))
24
hv_call_get_vp_registers(u32 vp_index,u64 partition_id,u16 count,union hv_input_vtl input_vtl,struct hv_register_assoc * registers)25 int hv_call_get_vp_registers(u32 vp_index, u64 partition_id, u16 count,
26 union hv_input_vtl input_vtl,
27 struct hv_register_assoc *registers)
28 {
29 struct hv_input_get_vp_registers *input_page;
30 union hv_register_value *output_page;
31 u16 completed = 0;
32 unsigned long remaining = count;
33 int rep_count, i;
34 u64 status = HV_STATUS_SUCCESS;
35 unsigned long flags;
36
37 local_irq_save(flags);
38
39 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
40 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
41
42 input_page->partition_id = partition_id;
43 input_page->vp_index = vp_index;
44 input_page->input_vtl.as_uint8 = input_vtl.as_uint8;
45 input_page->rsvd_z8 = 0;
46 input_page->rsvd_z16 = 0;
47
48 while (remaining) {
49 rep_count = min(remaining, HV_GET_REGISTER_BATCH_SIZE);
50 for (i = 0; i < rep_count; ++i)
51 input_page->names[i] = registers[i].name;
52
53 status = hv_do_rep_hypercall(HVCALL_GET_VP_REGISTERS, rep_count,
54 0, input_page, output_page);
55 if (!hv_result_success(status))
56 break;
57
58 completed = hv_repcomp(status);
59 for (i = 0; i < completed; ++i)
60 registers[i].value = output_page[i];
61
62 registers += completed;
63 remaining -= completed;
64 }
65 local_irq_restore(flags);
66
67 return hv_result_to_errno(status);
68 }
69 EXPORT_SYMBOL_GPL(hv_call_get_vp_registers);
70
hv_call_set_vp_registers(u32 vp_index,u64 partition_id,u16 count,union hv_input_vtl input_vtl,struct hv_register_assoc * registers)71 int hv_call_set_vp_registers(u32 vp_index, u64 partition_id, u16 count,
72 union hv_input_vtl input_vtl,
73 struct hv_register_assoc *registers)
74 {
75 struct hv_input_set_vp_registers *input_page;
76 u16 completed = 0;
77 unsigned long remaining = count;
78 int rep_count;
79 u64 status = HV_STATUS_SUCCESS;
80 unsigned long flags;
81
82 local_irq_save(flags);
83 input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
84
85 input_page->partition_id = partition_id;
86 input_page->vp_index = vp_index;
87 input_page->input_vtl.as_uint8 = input_vtl.as_uint8;
88 input_page->rsvd_z8 = 0;
89 input_page->rsvd_z16 = 0;
90
91 while (remaining) {
92 rep_count = min(remaining, HV_SET_REGISTER_BATCH_SIZE);
93 memcpy(input_page->elements, registers,
94 sizeof(struct hv_register_assoc) * rep_count);
95
96 status = hv_do_rep_hypercall(HVCALL_SET_VP_REGISTERS, rep_count,
97 0, input_page, NULL);
98 if (!hv_result_success(status))
99 break;
100
101 completed = hv_repcomp(status);
102 registers += completed;
103 remaining -= completed;
104 }
105
106 local_irq_restore(flags);
107
108 return hv_result_to_errno(status);
109 }
110 EXPORT_SYMBOL_GPL(hv_call_set_vp_registers);
111
hv_call_get_partition_property(u64 partition_id,u64 property_code,u64 * property_value)112 int hv_call_get_partition_property(u64 partition_id,
113 u64 property_code,
114 u64 *property_value)
115 {
116 u64 status;
117 unsigned long flags;
118 struct hv_input_get_partition_property *input;
119 struct hv_output_get_partition_property *output;
120
121 local_irq_save(flags);
122 input = *this_cpu_ptr(hyperv_pcpu_input_arg);
123 output = *this_cpu_ptr(hyperv_pcpu_output_arg);
124 memset(input, 0, sizeof(*input));
125 input->partition_id = partition_id;
126 input->property_code = property_code;
127 status = hv_do_hypercall(HVCALL_GET_PARTITION_PROPERTY, input, output);
128
129 if (!hv_result_success(status)) {
130 local_irq_restore(flags);
131 return hv_result_to_errno(status);
132 }
133 *property_value = output->property_value;
134
135 local_irq_restore(flags);
136
137 return 0;
138 }
139 EXPORT_SYMBOL_GPL(hv_call_get_partition_property);
140
141 /*
142 * Handle any pre-processing before going into the guest mode on this cpu, most
143 * notably call schedule(). Must be invoked with both preemption and
144 * interrupts enabled.
145 *
146 * Returns: 0 on success, -errno on error.
147 */
mshv_do_pre_guest_mode_work(ulong th_flags)148 int mshv_do_pre_guest_mode_work(ulong th_flags)
149 {
150 if (th_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
151 return -EINTR;
152
153 if (th_flags & _TIF_NEED_RESCHED)
154 schedule();
155
156 if (th_flags & _TIF_NOTIFY_RESUME)
157 resume_user_mode_work(NULL);
158
159 return 0;
160 }
161 EXPORT_SYMBOL_GPL(mshv_do_pre_guest_mode_work);
162