<commit_before>#include <stddef.h>
#include <string.h>

#include <drv/console.h>

#define SYSOPEN		0x01
#define SYSCLOSE	0x02
#define SYSWRITEC	0x03
#define SYSWRITE0	0x04
#define SYSWRITE	0x05
#define SYSREAD		0x06
#define SYSFLEN		0x0C

#define MODE_READ	0x0
#define MODE_READBIN	0x1

#define MODE_WRITE	0x4
#define MODE_WRITEBIN	0x5

#ifdef CONFIG_SEMIHOSTING_DEBUG_BUFFERED

#define PRINT_BUFFER_SIZE	128

static char print_buffer[PRINT_BUFFER_SIZE];
static int print_n;

#endif /* CONFIG_SEMIHOSTING_DEBUG_BUFFERED */

/*
 * Call the handler
 */
static long smh_trap(unsigned int sysnum, void *addr)
{
	register long result asm("r0");
#if defined(CONFIG_ARM64)
	asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
#elif defined(CONFIG_CPU_ARMV7M)
	asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
#else
	/* Note - untested placeholder */
	asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
#endif
	return result;
}

/*
 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
 * descriptor or -1 on error.
 */
long smh_open(const char *fname, char *modestr)
{
	long fd;
	unsigned long mode;
	struct smh_open_s {
		const char *fname;
		unsigned long mode;
		size_t len;
	} open;

	debug_printk("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);

	/* Check the file mode */
	if (!(strcmp(modestr, "r"))) {
		mode = MODE_READ;
	} else if (!(strcmp(modestr, "rb"))) {
		mode = MODE_READBIN;
	} else if (!(strcmp(modestr, "w"))) {
		mode = MODE_WRITE;
	} else if (!(strcmp(modestr, "wb"))) {
		mode = MODE_WRITEBIN;
	} else {
		error_printk("%s: ERROR mode \'%s\' not supported\n", __func__,
		       modestr);
		return -1;
	}

	open.fname = fname;
	open.len = strlen(fname);
	open.mode = mode;

	/* Open the file on the host */
	fd = smh_trap(SYSOPEN, &open);
	if (fd == -1)
		error_printk("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
		       fname);

	return fd;
}

static int smh_write0(struct device *dev, unsigned char *buff, unsigned int len)
{
	smh_trap(SYSWRITE0, buff);

	return 0;
}

static int smh_writec(struct device *dev, unsigned char *buff, unsigned int len)
{
	int i;

#ifdef CONFIG_SEMIHOSTING_DEBUG_BUFFERED
	for (i = 0; i < len; i++) {
		if (print_n == (PRINT_BUFFER_SIZE - 1)) {
			print_buffer[print_n] = '\0';

			smh_write0(dev, print_buffer, PRINT_BUFFER_SIZE);
	
			print_n = 0;
			print_buffer[print_n] = buff[i];
		} else {
			print_buffer[print_n] = buff[i];
		}<commit_msg>arm: lib: semihosting: use SYSWRITE instead of SYSWRITE0<commit_after>#include <stddef.h>
#include <string.h>

#include <drv/console.h>

#define SYSOPEN		0x01
#define SYSCLOSE	0x02
#define SYSWRITEC	0x03
#define SYSWRITE0	0x04
#define SYSWRITE	0x05
#define SYSREAD		0x06
#define SYSFLEN		0x0C

#define MODE_READ	0x0
#define MODE_READBIN	0x1

#define MODE_WRITE	0x4
#define MODE_WRITEBIN	0x5

#ifdef CONFIG_SEMIHOSTING_DEBUG_BUFFERED

#define PRINT_BUFFER_SIZE	32

static char print_buffer[PRINT_BUFFER_SIZE];
static int print_n;

#endif /* CONFIG_SEMIHOSTING_DEBUG_BUFFERED */

/*
 * Call the handler
 */
static long smh_trap(unsigned int sysnum, void *addr)
{
	register long result asm("r0");
#if defined(CONFIG_ARM64)
	asm volatile ("hlt #0xf000" : "=r" (result) : "0"(sysnum), "r"(addr));
#elif defined(CONFIG_CPU_ARMV7M)
	asm volatile ("bkpt #0xAB" : "=r" (result) : "0"(sysnum), "r"(addr));
#else
	/* Note - untested placeholder */
	asm volatile ("svc #0x123456" : "=r" (result) : "0"(sysnum), "r"(addr));
#endif
	return result;
}

/*
 * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
 * descriptor or -1 on error.
 */
long smh_open(const char *fname, char *modestr)
{
	long fd;
	unsigned long mode;
	struct smh_open_s {
		const char *fname;
		unsigned long mode;
		size_t len;
	} open;

	debug_printk("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);

	/* Check the file mode */
	if (!(strcmp(modestr, "r"))) {
		mode = MODE_READ;
	} else if (!(strcmp(modestr, "rb"))) {
		mode = MODE_READBIN;
	} else if (!(strcmp(modestr, "w"))) {
		mode = MODE_WRITE;
	} else if (!(strcmp(modestr, "wb"))) {
		mode = MODE_WRITEBIN;
	} else {
		error_printk("%s: ERROR mode \'%s\' not supported\n", __func__,
		       modestr);
		return -1;
	}

	open.fname = fname;
	open.len = strlen(fname);
	open.mode = mode;

	/* Open the file on the host */
	fd = smh_trap(SYSOPEN, &open);
	if (fd == -1)
		error_printk("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
		       fname);

	return fd;
}

static int smh_write0(struct device *dev, unsigned char *buff, unsigned int len)
{
	struct smh_writec_s {
		int handle;
		void *buff;
		int size;
	} write0;

	write0.handle = 1;
	write0.buff = buff;
	write0.size = len;

	smh_trap(SYSWRITE, &write0);

	return 0;
}

static int smh_writec(struct device *dev, unsigned char *buff, unsigned int len)
{
	int i;

#ifdef CONFIG_SEMIHOSTING_DEBUG_BUFFERED
	for (i = 0; i < len; i++) {
		if (print_n == (PRINT_BUFFER_SIZE - 1)) {
			print_buffer[print_n] = '\0';

			smh_write0(dev, print_buffer, PRINT_BUFFER_SIZE);
	
			print_n = 0;
			print_buffer[print_n] = buff[i];
		} else {
			print_buffer[print_n] = buff[i];
		}
{'fraction_non_alphanumeric': 0.14939200926462073, 'fraction_numerical': 0.024898668210770122, 'mean_word_length': 4.060546875, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3595445', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2109, 'n_words': 564}