Put assembly routines in immobile space if it exists
[sbcl.git] / src / runtime / runtime.c
blobdd689b88532a1a8fab4280127babb6561a1155a9
1 /*
2 * main() entry point for a stand-alone SBCL image
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
16 #include "sbcl.h"
18 #include <stdio.h>
19 #include <string.h>
20 #include <ctype.h>
21 #ifndef LISP_FEATURE_WIN32
22 #include <libgen.h>
23 #endif
24 #include <sys/types.h>
25 #ifndef LISP_FEATURE_WIN32
26 #include <sys/wait.h>
27 #endif
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/file.h>
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include "runtime.h"
34 #ifndef LISP_FEATURE_WIN32
35 #include <sched.h>
36 #endif
37 #include <errno.h>
38 #include <locale.h>
39 #include <limits.h>
41 #if defined(SVR4) || defined(__linux__)
42 #include <time.h>
43 #endif
45 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
46 #include "signal.h"
47 #endif
49 #include "runtime.h"
50 #include "vars.h"
51 #include "globals.h"
52 #include "os.h"
53 #include "interr.h"
54 #include "alloc.h"
55 #include "interrupt.h"
56 #include "arch.h"
57 #include "gc.h"
58 #include "validate.h"
59 #include "core.h"
60 #include "save.h"
61 #include "lispregs.h"
62 #include "thread.h"
64 #include "genesis/static-symbols.h"
65 #include "genesis/symbol.h"
68 #ifdef irix
69 #include <string.h>
70 #include "interr.h"
71 #endif
73 extern char *sbcl_home;
74 char *sbcl_home = SBCL_PREFIX"/lib/sbcl/";
76 #ifdef LISP_FEATURE_HPUX
77 extern void *return_from_lisp_stub;
78 #include "genesis/closure.h"
79 #include "genesis/simple-fun.h"
80 #endif
83 /* SIGINT handler that invokes the monitor (for when Lisp isn't up to it) */
84 static void
85 sigint_handler(int signal, siginfo_t *info, os_context_t *context)
87 lose("\nSIGINT hit at 0x%08lX\n",
88 (unsigned long) *os_context_pc_addr(context));
91 /* (This is not static, because we want to be able to call it from
92 * Lisp land.) */
93 void
94 sigint_init(void)
96 SHOW("entering sigint_init()");
97 install_handler(SIGINT, sigint_handler, 1);
98 SHOW("leaving sigint_init()");
102 * helper functions for dealing with command line args
105 void *
106 successful_malloc(size_t size)
108 void* result = malloc(size);
109 if (0 == result) {
110 lose("malloc failure\n");
111 } else {
112 return result;
114 return (void *) NULL; /* dummy value: return something ... */
117 char *
118 copied_string(char *string)
120 return strcpy(successful_malloc(1+strlen(string)), string);
123 char *
124 copied_existing_filename_or_null(char *filename)
126 struct stat filename_stat;
127 if (stat(filename, &filename_stat)) { /* if failure */
128 return 0;
129 } else {
130 return copied_string(filename);
134 #ifndef LISP_FEATURE_WIN32
135 char *
136 copied_realpath(const char *pathname)
138 char *messy, *tidy;
139 size_t len;
141 /* realpath() supposedly can't be counted on to always return
142 * an absolute path, so we prepend the cwd to relative paths */
143 messy = NULL;
144 if (pathname[0] != '/') {
145 messy = successful_malloc(PATH_MAX + 1);
146 if (getcwd(messy, PATH_MAX + 1) == NULL) {
147 free(messy);
148 return NULL;
150 len = strlen(messy);
151 snprintf(messy + len, PATH_MAX + 1 - len, "/%s", pathname);
154 tidy = successful_malloc(PATH_MAX + 1);
155 if (realpath((messy ? messy : pathname), tidy) == NULL) {
156 free(messy);
157 free(tidy);
158 return NULL;
161 return tidy;
163 #endif /* LISP_FEATURE_WIN32 */
165 /* miscellaneous chattiness */
167 void
168 print_help()
170 puts(
171 "Usage: sbcl [runtime-options] [toplevel-options] [user-options]\n\
172 Common runtime options:\n\
173 --help Print this message and exit.\n\
174 --version Print version information and exit.\n\
175 --core <filename> Use the specified core file instead of the default.\n\
176 --dynamic-space-size <MiB> Size of reserved dynamic space in megabytes.\n\
177 --control-stack-size <MiB> Size of reserved control stack in megabytes.\n\
179 Common toplevel options:\n\
180 --sysinit <filename> System-wide init-file to use instead of default.\n\
181 --userinit <filename> Per-user init-file to use instead of default.\n\
182 --no-sysinit Inhibit processing of any system-wide init-file.\n\
183 --no-userinit Inhibit processing of any per-user init-file.\n\
184 --disable-debugger Invoke sb-ext:disable-debugger.\n\
185 --noprint Run a Read-Eval Loop without printing results.\n\
186 --script [<filename>] Skip #! line, disable debugger, avoid verbosity.\n\
187 --quit Exit with code 0 after option processing.\n\
188 --non-interactive Sets both --quit and --disable-debugger.\n\
189 Common toplevel options that are processed in order:\n\
190 --eval <form> Form to eval when processing this option.\n\
191 --load <filename> File to load when processing this option.\n\
193 User options are not processed by SBCL. All runtime options must\n\
194 appear before toplevel options, and all toplevel options must\n\
195 appear before user options.\n\
197 For more information please refer to the SBCL User Manual, which\n\
198 should be installed along with SBCL, and is also available from the\n\
199 website <http://www.sbcl.org/>.\n");
202 void
203 print_version()
205 printf("SBCL %s\n", SBCL_VERSION_STRING);
208 void
209 print_banner()
211 printf(
212 "This is SBCL %s, an implementation of ANSI Common Lisp.\n\
213 More information about SBCL is available at <http://www.sbcl.org/>.\n\
215 SBCL is free software, provided as is, with absolutely no warranty.\n\
216 It is mostly in the public domain; some portions are provided under\n\
217 BSD-style licenses. See the CREDITS and COPYING files in the\n\
218 distribution for more information.\n\
220 #ifdef LISP_FEATURE_WIN32
221 "\n\
222 WARNING: the Windows port is fragile, particularly for multithreaded\n\
223 code. Unfortunately, the development team currently lacks the time\n\
224 and resources this platform demands.\n\
226 #endif
227 , SBCL_VERSION_STRING);
230 /* Look for a core file to load, first in the directory named by the
231 * SBCL_HOME environment variable, then in a hardcoded default
232 * location. Returns a malloced copy of the core filename. */
233 char *
234 search_for_core ()
236 char *env_sbcl_home = getenv("SBCL_HOME");
237 char *lookhere;
238 char *stem = "/sbcl.core";
239 char *core;
241 if (!(env_sbcl_home && *env_sbcl_home))
242 env_sbcl_home = sbcl_home;
243 lookhere = (char *) calloc(strlen(env_sbcl_home) +
244 strlen(stem) +
246 sizeof(char));
247 sprintf(lookhere, "%s%s", env_sbcl_home, stem);
248 core = copied_existing_filename_or_null(lookhere);
250 if (!core) {
251 lose("can't find core file at %s\n", lookhere);
254 free(lookhere);
256 return core;
259 /* Try to find the path to an executable from argv[0], this is only
260 * used when os_get_runtime_executable_path() returns NULL */
261 #ifdef LISP_FEATURE_WIN32
262 char *
263 search_for_executable(const char *argv0)
265 return NULL;
267 #else /* LISP_FEATURE_WIN32 */
268 char *
269 search_for_executable(const char *argv0)
271 char *search, *start, *end, *buf;
273 /* If argv[0] contains a slash then it's probably an absolute path
274 * or relative to the current directory, so check if it exists. */
275 if (strchr(argv0, '/') != NULL && access(argv0, F_OK) == 0)
276 return copied_realpath(argv0);
278 /* Bail on an absolute path which doesn't exist */
279 if (argv0[0] == '/')
280 return NULL;
282 /* Otherwise check if argv[0] exists relative to any directory in PATH */
283 search = getenv("PATH");
284 if (search == NULL)
285 return NULL;
286 search = copied_string(search);
287 buf = successful_malloc(PATH_MAX + 1);
288 for (start = search; (end = strchr(start, ':')) != NULL; start = end + 1) {
289 *end = '\0';
290 snprintf(buf, PATH_MAX + 1, "%s/%s", start, argv0);
291 if (access(buf, F_OK) == 0) {
292 free(search);
293 search = copied_realpath(buf);
294 free(buf);
295 return search;
298 /* The above for-loop fails to process the last part of PATH if PATH does
299 * not end with ':'. We may consider appending an extra ':' to the end of
300 * SEARCH. -- houjingyi 2013-05-24 */
301 if (start != NULL && *start != '\0') {
302 snprintf(buf, PATH_MAX + 1, "%s/%s", start, argv0);
303 if (access(buf, F_OK) == 0) {
304 free(search);
305 search = copied_realpath(buf);
306 free(buf);
307 return search;
311 free(search);
312 free(buf);
313 return NULL;
315 #endif /* LISP_FEATURE_WIN32 */
317 size_t
318 parse_size_arg(char *arg, char *arg_name)
320 char *tail, *power_name;
321 size_t power, res;
323 res = strtoul(arg, &tail, 0);
325 if (arg == tail) {
326 lose("%s argument is not a number: %s", arg_name, arg);
327 } else if (tail[0]) {
328 int i, size;
329 power_name = copied_string(tail);
330 size = strlen(power_name);
331 for (i=0; i<size; i++)
332 power_name[i] = toupper(power_name[i]);
333 } else {
334 power = 20;
335 power_name = NULL;
337 if (power_name) {
338 if ((0==strcmp("KB", power_name)) ||
339 (0==strcmp("KIB", power_name))) {
340 power = 10;
341 } else if ((0==strcmp("MB", power_name)) ||
342 (0==strcmp("MIB", power_name))) {
343 power = 20;
344 } else if ((0==strcmp("GB", power_name)) ||
345 (0==strcmp("GIB", power_name))) {
346 power = 30;
347 } else {
348 lose("%s argument has an unknown suffix: %s", arg_name, tail);
350 free(power_name);
352 if ((res <= 0) ||
353 (res >= (SIZE_MAX >> power))) {
354 lose("%s argument is out of range: %s", arg_name, arg);
356 res <<= power;
357 return res;
360 char **posix_argv;
361 char *core_string;
363 struct runtime_options *runtime_options;
365 char *saved_runtime_path = NULL;
366 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
367 void pthreads_win32_init();
368 #endif
370 void print_locale_variable(const char *name)
372 char *value = getenv(name);
374 if (value) {
375 fprintf(stderr, "\n %s=%s", name, value);
379 void setup_locale()
381 if(setlocale(LC_ALL, "") == NULL) {
382 #ifndef LISP_FEATURE_WIN32
384 fprintf(stderr, "WARNING: Setting locale failed.\n");
385 fprintf(stderr, " Check the following variables for correct values:");
387 if (setlocale(LC_CTYPE, "") == NULL) {
388 print_locale_variable("LC_ALL");
389 print_locale_variable("LC_CTYPE");
390 print_locale_variable("LANG");
393 if (setlocale(LC_MESSAGES, "") == NULL) {
394 print_locale_variable("LC_MESSAGES");
396 if (setlocale(LC_COLLATE, "") == NULL) {
397 print_locale_variable("LC_COLLATE");
399 if (setlocale(LC_MONETARY, "") == NULL) {
400 print_locale_variable("LC_MONETARY");
402 if (setlocale(LC_NUMERIC, "") == NULL) {
403 print_locale_variable("LC_NUMERIC");
405 if (setlocale(LC_TIME, "") == NULL) {
406 print_locale_variable("LC_TIME");
408 fprintf(stderr, "\n");
410 #endif
413 void print_environment(int argc, char *argv[])
415 int n = 0;
416 printf("; Commandline arguments:\n");
417 while (n < argc) {
418 printf("; %2d: \"%s\"\n", n, argv[n]);
419 ++n;
421 n = 0;
422 printf(";\n; Environment:\n");
423 while (ENVIRON[n]) {
424 printf("; %2d: \"%s\"\n", n, ENVIRON[n]);
425 ++n;
429 struct lisp_startup_options lisp_startup_options;
431 main(int argc, char *argv[], char *envp[])
433 #ifdef LISP_FEATURE_WIN32
434 /* Exception handling support structure. Evil Win32 hack. */
435 struct lisp_exception_frame exception_frame;
436 #endif
438 /* the name of the core file we're to execute. Note that this is
439 * a malloc'ed string which should be freed eventually. */
440 char *core = 0;
441 char **sbcl_argv = 0;
442 os_vm_offset_t embedded_core_offset = 0;
443 char *runtime_path = 0;
445 /* other command line options */
446 boolean end_runtime_options = 0;
447 boolean disable_lossage_handler_p = 0;
448 boolean debug_environment_p = 0;
450 lispobj initial_function;
452 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
453 os_preinit();
454 pthreads_win32_init();
455 #endif
457 interrupt_init();
458 block_blockable_signals(0);
460 runtime_options = NULL;
462 /* Save the argv[0] derived runtime path in case
463 * os_get_runtime_executable_path(1) isn't able to get an
464 * externally-usable path later on. */
465 saved_runtime_path = search_for_executable(argv[0]);
467 /* Check early to see if this executable has an embedded core,
468 * which also populates runtime_options if the core has runtime
469 * options */
470 runtime_path = os_get_runtime_executable_path(0);
471 if (runtime_path || saved_runtime_path) {
472 os_vm_offset_t offset = search_for_embedded_core(
473 runtime_path ? runtime_path : saved_runtime_path);
474 if (offset != -1) {
475 embedded_core_offset = offset;
476 core = (runtime_path ? runtime_path :
477 copied_string(saved_runtime_path));
478 } else {
479 if (runtime_path)
480 free(runtime_path);
485 /* Parse our part of the command line (aka "runtime options"),
486 * stripping out those options that we handle. */
487 if (runtime_options != NULL) {
488 dynamic_space_size = runtime_options->dynamic_space_size;
489 thread_control_stack_size = runtime_options->thread_control_stack_size;
490 sbcl_argv = argv;
491 } else {
492 int argi = 1;
494 runtime_options = successful_malloc(sizeof(struct runtime_options));
496 while (argi < argc) {
497 char *arg = argv[argi];
498 if (0 == strcmp(arg, "--script")) {
499 /* This is both a runtime and a toplevel option. As a
500 * runtime option, it is equivalent to --noinform.
501 * This exits, and does not increment argi, so that
502 * TOPLEVEL-INIT sees the option. */
503 lisp_startup_options.noinform = 1;
504 end_runtime_options = 1;
505 disable_lossage_handler_p = 1;
506 lose_on_corruption_p = 1;
507 break;
508 } else if (0 == strcmp(arg, "--noinform")) {
509 lisp_startup_options.noinform = 1;
510 ++argi;
511 } else if (0 == strcmp(arg, "--core")) {
512 if (core) {
513 lose("more than one core file specified\n");
514 } else {
515 ++argi;
516 if (argi >= argc) {
517 lose("missing filename for --core argument\n");
519 core = copied_string(argv[argi]);
520 ++argi;
522 } else if (0 == strcmp(arg, "--help")) {
523 /* I think this is the (or a) usual convention: upon
524 * seeing "--help" we immediately print our help
525 * string and exit, ignoring everything else. */
526 print_help();
527 exit(0);
528 } else if (0 == strcmp(arg, "--version")) {
529 /* As in "--help" case, I think this is expected. */
530 print_version();
531 exit(0);
532 } else if (0 == strcmp(arg, "--dynamic-space-size")) {
533 ++argi;
534 if (argi >= argc)
535 lose("missing argument for --dynamic-space-size");
536 dynamic_space_size = parse_size_arg(argv[argi++],
537 "--dynamic-space-size");
538 # ifdef MAX_DYNAMIC_SPACE_END
539 if (!((DYNAMIC_SPACE_START <
540 DYNAMIC_SPACE_START+dynamic_space_size) &&
541 (DYNAMIC_SPACE_START+dynamic_space_size <=
542 MAX_DYNAMIC_SPACE_END))) {
543 char* suffix = "";
544 char* size = argv[argi-1];
545 if (!strchr(size, 'B') && !strchr(size, 'b')) {
546 suffix = " [MB]";
548 lose("--dynamic-space-size argument %s%s is too large, max %lu KB",
549 size, suffix,
550 (MAX_DYNAMIC_SPACE_END-DYNAMIC_SPACE_START) / 1024);
552 # endif
553 } else if (0 == strcmp(arg, "--control-stack-size")) {
554 ++argi;
555 if (argi >= argc)
556 lose("missing argument for --control-stack-size");
557 errno = 0;
558 thread_control_stack_size = parse_size_arg(argv[argi++], "--control-stack-size");
559 } else if (0 == strcmp(arg, "--debug-environment")) {
560 debug_environment_p = 1;
561 ++argi;
562 } else if (0 == strcmp(arg, "--disable-ldb")) {
563 disable_lossage_handler_p = 1;
564 ++argi;
565 } else if (0 == strcmp(arg, "--lose-on-corruption")) {
566 lose_on_corruption_p = 1;
567 ++argi;
568 } else if (0 == strcmp(arg, "--end-runtime-options")) {
569 end_runtime_options = 1;
570 ++argi;
571 break;
572 } else if (0 == strcmp(arg, "--merge-core-pages")) {
573 ++argi;
574 merge_core_pages = 1;
575 } else if (0 == strcmp(arg, "--no-merge-core-pages")) {
576 ++argi;
577 merge_core_pages = 0;
578 } else if (0 == strcmp(arg, "--default-merge-core-pages")) {
579 ++argi;
580 merge_core_pages = -1;
581 } else {
582 /* This option was unrecognized as a runtime option,
583 * so it must be a toplevel option or a user option,
584 * so we must be past the end of the runtime option
585 * section. */
586 break;
589 /* This is where we strip out those options that we handle. We
590 * also take this opportunity to make sure that we don't find
591 * an out-of-place "--end-runtime-options" option. */
593 char *argi0 = argv[argi];
594 int argj = 1;
595 /* (argc - argi) for the arguments, one for the binary,
596 and one for the terminating NULL. */
597 sbcl_argv = successful_malloc((2 + argc - argi) * sizeof(char *));
598 sbcl_argv[0] = argv[0];
599 while (argi < argc) {
600 char *arg = argv[argi++];
601 /* If we encounter --end-runtime-options for the first
602 * time after the point where we had to give up on
603 * runtime options, then the point where we had to
604 * give up on runtime options must've been a user
605 * error. */
606 if (!end_runtime_options &&
607 0 == strcmp(arg, "--end-runtime-options")) {
608 lose("bad runtime option \"%s\"\n", argi0);
610 sbcl_argv[argj++] = arg;
612 sbcl_argv[argj] = 0;
616 /* Align down to multiple of page_table page size, and to the appropriate
617 * stack alignment. */
618 dynamic_space_size &= ~(sword_t)(BACKEND_PAGE_BYTES-1);
619 #ifdef LISP_FEATURE_GENCGC
620 dynamic_space_size &= ~(sword_t)(GENCGC_CARD_BYTES-1);
621 #endif
622 thread_control_stack_size &= ~(sword_t)(CONTROL_STACK_ALIGNMENT_BYTES-1);
624 /* Preserve the runtime options for possible future core saving */
625 runtime_options->dynamic_space_size = dynamic_space_size;
626 runtime_options->thread_control_stack_size = thread_control_stack_size;
628 /* KLUDGE: os_vm_page_size is set by os_init(), and on some
629 * systems (e.g. Alpha) arch_init() needs need os_vm_page_size, so
630 * it must follow os_init(). -- WHN 2000-01-26 */
631 os_init(argv, envp);
632 /* os_init may re-execute the runtime, don't print anything before
633 * that, otherwise it will be duplicated. */
634 if (debug_environment_p) {
635 print_environment(argc, argv);
637 dyndebug_init();
638 arch_init();
639 allocate_spaces();
640 gc_init();
642 setup_locale();
644 /* If no core file was specified, look for one. */
645 if (!core) {
646 core = search_for_core();
649 if (!lisp_startup_options.noinform && embedded_core_offset == 0) {
650 print_banner();
651 fflush(stdout);
654 if (embedded_core_offset == 0) {
655 /* Here we make a last attempt at recognizing an embedded core,
656 * so that a file with an embedded core is a valid argument to
657 * --core. We take care that any decisions on special behaviour
658 * (suppressed banner, embedded options) have already been made
659 * before we reach this block, so that there is no observable
660 * difference between "embedded" and "bare" images given to
661 * --core. */
662 os_vm_offset_t offset = search_for_embedded_core(core);
663 if (offset != -1)
664 embedded_core_offset = offset;
667 globals_init();
669 /* Doing this immediately after the core has been located
670 * and before any random malloc() calls occur improves the chance
671 * of mapping dynamic space at our preferred addres (if movable).
672 * If not movable, it was already mapped in allocate_spaces(). */
673 initial_function = load_core_file(core, embedded_core_offset);
674 if (initial_function == NIL) {
675 lose("couldn't find initial function\n");
678 #if defined(SVR4) || defined(__linux__) || defined(__NetBSD__)
679 tzset();
680 #endif
682 define_var("nil", NIL, 1);
683 define_var("t", T, 1);
685 if (!disable_lossage_handler_p)
686 enable_lossage_handler();
688 #ifdef LISP_FEATURE_SB_DYNAMIC_CORE
689 os_link_runtime();
690 #endif
691 #ifdef LISP_FEATURE_HPUX
692 /* -1 = CLOSURE_FUN_OFFSET, 23 = SIMPLE_FUN_CODE_OFFSET, we are
693 * not in LANGUAGE_ASSEMBLY so we cant reach them. */
694 return_from_lisp_stub = (void *) ((char *)*((unsigned long *)
695 ((char *)initial_function + -1)) + 23);
696 #endif
698 gc_initialize_pointers();
700 arch_install_interrupt_handlers();
701 #ifndef LISP_FEATURE_WIN32
702 os_install_interrupt_handlers();
703 #else
704 /* wos_install_interrupt_handlers(handler); */
705 wos_install_interrupt_handlers(&exception_frame);
706 #endif
708 /* Pass core filename and the processed argv into Lisp. They'll
709 * need to be processed further there, to do locale conversion.
711 core_string = core;
712 posix_argv = sbcl_argv;
714 FSHOW((stderr, "/funcalling initial_function=0x%lx\n",
715 (unsigned long)initial_function));
716 create_initial_thread(initial_function);
717 lose("unexpected return from initial thread in main()\n");
718 return 0;