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     XEN_GUEST_HANDLE(compat_processor_csd_t) dps; \
56     XEN_GUEST_HANDLE_PARAM(xen_processor_csd_t) dps_param; \
57     if ( unlikely(!compat_handle_okay((_s_)->dp, (_s_)->dpcnt)) ) \
58             return -EFAULT; \
59     guest_from_compat_handle(dps, (_s_)->dp); \
60     dps_param = guest_handle_cast(dps, xen_processor_csd_t); \
61     (_d_)->dp = guest_handle_from_param(dps_param, xen_processor_csd_t); \
62 } while (0)
63     XLAT_processor_cx(xen_state, state);
64 #undef XLAT_processor_cx_HNDL_dp
65 
66     return 0;
67 }
68 
compat_set_cx_pminfo(uint32_t cpu,struct compat_processor_power * power)69 long compat_set_cx_pminfo(uint32_t cpu, struct compat_processor_power *power)
70 {
71     struct xen_processor_power *xen_power;
72     unsigned long xlat_page_current;
73 
74     xlat_malloc_init(xlat_page_current);
75 
76     xen_power = xlat_malloc_array(xlat_page_current,
77                                   struct xen_processor_power, 1);
78     if ( unlikely(xen_power == NULL) )
79 	return -EFAULT;
80 
81 #define XLAT_processor_power_HNDL_states(_d_, _s_) do { \
82     xen_processor_cx_t *xen_states = NULL; \
83 \
84     if ( likely((_s_)->count > 0) ) \
85     { \
86         XEN_GUEST_HANDLE(compat_processor_cx_t) states; \
87         compat_processor_cx_t state; \
88         int i; \
89 \
90         xen_states = xlat_malloc_array(xlat_page_current, \
91                                        xen_processor_cx_t, (_s_)->count); \
92         if ( unlikely(xen_states == NULL) ) \
93             return -EFAULT; \
94 \
95         if ( unlikely(!compat_handle_okay((_s_)->states, (_s_)->count)) ) \
96             return -EFAULT; \
97         guest_from_compat_handle(states, (_s_)->states); \
98 \
99         for ( i = 0; i < _s_->count; i++ ) \
100         { \
101            if ( unlikely(copy_from_guest_offset(&state, states, i, 1)) ) \
102                return -EFAULT; \
103            if ( unlikely(copy_from_compat_state(&xen_states[i], &state)) ) \
104                return -EFAULT; \
105         } \
106     } \
107 \
108     set_xen_guest_handle((_d_)->states, xen_states); \
109 } while (0)
110     XLAT_processor_power(xen_power, power);
111 #undef XLAT_processor_power_HNDL_states
112 
113     return set_cx_pminfo(cpu, xen_power);
114 }
115