|
|
1.1 root 1: /* $Id: recode-mmap.c,v 1.2 2008/07/01 02:00:53 fredette Exp $ */
2:
3: /* libtme/host/recode-mmap.c - recode code file for mmap() hosts: */
4:
5: /*
6: * Copyright (c) 2008 Matt Fredette
7: * All rights reserved.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: * 3. All advertising materials mentioning features or use of this software
18: * must display the following acknowledgement:
19: * This product includes software developed by Matt Fredette.
20: * 4. The name of the author may not be used to endorse or promote products
21: * derived from this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33: * POSSIBILITY OF SUCH DAMAGE.
34: */
35:
36: #include <tme/common.h>
37: _TME_RCSID("$Id: recode-mmap.c,v 1.2 2008/07/01 02:00:53 fredette Exp $");
38:
39: #if TME_HAVE_RECODE
40:
41: /* includes: */
42: #include "recode-impl.h"
43: #include <fcntl.h>
44: #include <sys/types.h>
45: #include <sys/mman.h>
46:
47: /* this host function allocates memory for building and running thunks: */
48: void
49: tme_recode_host_thunks_alloc(struct tme_recode_ic *ic,
50: tme_recode_thunk_off_t size_run)
51: {
52: int size_page;
53: int fd;
54: tme_recode_host_insn_t *memory;
55:
56: /* get the page size: */
57: size_page = getpagesize();
58: assert ((size_page % sizeof(tme_recode_host_insn_t)) == 0);
59:
60: /* make sure the run memory size is at least the minimum thunk size,
61: and round the run memory size up to the page size: */
62: size_run = TME_MAX(size_run, TME_RECODE_HOST_THUNK_SIZE_MAX);
63: size_run += (size_page - 1);
64: size_run -= (size_run % size_page);
65: assert (size_run >= TME_MAX(size_page, TME_RECODE_HOST_THUNK_SIZE_MAX));
66:
67: /* if MAP_ANON isn't defined, assume that we have to mmap /dev/zero
68: to map anonymous memory: */
69: #ifndef MAP_ANON
70: fd = open("/dev/zero", O_RDWR, 0);
71: if (fd < 0) {
72: abort();
73: }
74: #else /* MAP_ANON */
75: fd = -1;
76: #endif /* MAP_ANON */
77:
78: /* mmap memory that is readable, writable and executable: */
79: memory
80: = ((tme_recode_host_insn_t *)
81: mmap((void *) 0,
82: size_run,
83: (PROT_READ
84: | PROT_WRITE
85: | PROT_EXEC),
86: (MAP_SHARED
87: #ifdef MAP_ANON
88: | MAP_ANON
89: #endif /* MAP_ANON */
90: ),
91: fd,
92: 0));
93: if (memory == (tme_recode_host_insn_t *) MAP_FAILED) {
94: abort();
95: }
96:
97: /* return the memory: */
98: ic->tme_recode_mmap_ic_thunks_start = memory;
99: ic->tme_recode_ic_thunk_build_next = memory;
100: ic->tme_recode_ic_thunk_build_end = memory + (size_run / sizeof(tme_recode_host_insn_t));
101: }
102:
103: /* this host function starts building a new thunk. it returns nonzero
104: if a thunk of TME_RECODE_HOST_THUNK_SIZE_MAX bytes can be built: */
105: int
106: tme_recode_host_thunk_start(struct tme_recode_ic *ic)
107: {
108: return ((ic->tme_recode_ic_thunk_build_end
109: - ic->tme_recode_ic_thunk_build_next)
110: >= ((TME_RECODE_HOST_THUNK_SIZE_MAX
111: + sizeof(tme_recode_host_insn_t)
112: - 1)
113: / sizeof(tme_recode_host_insn_t)));
114: }
115:
116: /* this host function finishes building a new thunk: */
117: void
118: tme_recode_host_thunk_finish(struct tme_recode_ic *ic)
119: {
120:
121: /* round the next build address up to the thunk alignment: */
122: ic->tme_recode_ic_thunk_build_next
123: = ((tme_recode_host_insn_t *)
124: ((((unsigned long) ic->tme_recode_ic_thunk_build_next)
125: + TME_RECODE_HOST_THUNK_ALIGN
126: - 1)
127: & (0 - (unsigned long) TME_RECODE_HOST_THUNK_ALIGN)));
128: }
129:
130: /* this host function invalidates all thunks starting from the given
131: thunk offset: */
132: void
133: tme_recode_host_thunk_invalidate_all(struct tme_recode_ic *ic,
134: tme_recode_thunk_off_t thunk_off)
135: {
136:
137: /* reset the build address: */
138: ic->tme_recode_ic_thunk_build_next = ic->tme_recode_mmap_ic_thunks_start + thunk_off;
139: tme_recode_host_thunk_finish(ic);
140: }
141:
142: #endif /* TME_HAVE_RECODE */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.