xref: /freebsd/contrib/processor-trace/libipt/internal/include/pt_section.h (revision 85f87cf491bec6f90948a85b10f5523ea24db9e3)
174fe6c29SRuslan Bukin /*
2*85f87cf4SRuslan Bukin  * Copyright (c) 2013-2019, Intel Corporation
374fe6c29SRuslan Bukin  *
474fe6c29SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
574fe6c29SRuslan Bukin  * modification, are permitted provided that the following conditions are met:
674fe6c29SRuslan Bukin  *
774fe6c29SRuslan Bukin  *  * Redistributions of source code must retain the above copyright notice,
874fe6c29SRuslan Bukin  *    this list of conditions and the following disclaimer.
974fe6c29SRuslan Bukin  *  * Redistributions in binary form must reproduce the above copyright notice,
1074fe6c29SRuslan Bukin  *    this list of conditions and the following disclaimer in the documentation
1174fe6c29SRuslan Bukin  *    and/or other materials provided with the distribution.
1274fe6c29SRuslan Bukin  *  * Neither the name of Intel Corporation nor the names of its contributors
1374fe6c29SRuslan Bukin  *    may be used to endorse or promote products derived from this software
1474fe6c29SRuslan Bukin  *    without specific prior written permission.
1574fe6c29SRuslan Bukin  *
1674fe6c29SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1774fe6c29SRuslan Bukin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1874fe6c29SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1974fe6c29SRuslan Bukin  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2074fe6c29SRuslan Bukin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2174fe6c29SRuslan Bukin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2274fe6c29SRuslan Bukin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2374fe6c29SRuslan Bukin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2474fe6c29SRuslan Bukin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2574fe6c29SRuslan Bukin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2674fe6c29SRuslan Bukin  * POSSIBILITY OF SUCH DAMAGE.
2774fe6c29SRuslan Bukin  */
2874fe6c29SRuslan Bukin 
2974fe6c29SRuslan Bukin #ifndef PT_SECTION_H
3074fe6c29SRuslan Bukin #define PT_SECTION_H
3174fe6c29SRuslan Bukin 
3274fe6c29SRuslan Bukin #include <stdint.h>
3374fe6c29SRuslan Bukin #include <stddef.h>
3474fe6c29SRuslan Bukin 
3574fe6c29SRuslan Bukin #if defined(FEATURE_THREADS)
3674fe6c29SRuslan Bukin #  include <threads.h>
3774fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */
3874fe6c29SRuslan Bukin 
3974fe6c29SRuslan Bukin #include "intel-pt.h"
4074fe6c29SRuslan Bukin 
4174fe6c29SRuslan Bukin struct pt_block_cache;
4274fe6c29SRuslan Bukin 
4374fe6c29SRuslan Bukin 
4474fe6c29SRuslan Bukin /* A section of contiguous memory loaded from a file. */
4574fe6c29SRuslan Bukin struct pt_section {
4674fe6c29SRuslan Bukin 	/* The name of the file. */
4774fe6c29SRuslan Bukin 	char *filename;
4874fe6c29SRuslan Bukin 
4974fe6c29SRuslan Bukin 	/* The offset into the file. */
5074fe6c29SRuslan Bukin 	uint64_t offset;
5174fe6c29SRuslan Bukin 
5274fe6c29SRuslan Bukin 	/* The (adjusted) size in bytes.  The size is truncated to match the
5374fe6c29SRuslan Bukin 	 * actual file size.
5474fe6c29SRuslan Bukin 	 */
5574fe6c29SRuslan Bukin 	uint64_t size;
5674fe6c29SRuslan Bukin 
5774fe6c29SRuslan Bukin 	/* A pointer to OS-specific file status for detecting changes.
5874fe6c29SRuslan Bukin 	 *
5974fe6c29SRuslan Bukin 	 * The status is initialized on first pt_section_map() and will be
6074fe6c29SRuslan Bukin 	 * left in the section until the section is destroyed.  This field
6174fe6c29SRuslan Bukin 	 * is owned by the OS-specific mmap-based section implementation.
6274fe6c29SRuslan Bukin 	 */
6374fe6c29SRuslan Bukin 	void *status;
6474fe6c29SRuslan Bukin 
6574fe6c29SRuslan Bukin 	/* A pointer to implementation-specific mapping information - NULL if
6674fe6c29SRuslan Bukin 	 * the section is currently not mapped.
6774fe6c29SRuslan Bukin 	 *
6874fe6c29SRuslan Bukin 	 * This field is set in pt_section_map() and owned by the mapping
6974fe6c29SRuslan Bukin 	 * implementation.
7074fe6c29SRuslan Bukin 	 */
7174fe6c29SRuslan Bukin 	void *mapping;
7274fe6c29SRuslan Bukin 
7374fe6c29SRuslan Bukin 	/* A pointer to an optional block cache.
7474fe6c29SRuslan Bukin 	 *
7574fe6c29SRuslan Bukin 	 * The cache is created on request and destroyed implicitly when the
7674fe6c29SRuslan Bukin 	 * section is unmapped.
7774fe6c29SRuslan Bukin 	 *
7874fe6c29SRuslan Bukin 	 * We read this field without locking and only lock the section in order
7974fe6c29SRuslan Bukin 	 * to install the block cache.
8074fe6c29SRuslan Bukin 	 *
8174fe6c29SRuslan Bukin 	 * We rely on guaranteed atomic operations as specified in section 8.1.1
8274fe6c29SRuslan Bukin 	 * in Volume 3A of the Intel(R) Software Developer's Manual at
8374fe6c29SRuslan Bukin 	 * http://www.intel.com/sdm.
8474fe6c29SRuslan Bukin 	 */
8574fe6c29SRuslan Bukin 	struct pt_block_cache *bcache;
8674fe6c29SRuslan Bukin 
8774fe6c29SRuslan Bukin 	/* A pointer to the iscache attached to this section.
8874fe6c29SRuslan Bukin 	 *
8974fe6c29SRuslan Bukin 	 * The pointer is initialized when the iscache attaches and cleared when
9074fe6c29SRuslan Bukin 	 * it detaches again.  There can be at most one iscache attached to this
9174fe6c29SRuslan Bukin 	 * section at any time.
9274fe6c29SRuslan Bukin 	 *
9374fe6c29SRuslan Bukin 	 * In addition to attaching, the iscache will need to obtain a reference
9474fe6c29SRuslan Bukin 	 * to the section, which it needs to drop again after detaching.
9574fe6c29SRuslan Bukin 	 */
9674fe6c29SRuslan Bukin 	struct pt_image_section_cache *iscache;
9774fe6c29SRuslan Bukin 
9874fe6c29SRuslan Bukin 	/* A pointer to the unmap function - NULL if the section is currently
9974fe6c29SRuslan Bukin 	 * not mapped.
10074fe6c29SRuslan Bukin 	 *
10174fe6c29SRuslan Bukin 	 * This field is set in pt_section_map() and owned by the mapping
10274fe6c29SRuslan Bukin 	 * implementation.
10374fe6c29SRuslan Bukin 	 */
10474fe6c29SRuslan Bukin 	int (*unmap)(struct pt_section *sec);
10574fe6c29SRuslan Bukin 
10674fe6c29SRuslan Bukin 	/* A pointer to the read function - NULL if the section is currently
10774fe6c29SRuslan Bukin 	 * not mapped.
10874fe6c29SRuslan Bukin 	 *
10974fe6c29SRuslan Bukin 	 * This field is set in pt_section_map() and owned by the mapping
11074fe6c29SRuslan Bukin 	 * implementation.
11174fe6c29SRuslan Bukin 	 */
11274fe6c29SRuslan Bukin 	int (*read)(const struct pt_section *sec, uint8_t *buffer,
11374fe6c29SRuslan Bukin 		    uint16_t size, uint64_t offset);
11474fe6c29SRuslan Bukin 
11574fe6c29SRuslan Bukin 	/* A pointer to the memsize function - NULL if the section is currently
11674fe6c29SRuslan Bukin 	 * not mapped.
11774fe6c29SRuslan Bukin 	 *
11874fe6c29SRuslan Bukin 	 * This field is set in pt_section_map() and owned by the mapping
11974fe6c29SRuslan Bukin 	 * implementation.
12074fe6c29SRuslan Bukin 	 */
12174fe6c29SRuslan Bukin 	int (*memsize)(const struct pt_section *section, uint64_t *size);
12274fe6c29SRuslan Bukin 
12374fe6c29SRuslan Bukin #if defined(FEATURE_THREADS)
12474fe6c29SRuslan Bukin 	/* A lock protecting this section.
12574fe6c29SRuslan Bukin 	 *
12674fe6c29SRuslan Bukin 	 * Most operations do not require the section to be locked.  All
12774fe6c29SRuslan Bukin 	 * actual locking should be handled by pt_section_* functions.
12874fe6c29SRuslan Bukin 	 */
12974fe6c29SRuslan Bukin 	mtx_t lock;
13074fe6c29SRuslan Bukin 
13174fe6c29SRuslan Bukin 	/* A lock protecting the @iscache and @acount fields.
13274fe6c29SRuslan Bukin 	 *
13374fe6c29SRuslan Bukin 	 * We need separate locks to protect against a deadlock scenario when
13474fe6c29SRuslan Bukin 	 * the iscache is mapping or unmapping this section.
13574fe6c29SRuslan Bukin 	 *
13674fe6c29SRuslan Bukin 	 * The attach lock must not be taken while holding the section lock; the
13774fe6c29SRuslan Bukin 	 * other way round is OK.
13874fe6c29SRuslan Bukin 	 */
13974fe6c29SRuslan Bukin 	mtx_t alock;
14074fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */
14174fe6c29SRuslan Bukin 
14274fe6c29SRuslan Bukin 	/* The number of current users.  The last user destroys the section. */
14374fe6c29SRuslan Bukin 	uint16_t ucount;
14474fe6c29SRuslan Bukin 
14574fe6c29SRuslan Bukin 	/* The number of attaches.  This must be <= @ucount. */
14674fe6c29SRuslan Bukin 	uint16_t acount;
14774fe6c29SRuslan Bukin 
14874fe6c29SRuslan Bukin 	/* The number of current mappers.  The last unmaps the section. */
14974fe6c29SRuslan Bukin 	uint16_t mcount;
15074fe6c29SRuslan Bukin };
15174fe6c29SRuslan Bukin 
15274fe6c29SRuslan Bukin /* Create a section.
15374fe6c29SRuslan Bukin  *
15474fe6c29SRuslan Bukin  * The returned section describes the contents of @file starting at @offset
15574fe6c29SRuslan Bukin  * for @size bytes.
15674fe6c29SRuslan Bukin  *
15774fe6c29SRuslan Bukin  * If @file is shorter than the requested @size, the section is silently
15874fe6c29SRuslan Bukin  * truncated to the size of @file.
15974fe6c29SRuslan Bukin  *
16074fe6c29SRuslan Bukin  * If @offset lies beyond the end of @file, no section is created.
16174fe6c29SRuslan Bukin  *
16274fe6c29SRuslan Bukin  * The returned section is not mapped and starts with a user count of one and
16374fe6c29SRuslan Bukin  * instruction caching enabled.
16474fe6c29SRuslan Bukin  *
165*85f87cf4SRuslan Bukin  * Returns zero on success, a negative pt_error_code otherwise.
166*85f87cf4SRuslan Bukin  * Returns -pte_internal if @psection is NULL.
167*85f87cf4SRuslan Bukin  * Returns -pte_nomem when running out of memory.
168*85f87cf4SRuslan Bukin  * Returns -pte_bad_file if @filename cannot be opened.
169*85f87cf4SRuslan Bukin  * Returns -pte_invalid if @offset lies beyond @file.
170*85f87cf4SRuslan Bukin  * Returns -pte_invalid if @filename is too long.
17174fe6c29SRuslan Bukin  */
172*85f87cf4SRuslan Bukin extern int pt_mk_section(struct pt_section **psection, const char *filename,
173*85f87cf4SRuslan Bukin 			 uint64_t offset, uint64_t size);
17474fe6c29SRuslan Bukin 
17574fe6c29SRuslan Bukin /* Lock a section.
17674fe6c29SRuslan Bukin  *
17774fe6c29SRuslan Bukin  * Locks @section.  The section must not be locked.
17874fe6c29SRuslan Bukin  *
17974fe6c29SRuslan Bukin  * Returns a new section on success, NULL otherwise.
18074fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
18174fe6c29SRuslan Bukin  */
18274fe6c29SRuslan Bukin extern int pt_section_lock(struct pt_section *section);
18374fe6c29SRuslan Bukin 
18474fe6c29SRuslan Bukin /* Unlock a section.
18574fe6c29SRuslan Bukin  *
18674fe6c29SRuslan Bukin  * Unlocks @section.  The section must be locked.
18774fe6c29SRuslan Bukin  *
18874fe6c29SRuslan Bukin  * Returns a new section on success, NULL otherwise.
18974fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
19074fe6c29SRuslan Bukin  */
19174fe6c29SRuslan Bukin extern int pt_section_unlock(struct pt_section *section);
19274fe6c29SRuslan Bukin 
19374fe6c29SRuslan Bukin /* Add another user.
19474fe6c29SRuslan Bukin  *
19574fe6c29SRuslan Bukin  * Increments the user count of @section.
19674fe6c29SRuslan Bukin  *
19774fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
19874fe6c29SRuslan Bukin  * Returns -pte_internal if @section is NULL.
19974fe6c29SRuslan Bukin  * Returns -pte_overflow if the user count would overflow.
20074fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
20174fe6c29SRuslan Bukin  */
20274fe6c29SRuslan Bukin extern int pt_section_get(struct pt_section *section);
20374fe6c29SRuslan Bukin 
20474fe6c29SRuslan Bukin /* Remove a user.
20574fe6c29SRuslan Bukin  *
20674fe6c29SRuslan Bukin  * Decrements the user count of @section.  Destroys the section if the
20774fe6c29SRuslan Bukin  * count reaches zero.
20874fe6c29SRuslan Bukin  *
20974fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
21074fe6c29SRuslan Bukin  * Returns -pte_internal if @section is NULL.
21174fe6c29SRuslan Bukin  * Returns -pte_internal if the user count is already zero.
21274fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
21374fe6c29SRuslan Bukin  */
21474fe6c29SRuslan Bukin extern int pt_section_put(struct pt_section *section);
21574fe6c29SRuslan Bukin 
21674fe6c29SRuslan Bukin /* Attaches the image section cache user.
21774fe6c29SRuslan Bukin  *
21874fe6c29SRuslan Bukin  * Similar to pt_section_get() but sets @section->iscache to @iscache.
21974fe6c29SRuslan Bukin  *
22074fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
22174fe6c29SRuslan Bukin  * Returns -pte_internal if @section or @iscache is NULL.
22274fe6c29SRuslan Bukin  * Returns -pte_internal if a different cache is already attached.
22374fe6c29SRuslan Bukin  * Returns -pte_overflow if the attach count would overflow.
22474fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
22574fe6c29SRuslan Bukin  */
22674fe6c29SRuslan Bukin extern int pt_section_attach(struct pt_section *section,
22774fe6c29SRuslan Bukin 			     struct pt_image_section_cache *iscache);
22874fe6c29SRuslan Bukin 
22974fe6c29SRuslan Bukin /* Detaches the image section cache user.
23074fe6c29SRuslan Bukin  *
23174fe6c29SRuslan Bukin  * Similar to pt_section_put() but clears @section->iscache.
23274fe6c29SRuslan Bukin  *
23374fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
23474fe6c29SRuslan Bukin  * Returns -pte_internal if @section or @iscache is NULL.
23574fe6c29SRuslan Bukin  * Returns -pte_internal if the attach count is already zero.
23674fe6c29SRuslan Bukin  * Returns -pte_internal if @section->iscache is not equal to @iscache.
23774fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
23874fe6c29SRuslan Bukin  */
23974fe6c29SRuslan Bukin extern int pt_section_detach(struct pt_section *section,
24074fe6c29SRuslan Bukin 			     struct pt_image_section_cache *iscache);
24174fe6c29SRuslan Bukin 
24274fe6c29SRuslan Bukin /* Return the filename of @section. */
24374fe6c29SRuslan Bukin extern const char *pt_section_filename(const struct pt_section *section);
24474fe6c29SRuslan Bukin 
24574fe6c29SRuslan Bukin /* Return the offset of the section in bytes. */
24674fe6c29SRuslan Bukin extern uint64_t pt_section_offset(const struct pt_section *section);
24774fe6c29SRuslan Bukin 
24874fe6c29SRuslan Bukin /* Return the size of the section in bytes. */
24974fe6c29SRuslan Bukin extern uint64_t pt_section_size(const struct pt_section *section);
25074fe6c29SRuslan Bukin 
25174fe6c29SRuslan Bukin /* Return the amount of memory currently used by the section in bytes.
25274fe6c29SRuslan Bukin  *
25374fe6c29SRuslan Bukin  * We only consider the amount of memory required for mapping @section; we
25474fe6c29SRuslan Bukin  * ignore the size of the section object itself and the size of the status
25574fe6c29SRuslan Bukin  * object.
25674fe6c29SRuslan Bukin  *
25774fe6c29SRuslan Bukin  * If @section is currently not mapped, the size is zero.
25874fe6c29SRuslan Bukin  *
25974fe6c29SRuslan Bukin  * Returns zero on success, a negative pt_error_code otherwise.
26074fe6c29SRuslan Bukin  * Returns -pte_internal if @size of @section is NULL.
26174fe6c29SRuslan Bukin  */
26274fe6c29SRuslan Bukin extern int pt_section_memsize(struct pt_section *section, uint64_t *size);
26374fe6c29SRuslan Bukin 
26474fe6c29SRuslan Bukin /* Allocate a block cache.
26574fe6c29SRuslan Bukin  *
26674fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
26774fe6c29SRuslan Bukin  * Returns -pte_internal if @section is NULL.
26874fe6c29SRuslan Bukin  * Returns -pte_nomem if the block cache can't be allocated.
26974fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
27074fe6c29SRuslan Bukin  */
27174fe6c29SRuslan Bukin extern int pt_section_alloc_bcache(struct pt_section *section);
27274fe6c29SRuslan Bukin 
27374fe6c29SRuslan Bukin /* Request block caching.
27474fe6c29SRuslan Bukin  *
27574fe6c29SRuslan Bukin  * The caller must ensure that @section is mapped.
27674fe6c29SRuslan Bukin  */
pt_section_request_bcache(struct pt_section * section)27774fe6c29SRuslan Bukin static inline int pt_section_request_bcache(struct pt_section *section)
27874fe6c29SRuslan Bukin {
27974fe6c29SRuslan Bukin 	if (!section)
28074fe6c29SRuslan Bukin 		return -pte_internal;
28174fe6c29SRuslan Bukin 
28274fe6c29SRuslan Bukin 	if (section->bcache)
28374fe6c29SRuslan Bukin 		return 0;
28474fe6c29SRuslan Bukin 
28574fe6c29SRuslan Bukin 	return pt_section_alloc_bcache(section);
28674fe6c29SRuslan Bukin }
28774fe6c29SRuslan Bukin 
28874fe6c29SRuslan Bukin /* Return @section's block cache, if available.
28974fe6c29SRuslan Bukin  *
29074fe6c29SRuslan Bukin  * The caller must ensure that @section is mapped.
29174fe6c29SRuslan Bukin  *
29274fe6c29SRuslan Bukin  * The cache is not use-counted.  It is only valid as long as the caller keeps
29374fe6c29SRuslan Bukin  * @section mapped.
29474fe6c29SRuslan Bukin  */
29574fe6c29SRuslan Bukin static inline struct pt_block_cache *
pt_section_bcache(const struct pt_section * section)29674fe6c29SRuslan Bukin pt_section_bcache(const struct pt_section *section)
29774fe6c29SRuslan Bukin {
29874fe6c29SRuslan Bukin 	if (!section)
29974fe6c29SRuslan Bukin 		return NULL;
30074fe6c29SRuslan Bukin 
30174fe6c29SRuslan Bukin 	return section->bcache;
30274fe6c29SRuslan Bukin }
30374fe6c29SRuslan Bukin 
30474fe6c29SRuslan Bukin /* Create the OS-specific file status.
30574fe6c29SRuslan Bukin  *
30674fe6c29SRuslan Bukin  * On success, allocates a status object, provides a pointer to it in @pstatus
30774fe6c29SRuslan Bukin  * and provides the file size in @psize.
30874fe6c29SRuslan Bukin  *
30974fe6c29SRuslan Bukin  * The status object will be free()'ed when its section is.
31074fe6c29SRuslan Bukin  *
31174fe6c29SRuslan Bukin  * This function is implemented in the OS-specific section implementation.
31274fe6c29SRuslan Bukin  *
31374fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
31474fe6c29SRuslan Bukin  * Returns -pte_internal if @pstatus, @psize, or @filename is NULL.
31574fe6c29SRuslan Bukin  * Returns -pte_bad_image if @filename can't be opened.
31674fe6c29SRuslan Bukin  * Returns -pte_nomem if the status object can't be allocated.
31774fe6c29SRuslan Bukin  */
31874fe6c29SRuslan Bukin extern int pt_section_mk_status(void **pstatus, uint64_t *psize,
31974fe6c29SRuslan Bukin 				const char *filename);
32074fe6c29SRuslan Bukin 
32174fe6c29SRuslan Bukin /* Perform on-map maintenance work.
32274fe6c29SRuslan Bukin  *
32374fe6c29SRuslan Bukin  * Notifies an attached image section cache about the mapping of @section.
32474fe6c29SRuslan Bukin  *
32574fe6c29SRuslan Bukin  * This function is called by the OS-specific pt_section_map() implementation
32674fe6c29SRuslan Bukin  * after @section has been successfully mapped and @section has been unlocked.
32774fe6c29SRuslan Bukin  *
32874fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
32974fe6c29SRuslan Bukin  * Returns -pte_internal if @section is NULL.
33074fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
33174fe6c29SRuslan Bukin  */
33274fe6c29SRuslan Bukin extern int pt_section_on_map_lock(struct pt_section *section);
33374fe6c29SRuslan Bukin 
pt_section_on_map(struct pt_section * section)33474fe6c29SRuslan Bukin static inline int pt_section_on_map(struct pt_section *section)
33574fe6c29SRuslan Bukin {
33674fe6c29SRuslan Bukin 	if (section && !section->iscache)
33774fe6c29SRuslan Bukin 		return 0;
33874fe6c29SRuslan Bukin 
33974fe6c29SRuslan Bukin 	return pt_section_on_map_lock(section);
34074fe6c29SRuslan Bukin }
34174fe6c29SRuslan Bukin 
34274fe6c29SRuslan Bukin /* Map a section.
34374fe6c29SRuslan Bukin  *
34474fe6c29SRuslan Bukin  * Maps @section into memory.  Mappings are use-counted.  The number of
34574fe6c29SRuslan Bukin  * pt_section_map() calls must match the number of pt_section_unmap()
34674fe6c29SRuslan Bukin  * calls.
34774fe6c29SRuslan Bukin  *
34874fe6c29SRuslan Bukin  * This function is implemented in the OS-specific section implementation.
34974fe6c29SRuslan Bukin  *
35074fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
35174fe6c29SRuslan Bukin  * Returns -pte_internal if @section is NULL.
35274fe6c29SRuslan Bukin  * Returns -pte_bad_image if @section changed or can't be opened.
35374fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
35474fe6c29SRuslan Bukin  * Returns -pte_nomem if @section can't be mapped into memory.
35574fe6c29SRuslan Bukin  * Returns -pte_overflow if the map count would overflow.
35674fe6c29SRuslan Bukin  */
35774fe6c29SRuslan Bukin extern int pt_section_map(struct pt_section *section);
35874fe6c29SRuslan Bukin 
35974fe6c29SRuslan Bukin /* Share a section mapping.
36074fe6c29SRuslan Bukin  *
36174fe6c29SRuslan Bukin  * Increases the map count for @section without notifying an attached image
36274fe6c29SRuslan Bukin  * section cache.
36374fe6c29SRuslan Bukin  *
36474fe6c29SRuslan Bukin  * This function should only be used by the attached image section cache to
36574fe6c29SRuslan Bukin  * resolve a deadlock scenario when mapping a section it intends to cache.
36674fe6c29SRuslan Bukin  *
36774fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
36874fe6c29SRuslan Bukin  * Returns -pte_internal if @section is NULL.
36974fe6c29SRuslan Bukin  * Returns -pte_internal if @section->mcount is zero.
37074fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
37174fe6c29SRuslan Bukin  */
37274fe6c29SRuslan Bukin extern int pt_section_map_share(struct pt_section *section);
37374fe6c29SRuslan Bukin 
37474fe6c29SRuslan Bukin /* Unmap a section.
37574fe6c29SRuslan Bukin  *
37674fe6c29SRuslan Bukin  * Unmaps @section from memory.
37774fe6c29SRuslan Bukin  *
37874fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
37974fe6c29SRuslan Bukin  * Returns -pte_internal if @section is NULL.
38074fe6c29SRuslan Bukin  * Returns -pte_bad_lock on any locking error.
38174fe6c29SRuslan Bukin  * Returns -pte_internal if @section has not been mapped.
38274fe6c29SRuslan Bukin  */
38374fe6c29SRuslan Bukin extern int pt_section_unmap(struct pt_section *section);
38474fe6c29SRuslan Bukin 
38574fe6c29SRuslan Bukin /* Read memory from a section.
38674fe6c29SRuslan Bukin  *
38774fe6c29SRuslan Bukin  * Reads at most @size bytes from @section at @offset into @buffer.  @section
38874fe6c29SRuslan Bukin  * must be mapped.
38974fe6c29SRuslan Bukin  *
39074fe6c29SRuslan Bukin  * Returns the number of bytes read on success, a negative error code otherwise.
39174fe6c29SRuslan Bukin  * Returns -pte_internal if @section or @buffer are NULL.
39274fe6c29SRuslan Bukin  * Returns -pte_nomap if @offset is beyond the end of the section.
39374fe6c29SRuslan Bukin  */
39474fe6c29SRuslan Bukin extern int pt_section_read(const struct pt_section *section, uint8_t *buffer,
39574fe6c29SRuslan Bukin 			   uint16_t size, uint64_t offset);
39674fe6c29SRuslan Bukin 
39774fe6c29SRuslan Bukin #endif /* PT_SECTION_H */
398