xref: /titanic_41/usr/src/lib/libtnfctl/targmem.c (revision f8919bdadda3ebb97bd55cc14a16e0271ed57615)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1994, by Sun Microsytems, Inc.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Function to allocate memory in target process (used by combinations).
30  */
31 
32 #ifndef DEBUG
33 #define	NDEBUG	1
34 #endif
35 
36 #include <assert.h>
37 #include "tnfctl_int.h"
38 #include "prb_internals.h"
39 #include "dbg.h"
40 
41 
42 /*
43  * _tnfctl_targmem_alloc() - allocates memory in the target process.
44  */
45 tnfctl_errcode_t
46 _tnfctl_targmem_alloc(tnfctl_handle_t *hndl, size_t size, uintptr_t *addr_p)
47 {
48 	int			miscstat;
49 	tnf_memseg_t		memseg;
50 
51 	assert(hndl->memseg_p != NULL);
52 	*addr_p = NULL;
53 
54 	/* read the memseg block from the target process */
55 	miscstat = hndl->p_read(hndl->proc_p, hndl->memseg_p, &memseg,
56 		sizeof (memseg));
57 	if (miscstat)
58 		return (TNFCTL_ERR_INTERNAL);
59 
60 	/* if there is memory left, allocate it */
61 	if ((memseg.min_p + memseg.i_reqsz) <= (memseg.max_p - size)) {
62 		memseg.max_p -= size;
63 
64 		miscstat = hndl->p_write(hndl->proc_p, hndl->memseg_p,
65 			&memseg, sizeof (memseg));
66 		if (miscstat)
67 			return (TNFCTL_ERR_INTERNAL);
68 
69 		*addr_p = (uintptr_t) memseg.max_p;
70 
71 		DBG_TNF_PROBE_2(_tnfctl_targmem_alloc_1, "libtnfctl",
72 			"sunw%verbosity 3",
73 			tnf_long, size_allocated, size,
74 			tnf_opaque, at_location, *addr_p);
75 
76 		return (TNFCTL_ERR_NONE);
77 	} else {
78 		return (TNFCTL_ERR_INTERNAL);
79 	}
80 }
81