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