source: trunk/source/lisp-kernel/image.c @ 10565

Last change on this file since 10565 was 10565, checked in by gb, 12 years ago

Merge changes from branches/win64.

As well as the expected low-level exception/suspend/interrupt stuff,
these changes also include changes to [f]printf format strings. Note
that on win64, a 'long' is 32-bits wide, which complicates matters:

  • an address (viewed as an integer) or a natural-sized integer isn't

(portably) a long, and so can't be printed with %l.

  • an address (viewed as an integer) or a natural-sized integer isn't

(portably) a long long, and so can't be printed with %ll.

  • an address (viewed as an integer) or a natural-sized integer can be

portably printed with '%p', but implementations differ as to whether
or not '%p' prepends a gratuitous '0x' to the hex address. (Linux
does, other current platforms seem not to.)

The approach that seems to work is to cast arguments to natural, then
to u64_t, then use %ll. That approach probably isn't taken consistently
(yet), so some debugging information printed by the kernel may be
incorrect.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1/*
2   Copyright (C) 2002 Clozure Associates
3   This file is part of OpenMCL. 
4
5   OpenMCL is licensed under the terms of the Lisp Lesser GNU Public
6   License , known as the LLGPL and distributed with OpenMCL as the
7   file "LICENSE".  The LLGPL consists of a preamble and the LGPL,
8   which is distributed with OpenMCL as the file "LGPL".  Where these
9   conflict, the preamble takes precedence. 
10
11   OpenMCL is referenced in the preamble as the "LIBRARY."
12
13   The LLGPL is also available online at
14   http://opensource.franz.com/preamble.html
15*/
16
17#include "lisp.h"
18#include "lisp_globals.h"
19#include "area.h"
20#include "image.h"
21#include "gc.h"
22#include <errno.h>
23#include <unistd.h>
24#ifndef WINDOWS
25#include <sys/mman.h>
26#endif
27#include <stdio.h>
28
29
30
31#if defined(PPC64) || defined(X8632)
32#define RELOCATABLE_FULLTAG_MASK \
33  ((1<<fulltag_cons)|(1<<fulltag_misc))
34#else
35#ifdef X8664
36#define RELOCATABLE_FULLTAG_MASK \
37  ((1<<fulltag_cons)|(1<<fulltag_misc)|(1<<fulltag_symbol)|(1<<fulltag_function))
38#else
39#define RELOCATABLE_FULLTAG_MASK \
40  ((1<<fulltag_cons)|(1<<fulltag_nil)|(1<<fulltag_misc))
41#endif
42#endif
43
44void
45relocate_area_contents(area *a, LispObj bias)
46{
47  LispObj
48    *start = (LispObj *)(a->low), 
49    *end = (LispObj *)(a->active),
50    low = (LispObj)image_base - bias,
51    high = ptr_to_lispobj(active_dynamic_area->active) - bias,
52    w0;
53  int fulltag;
54
55  while (start < end) {
56    w0 = *start;
57    fulltag = fulltag_of(w0);
58    if (immheader_tag_p(fulltag)) {
59      start = (LispObj *)skip_over_ivector((natural)start, w0);
60    } else {
61#ifdef X8664
62      if (header_subtag(w0) == subtag_function) {
63        int skip = (int) start[1];
64     
65        start += skip;
66        if (((LispObj) start) & node_size) {
67          --start;
68        }
69        w0 = *start;
70        fulltag = fulltag_of(w0);
71      }
72#endif
73
74      if ((w0 >= low) && (w0 < high) &&
75          ((1<<fulltag) & RELOCATABLE_FULLTAG_MASK)) {
76        *start = (w0+bias);
77      }
78      w0 = *++start;
79      fulltag = fulltag_of(w0);
80      if ((w0 >= low) && (w0 < high) &&
81          ((1<<fulltag) & RELOCATABLE_FULLTAG_MASK)) {
82        *start = (w0+bias);
83      }
84      ++start;
85    }
86  }
87}
88     
89
90
91
92off_t
93seek_to_next_page(int fd)
94{
95  off_t pos = lseek(fd, 0, SEEK_CUR);
96  pos = align_to_power_of_2(pos, log2_page_size);
97  return lseek(fd, pos, SEEK_SET);
98}
99 
100/*
101  fd is positioned to EOF; header has been allocated by caller.
102  If we find a trailer (and that leads us to the header), read
103  the header & return true else return false.
104*/
105Boolean
106find_openmcl_image_file_header(int fd, openmcl_image_file_header *header)
107{
108  openmcl_image_file_trailer trailer;
109  int disp;
110  off_t pos;
111  unsigned version, flags;
112
113  pos = lseek(fd, 0, SEEK_END);
114  if (pos < 0) {
115    return false;
116  }
117  pos -= sizeof(trailer);
118
119  if (lseek(fd, pos, SEEK_SET) < 0) {
120    return false;
121  }
122  if (read(fd, &trailer, sizeof(trailer)) != sizeof(trailer)) {
123    return false;
124  }
125  if ((trailer.sig0 != IMAGE_SIG0) ||
126      (trailer.sig1 != IMAGE_SIG1) ||
127      (trailer.sig2 != IMAGE_SIG2)) {
128    return false;
129  }
130  disp = trailer.delta;
131 
132  if (disp >= 0) {
133    return false;
134  }
135  if (lseek(fd, disp, SEEK_CUR) < 0) {
136    return false;
137  }
138  if (read(fd, header, sizeof(openmcl_image_file_header)) !=
139      sizeof(openmcl_image_file_header)) {
140    return false;
141  }
142  if ((header->sig0 != IMAGE_SIG0) ||
143      (header->sig1 != IMAGE_SIG1) ||
144      (header->sig2 != IMAGE_SIG2) ||
145      (header->sig3 != IMAGE_SIG3)) {
146    return false;
147  }
148  version = (header->abi_version) & 0xffff;
149  if (version < ABI_VERSION_MIN) {
150    fprintf(stderr, "Heap image is too old for this kernel.\n");
151    return false;
152  }
153  if (version > ABI_VERSION_MAX) {
154    fprintf(stderr, "Heap image is too new for this kernel.\n");
155    return false;
156  }
157  flags = header->flags;
158  if (flags != PLATFORM) {
159    fprintf(stderr, "Heap image was saved for another platform.\n");
160    return false;
161  }
162  return true;
163}
164
165void
166load_image_section(int fd, openmcl_image_section_header *sect)
167{
168  extern area* allocate_dynamic_area(unsigned);
169  off_t
170    pos = seek_to_next_page(fd), advance;
171  int 
172    mem_size = sect->memory_size;
173  void *addr;
174  area *a;
175
176  advance = mem_size;
177  switch(sect->code) {
178  case AREA_READONLY:
179    if (!MapFile(pure_space_active,
180                 pos,
181                 align_to_power_of_2(mem_size,log2_page_size),
182                 MEMPROTECT_RX,
183                 fd)) {
184      return;
185    }
186    a = new_area(pure_space_active, pure_space_limit, AREA_READONLY);
187    pure_space_active += mem_size;
188    a->active = pure_space_active;
189    sect->area = a;     
190    break;
191
192  case AREA_STATIC:
193    if (!MapFile(static_space_active,
194                 pos,
195                 align_to_power_of_2(mem_size,log2_page_size),
196                 MEMPROTECT_RWX,
197                 fd)) {
198      return;
199    }
200    a = new_area(static_space_active, static_space_limit, AREA_STATIC);
201    static_space_active += mem_size;
202    a->active = static_space_active;
203    sect->area = a;
204    break;
205
206  case AREA_DYNAMIC:
207    a = allocate_dynamic_area(mem_size);
208    if (!MapFile(a->low,
209                 pos,
210                 align_to_power_of_2(mem_size,log2_page_size),
211                 MEMPROTECT_RWX,
212                 fd)) {
213      return;
214    }
215
216    a->static_dnodes = sect->static_dnodes;
217    sect->area = a;
218    break;
219
220  case AREA_MANAGED_STATIC:
221    a = new_area(pure_space_limit, pure_space_limit, AREA_MANAGED_STATIC);
222    sect->area = a;
223    break;
224
225  default:
226    return;
227   
228  }
229  lseek(fd, pos+advance, SEEK_SET);
230}
231
232LispObj
233load_openmcl_image(int fd, openmcl_image_file_header *h)
234{
235  LispObj image_nil = 0;
236  area *a;
237  if (find_openmcl_image_file_header(fd, h)) {
238    int i, nsections = h->nsections;
239    openmcl_image_section_header sections[nsections], *sect=sections;
240    LispObj bias = image_base - ACTUAL_IMAGE_BASE(h);
241#if (WORD_SIZE== 64)
242    signed_natural section_data_delta = 
243      ((signed_natural)(h->section_data_offset_high) << 32L) | h->section_data_offset_low;
244#endif
245
246    if (read (fd, sections, nsections*sizeof(openmcl_image_section_header)) !=
247        nsections * sizeof(openmcl_image_section_header)) {
248      return 0;
249    }
250#if WORD_SIZE == 64
251    lseek(fd, section_data_delta, SEEK_CUR);
252#endif
253    for (i = 0; i < nsections; i++, sect++) {
254      load_image_section(fd, sect);
255      a = sect->area;
256      if (a == NULL) {
257        return 0;
258      }
259    }
260
261    for (i = 0, sect = sections; i < nsections; i++, sect++) {
262      a = sect->area;
263      switch(sect->code) {
264      case AREA_STATIC:
265        nilreg_area = a;
266#ifdef PPC
267#ifdef PPC64
268        image_nil = ptr_to_lispobj(a->low + (1024*4) + sizeof(lispsymbol) + fulltag_misc);
269#else
270        image_nil = (LispObj)(a->low + 8 + 8 + (1024*4) + fulltag_nil);
271#endif
272#endif
273#ifdef X86
274#ifdef X8664
275        image_nil = (LispObj)(a->low) + (1024*4) + fulltag_nil;
276#else
277        image_nil = (LispObj)(a->low) + (1024*4) + fulltag_cons;
278#endif
279#endif
280        set_nil(image_nil);
281        if (bias) {
282          relocate_area_contents(a, bias);
283        }
284        make_dynamic_heap_executable(a->low, a->active);
285        add_area_holding_area_lock(a);
286        break;
287       
288      case AREA_READONLY:
289        readonly_area = a;
290        add_area_holding_area_lock(a);
291        break;
292      }
293    }
294    for (i = 0, sect = sections; i < nsections; i++, sect++) {
295      a = sect->area;
296      switch(sect->code) {
297      case AREA_MANAGED_STATIC:
298        if (bias) {
299          relocate_area_contents(a, bias);
300        }
301        managed_static_area = a;
302        add_area_holding_area_lock(a);
303        break;
304      case AREA_DYNAMIC:
305        if (bias) {
306          relocate_area_contents(a, bias);
307        }
308        resize_dynamic_heap(a->active, lisp_heap_gc_threshold);
309        xMakeDataExecutable(a->low, a->active - a->low);
310        break;
311      }
312    }
313  }
314  return image_nil;
315}
316 
317void
318prepare_to_write_dynamic_space()
319{
320  area *a = active_dynamic_area;
321  LispObj
322    *start = (LispObj *)(a->low),
323    *end = (LispObj *) (a->active),
324    x1;
325  int tag, subtag, element_count;
326
327  while (start < end) {
328    x1 = *start;
329    tag = fulltag_of(x1);
330    if (immheader_tag_p(tag)) {
331      subtag = header_subtag(x1);
332      if (subtag == subtag_macptr) {
333        if (start[1]) {
334          /* Leave NULL pointers alone */
335          *start = make_header(subtag_dead_macptr,header_element_count(x1));
336        }
337      }
338      start = (LispObj *)skip_over_ivector((natural)start, x1);
339    } else if (nodeheader_tag_p(tag)) {
340      element_count = header_element_count(x1) | 1;
341      start += (element_count+1);
342    } else {
343      start += 2;
344    }
345  }
346}
347
348OSErr
349write_area_pages(int fd, area *a)
350{
351  natural total = a->active - a->low, count, done=0;
352  signed_natural n;
353  char buffer[32768];
354
355  while (total) {
356    if (total > 32768) {
357      count = 32768;
358    } else {
359      count = total;
360    }
361    memmove(buffer,a->low+done,count);
362    n = write(fd, buffer, count);
363    if (n < 0) {
364      return n;
365    }
366    total -= n;
367    done += n;
368  }
369  return 0;
370}
371 
372
373int
374write_file_and_section_headers(int fd, 
375                               openmcl_image_file_header *file_header,
376                               openmcl_image_section_header* section_headers,
377                               int nsections,
378                               off_t *header_pos)
379{
380  *header_pos = seek_to_next_page(fd);
381
382  if (lseek (fd, *header_pos, SEEK_SET) < 0) {
383    return errno;
384  }
385  if (write(fd, file_header, sizeof(*file_header)) != sizeof(*file_header)) {
386    return errno;
387  }
388  if (write(fd, section_headers, sizeof(section_headers[0])*nsections)
389      != (sizeof(section_headers[0])*nsections)) {
390    return errno;
391  }
392  return 0;
393}
394 
395 
396OSErr
397save_application(unsigned fd)
398{
399  openmcl_image_file_header fh;
400  openmcl_image_section_header sections[NUM_IMAGE_SECTIONS];
401  openmcl_image_file_trailer trailer;
402  area *areas[NUM_IMAGE_SECTIONS], *a;
403  int i, err;
404  off_t header_pos, eof_pos;
405#if WORD_SIZE == 64
406  off_t image_data_pos;
407  signed_natural section_data_delta;
408#endif
409
410  areas[0] = nilreg_area; 
411  areas[1] = active_dynamic_area;
412  areas[2] = readonly_area;
413  areas[3] = managed_static_area;
414  for (i = 0; i < NUM_IMAGE_SECTIONS; i++) {
415    a = areas[i];
416    sections[i].code = a->code;
417    sections[i].area = NULL;
418    sections[i].memory_size  = a->active - a->low;
419    if (a == active_dynamic_area) {
420      sections[i].static_dnodes = tenured_area->static_dnodes;
421    } else {
422      sections[i].static_dnodes = 0;
423    }
424  }
425  fh.sig0 = IMAGE_SIG0;
426  fh.sig1 = IMAGE_SIG1;
427  fh.sig2 = IMAGE_SIG2;
428  fh.sig3 = IMAGE_SIG3;
429  fh.timestamp = time(NULL);
430  CANONICAL_IMAGE_BASE(&fh) = IMAGE_BASE_ADDRESS;
431  ACTUAL_IMAGE_BASE(&fh) = image_base;
432  fh.nsections = NUM_IMAGE_SECTIONS;
433  fh.abi_version=ABI_VERSION_CURRENT;
434#if WORD_SIZE == 64
435  fh.section_data_offset_high = 0;
436  fh.section_data_offset_low = 0;
437#else
438  fh.pad0[0] = fh.pad0[1] = 0;
439  fh.pad1[0] = fh.pad1[1] = fh.pad1[2] = fh.pad1[3] = 0;
440#endif
441  fh.flags = PLATFORM;
442
443#if WORD_SIZE == 64
444  image_data_pos = seek_to_next_page(fd);
445#else
446  err = write_file_and_section_headers(fd, &fh, sections, NUM_IMAGE_SECTIONS, &header_pos);
447  if (err) {
448    return err;
449  }
450#endif
451
452  /*
453    Coerce macptrs to dead_macptrs.
454  */
455 
456  prepare_to_write_dynamic_space(active_dynamic_area);
457
458  /*
459    lisp_global(GC_NUM) and lisp_global(FWDNUM) are persistent,
460    as is DELETED_STATIC_PAIRS.
461    Nothing else is even meaningful at this point.
462  */
463  for (i = MIN_KERNEL_GLOBAL; i < 0; i++) {
464    switch (i) {
465    case FWDNUM:
466    case GC_NUM:
467    case STATIC_CONSES:
468    case WEAK_GC_METHOD:
469      break;
470    default:
471      lisp_global(i) = 0;
472    }
473  }
474
475  for (i = 0; i < NUM_IMAGE_SECTIONS; i++) {
476    natural n, nstatic;
477    a = areas[i];
478    seek_to_next_page(fd);
479    n = sections[i].memory_size;
480    nstatic = sections[i].static_dnodes;
481    if (a->code == AREA_READONLY) {
482      /*
483         Darwin seems to have problems writing the readonly area for
484         some reason.  It seems to work better to write a page at a
485         time.
486      */
487      if (write_area_pages(fd, a) != 0) {
488        return errno;
489      }
490    } else {
491      if (write(fd, a->low, n) != n) {
492        return errno;
493      }
494    }
495  }
496
497#if WORD_SIZE == 64
498  seek_to_next_page(fd);
499  section_data_delta = -((lseek(fd,0,SEEK_CUR)+sizeof(fh)+sizeof(sections)) -
500                         image_data_pos);
501  fh.section_data_offset_high = (int)(section_data_delta>>32L);
502  fh.section_data_offset_low = (unsigned)section_data_delta;
503  err =  write_file_and_section_headers(fd, &fh, sections, NUM_IMAGE_SECTIONS, &header_pos);
504  if (err) {
505    return err;
506  } 
507#endif
508
509  trailer.sig0 = IMAGE_SIG0;
510  trailer.sig1 = IMAGE_SIG1;
511  trailer.sig2 = IMAGE_SIG2;
512  eof_pos = lseek(fd, 0, SEEK_CUR) + sizeof(trailer);
513  trailer.delta = (int) (header_pos-eof_pos);
514  if (write(fd, &trailer, sizeof(trailer)) == sizeof(trailer)) {
515#ifndef WINDOWS
516    fsync(fd);
517#endif
518    close(fd);
519    return 0;
520  } 
521  i = errno;
522  close(fd);
523  return i;
524}
525     
526
527
528
Note: See TracBrowser for help on using the repository browser.