xref: /illumos-gate/usr/src/lib/efcode/engine/init.c (revision 68ac2337)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*68ac2337Sjl  * Common Development and Distribution License (the "License").
6*68ac2337Sjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*68ac2337Sjl  * Copyright 2007 Sun Microsystems, Inc.   All rights reserved.
23*68ac2337Sjl  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <fcode/private.h>
337c478bd9Sstevel@tonic-gate #include <fcode/log.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate fcode_env_t *initial_env = 0;
36*68ac2337Sjl int dict_size = 0x4000000;	/* 64Mb, hopefully big enough... */
377c478bd9Sstevel@tonic-gate int stack_size = 0x200;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate void *
safe_malloc(size_t n,char * f,int l)407c478bd9Sstevel@tonic-gate safe_malloc(size_t n, char *f, int l)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	void *p;
437c478bd9Sstevel@tonic-gate 
44*68ac2337Sjl 	p = malloc((size_t)n);
457c478bd9Sstevel@tonic-gate #if defined(__sparcv9)
467c478bd9Sstevel@tonic-gate 	/*
477c478bd9Sstevel@tonic-gate 	 * For Ultrasparc, we must force addresses to be less than 4Gb,
487c478bd9Sstevel@tonic-gate 	 * since Fcode assumes that addresses can be stored in 32 bits.
497c478bd9Sstevel@tonic-gate 	 * To get around this would require turning all addresses into
507c478bd9Sstevel@tonic-gate 	 * cookies, which is a lot of work.
517c478bd9Sstevel@tonic-gate 	 */
527c478bd9Sstevel@tonic-gate 	if (((uint64_t)p) >= 0x100000000) {
537c478bd9Sstevel@tonic-gate 		log_message(MSG_WARN, "Malloc returned address > 4Gb\n");
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
567c478bd9Sstevel@tonic-gate 	if (p) {
57*68ac2337Sjl 		memset(p, 0, (size_t)n);
587c478bd9Sstevel@tonic-gate 	} else
597c478bd9Sstevel@tonic-gate 		log_message(MSG_ERROR, "%s:%d:Malloc(%llx) failed\n", f, l,
607c478bd9Sstevel@tonic-gate 		    (uint64_t)n);
617c478bd9Sstevel@tonic-gate 	return (p);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate void *
safe_realloc(void * p,size_t n,char * f,int l)657c478bd9Sstevel@tonic-gate safe_realloc(void *p, size_t n, char *f, int l)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	void *newp;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if ((newp = safe_malloc(n, f, l)) == NULL) {
707c478bd9Sstevel@tonic-gate 		log_message(MSG_ERROR, "%s:%d:realloc(%p, %x) failed\n", f, l,
717c478bd9Sstevel@tonic-gate 		    p, n);
727c478bd9Sstevel@tonic-gate 		safe_free(p, f, l);
737c478bd9Sstevel@tonic-gate 		return (NULL);
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 	if (p) {
767c478bd9Sstevel@tonic-gate 		memcpy(newp, p, n);
777c478bd9Sstevel@tonic-gate 		safe_free(p, f, l);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	return (newp);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
safe_free(void * p,char * f,int l)837c478bd9Sstevel@tonic-gate safe_free(void *p, char *f, int l)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	if (p) {
867c478bd9Sstevel@tonic-gate 		free(p);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate char *
safe_strdup(char * s,char * f,int l)917c478bd9Sstevel@tonic-gate safe_strdup(char *s, char *f, int l)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	char *p = strdup(s);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	return (p);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #pragma init(_init)
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static void
_init(void)1017c478bd9Sstevel@tonic-gate _init(void)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	int i;
1047c478bd9Sstevel@tonic-gate 	acf_t f_error_addr;
1057c478bd9Sstevel@tonic-gate 	fcode_env_t *env;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	NOTICE;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	fcode_impl_count = 0;
1107c478bd9Sstevel@tonic-gate 	env = MALLOC(sizeof (fcode_env_t));
1117c478bd9Sstevel@tonic-gate 	env->table = MALLOC((MAX_FCODE + 1) * sizeof (fcode_token));
1127c478bd9Sstevel@tonic-gate 	env->base = MALLOC(dict_size);
1137c478bd9Sstevel@tonic-gate 	env->here = env->base;
1147c478bd9Sstevel@tonic-gate 	env->ds = env->ds0 = MALLOC(stack_size * sizeof (fstack_t));
1157c478bd9Sstevel@tonic-gate 	env->rs = env->rs0 = MALLOC(stack_size * sizeof (fstack_t));
1167c478bd9Sstevel@tonic-gate 	env->order = MALLOC(MAX_ORDER * sizeof (token_t));
1177c478bd9Sstevel@tonic-gate 	env->input = MALLOC(sizeof (input_typ));
1187c478bd9Sstevel@tonic-gate 	env->num_base = 0x10;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/* Setup the initial forth environment */
1217c478bd9Sstevel@tonic-gate 	do_forth(env);
1227c478bd9Sstevel@tonic-gate 	do_definitions(env);
1237c478bd9Sstevel@tonic-gate 	install_handlers(env);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	initial_env = env;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * Need to define this early because it is the default for
1297c478bd9Sstevel@tonic-gate 	 * all unimpl, FCODE functions
1307c478bd9Sstevel@tonic-gate 	 */
1317c478bd9Sstevel@tonic-gate 	P1275(0x0fc, IMMEDIATE,	"ferror",		f_error);
1327c478bd9Sstevel@tonic-gate 	f_error_addr = LINK_TO_ACF(env->lastlink);
1337c478bd9Sstevel@tonic-gate 	for (i = 0; i <= MAX_FCODE; i++) {
1347c478bd9Sstevel@tonic-gate 		DEBUGF(ANY, env->table[i].usage = 0);
1357c478bd9Sstevel@tonic-gate 		SET_TOKEN(i, IMMEDIATE, "ferror", f_error_addr);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	fcode_impl_count = 0;
1387c478bd9Sstevel@tonic-gate }
139