Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / sys / kern / subr_mchain.c
blob239564e9932b41a776b733d062028becb7196f7e
1 /*-
2 * Copyright (c) 2000, 2001 Boris Popov
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/endian.h>
36 #include <sys/errno.h>
37 #include <sys/mbuf.h>
38 #include <sys/module.h>
39 #include <sys/uio.h>
41 #include <sys/mchain.h>
43 MODULE_VERSION(libmchain, 1);
45 #define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \
46 __LINE__ , ## __VA_ARGS__)
48 #define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \
49 __LINE__ , ## __VA_ARGS__)
52 * Various helper functions
54 int
55 mb_init(struct mbchain *mbp)
57 struct mbuf *m;
59 m = m_gethdr(M_WAIT, MT_DATA);
60 m->m_len = 0;
61 mb_initm(mbp, m);
62 return 0;
65 void
66 mb_initm(struct mbchain *mbp, struct mbuf *m)
68 bzero(mbp, sizeof(*mbp));
69 mbp->mb_top = mbp->mb_cur = m;
70 mbp->mb_mleft = M_TRAILINGSPACE(m);
73 void
74 mb_done(struct mbchain *mbp)
76 if (mbp->mb_top) {
77 m_freem(mbp->mb_top);
78 mbp->mb_top = NULL;
82 struct mbuf *
83 mb_detach(struct mbchain *mbp)
85 struct mbuf *m;
87 m = mbp->mb_top;
88 mbp->mb_top = NULL;
89 return m;
92 int
93 mb_fixhdr(struct mbchain *mbp)
95 return mbp->mb_top->m_pkthdr.len = m_fixhdr(mbp->mb_top);
99 * Check if object of size 'size' fit to the current position and
100 * allocate new mbuf if not. Advance pointers and increase length of mbuf(s).
101 * Return pointer to the object placeholder or NULL if any error occured.
102 * Note: size should be <= MLEN
104 caddr_t
105 mb_reserve(struct mbchain *mbp, int size)
107 struct mbuf *m, *mn;
108 caddr_t bpos;
110 if (size > MLEN)
111 panic("mb_reserve: size = %d\n", size);
112 m = mbp->mb_cur;
113 if (mbp->mb_mleft < size) {
114 mn = m_get(M_WAIT, MT_DATA);
115 mbp->mb_cur = m->m_next = mn;
116 m = mn;
117 m->m_len = 0;
118 mbp->mb_mleft = M_TRAILINGSPACE(m);
120 mbp->mb_mleft -= size;
121 mbp->mb_count += size;
122 bpos = mtod(m, caddr_t) + m->m_len;
123 m->m_len += size;
124 return bpos;
128 mb_put_uint8(struct mbchain *mbp, u_int8_t x)
130 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
134 mb_put_uint16be(struct mbchain *mbp, u_int16_t x)
136 x = htobe16(x);
137 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
141 mb_put_uint16le(struct mbchain *mbp, u_int16_t x)
143 x = htole16(x);
144 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
148 mb_put_uint32be(struct mbchain *mbp, u_int32_t x)
150 x = htobe32(x);
151 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
155 mb_put_uint32le(struct mbchain *mbp, u_int32_t x)
157 x = htole32(x);
158 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
162 mb_put_int64be(struct mbchain *mbp, int64_t x)
164 x = htobe64(x);
165 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
169 mb_put_int64le(struct mbchain *mbp, int64_t x)
171 x = htole64(x);
172 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM);
176 mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type)
178 struct mbuf *m;
179 caddr_t dst;
180 c_caddr_t src;
181 int cplen, error, mleft, count;
182 size_t srclen, dstlen;
184 m = mbp->mb_cur;
185 mleft = mbp->mb_mleft;
187 while (size > 0) {
188 if (mleft == 0) {
189 if (m->m_next == NULL)
190 m = m_getm(m, size, M_WAIT, MT_DATA);
191 else
192 m = m->m_next;
193 mleft = M_TRAILINGSPACE(m);
194 continue;
196 cplen = mleft > size ? size : mleft;
197 srclen = dstlen = cplen;
198 dst = mtod(m, caddr_t) + m->m_len;
199 switch (type) {
200 case MB_MCUSTOM:
201 srclen = size;
202 dstlen = mleft;
203 error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen);
204 if (error)
205 return error;
206 break;
207 case MB_MINLINE:
208 for (src = source, count = cplen; count; count--)
209 *dst++ = *src++;
210 break;
211 case MB_MSYSTEM:
212 bcopy(source, dst, cplen);
213 break;
214 case MB_MUSER:
215 error = copyin(source, dst, cplen);
216 if (error)
217 return error;
218 break;
219 case MB_MZERO:
220 bzero(dst, cplen);
221 break;
223 size -= srclen;
224 source += srclen;
225 m->m_len += dstlen;
226 mleft -= dstlen;
227 mbp->mb_count += dstlen;
229 mbp->mb_cur = m;
230 mbp->mb_mleft = mleft;
231 return 0;
235 mb_put_mbuf(struct mbchain *mbp, struct mbuf *m)
237 mbp->mb_cur->m_next = m;
238 while (m) {
239 mbp->mb_count += m->m_len;
240 if (m->m_next == NULL)
241 break;
242 m = m->m_next;
244 mbp->mb_mleft = M_TRAILINGSPACE(m);
245 mbp->mb_cur = m;
246 return 0;
250 * copies a uio scatter/gather list to an mbuf chain.
253 mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size)
255 long left;
256 int mtype, error;
258 mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
260 while (size > 0 && uiop->uio_resid) {
261 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
262 return EFBIG;
263 left = uiop->uio_iov->iov_len;
264 if (left == 0) {
265 uiop->uio_iov++;
266 uiop->uio_iovcnt--;
267 continue;
269 if (left > size)
270 left = size;
271 error = mb_put_mem(mbp, uiop->uio_iov->iov_base, left, mtype);
272 if (error)
273 return error;
274 uiop->uio_offset += left;
275 uiop->uio_resid -= left;
276 uiop->uio_iov->iov_base =
277 (char *)uiop->uio_iov->iov_base + left;
278 uiop->uio_iov->iov_len -= left;
279 size -= left;
281 return 0;
285 * Routines for fetching data from an mbuf chain
288 md_init(struct mdchain *mdp)
290 struct mbuf *m;
292 m = m_gethdr(M_WAIT, MT_DATA);
293 m->m_len = 0;
294 md_initm(mdp, m);
295 return 0;
298 void
299 md_initm(struct mdchain *mdp, struct mbuf *m)
301 bzero(mdp, sizeof(*mdp));
302 mdp->md_top = mdp->md_cur = m;
303 mdp->md_pos = mtod(m, u_char*);
306 void
307 md_done(struct mdchain *mdp)
309 if (mdp->md_top) {
310 m_freem(mdp->md_top);
311 mdp->md_top = NULL;
316 * Append a separate mbuf chain. It is caller responsibility to prevent
317 * multiple calls to fetch/record routines.
319 void
320 md_append_record(struct mdchain *mdp, struct mbuf *top)
322 struct mbuf *m;
324 if (mdp->md_top == NULL) {
325 md_initm(mdp, top);
326 return;
328 m = mdp->md_top;
329 while (m->m_nextpkt)
330 m = m->m_nextpkt;
331 m->m_nextpkt = top;
332 top->m_nextpkt = NULL;
333 return;
337 * Put next record in place of existing
340 md_next_record(struct mdchain *mdp)
342 struct mbuf *m;
344 if (mdp->md_top == NULL)
345 return ENOENT;
346 m = mdp->md_top->m_nextpkt;
347 md_done(mdp);
348 if (m == NULL)
349 return ENOENT;
350 md_initm(mdp, m);
351 return 0;
355 md_get_uint8(struct mdchain *mdp, u_int8_t *x)
357 return md_get_mem(mdp, x, 1, MB_MINLINE);
361 md_get_uint16(struct mdchain *mdp, u_int16_t *x)
363 return md_get_mem(mdp, (caddr_t)x, 2, MB_MINLINE);
367 md_get_uint16le(struct mdchain *mdp, u_int16_t *x)
369 u_int16_t v;
370 int error = md_get_uint16(mdp, &v);
372 if (x != NULL)
373 *x = le16toh(v);
374 return error;
378 md_get_uint16be(struct mdchain *mdp, u_int16_t *x) {
379 u_int16_t v;
380 int error = md_get_uint16(mdp, &v);
382 if (x != NULL)
383 *x = be16toh(v);
384 return error;
388 md_get_uint32(struct mdchain *mdp, u_int32_t *x)
390 return md_get_mem(mdp, (caddr_t)x, 4, MB_MINLINE);
394 md_get_uint32be(struct mdchain *mdp, u_int32_t *x)
396 u_int32_t v;
397 int error;
399 error = md_get_uint32(mdp, &v);
400 if (x != NULL)
401 *x = be32toh(v);
402 return error;
406 md_get_uint32le(struct mdchain *mdp, u_int32_t *x)
408 u_int32_t v;
409 int error;
411 error = md_get_uint32(mdp, &v);
412 if (x != NULL)
413 *x = le32toh(v);
414 return error;
418 md_get_int64(struct mdchain *mdp, int64_t *x)
420 return md_get_mem(mdp, (caddr_t)x, 8, MB_MINLINE);
424 md_get_int64be(struct mdchain *mdp, int64_t *x)
426 int64_t v;
427 int error;
429 error = md_get_int64(mdp, &v);
430 if (x != NULL)
431 *x = be64toh(v);
432 return error;
436 md_get_int64le(struct mdchain *mdp, int64_t *x)
438 int64_t v;
439 int error;
441 error = md_get_int64(mdp, &v);
442 if (x != NULL)
443 *x = le64toh(v);
444 return error;
448 md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type)
450 struct mbuf *m = mdp->md_cur;
451 int error;
452 u_int count;
453 u_char *s;
455 while (size > 0) {
456 if (m == NULL) {
457 MBERROR("incomplete copy\n");
458 return EBADRPC;
460 s = mdp->md_pos;
461 count = mtod(m, u_char*) + m->m_len - s;
462 if (count == 0) {
463 mdp->md_cur = m = m->m_next;
464 if (m)
465 s = mdp->md_pos = mtod(m, caddr_t);
466 continue;
468 if (count > size)
469 count = size;
470 size -= count;
471 mdp->md_pos += count;
472 if (target == NULL)
473 continue;
474 switch (type) {
475 case MB_MUSER:
476 error = copyout(s, target, count);
477 if (error)
478 return error;
479 break;
480 case MB_MSYSTEM:
481 bcopy(s, target, count);
482 break;
483 case MB_MINLINE:
484 while (count--)
485 *target++ = *s++;
486 continue;
488 target += count;
490 return 0;
494 md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **ret)
496 struct mbuf *m = mdp->md_cur, *rm;
498 rm = m_copym(m, mdp->md_pos - mtod(m, u_char*), size, M_WAIT);
499 md_get_mem(mdp, NULL, size, MB_MZERO);
500 *ret = rm;
501 return 0;
505 md_get_uio(struct mdchain *mdp, struct uio *uiop, int size)
507 char *uiocp;
508 long left;
509 int mtype, error;
511 mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
512 while (size > 0 && uiop->uio_resid) {
513 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
514 return EFBIG;
515 left = uiop->uio_iov->iov_len;
516 if (left == 0) {
517 uiop->uio_iov++;
518 uiop->uio_iovcnt--;
519 continue;
521 uiocp = uiop->uio_iov->iov_base;
522 if (left > size)
523 left = size;
524 error = md_get_mem(mdp, uiocp, left, mtype);
525 if (error)
526 return error;
527 uiop->uio_offset += left;
528 uiop->uio_resid -= left;
529 uiop->uio_iov->iov_base =
530 (char *)uiop->uio_iov->iov_base + left;
531 uiop->uio_iov->iov_len -= left;
532 size -= left;
534 return 0;