xref: /onnv-gate/usr/src/lib/efcode/engine/init.c (revision 3354:d1bcdcd4c87a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3354Sjl139090  * Common Development and Distribution License (the "License").
6*3354Sjl139090  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3354Sjl139090  * Copyright 2007 Sun Microsystems, Inc.   All rights reserved.
23*3354Sjl139090  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <fcode/private.h>
330Sstevel@tonic-gate #include <fcode/log.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate fcode_env_t *initial_env = 0;
36*3354Sjl139090 int dict_size = 0x4000000;	/* 64Mb, hopefully big enough... */
370Sstevel@tonic-gate int stack_size = 0x200;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate void *
safe_malloc(size_t n,char * f,int l)400Sstevel@tonic-gate safe_malloc(size_t n, char *f, int l)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	void *p;
430Sstevel@tonic-gate 
44*3354Sjl139090 	p = malloc((size_t)n);
450Sstevel@tonic-gate #if defined(__sparcv9)
460Sstevel@tonic-gate 	/*
470Sstevel@tonic-gate 	 * For Ultrasparc, we must force addresses to be less than 4Gb,
480Sstevel@tonic-gate 	 * since Fcode assumes that addresses can be stored in 32 bits.
490Sstevel@tonic-gate 	 * To get around this would require turning all addresses into
500Sstevel@tonic-gate 	 * cookies, which is a lot of work.
510Sstevel@tonic-gate 	 */
520Sstevel@tonic-gate 	if (((uint64_t)p) >= 0x100000000) {
530Sstevel@tonic-gate 		log_message(MSG_WARN, "Malloc returned address > 4Gb\n");
540Sstevel@tonic-gate 	}
550Sstevel@tonic-gate #endif	/* __sparcv9 */
560Sstevel@tonic-gate 	if (p) {
57*3354Sjl139090 		memset(p, 0, (size_t)n);
580Sstevel@tonic-gate 	} else
590Sstevel@tonic-gate 		log_message(MSG_ERROR, "%s:%d:Malloc(%llx) failed\n", f, l,
600Sstevel@tonic-gate 		    (uint64_t)n);
610Sstevel@tonic-gate 	return (p);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate 
640Sstevel@tonic-gate void *
safe_realloc(void * p,size_t n,char * f,int l)650Sstevel@tonic-gate safe_realloc(void *p, size_t n, char *f, int l)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	void *newp;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if ((newp = safe_malloc(n, f, l)) == NULL) {
700Sstevel@tonic-gate 		log_message(MSG_ERROR, "%s:%d:realloc(%p, %x) failed\n", f, l,
710Sstevel@tonic-gate 		    p, n);
720Sstevel@tonic-gate 		safe_free(p, f, l);
730Sstevel@tonic-gate 		return (NULL);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	if (p) {
760Sstevel@tonic-gate 		memcpy(newp, p, n);
770Sstevel@tonic-gate 		safe_free(p, f, l);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	return (newp);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate void
safe_free(void * p,char * f,int l)830Sstevel@tonic-gate safe_free(void *p, char *f, int l)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	if (p) {
860Sstevel@tonic-gate 		free(p);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate char *
safe_strdup(char * s,char * f,int l)910Sstevel@tonic-gate safe_strdup(char *s, char *f, int l)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	char *p = strdup(s);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	return (p);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #pragma init(_init)
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static void
_init(void)1010Sstevel@tonic-gate _init(void)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	int i;
1040Sstevel@tonic-gate 	acf_t f_error_addr;
1050Sstevel@tonic-gate 	fcode_env_t *env;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	NOTICE;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	fcode_impl_count = 0;
1100Sstevel@tonic-gate 	env = MALLOC(sizeof (fcode_env_t));
1110Sstevel@tonic-gate 	env->table = MALLOC((MAX_FCODE + 1) * sizeof (fcode_token));
1120Sstevel@tonic-gate 	env->base = MALLOC(dict_size);
1130Sstevel@tonic-gate 	env->here = env->base;
1140Sstevel@tonic-gate 	env->ds = env->ds0 = MALLOC(stack_size * sizeof (fstack_t));
1150Sstevel@tonic-gate 	env->rs = env->rs0 = MALLOC(stack_size * sizeof (fstack_t));
1160Sstevel@tonic-gate 	env->order = MALLOC(MAX_ORDER * sizeof (token_t));
1170Sstevel@tonic-gate 	env->input = MALLOC(sizeof (input_typ));
1180Sstevel@tonic-gate 	env->num_base = 0x10;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/* Setup the initial forth environment */
1210Sstevel@tonic-gate 	do_forth(env);
1220Sstevel@tonic-gate 	do_definitions(env);
1230Sstevel@tonic-gate 	install_handlers(env);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	initial_env = env;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/*
1280Sstevel@tonic-gate 	 * Need to define this early because it is the default for
1290Sstevel@tonic-gate 	 * all unimpl, FCODE functions
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	P1275(0x0fc, IMMEDIATE,	"ferror",		f_error);
1320Sstevel@tonic-gate 	f_error_addr = LINK_TO_ACF(env->lastlink);
1330Sstevel@tonic-gate 	for (i = 0; i <= MAX_FCODE; i++) {
1340Sstevel@tonic-gate 		DEBUGF(ANY, env->table[i].usage = 0);
1350Sstevel@tonic-gate 		SET_TOKEN(i, IMMEDIATE, "ferror", f_error_addr);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	fcode_impl_count = 0;
1380Sstevel@tonic-gate }
139