s390x/tcg: Implement 32/128 bit for VECTOR FP TEST DATA CLASS IMMEDIATE
[qemu/kevin.git] / chardev / char-ringbuf.c
blobd40d21d3cf78cb0ab5ac997909c6009b313ca41b
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "chardev/char.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-commands-char.h"
29 #include "qemu/base64.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qom/object.h"
34 /* Ring buffer chardev */
36 struct RingBufChardev {
37 Chardev parent;
38 size_t size;
39 size_t prod;
40 size_t cons;
41 uint8_t *cbuf;
43 typedef struct RingBufChardev RingBufChardev;
45 DECLARE_INSTANCE_CHECKER(RingBufChardev, RINGBUF_CHARDEV,
46 TYPE_CHARDEV_RINGBUF)
48 static size_t ringbuf_count(const Chardev *chr)
50 const RingBufChardev *d = RINGBUF_CHARDEV(chr);
52 return d->prod - d->cons;
55 static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len)
57 RingBufChardev *d = RINGBUF_CHARDEV(chr);
58 int i;
60 if (!buf || (len < 0)) {
61 return -1;
64 for (i = 0; i < len; i++) {
65 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
66 if (d->prod - d->cons > d->size) {
67 d->cons = d->prod - d->size;
71 return len;
74 static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len)
76 RingBufChardev *d = RINGBUF_CHARDEV(chr);
77 int i;
79 qemu_mutex_lock(&chr->chr_write_lock);
80 for (i = 0; i < len && d->cons != d->prod; i++) {
81 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
83 qemu_mutex_unlock(&chr->chr_write_lock);
85 return i;
88 static void char_ringbuf_finalize(Object *obj)
90 RingBufChardev *d = RINGBUF_CHARDEV(obj);
92 g_free(d->cbuf);
95 static void qemu_chr_open_ringbuf(Chardev *chr,
96 ChardevBackend *backend,
97 bool *be_opened,
98 Error **errp)
100 ChardevRingbuf *opts = backend->u.ringbuf.data;
101 RingBufChardev *d = RINGBUF_CHARDEV(chr);
103 d->size = opts->has_size ? opts->size : 65536;
105 /* The size must be power of 2 */
106 if (d->size & (d->size - 1)) {
107 error_setg(errp, "size of ringbuf chardev must be power of two");
108 return;
111 d->prod = 0;
112 d->cons = 0;
113 d->cbuf = g_malloc0(d->size);
116 void qmp_ringbuf_write(const char *device, const char *data,
117 bool has_format, enum DataFormat format,
118 Error **errp)
120 Chardev *chr;
121 const uint8_t *write_data;
122 int ret;
123 gsize write_count;
125 chr = qemu_chr_find(device);
126 if (!chr) {
127 error_setg(errp, "Device '%s' not found", device);
128 return;
131 if (!CHARDEV_IS_RINGBUF(chr)) {
132 error_setg(errp, "%s is not a ringbuf device", device);
133 return;
136 if (has_format && (format == DATA_FORMAT_BASE64)) {
137 write_data = qbase64_decode(data, -1,
138 &write_count,
139 errp);
140 if (!write_data) {
141 return;
143 } else {
144 write_data = (uint8_t *)data;
145 write_count = strlen(data);
148 ret = ringbuf_chr_write(chr, write_data, write_count);
150 if (write_data != (uint8_t *)data) {
151 g_free((void *)write_data);
154 if (ret < 0) {
155 error_setg(errp, "Failed to write to device %s", device);
156 return;
160 char *qmp_ringbuf_read(const char *device, int64_t size,
161 bool has_format, enum DataFormat format,
162 Error **errp)
164 Chardev *chr;
165 uint8_t *read_data;
166 size_t count;
167 char *data;
169 chr = qemu_chr_find(device);
170 if (!chr) {
171 error_setg(errp, "Device '%s' not found", device);
172 return NULL;
175 if (!CHARDEV_IS_RINGBUF(chr)) {
176 error_setg(errp, "%s is not a ringbuf device", device);
177 return NULL;
180 if (size <= 0) {
181 error_setg(errp, "size must be greater than zero");
182 return NULL;
185 count = ringbuf_count(chr);
186 size = size > count ? count : size;
187 read_data = g_malloc(size + 1);
189 ringbuf_chr_read(chr, read_data, size);
191 if (has_format && (format == DATA_FORMAT_BASE64)) {
192 data = g_base64_encode(read_data, size);
193 g_free(read_data);
194 } else {
196 * FIXME should read only complete, valid UTF-8 characters up
197 * to @size bytes. Invalid sequences should be replaced by a
198 * suitable replacement character. Except when (and only
199 * when) ring buffer lost characters since last read, initial
200 * continuation characters should be dropped.
202 read_data[size] = 0;
203 data = (char *)read_data;
206 return data;
209 static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
210 Error **errp)
212 int val;
213 ChardevRingbuf *ringbuf;
215 backend->type = CHARDEV_BACKEND_KIND_RINGBUF;
216 ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1);
217 qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf));
219 val = qemu_opt_get_size(opts, "size", 0);
220 if (val != 0) {
221 ringbuf->has_size = true;
222 ringbuf->size = val;
226 static void char_ringbuf_class_init(ObjectClass *oc, void *data)
228 ChardevClass *cc = CHARDEV_CLASS(oc);
230 cc->parse = qemu_chr_parse_ringbuf;
231 cc->open = qemu_chr_open_ringbuf;
232 cc->chr_write = ringbuf_chr_write;
235 static const TypeInfo char_ringbuf_type_info = {
236 .name = TYPE_CHARDEV_RINGBUF,
237 .parent = TYPE_CHARDEV,
238 .class_init = char_ringbuf_class_init,
239 .instance_size = sizeof(RingBufChardev),
240 .instance_finalize = char_ringbuf_finalize,
243 /* Bug-compatibility: */
244 static const TypeInfo char_memory_type_info = {
245 .name = TYPE_CHARDEV_MEMORY,
246 .parent = TYPE_CHARDEV_RINGBUF,
249 static void register_types(void)
251 type_register_static(&char_ringbuf_type_info);
252 type_register_static(&char_memory_type_info);
255 type_init(register_types);