OSDN Git Service

ldso: merge duplicate dl-syscalls.h code to common header
[uclinux-h8/uClibc.git] / ldso / ldso / bfin / dl-syscalls.h
1 /* Copyright (C) 2003, 2004 Red Hat, Inc.
2    Contributed by Alexandre Oliva <aoliva@redhat.com>
3
4 This file is part of uClibc.
5
6 uClibc is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 uClibc is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with uClibc; see the file COPYING.LIB.  If not, write to
18 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
19 USA.  */
20
21 #include <sys/mman.h>
22
23 /* The code below is extracted from libc/sysdeps/linux/frv/_mmap.c */
24
25 #if DYNAMIC_LOADER_IN_SIMULATOR
26 #define __NR___syscall_mmap2        __NR_mmap2
27 static __inline__ _syscall6(__ptr_t, __syscall_mmap2, __ptr_t, addr,
28         size_t, len, int, prot, int, flags, int, fd, off_t, offset)
29
30 /* Make sure we don't get another definition of _dl_mmap from the
31    machine-independent code.  */
32 #undef __NR_mmap
33 #undef __NR_mmap2
34
35 /* This is always 12, even on architectures where PAGE_SHIFT != 12.  */
36 # ifndef MMAP2_PAGE_SHIFT
37 #  define MMAP2_PAGE_SHIFT 12
38 # endif
39
40 #include <bits/uClibc_page.h> /* for PAGE_SIZE */
41 static __always_inline void *_dl_memset(void*,int,size_t);
42 static __always_inline ssize_t _dl_pread(int fd, void *buf, size_t count, off_t offset);
43
44 static __ptr_t
45 _dl_mmap(__ptr_t addr, size_t len, int prot, int flags, int fd, __off_t offset)
46 {
47   size_t plen = (len + PAGE_SIZE - 1) & -PAGE_SIZE;
48
49 /* This is a hack to enable the dynamic loader to run within a
50    simulator that doesn't support mmap, with a number of very ugly
51    tricks.  Also, it's not as useful as it sounds, since only dynamic
52    executables without DT_NEEDED dependencies can be run.  AFAIK, they
53    can only be created with -pie.  This trick suffices to enable the
54    dynamic loader to obtain a blank page that it maps early in the
55    bootstrap. */
56   if ((flags & MAP_FIXED) == 0)
57     {
58       void *_dl_mmap_base = 0;
59       __ptr_t *ret = 0;
60
61       if (! _dl_mmap_base)
62         {
63           void *stack;
64           __asm__ ("mov sp, %0" : "=r" (stack));
65           _dl_mmap_base = (void *)(((long)stack + 2 * PAGE_SIZE) & -PAGE_SIZE);
66         retry:
67           if (((void **)_dl_mmap_base)[0] == _dl_mmap_base
68               && ((void **)_dl_mmap_base)[1023] == _dl_mmap_base
69               && (((void **)_dl_mmap_base)[177]
70                   == ((void **)_dl_mmap_base)[771]))
71             {
72               while (((void**)_dl_mmap_base)[177])
73                 {
74                   _dl_mmap_base = ((void**)_dl_mmap_base)[177];
75                   if (!(((void **)_dl_mmap_base)[0] == _dl_mmap_base
76                         && ((void **)_dl_mmap_base)[1023] == _dl_mmap_base
77                         && (((void **)_dl_mmap_base)[177]
78                             == ((void**)_dl_mmap_base)[771])))
79                     ((void(*)())0)();
80                 }
81             }
82           else
83             {
84               int i;
85               for (i = 0; i < (int)PAGE_SIZE; i++)
86                 if (*(char*)(_dl_mmap_base + i))
87                   break;
88               if (i != PAGE_SIZE)
89                 {
90                   _dl_mmap_base = (void*)((long)_dl_mmap_base + PAGE_SIZE);
91                   goto retry;
92                 }
93               ((void**)_dl_mmap_base)[-1] =
94                 ((void**)_dl_mmap_base)[0] =
95                 ((void**)_dl_mmap_base)[1023] =
96                 _dl_mmap_base;
97             }
98         }
99
100       if (_dl_mmap_base)
101         {
102           if (!(((void **)_dl_mmap_base)[0] == _dl_mmap_base
103                 && ((void **)_dl_mmap_base)[1023] == _dl_mmap_base
104                 && (((void **)_dl_mmap_base)[177]
105                     == ((void**)_dl_mmap_base)[771])))
106             ((void(*)())0)();
107           ret = (__ptr_t)((char*)_dl_mmap_base + PAGE_SIZE);
108           _dl_mmap_base =
109             ((void**)_dl_mmap_base)[177] =
110             ((void**)_dl_mmap_base)[771] =
111             (char*)_dl_mmap_base + plen + PAGE_SIZE;
112           ((void**)_dl_mmap_base)[0] =
113             ((void**)_dl_mmap_base)[1023] =
114             _dl_mmap_base;
115         }
116
117       if ((flags & MAP_ANONYMOUS) != 0)
118         {
119           _dl_memset (ret, 0, plen);
120           return ret;
121         }
122
123       flags |= MAP_FIXED;
124       addr = ret;
125     }
126     if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1)) {
127 #if 0
128         __set_errno (EINVAL);
129 #endif
130         return MAP_FAILED;
131     }
132     if ((flags & MAP_FIXED) != 0)
133       {
134         if (_dl_pread(fd, addr, len, offset) != (ssize_t)len)
135           return (void*)MAP_FAILED;
136         if (plen != len)
137           _dl_memset (addr + len, 0, plen - len);
138         return addr;
139       }
140     return(__syscall_mmap2(addr, len, prot, flags, fd, (off_t) (offset >> MMAP2_PAGE_SHIFT)));
141 }
142 #endif
143
144 #ifdef __NR_pread
145 #ifdef DYNAMIC_LOADER_IN_SIMULATOR
146 #include <unistd.h>
147
148 #define __NR___syscall_lseek __NR_lseek
149 static __always_inline unsigned long _dl_read(int fd, const void *buf, unsigned long count);
150
151 static __always_inline _syscall3(__off_t, __syscall_lseek, int, fd, __off_t, offset,
152                         int, whence)
153 static __always_inline ssize_t
154 _dl_pread(int fd, void *buf, size_t count, off_t offset)
155 {
156   __off_t orig = __syscall_lseek (fd, 0, SEEK_CUR);
157   ssize_t ret;
158
159   if (orig == -1)
160     return -1;
161
162   if (__syscall_lseek (fd, offset, SEEK_SET) != offset)
163     return -1;
164
165   ret = _dl_read (fd, buf, count);
166
167   if (__syscall_lseek (fd, orig, SEEK_SET) != orig)
168     ((void(*)())0)();
169
170   return ret;
171 }
172 #else
173 #define __NR___syscall_pread __NR_pread
174 static __always_inline _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
175                         size_t, count, off_t, offset_hi, off_t, offset_lo)
176
177 static __always_inline ssize_t
178 _dl_pread(int fd, void *buf, size_t count, off_t offset)
179 {
180   return(__syscall_pread(fd,buf,count,__LONG_LONG_PAIR (offset >> 31, offset)));
181 }
182 #endif
183 #endif
184
185 #ifdef __NR_sram_alloc
186 #define __NR__dl_sram_alloc __NR_sram_alloc
187 static __always_inline _syscall2(__ptr_t, _dl_sram_alloc,
188                         size_t, len, unsigned long, flags)
189 #endif
190
191 #ifdef __NR_sram_free
192 #define __NR__dl_sram_free __NR_sram_free
193 static __always_inline _syscall1(int, _dl_sram_free, __ptr_t, addr)
194 #endif
195
196 #ifdef __NR_dma_memcpy
197 #define __NR__dl_dma_memcpy __NR_dma_memcpy
198 static __always_inline _syscall3(__ptr_t, _dl_dma_memcpy,
199                         __ptr_t, dest, __ptr_t, src, size_t, len)
200 #endif
201
202 #define __UCLIBC_MMAP_HAS_6_ARGS__