Skip to content
Snippets Groups Projects
Commit e88e4794 authored by theo's avatar theo
Browse files

programme user_cmd.c qui ne marche pas

parent 564a45d3
No related branches found
No related tags found
No related merge requests found
......@@ -10,24 +10,26 @@
.extern user_stack DATA // this adress can be read like: ldr Rx, =user_stack
switch_to_user_mode:
ldr r0, =user_stack
msr psp, r0
mov r0, #0x03
switch_to_user_mode:
mov r0, #3
mov r3, USER_STACK_SIZE
lsl r3, #2 // r3 = USER_STACK_SIZE * 4
ldr r1, =user_stack
add r1, r1, r3
msr psp, r1
msr control, r0
isb
bx lr
.equ bad_addr,0x90000
.thumb_func
asm_test_fault:
ldr r0,=bad_addr
ldr r1,[r0] // mem access error
ldr r1,[r0]
mov r2,#2
mov r3,#3
nop
......@@ -35,3 +37,4 @@ asm_test_fault:
.ltorg
......@@ -23,57 +23,29 @@
#include "user_cmd.h"
#include "uart.h"
#define MMFAR *(unsigned *)0xE000ED34
#define MAX_ERRORS 100
unsigned user_stack[USER_STACK_SIZE];
unsigned error_addresses[MAX_ERRORS];
unsigned error_codes[MAX_ERRORS];
unsigned error_count = 0;
// space for user stack
void *user_starting_address;
int *p_sram2=(int *)0x2007c000, a, *p_out_of_mem=(int *)0x80000;
void switch_to_user_mode();
void asm_test_fault();
uint32_t error_adresses[100];
uint32_t error_count = 0;
uint32_t error_codes[100];
void MemManage_Handler() {
// Enable the memory management fault exception
SCB->SHCSR |= (1 << 16);
// Enable the MPU
MPU->CTRL |= 1;
// Save the address and error code
unsigned error_address = (*((volatile unsigned *)(0xE000ED34)));
unsigned error_code = SCB->CFSR;
if (error_count < MAX_ERRORS) {
error_addresses[error_count] = error_address;
error_codes[error_count] = error_code;
void MemManage_Handler()
{
error_adresses[error_count] = MMFAR;
error_codes[error_count] = SCB->CFSR;
SCB->CFSR = 0xFFFF;
LPC_GPIO2->FIOPIN = error_count;
error_count++;
}
// Clear the error register
SCB->CFSR = 0xFFFF;
// Turn on the LEDs
LPC_GPIO2->FIOPIN |= 0xFF;
// Modify the PC value on the user stack to restart the user session
__asm volatile (
"mrs r0, psp\n" // Load the Process Stack Pointer value into r0
"ldr r1, =user_starting_address\n"
"ldr r1, [r1]\n"
"str r1, [r0, #24]\n" // Store the value of user_starting_address into the stacked PC value in the exception stack frame
"ldr r2, =user_stack\n" // Load the address of the user stack into r2
"msr psp, r2\n" // Set PSP to the start of the user stack
: : : "r0", "r1", "r2" // Tell the compiler that r0, r1, and r2 are being used
);
}
void test_supervisor_mode()
{
......@@ -99,19 +71,12 @@ struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) {
uart0_send_ref((unsigned char)ch); // Envoie le caractère via UART
return ch;
}
int fgetc(FILE *f) {
unsigned char ch = uart0_rec_byte_ref(); // Reçoit un caractère via UART
return (int)ch;
}
#define MAX_MEMORY_SIZE 1024
unsigned simulated_memory_space[MAX_MEMORY_SIZE];
int main(void)
{
uart0_init_ref();
SCB->SHCSR |= (1 << 16);
// MPU configuration here...
// Region 0 (Flash)
......@@ -179,7 +144,11 @@ int main(void)
// testing memory accesses in user mode:
//test_user_mode(); // to be removed after checking
for (unsigned i = 0; i < MAX_MEMORY_SIZE; ++i) {
simulated_memory_space[i] = i; // Remplissage avec des données factices
}
while (1) {
exec_user_read_write();
}
......
/*
* user_interpreter.c
*
* Created on: 12 nov. 2023
* Author: Vincent
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdarg.h>
#include "uart.h"
#include "user_cmd.h"
#define INPUT_BUFFER_SIZE 200
#define ERROR_MESSAGE() printf("Invalid input! Use format: <8-digit hex address> for read or <8-digit hex address>=<8-digit hex value> for write\n");
extern unsigned fault_address_buffer[20]; // Assume MAX_ERRORS is 20
extern unsigned fault_code_buffer[20];
extern volatile unsigned fault_count;
bool current_mode = false; // false for user, true for supervisor
// Prototypes for functions
void switchOperatingMode();
void showFaultStatus();
void promptUser();
void showMode();
void processMemoryAccess(char *inputBuffer);
// Function to switch operating mode
void switchOperatingMode() {
current_mode = !current_mode;
printf("Operating mode switched to %s.\n", current_mode ? "Supervisor" : "User");
// Code to actually switch mode would be here
#define MAX_INPUT_LENGTH 200
#define MAX_ERRORS 20
// Fausse mémoire et compteur d'erreur pour la simulation
unsigned simulated_memory_space[1024] = {0};
unsigned fault_address_buffer[MAX_ERRORS] = {0};
unsigned fault_code_buffer[MAX_ERRORS] = {0};
unsigned fault_count = 0;
bool is_supervisor_mode = false; // false pour le mode utilisateur, true pour le mode superviseur
// Prototypes de fonctions pour la commutation de mode et l'affichage des erreurs
void switch_mode();
void display_status();
unsigned read_memory(unsigned addr);
void write_memory(unsigned addr, unsigned value);
void SVC_Handler()
{
// to be filled for exercise 4
}
// Display fault information
void showFaultStatus() {
printf("There are currently %u errors.\n", fault_count);
// Code to display errors would be here
}
// Show prompt to the user
void promptUser() {
printf("Enter command: ('switch' to change mode, 'status' to view errors)\n");
printf("Or enter a hex address to read (<address>) or write (<address>,<value>):\n");
// Fonction pour basculer entre les modes utilisateur et superviseur
void switch_mode() {
is_supervisor_mode = !is_supervisor_mode;
printf("Switched to %s mode.\n", is_supervisor_mode ? "Supervisor" : "User");
}
// Display current operating mode
void showMode() {
printf("Currently in %s mode.\n", current_mode ? "Supervisor" : "User");
// Fonction pour afficher les erreurs d'accès mémoire
void display_status() {
if (fault_count == 0) {
printf("No access errors recorded.\n");
} else {
printf("Access errors recorded:\n");
for (unsigned i = 0; i < fault_count; ++i) {
printf("Error %u: Address: 0x%08X, Code: 0x%08X\n", i, fault_address_buffer[i], fault_code_buffer[i]);
}
// Parse and process memory access from user input
void processMemoryAccess(char *inputBuffer) {
int i = 0, commaCount = 0, valuePosition;
unsigned memAddress, memValue;
while (inputBuffer[i] != '\0') {
if (inputBuffer[i] == ',') {
commaCount++;
if (commaCount > 1) break;
inputBuffer[i] = '\0';
valuePosition = i + 1;
} else if (!isxdigit(inputBuffer[i])) {
break;
}
i++;
}
if (commaCount > 1 || i < 2 || i > 17) {
ERROR_MESSAGE();
return;
// Modifiez les fonctions read_memory et write_memory pour inclure la validation de la MPU
unsigned read_memory(unsigned addr) {
if (!validate_mpu_address(addr)) {
printf("MPU violation: Read access denied for address 0x%08X.\n", addr);
return 0; // Retourne 0 pour indiquer un échec de lecture
}
return simulated_memory_space[addr]; // Lecture réussie
}
sscanf(inputBuffer, "%x", &memAddress);
if (commaCount == 0) {
printf("Read address: 0x%08x\n", memAddress);
// Simulate read
} else {
sscanf(inputBuffer + valuePosition, "%x", &memValue);
printf("Write address: 0x%08x with value 0x%08x\n", memAddress, memValue);
// Simulate write
void write_memory(unsigned addr, unsigned value) {
if (!validate_mpu_address(addr)) {
printf("MPU violation: Write access denied for address 0x%08X.\n", addr);
return; // Échec de l'écriture, sortie anticipée
}
simulated_memory_space[addr] = value; // Écriture réussie
}void exec_user_read_write() {
char input[MAX_INPUT_LENGTH];
unsigned addr, value;
printf("Enter command (%s mode): ", is_supervisor_mode ? "Supervisor" : "User");
if (fgets(input, sizeof(input), stdin) == NULL) {
printf("Error reading input.\n");
return;
}
// Main function to process read/write from user
void executeReadWrite() {
char inputBuffer[INPUT_BUFFER_SIZE];
// Enlever le caractère de nouvelle ligne si présent
input[strcspn(input, "\n")] = '\0';
showMode();
promptUser();
// Read user input
fflush(stdin);
fgets(inputBuffer, sizeof(inputBuffer), stdin);
inputBuffer[strcspn(inputBuffer, "\n")] = 0; // Remove newline character
// Process command
if (strncmp(inputBuffer, "switch", 6) == 0) {
switchOperatingMode();
} else if (strncmp(inputBuffer, "status", 6) == 0) {
showFaultStatus();
// Traitement des commandes "switch" et "status"
if (strcmp(input, "switch") == 0) {
switch_mode();
} else if (strcmp(input, "status") == 0) {
display_status();
} else {
processMemoryAccess(inputBuffer);
char *comma = strchr(input, ',');
if (comma) {
*comma = '\0'; // Séparer la chaîne en deux parties
addr = strtoul(input, NULL, 16);
value = strtoul(comma + 1, NULL, 16);
write_memory(addr, value);
printf("Written 0x%08X to address 0x%08X\n", value, addr);
} else if (isxdigit(input[0])) {
addr = strtoul(input, NULL, 16);
value = read_memory(addr);
printf("Value at address 0x%08X: 0x%08X\n", addr, value);
} else {
printf("Syntax error! Please check your input format.\n");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment