1/* Tru64 UNIX host-specific hook definitions.
2   Copyright (C) 2011 Free Software Foundation, Inc.
3
4   This file is part of GCC.
5
6   GCC is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 3, or (at your
9   option) any later version.
10
11   GCC is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14   License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with GCC; see the file COPYING3.  If not see
18   <http://www.gnu.org/licenses/>.  */
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include <sys/mman.h>
24/* Inhibit inclusion of <sys/mount.h>, unnecessary and errors out due to
25   use of poisoned bcmp, bcopy.  */
26#define _SYS_MOUNT_H_
27#include <sys/procfs.h>
28#include "hosthooks.h"
29#include "hosthooks-def.h"
30
31
32#undef HOST_HOOKS_GT_PCH_GET_ADDRESS
33#define HOST_HOOKS_GT_PCH_GET_ADDRESS osf_gt_pch_get_address
34#undef HOST_HOOKS_GT_PCH_USE_ADDRESS
35#define HOST_HOOKS_GT_PCH_USE_ADDRESS osf_gt_pch_use_address
36
37/* The mmap ADDR parameter may be ignored without MAP_FIXED set.  Before we
38   give up, check existing mappings with ioctl(PIOCMAP) to see if the space
39   is really free.  */
40
41static void *
42mmap_fixed (void *addr, size_t len, int prot, int flags, int fd, off_t off)
43{
44  void *base;
45
46  base = mmap ((caddr_t) addr, len, prot, flags, fd, off);
47
48  if (base != addr)
49    {
50      /* PID_MAX is SHRT_MAX on Tru64 UNIX V4.0, but INT_MAX on V5.1.
51	 Allow for both.  "/proc/" + INT_MAX + '\0'.  */
52      char pname[6+10+1];
53      int procfd, nmap;
54      prmap_t *pmap;
55      int i, overlap = 0;
56
57      if (base != (void *) MAP_FAILED)
58	munmap ((caddr_t) base, len);
59
60      /* Check if there's any mapping overlapping [addr, addr+len).  */
61
62      snprintf (pname, sizeof (pname), "/proc/%d", getpid ());
63      procfd = open (pname, O_RDONLY);
64      if (procfd == -1)
65	return ((void *) MAP_FAILED);
66      if (ioctl (procfd, PIOCNMAP, &nmap) == -1)
67	return ((void *) MAP_FAILED);
68      pmap = (prmap_t *) xmalloc (sizeof (*pmap) * (nmap+1));
69      if (ioctl (procfd, PIOCMAP, pmap) == -1)
70	return ((void *) MAP_FAILED);
71
72      /* It seems like pmap[] is sorted by address, but can we rely on
73	 that?  */
74      for (i = 0; i < nmap; i++)
75	{
76	  uintptr_t map_start = (uintptr_t) pmap[i].pr_vaddr;
77	  uintptr_t map_end = map_start + pmap[i].pr_size;
78
79	  if ((uintptr_t) addr < map_end
80	      && (uintptr_t) addr+len > map_start)
81	    {
82	      overlap = 1;
83	      break;
84	    }
85	}
86      free (pmap);
87      close (procfd);
88
89      if (!overlap)
90	base = mmap ((caddr_t) addr, len, prot, flags | MAP_FIXED, fd, off);
91      else
92	base = mmap ((caddr_t) addr, len, prot, flags, fd, off);
93    }
94
95  return base;
96}
97
98/* For various ports, try to guess a fixed spot in the vm space that's
99   probably free.  Take the middle between start of text segment and
100   dynamic loader space.  See <sys/machine/addrconf.h> and Tru64 UNIX
101   Assembly Language Programmer's Guide, p.6-18, Figure 6-3: Default Layout
102   of Memory (User Program View).  */
103#define TRY_EMPTY_VM_SPACE	0x20050000000
104
105/* Determine a location where we might be able to reliably allocate
106   SIZE bytes.  FD is the PCH file, though we should return with the
107   file unmapped.  */
108
109static void *
110osf_gt_pch_get_address (size_t size, int fd)
111{
112  void *addr;
113
114  addr = mmap_fixed ((caddr_t) TRY_EMPTY_VM_SPACE, size,
115		     PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
116
117  /* If we failed the map, that means there's *no* free space.  */
118  if (addr == (void *) MAP_FAILED)
119    return NULL;
120  /* Unmap the area before returning.  */
121  munmap ((caddr_t) addr, size);
122
123  return addr;
124}
125
126/* Map SIZE bytes of FD+OFFSET at BASE.  Return 1 if we succeeded at
127   mapping the data at BASE, -1 if we couldn't.  */
128
129static int
130osf_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
131{
132  void *addr;
133
134  /* We're called with size == 0 if we're not planning to load a PCH
135     file at all.  This allows the hook to free any static space that
136     we might have allocated at link time.  */
137  if (size == 0)
138    return -1;
139
140  addr = mmap_fixed ((caddr_t) base, size,
141		     PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, offset);
142
143  return addr == base ? 1 : -1;
144}
145
146
147const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;
148