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_FILE_H 3074fe6c29SRuslan Bukin #define PT_SECTION_FILE_H 3174fe6c29SRuslan Bukin 3274fe6c29SRuslan Bukin #include <stdio.h> 3374fe6c29SRuslan Bukin #include <stdint.h> 3474fe6c29SRuslan Bukin 3574fe6c29SRuslan Bukin #if defined(FEATURE_THREADS) 3674fe6c29SRuslan Bukin # include <threads.h> 3774fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */ 3874fe6c29SRuslan Bukin 3974fe6c29SRuslan Bukin struct pt_section; 4074fe6c29SRuslan Bukin 4174fe6c29SRuslan Bukin 4274fe6c29SRuslan Bukin /* File-based section mapping information. */ 4374fe6c29SRuslan Bukin struct pt_sec_file_mapping { 4474fe6c29SRuslan Bukin /* The FILE pointer. */ 4574fe6c29SRuslan Bukin FILE *file; 4674fe6c29SRuslan Bukin 4774fe6c29SRuslan Bukin /* The begin and end of the section as offset into @file. */ 4874fe6c29SRuslan Bukin long begin, end; 4974fe6c29SRuslan Bukin 5074fe6c29SRuslan Bukin #if defined(FEATURE_THREADS) 5174fe6c29SRuslan Bukin /* A lock protecting read access to this file. 5274fe6c29SRuslan Bukin * 5374fe6c29SRuslan Bukin * Since we need to first set the file position indication before 5474fe6c29SRuslan Bukin * we can read, there's a race on the file position. 5574fe6c29SRuslan Bukin */ 5674fe6c29SRuslan Bukin mtx_t lock; 5774fe6c29SRuslan Bukin #endif /* defined(FEATURE_THREADS) */ 5874fe6c29SRuslan Bukin }; 5974fe6c29SRuslan Bukin 6074fe6c29SRuslan Bukin 6174fe6c29SRuslan Bukin /* Map a section based on file operations. 6274fe6c29SRuslan Bukin * 6374fe6c29SRuslan Bukin * The caller has already opened the file for reading. 6474fe6c29SRuslan Bukin * 6574fe6c29SRuslan Bukin * On success, sets @section's mapping, unmap, and read pointers. 6674fe6c29SRuslan Bukin * 6774fe6c29SRuslan Bukin * Returns zero on success, a negative error code otherwise. 6874fe6c29SRuslan Bukin * Returns -pte_internal if @section or @file are NULL. 6974fe6c29SRuslan Bukin * Returns -pte_invalid if @section can't be mapped. 7074fe6c29SRuslan Bukin */ 7174fe6c29SRuslan Bukin extern int pt_sec_file_map(struct pt_section *section, FILE *file); 7274fe6c29SRuslan Bukin 7374fe6c29SRuslan Bukin /* Unmap a section based on file operations. 7474fe6c29SRuslan Bukin * 7574fe6c29SRuslan Bukin * On success, clears @section's mapping, unmap, and read pointers. 7674fe6c29SRuslan Bukin * 7774fe6c29SRuslan Bukin * Returns zero on success, a negative error code otherwise. 7874fe6c29SRuslan Bukin * Returns -pte_internal if @section is NULL. 7974fe6c29SRuslan Bukin * Returns -pte_internal if @section has not been mapped. 8074fe6c29SRuslan Bukin */ 8174fe6c29SRuslan Bukin extern int pt_sec_file_unmap(struct pt_section *section); 8274fe6c29SRuslan Bukin 8374fe6c29SRuslan Bukin /* Read memory from a file based section. 8474fe6c29SRuslan Bukin * 8574fe6c29SRuslan Bukin * Reads at most @size bytes from @section at @offset into @buffer. 8674fe6c29SRuslan Bukin * 8774fe6c29SRuslan Bukin * Returns the number of bytes read on success, a negative error code otherwise. 8874fe6c29SRuslan Bukin * Returns -pte_invalid if @section or @buffer are NULL. 8974fe6c29SRuslan Bukin * Returns -pte_nomap if @offset is beyond the end of the section. 9074fe6c29SRuslan Bukin */ 9174fe6c29SRuslan Bukin extern int pt_sec_file_read(const struct pt_section *section, uint8_t *buffer, 9274fe6c29SRuslan Bukin uint16_t size, uint64_t offset); 9374fe6c29SRuslan Bukin 9474fe6c29SRuslan Bukin /* Compute the memory size of a section based on file operations. 9574fe6c29SRuslan Bukin * 9674fe6c29SRuslan Bukin * On success, provides the amount of memory used for mapping @section in bytes 9774fe6c29SRuslan Bukin * in @size. 9874fe6c29SRuslan Bukin * 9974fe6c29SRuslan Bukin * Returns zero on success, a negative error code otherwise. 10074fe6c29SRuslan Bukin * Returns -pte_internal if @section or @size is NULL. 10174fe6c29SRuslan Bukin * Returns -pte_internal if @section has not been mapped. 10274fe6c29SRuslan Bukin */ 10374fe6c29SRuslan Bukin extern int pt_sec_file_memsize(const struct pt_section *section, 10474fe6c29SRuslan Bukin uint64_t *size); 10574fe6c29SRuslan Bukin 10674fe6c29SRuslan Bukin #endif /* PT_SECTION_FILE_H */ 107