tools, slub: Fix off-by-one buffer corruption after readlink() call
[linux-2.6/linux-mips.git] / arch / sparc / kernel / hvapi.c
blobc2d055d8ba9e273ed8e996c7133b553e75ffcbc7
1 /* hvapi.c: Hypervisor API management.
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
4 */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <asm/hypervisor.h>
10 #include <asm/oplib.h>
12 /* If the hypervisor indicates that the API setting
13 * calls are unsupported, by returning HV_EBADTRAP or
14 * HV_ENOTSUPPORTED, we assume that API groups with the
15 * PRE_API flag set are major 1 minor 0.
17 struct api_info {
18 unsigned long group;
19 unsigned long major;
20 unsigned long minor;
21 unsigned int refcnt;
22 unsigned int flags;
23 #define FLAG_PRE_API 0x00000001
26 static struct api_info api_table[] = {
27 { .group = HV_GRP_SUN4V, .flags = FLAG_PRE_API },
28 { .group = HV_GRP_CORE, .flags = FLAG_PRE_API },
29 { .group = HV_GRP_INTR, },
30 { .group = HV_GRP_SOFT_STATE, },
31 { .group = HV_GRP_TM, },
32 { .group = HV_GRP_PCI, .flags = FLAG_PRE_API },
33 { .group = HV_GRP_LDOM, },
34 { .group = HV_GRP_SVC_CHAN, .flags = FLAG_PRE_API },
35 { .group = HV_GRP_NCS, .flags = FLAG_PRE_API },
36 { .group = HV_GRP_RNG, },
37 { .group = HV_GRP_PBOOT, },
38 { .group = HV_GRP_TPM, },
39 { .group = HV_GRP_SDIO, },
40 { .group = HV_GRP_SDIO_ERR, },
41 { .group = HV_GRP_REBOOT_DATA, },
42 { .group = HV_GRP_NIAG_PERF, .flags = FLAG_PRE_API },
43 { .group = HV_GRP_FIRE_PERF, },
44 { .group = HV_GRP_N2_CPU, },
45 { .group = HV_GRP_NIU, },
46 { .group = HV_GRP_VF_CPU, },
47 { .group = HV_GRP_KT_CPU, },
48 { .group = HV_GRP_DIAG, .flags = FLAG_PRE_API },
51 static DEFINE_SPINLOCK(hvapi_lock);
53 static struct api_info *__get_info(unsigned long group)
55 int i;
57 for (i = 0; i < ARRAY_SIZE(api_table); i++) {
58 if (api_table[i].group == group)
59 return &api_table[i];
61 return NULL;
64 static void __get_ref(struct api_info *p)
66 p->refcnt++;
69 static void __put_ref(struct api_info *p)
71 if (--p->refcnt == 0) {
72 unsigned long ignore;
74 sun4v_set_version(p->group, 0, 0, &ignore);
75 p->major = p->minor = 0;
79 /* Register a hypervisor API specification. It indicates the
80 * API group and desired major+minor.
82 * If an existing API registration exists '0' (success) will
83 * be returned if it is compatible with the one being registered.
84 * Otherwise a negative error code will be returned.
86 * Otherwise an attempt will be made to negotiate the requested
87 * API group/major/minor with the hypervisor, and errors returned
88 * if that does not succeed.
90 int sun4v_hvapi_register(unsigned long group, unsigned long major,
91 unsigned long *minor)
93 struct api_info *p;
94 unsigned long flags;
95 int ret;
97 spin_lock_irqsave(&hvapi_lock, flags);
98 p = __get_info(group);
99 ret = -EINVAL;
100 if (p) {
101 if (p->refcnt) {
102 ret = -EINVAL;
103 if (p->major == major) {
104 *minor = p->minor;
105 ret = 0;
107 } else {
108 unsigned long actual_minor;
109 unsigned long hv_ret;
111 hv_ret = sun4v_set_version(group, major, *minor,
112 &actual_minor);
113 ret = -EINVAL;
114 if (hv_ret == HV_EOK) {
115 *minor = actual_minor;
116 p->major = major;
117 p->minor = actual_minor;
118 ret = 0;
119 } else if (hv_ret == HV_EBADTRAP ||
120 hv_ret == HV_ENOTSUPPORTED) {
121 if (p->flags & FLAG_PRE_API) {
122 if (major == 1) {
123 p->major = 1;
124 p->minor = 0;
125 *minor = 0;
126 ret = 0;
132 if (ret == 0)
133 __get_ref(p);
135 spin_unlock_irqrestore(&hvapi_lock, flags);
137 return ret;
139 EXPORT_SYMBOL(sun4v_hvapi_register);
141 void sun4v_hvapi_unregister(unsigned long group)
143 struct api_info *p;
144 unsigned long flags;
146 spin_lock_irqsave(&hvapi_lock, flags);
147 p = __get_info(group);
148 if (p)
149 __put_ref(p);
150 spin_unlock_irqrestore(&hvapi_lock, flags);
152 EXPORT_SYMBOL(sun4v_hvapi_unregister);
154 int sun4v_hvapi_get(unsigned long group,
155 unsigned long *major,
156 unsigned long *minor)
158 struct api_info *p;
159 unsigned long flags;
160 int ret;
162 spin_lock_irqsave(&hvapi_lock, flags);
163 ret = -EINVAL;
164 p = __get_info(group);
165 if (p && p->refcnt) {
166 *major = p->major;
167 *minor = p->minor;
168 ret = 0;
170 spin_unlock_irqrestore(&hvapi_lock, flags);
172 return ret;
174 EXPORT_SYMBOL(sun4v_hvapi_get);
176 void __init sun4v_hvapi_init(void)
178 unsigned long group, major, minor;
180 group = HV_GRP_SUN4V;
181 major = 1;
182 minor = 0;
183 if (sun4v_hvapi_register(group, major, &minor))
184 goto bad;
186 group = HV_GRP_CORE;
187 major = 1;
188 minor = 1;
189 if (sun4v_hvapi_register(group, major, &minor))
190 goto bad;
192 return;
194 bad:
195 prom_printf("HVAPI: Cannot register API group "
196 "%lx with major(%u) minor(%u)\n",
197 group, major, minor);
198 prom_halt();