1 /******************************************************************************
2 * cpu_idle.c -- adapt x86/acpi/cpu_idle.c to compat guest.
3 *
4 * Copyright (C) 2007, 2008 Intel Corporation
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; If not, see <http://www.gnu.org/licenses/>.
20 *
21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 */
23
24 #include <xen/types.h>
25 #include <xen/xmalloc.h>
26 #include <xen/guest_access.h>
27 #include <xen/pmstat.h>
28 #include <compat/platform.h>
29
30 CHECK_processor_csd;
31
32 DEFINE_XEN_GUEST_HANDLE(compat_processor_csd_t);
33 DEFINE_XEN_GUEST_HANDLE(compat_processor_cx_t);
34
xlat_malloc(unsigned long * xlat_page_current,size_t size)35 void *xlat_malloc(unsigned long *xlat_page_current, size_t size)
36 {
37 void *ret;
38
39 /* normalize size to be 64 * n */
40 size = (size + 0x3fUL) & ~0x3fUL;
41
42 if ( unlikely(size > xlat_page_left_size(*xlat_page_current)) )
43 return NULL;
44
45 ret = (void *) *xlat_page_current;
46 *xlat_page_current += size;
47
48 return ret;
49 }
50
copy_from_compat_state(xen_processor_cx_t * xen_state,compat_processor_cx_t * state)51 static int copy_from_compat_state(xen_processor_cx_t *xen_state,
52 compat_processor_cx_t *state)
53 {
54 #define XLAT_processor_cx_HNDL_dp(_d_, _s_) do { \
55 if ( unlikely(!compat_handle_okay((_s_)->dp, (_s_)->dpcnt)) ) \
56 return -EFAULT; \
57 guest_from_compat_handle((_d_)->dp, (_s_)->dp); \
58 } while (0)
59 XLAT_processor_cx(xen_state, state);
60 #undef XLAT_processor_cx_HNDL_dp
61
62 return 0;
63 }
64
compat_set_cx_pminfo(uint32_t cpu,struct compat_processor_power * power)65 long compat_set_cx_pminfo(uint32_t cpu, struct compat_processor_power *power)
66 {
67 struct xen_processor_power *xen_power;
68 unsigned long xlat_page_current;
69
70 xlat_malloc_init(xlat_page_current);
71
72 xen_power = xlat_malloc_array(xlat_page_current,
73 struct xen_processor_power, 1);
74 if ( unlikely(xen_power == NULL) )
75 return -EFAULT;
76
77 #define XLAT_processor_power_HNDL_states(_d_, _s_) do { \
78 xen_processor_cx_t *xen_states = NULL; \
79 \
80 if ( likely((_s_)->count > 0) ) \
81 { \
82 XEN_GUEST_HANDLE(compat_processor_cx_t) states; \
83 compat_processor_cx_t state; \
84 int i; \
85 \
86 xen_states = xlat_malloc_array(xlat_page_current, \
87 xen_processor_cx_t, (_s_)->count); \
88 if ( unlikely(xen_states == NULL) ) \
89 return -EFAULT; \
90 \
91 if ( unlikely(!compat_handle_okay((_s_)->states, (_s_)->count)) ) \
92 return -EFAULT; \
93 guest_from_compat_handle(states, (_s_)->states); \
94 \
95 for ( i = 0; i < _s_->count; i++ ) \
96 { \
97 if ( unlikely(copy_from_guest_offset(&state, states, i, 1)) ) \
98 return -EFAULT; \
99 if ( unlikely(copy_from_compat_state(&xen_states[i], &state)) ) \
100 return -EFAULT; \
101 } \
102 } \
103 \
104 set_xen_guest_handle((_d_)->states, xen_states); \
105 } while (0)
106 XLAT_processor_power(xen_power, power);
107 #undef XLAT_processor_power_HNDL_states
108
109 return set_cx_pminfo(cpu, xen_power);
110 }
111