source: branches/purify/source/lisp-kernel/image.c @ 12890

Last change on this file since 12890 was 12890, checked in by gb, 11 years ago

Work-in-progress. Current plan:

  • memory layout for x8664 (at least) pure area (1GB reserved) managed-static area unmapped, reserved for expansion of managed-static, static-cons areas (127GB) static cons area dynamic area(s).
  • refbits: sparse, but essentially the same size as before.
  • need to memoize writes of dynamic pointers (static-cons area or above) to managed static area.
  • need to save/restore the refbits that belong to the managed-static area in the image.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 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#include <limits.h>
29
30
31
32#if defined(PPC64) || defined(X8632)
33#define RELOCATABLE_FULLTAG_MASK \
34  ((1<<fulltag_cons)|(1<<fulltag_misc))
35#else
36#ifdef X8664
37#define RELOCATABLE_FULLTAG_MASK \
38  ((1<<fulltag_cons)|(1<<fulltag_misc)|(1<<fulltag_symbol)|(1<<fulltag_function))
39#else
40#define RELOCATABLE_FULLTAG_MASK \
41  ((1<<fulltag_cons)|(1<<fulltag_nil)|(1<<fulltag_misc))
42#endif
43#endif
44
45void
46relocate_area_contents(area *a, LispObj bias)
47{
48  LispObj
49    *start = (LispObj *)(a->low), 
50    *end = (LispObj *)(a->active),
51    low = (LispObj)image_base - bias,
52    high = ptr_to_lispobj(active_dynamic_area->active) - bias,
53    w0;
54  int fulltag;
55
56  while (start < end) {
57    w0 = *start;
58    fulltag = fulltag_of(w0);
59    if (immheader_tag_p(fulltag)) {
60      start = (LispObj *)skip_over_ivector((natural)start, w0);
61    } else {
62#ifdef X86
63      if (header_subtag(w0) == subtag_function) {
64#ifdef X8664
65        int skip = ((int) start[1])+1;
66#else
67        int skip = ((unsigned short)start[1])+1;
68        extern void update_self_references(LispObj *);
69        update_self_references(start);
70#endif
71     
72        start += skip;
73        if (((LispObj) start) & node_size) {
74          --start;
75        }
76        w0 = *start;
77        fulltag = fulltag_of(w0);
78      }
79#endif
80
81      if ((w0 >= low) && (w0 < high) &&
82          ((1<<fulltag) & RELOCATABLE_FULLTAG_MASK)) {
83        *start = (w0+bias);
84      }
85      w0 = *++start;
86      fulltag = fulltag_of(w0);
87      if ((w0 >= low) && (w0 < high) &&
88          ((1<<fulltag) & RELOCATABLE_FULLTAG_MASK)) {
89        *start = (w0+bias);
90      }
91      ++start;
92    }
93  }
94}
95     
96
97
98
99off_t
100seek_to_next_page(int fd)
101{
102  off_t pos = LSEEK(fd, 0, SEEK_CUR);
103  pos = align_to_power_of_2(pos, log2_page_size);
104  return LSEEK(fd, pos, SEEK_SET);
105}
106 
107/*
108  fd is positioned to EOF; header has been allocated by caller.
109  If we find a trailer (and that leads us to the header), read
110  the header & return true else return false.
111*/
112Boolean
113find_openmcl_image_file_header(int fd, openmcl_image_file_header *header)
114{
115  openmcl_image_file_trailer trailer;
116  int disp;
117  off_t pos;
118  unsigned version, flags;
119
120  pos = LSEEK(fd, 0, SEEK_END);
121  if (pos < 0) {
122    return false;
123  }
124  pos -= sizeof(trailer);
125
126  if (LSEEK(fd, pos, SEEK_SET) < 0) {
127    return false;
128  }
129  if (read(fd, &trailer, sizeof(trailer)) != sizeof(trailer)) {
130    return false;
131  }
132  if ((trailer.sig0 != IMAGE_SIG0) ||
133      (trailer.sig1 != IMAGE_SIG1) ||
134      (trailer.sig2 != IMAGE_SIG2)) {
135    return false;
136  }
137  disp = trailer.delta;
138 
139  if (disp >= 0) {
140    return false;
141  }
142  if (LSEEK(fd, disp, SEEK_CUR) < 0) {
143    return false;
144  }
145  if (read(fd, header, sizeof(openmcl_image_file_header)) !=
146      sizeof(openmcl_image_file_header)) {
147    return false;
148  }
149  if ((header->sig0 != IMAGE_SIG0) ||
150      (header->sig1 != IMAGE_SIG1) ||
151      (header->sig2 != IMAGE_SIG2) ||
152      (header->sig3 != IMAGE_SIG3)) {
153    return false;
154  }
155  version = (header->abi_version) & 0xffff;
156  if (version < ABI_VERSION_MIN) {
157    fprintf(dbgout, "Heap image is too old for this kernel.\n");
158    return false;
159  }
160  if (version > ABI_VERSION_MAX) {
161    fprintf(dbgout, "Heap image is too new for this kernel.\n");
162    return false;
163  }
164  flags = header->flags;
165  if (flags != PLATFORM) {
166    fprintf(dbgout, "Heap image was saved for another platform.\n");
167    return false;
168  }
169  return true;
170}
171
172void
173load_image_section(int fd, openmcl_image_section_header *sect)
174{
175  extern area* allocate_dynamic_area(unsigned);
176  off_t
177    pos = seek_to_next_page(fd), advance;
178  natural
179    mem_size = sect->memory_size;
180  void *addr;
181  area *a;
182
183  advance = mem_size;
184  switch(sect->code) {
185  case AREA_READONLY:
186    if (!MapFile(pure_space_active,
187                 pos,
188                 align_to_power_of_2(mem_size,log2_page_size),
189                 MEMPROTECT_RX,
190                 fd)) {
191      return;
192    }
193    a = new_area(pure_space_active, pure_space_limit, AREA_READONLY);
194    pure_space_active += mem_size;
195    a->active = pure_space_active;
196    sect->area = a;     
197    break;
198
199  case AREA_STATIC:
200    if (!MapFile(static_space_active,
201                 pos,
202                 align_to_power_of_2(mem_size,log2_page_size),
203                 MEMPROTECT_RWX,
204                 fd)) {
205      return;
206    }
207    a = new_area(static_space_active, static_space_limit, AREA_STATIC);
208    static_space_active += mem_size;
209    a->active = static_space_active;
210    sect->area = a;
211    break;
212
213  case AREA_DYNAMIC:
214    a = allocate_dynamic_area(mem_size);
215    if (!MapFile(a->low,
216                 pos,
217                 align_to_power_of_2(mem_size,log2_page_size),
218                 MEMPROTECT_RWX,
219                 fd)) {
220      return;
221    }
222
223    a->static_dnodes = sect->static_dnodes;
224    sect->area = a;
225    break;
226
227  case AREA_MANAGED_STATIC:
228    a = new_area(pure_space_limit, pure_space_limit+align_to_power_of_2(mem_size,log2_page_size), AREA_MANAGED_STATIC);
229    a->active = a->low+mem_size;
230    if (mem_size) {
231      if (!MapFile(a->low,
232                   pos,
233                   align_to_power_of_2(mem_size,log2_page_size),
234                   MEMPROTECT_RWX,
235                   fd)) {
236        return;
237      }
238    }
239    /* Need to save/restore persistent refbits. */
240    sect->area = a;
241    managed_static_area = a;
242    break;
243
244    /* In many respects, the static_cons_area is part of the dynamic
245       area; it's physically adjacent to it (immediately precedes the
246       dynamic area in memory) and its contents are subject to full
247       GC (but not compaction.)  It's maintained as a seperate section
248       in the image file, at least for now. */
249
250
251  case AREA_STATIC_CONS:
252    addr = (void *) lisp_global(HEAP_START);
253    a = new_area(addr-align_to_power_of_2(mem_size,log2_page_size), addr, AREA_STATIC_CONS);
254    sect->area = a;
255    static_cons_area = a;
256    break;
257
258  default:
259    return;
260   
261  }
262  LSEEK(fd, pos+advance, SEEK_SET);
263}
264
265LispObj
266load_openmcl_image(int fd, openmcl_image_file_header *h)
267{
268  LispObj image_nil = 0;
269  area *a;
270  if (find_openmcl_image_file_header(fd, h)) {
271    int i, nsections = h->nsections;
272    openmcl_image_section_header sections[nsections], *sect=sections;
273    LispObj bias = image_base - ACTUAL_IMAGE_BASE(h);
274#if (WORD_SIZE== 64)
275    signed_natural section_data_delta = 
276      ((signed_natural)(h->section_data_offset_high) << 32L) | h->section_data_offset_low;
277#endif
278
279    if (read (fd, sections, nsections*sizeof(openmcl_image_section_header)) !=
280        nsections * sizeof(openmcl_image_section_header)) {
281      return 0;
282    }
283#if WORD_SIZE == 64
284    LSEEK(fd, section_data_delta, SEEK_CUR);
285#endif
286    for (i = 0; i < nsections; i++, sect++) {
287      load_image_section(fd, sect);
288      a = sect->area;
289      if (a == NULL) {
290        return 0;
291      }
292    }
293
294    for (i = 0, sect = sections; i < nsections; i++, sect++) {
295      a = sect->area;
296      switch(sect->code) {
297      case AREA_STATIC:
298        nilreg_area = a;
299#ifdef PPC
300#ifdef PPC64
301        image_nil = ptr_to_lispobj(a->low + (1024*4) + sizeof(lispsymbol) + fulltag_misc);
302#else
303        image_nil = (LispObj)(a->low + 8 + 8 + (1024*4) + fulltag_nil);
304#endif
305#endif
306#ifdef X86
307#ifdef X8664
308        image_nil = (LispObj)(a->low) + (1024*4) + fulltag_nil;
309#else
310        image_nil = (LispObj)(a->low) + (1024*4) + fulltag_cons;
311#endif
312#endif
313        set_nil(image_nil);
314        if (bias) {
315          relocate_area_contents(a, bias);
316        }
317        make_dynamic_heap_executable(a->low, a->active);
318        add_area_holding_area_lock(a);
319        break;
320       
321      case AREA_READONLY:
322        readonly_area = a;
323        add_area_holding_area_lock(a);
324        break;
325      }
326    }
327    for (i = 0, sect = sections; i < nsections; i++, sect++) {
328      a = sect->area;
329      switch(sect->code) {
330      case AREA_MANAGED_STATIC:
331        if (bias) {
332          relocate_area_contents(a, bias);
333        }
334        add_area_holding_area_lock(a);
335        break;
336      case AREA_STATIC_CONS:
337        if (bias) {
338          relocate_area_contents(a, bias);
339        }
340        add_area_holding_area_lock(a);
341        break;
342      case AREA_DYNAMIC:
343        if (bias) {
344          relocate_area_contents(a, bias);
345        }
346        resize_dynamic_heap(a->active, lisp_heap_gc_threshold);
347        xMakeDataExecutable(a->low, a->active - a->low);
348        break;
349      }
350    }
351  }
352  return image_nil;
353}
354 
355void
356prepare_to_write_dynamic_space()
357{
358  area *a = active_dynamic_area;
359  LispObj
360    *start = (LispObj *)(a->low),
361    *end = (LispObj *) (a->active),
362    x1;
363  int tag, subtag, element_count;
364
365  while (start < end) {
366    x1 = *start;
367    tag = fulltag_of(x1);
368    if (immheader_tag_p(tag)) {
369      subtag = header_subtag(x1);
370      if (subtag == subtag_macptr) {
371        if ((start[1] >= (natural)0x10000) && (start[1] < (natural)-0x10000)) {
372          /* Leave small pointers alone */
373          *start = make_header(subtag_dead_macptr,header_element_count(x1));
374        }
375      }
376      start = (LispObj *)skip_over_ivector((natural)start, x1);
377    } else if (nodeheader_tag_p(tag)) {
378      element_count = header_element_count(x1) | 1;
379      start += (element_count+1);
380    } else {
381      start += 2;
382    }
383  }
384}
385
386 
387
388int
389write_file_and_section_headers(int fd, 
390                               openmcl_image_file_header *file_header,
391                               openmcl_image_section_header* section_headers,
392                               int nsections,
393                               off_t *header_pos)
394{
395  *header_pos = seek_to_next_page(fd);
396
397  if (LSEEK (fd, *header_pos, SEEK_SET) < 0) {
398    return errno;
399  }
400  if (write(fd, file_header, sizeof(*file_header)) != sizeof(*file_header)) {
401    return errno;
402  }
403  if (write(fd, section_headers, sizeof(section_headers[0])*nsections)
404      != (sizeof(section_headers[0])*nsections)) {
405    return errno;
406  }
407  return 0;
408}
409 
410natural
411writebuf(int fd, char *bytes, natural n)
412{
413  natural remain = n, this_size;
414  signed_natural result;
415
416  while (remain) {
417    this_size = remain;
418    if (this_size > INT_MAX) {
419      this_size = INT_MAX;
420    }
421    result = write(fd, bytes, this_size);
422    if (result < 0) {
423      return errno;
424    }
425    bytes += result;
426
427    remain -= result;
428  }
429  return 0;
430}
431
432OSErr
433save_application(unsigned fd, Boolean egc_was_enabled)
434{
435  openmcl_image_file_header fh;
436  openmcl_image_section_header sections[NUM_IMAGE_SECTIONS];
437  openmcl_image_file_trailer trailer;
438  area *areas[NUM_IMAGE_SECTIONS], *a;
439  int i, err;
440  off_t header_pos, eof_pos;
441#if WORD_SIZE == 64
442  off_t image_data_pos;
443  signed_natural section_data_delta;
444#endif
445
446  areas[0] = nilreg_area; 
447  areas[1] = active_dynamic_area;
448  areas[2] = readonly_area;
449  areas[3] = managed_static_area;
450  areas[4] = static_cons_area;
451  for (i = 0; i < NUM_IMAGE_SECTIONS; i++) {
452    a = areas[i];
453    sections[i].code = a->code;
454    sections[i].area = NULL;
455    sections[i].memory_size  = a->active - a->low;
456    if (a == active_dynamic_area) {
457      sections[i].static_dnodes = tenured_area->static_dnodes;
458    } else {
459      sections[i].static_dnodes = 0;
460    }
461  }
462  fh.sig0 = IMAGE_SIG0;
463  fh.sig1 = IMAGE_SIG1;
464  fh.sig2 = IMAGE_SIG2;
465  fh.sig3 = IMAGE_SIG3;
466  fh.timestamp = time(NULL);
467  CANONICAL_IMAGE_BASE(&fh) = IMAGE_BASE_ADDRESS;
468  ACTUAL_IMAGE_BASE(&fh) = image_base;
469  fh.nsections = NUM_IMAGE_SECTIONS;
470  fh.abi_version=ABI_VERSION_CURRENT;
471#if WORD_SIZE == 64
472  fh.section_data_offset_high = 0;
473  fh.section_data_offset_low = 0;
474#else
475  fh.pad0[0] = fh.pad0[1] = 0;
476  fh.pad1[0] = fh.pad1[1] = fh.pad1[2] = fh.pad1[3] = 0;
477#endif
478  fh.flags = PLATFORM;
479
480#if WORD_SIZE == 64
481  image_data_pos = seek_to_next_page(fd);
482#else
483  err = write_file_and_section_headers(fd, &fh, sections, NUM_IMAGE_SECTIONS, &header_pos);
484  if (err) {
485    return err;
486  }
487#endif
488
489  /*
490    Coerce macptrs to dead_macptrs.
491  */
492 
493  prepare_to_write_dynamic_space(active_dynamic_area);
494
495  {
496    area *g0_area = g1_area->younger;
497
498    /* Save GC config */
499    lisp_global(LISP_HEAP_THRESHOLD) = lisp_heap_gc_threshold;
500    lisp_global(G0_THRESHOLD) = g0_area->threshold;
501    lisp_global(G1_THRESHOLD) = g1_area->threshold;
502    lisp_global(G2_THRESHOLD) = g2_area->threshold;
503    lisp_global(EGC_ENABLED) = (LispObj)egc_was_enabled;
504  }
505  /*
506    lisp_global(GC_NUM) and lisp_global(FWDNUM) are persistent,
507    as is DELETED_STATIC_PAIRS.
508    Nothing else is even meaningful at this point.
509  */
510  for (i = MIN_KERNEL_GLOBAL; i < 0; i++) {
511    switch (i) {
512    case FWDNUM:
513    case GC_NUM:
514    case STATIC_CONSES:
515    case WEAK_GC_METHOD:
516    case LISP_HEAP_THRESHOLD:
517    case EGC_ENABLED:
518    case G0_THRESHOLD:
519    case G1_THRESHOLD:
520    case G2_THRESHOLD:
521      break;
522    default:
523      lisp_global(i) = 0;
524    }
525  }
526
527  for (i = 0; i < NUM_IMAGE_SECTIONS; i++) {
528    natural n;
529    a = areas[i];
530    seek_to_next_page(fd);
531    n = sections[i].memory_size;
532    if (writebuf(fd, a->low, n)) {
533        return errno;
534    }
535  }
536
537#if WORD_SIZE == 64
538  seek_to_next_page(fd);
539  section_data_delta = -((LSEEK(fd,0,SEEK_CUR)+sizeof(fh)+sizeof(sections)) -
540                         image_data_pos);
541  fh.section_data_offset_high = (int)(section_data_delta>>32L);
542  fh.section_data_offset_low = (unsigned)section_data_delta;
543  err =  write_file_and_section_headers(fd, &fh, sections, NUM_IMAGE_SECTIONS, &header_pos);
544  if (err) {
545    return err;
546  } 
547#endif
548
549  trailer.sig0 = IMAGE_SIG0;
550  trailer.sig1 = IMAGE_SIG1;
551  trailer.sig2 = IMAGE_SIG2;
552  eof_pos = LSEEK(fd, 0, SEEK_CUR) + sizeof(trailer);
553  trailer.delta = (int) (header_pos-eof_pos);
554  if (write(fd, &trailer, sizeof(trailer)) == sizeof(trailer)) {
555#ifndef WINDOWS
556    fsync(fd);
557#endif
558    close(fd);
559    return 0;
560  } 
561  i = errno;
562  close(fd);
563  return i;
564}
565     
566
567
568
Note: See TracBrowser for help on using the repository browser.