xref: /illumos-gate/usr/src/boot/i386/libi386/bio.c (revision 22028508)
13273f292SToomas Soome /*
23273f292SToomas Soome  * Copyright 2018 Toomas Soome <tsoome@me.com>
33273f292SToomas Soome  *
43273f292SToomas Soome  * Redistribution and use in source and binary forms, with or without
53273f292SToomas Soome  * modification, are permitted provided that the following conditions
63273f292SToomas Soome  * are met:
73273f292SToomas Soome  * 1. Redistributions of source code must retain the above copyright
83273f292SToomas Soome  *    notice, this list of conditions and the following disclaimer.
93273f292SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
103273f292SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
113273f292SToomas Soome  *    documentation and/or other materials provided with the distribution.
123273f292SToomas Soome  *
133273f292SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
143273f292SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
153273f292SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
163273f292SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
173273f292SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
183273f292SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
193273f292SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
203273f292SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
213273f292SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
223273f292SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
233273f292SToomas Soome  * SUCH DAMAGE.
243273f292SToomas Soome  */
253273f292SToomas Soome 
263273f292SToomas Soome #include <sys/cdefs.h>
273273f292SToomas Soome #include <stand.h>
283273f292SToomas Soome #include <libi386.h>
293273f292SToomas Soome 
303273f292SToomas Soome /*
313273f292SToomas Soome  * The idea is borrowed from pxe.c and zfsimpl.c. The original buffer
323273f292SToomas Soome  * space in pxe.c was 2x 0x2000. Allocating it from BSS will give us needed
333273f292SToomas Soome  * memory below 1MB and usable for real mode calls.
343273f292SToomas Soome  *
353273f292SToomas Soome  * Note the allocations and frees are to be done in reverse order (LIFO).
363273f292SToomas Soome  */
373273f292SToomas Soome 
383273f292SToomas Soome #define	BIO_BUFFER_SIZE	0x4000
393273f292SToomas Soome static char bio_buffer[BIO_BUFFER_SIZE];
403273f292SToomas Soome static char *bio_buffer_end = bio_buffer + BIO_BUFFER_SIZE;
413273f292SToomas Soome static char *bio_buffer_ptr = bio_buffer;
423273f292SToomas Soome 
433273f292SToomas Soome void *
bio_alloc(size_t size)443273f292SToomas Soome bio_alloc(size_t size)
453273f292SToomas Soome {
463273f292SToomas Soome 	char *ptr;
473273f292SToomas Soome 
483273f292SToomas Soome 	ptr = bio_buffer_ptr;
493273f292SToomas Soome 	if (ptr + size > bio_buffer_end)
503273f292SToomas Soome 		return (NULL);
513273f292SToomas Soome 	bio_buffer_ptr += size;
523273f292SToomas Soome 
533273f292SToomas Soome 	return (ptr);
543273f292SToomas Soome }
553273f292SToomas Soome 
563273f292SToomas Soome void
bio_free(void * ptr,size_t size)573273f292SToomas Soome bio_free(void *ptr, size_t size)
583273f292SToomas Soome {
593273f292SToomas Soome 
603273f292SToomas Soome 	if (ptr == NULL)
613273f292SToomas Soome 		return;
623273f292SToomas Soome 
633273f292SToomas Soome 	bio_buffer_ptr -= size;
643273f292SToomas Soome 	if (bio_buffer_ptr != ptr)
6596cf0467SToomas Soome 		panic("bio_alloc()/bio_free() mismatch");
663273f292SToomas Soome }
67