xref: /freebsd/stand/i386/libi386/bio.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
175772fa2SToomas Soome /*-
2*46aedfa2SToomas Soome  * Copyright 2018 Toomas Soome <tsoome@me.com>
3*46aedfa2SToomas Soome  *
475772fa2SToomas Soome  * Redistribution and use in source and binary forms, with or without
575772fa2SToomas Soome  * modification, are permitted provided that the following conditions
675772fa2SToomas Soome  * are met:
775772fa2SToomas Soome  * 1. Redistributions of source code must retain the above copyright
875772fa2SToomas Soome  *    notice, this list of conditions and the following disclaimer.
975772fa2SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
1075772fa2SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
1175772fa2SToomas Soome  *    documentation and/or other materials provided with the distribution.
1275772fa2SToomas Soome  *
1375772fa2SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1475772fa2SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1575772fa2SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1675772fa2SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1775772fa2SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1875772fa2SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1975772fa2SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2075772fa2SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2175772fa2SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2275772fa2SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2375772fa2SToomas Soome  * SUCH DAMAGE.
2475772fa2SToomas Soome  */
2575772fa2SToomas Soome 
2675772fa2SToomas Soome #include <stand.h>
2775772fa2SToomas Soome #include "libi386.h"
2875772fa2SToomas Soome 
2975772fa2SToomas Soome /*
3075772fa2SToomas Soome  * The idea is borrowed from pxe.c and zfsimpl.c. The original buffer
3175772fa2SToomas Soome  * space in pxe.c was 2x 0x2000. Allocating it from BSS will give us needed
3275772fa2SToomas Soome  * memory below 1MB and usable for real mode calls.
3375772fa2SToomas Soome  *
3475772fa2SToomas Soome  * Note the allocations and frees are to be done in reverse order (LIFO).
3575772fa2SToomas Soome  */
3675772fa2SToomas Soome 
3775772fa2SToomas Soome static char bio_buffer[BIO_BUFFER_SIZE];
3875772fa2SToomas Soome static char *bio_buffer_end = bio_buffer + BIO_BUFFER_SIZE;
3975772fa2SToomas Soome static char *bio_buffer_ptr = bio_buffer;
4075772fa2SToomas Soome 
4175772fa2SToomas Soome void *
bio_alloc(size_t size)4275772fa2SToomas Soome bio_alloc(size_t size)
4375772fa2SToomas Soome {
4475772fa2SToomas Soome 	char *ptr;
4575772fa2SToomas Soome 
4675772fa2SToomas Soome 	ptr = bio_buffer_ptr;
4775772fa2SToomas Soome 	if (ptr + size > bio_buffer_end)
4875772fa2SToomas Soome 		return (NULL);
4975772fa2SToomas Soome 	bio_buffer_ptr += size;
5075772fa2SToomas Soome 
5175772fa2SToomas Soome 	return (ptr);
5275772fa2SToomas Soome }
5375772fa2SToomas Soome 
5475772fa2SToomas Soome void
bio_free(void * ptr,size_t size)5575772fa2SToomas Soome bio_free(void *ptr, size_t size)
5675772fa2SToomas Soome {
5775772fa2SToomas Soome 
5875772fa2SToomas Soome 	if (ptr == NULL)
5975772fa2SToomas Soome 		return;
6075772fa2SToomas Soome 
6175772fa2SToomas Soome 	bio_buffer_ptr -= size;
6275772fa2SToomas Soome 	if (bio_buffer_ptr != ptr)
6375772fa2SToomas Soome 		panic("bio_alloc()/bio_free() mismatch\n");
6475772fa2SToomas Soome }
65