arch/xtensa/src/esp32: Add a PROCFS entry for the internal memory

Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
This commit is contained in:
Abdelatif Guettouche 2020-09-17 16:11:58 +01:00 committed by Alan Carvalho de Assis
parent a1318926b4
commit 7ac5f7a35b
7 changed files with 430 additions and 0 deletions

View File

@ -104,6 +104,11 @@ config XTENSA_IMEM_REGION_SIZE
range 0x2000 0x28000
default 0x28000
config XTENSA_IMEM_PROCFS
bool "Internal memory PROCFS support"
default n
depends on XTENSA_USE_SEPERATE_IMEM && !DISABLE_MOUNTPOINT && FS_PROCFS && FS_PROCFS_REGISTER
source arch/xtensa/src/lx6/Kconfig
if ARCH_CHIP_ESP32
source arch/xtensa/src/esp32/Kconfig

View File

@ -92,11 +92,15 @@ extern "C"
#endif
#ifdef CONFIG_XTENSA_USE_SEPERATE_IMEM
struct mallinfo; /* Forward reference, see malloc.h */
void up_imm_initialize(void);
FAR void *up_imm_malloc(size_t size);
void up_imm_free(FAR void *mem);
FAR void *up_imm_memalign(size_t alignment, size_t size);
bool up_imm_heapmember(FAR void *mem);
int up_imm_mallinfo(FAR struct mallinfo *info);
#endif
#undef EXTERN

View File

@ -149,6 +149,10 @@ CHIP_CSRCS += esp32_tim_lowerhalf.c
endif
endif
ifeq ($(CONFIG_XTENSA_IMEM_PROCFS),y)
CHIP_CSRCS += esp32_procfs_imm.c
endif
ifeq ($(CONFIG_ARCH_USE_MODULE_TEXT),y)
CHIP_CSRCS += esp32_modtext.c
CMN_ASRCS += xtensa_loadstore.S

View File

@ -26,6 +26,8 @@
#include <nuttx/arch.h>
#include <nuttx/mm/mm.h>
#include <malloc.h>
#include "xtensa.h"
#if CONFIG_XTENSA_USE_SEPERATE_IMEM
@ -116,4 +118,19 @@ bool up_imm_heapmember(FAR void *mem)
{
return mm_heapmember(&g_iheap, mem);
}
/****************************************************************************
* Name: up_imm_mallinfo
*
* Description:
* mallinfo returns a copy of updated current heap information for the
* user heap.
*
****************************************************************************/
int up_imm_mallinfo(FAR struct mallinfo *info)
{
return mm_mallinfo(&g_iheap, info);
}
#endif /* CONFIG_XTENSA_USE_SEPERATE_IMEM */

View File

@ -0,0 +1,319 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_procfs_imm.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/sched.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/fs/dirent.h>
#include <arch/irq.h>
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
defined(CONFIG_FS_PROCFS_REGISTER) && defined(CONFIG_XTENSA_IMEM_PROCFS)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define IMM_LINELEN 64
/****************************************************************************
* Private Types
****************************************************************************/
/* This enumeration identifies all of the thread attributes that can be
* accessed via the procfs file system.
*/
/* This structure describes one open "file" */
struct imm_file_s
{
struct procfs_file_s base; /* Base open file structure */
unsigned int linesize; /* Number of valid characters in line[] */
char line[IMM_LINELEN]; /* Pre-allocated buffer for formatted lines */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* File system methods */
static int imm_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int imm_close(FAR struct file *filep);
static ssize_t imm_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static int imm_dup(FAR const struct file *oldp,
FAR struct file *newp);
static int imm_stat(FAR const char *relpath, FAR struct stat *buf);
/****************************************************************************
* Public Data
****************************************************************************/
/* See include/nutts/fs/procfs.h */
static const struct procfs_operations imm_procfsoperations =
{
imm_open, /* open */
imm_close, /* close */
imm_read, /* read */
NULL, /* write */
imm_dup, /* dup */
NULL, /* opendir */
NULL, /* closedir */
NULL, /* readdir */
NULL, /* rewinddir */
imm_stat /* stat */
};
static const struct procfs_entry_s g_procfs_imm =
{
"imm",
&imm_procfsoperations
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: imm_open
****************************************************************************/
static int imm_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct imm_file_s *priv;
finfo("Open '%s'\n", relpath);
/* PROCFS is read-only. Any attempt to open with any kind of write
* access is not permitted.
*
* REVISIT: Write-able proc files could be quite useful.
*/
if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
{
ferr("ERROR: Only O_RDONLY supported\n");
return -EACCES;
}
/* "imm" is the only acceptable value for the relpath */
if (strcmp(relpath, "imm") != 0)
{
ferr("ERROR: relpath is '%s'\n", relpath);
return -ENOENT;
}
/* Allocate a container to hold the task and attribute selection */
priv = (FAR struct imm_file_s *)kmm_zalloc(sizeof(struct imm_file_s));
if (!priv)
{
ferr("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
/* Save the index as the open-specific state in filep->f_priv */
filep->f_priv = (FAR void *)priv;
return OK;
}
/****************************************************************************
* Name: imm_close
****************************************************************************/
static int imm_close(FAR struct file *filep)
{
FAR struct imm_file_s *priv;
/* Recover our private data from the struct file instance */
priv = (FAR struct imm_file_s *)filep->f_priv;
DEBUGASSERT(priv);
/* Release the file attributes structure */
kmm_free(priv);
filep->f_priv = NULL;
return OK;
}
/****************************************************************************
* Name: imm_read
****************************************************************************/
static ssize_t imm_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct imm_file_s *priv;
size_t linesize;
size_t copysize;
size_t remaining;
size_t totalsize;
struct mallinfo mem;
off_t offset = filep->f_pos;
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
/* Recover our private data from the struct file instance */
priv = (FAR struct imm_file_s *)filep->f_priv;
DEBUGASSERT(priv);
up_imm_mallinfo(&mem);
remaining = buflen;
totalsize = 0;
linesize = snprintf(priv->line,
IMM_LINELEN,
" total used free largest\n");
copysize = procfs_memcpy(priv->line, linesize, buffer, remaining, &offset);
totalsize += copysize;
buffer += copysize;
remaining -= copysize;
if (totalsize >= buflen)
{
return totalsize;
}
linesize = snprintf(priv->line,
IMM_LINELEN,
"Mem: %11d%11d%11d%11d\n",
mem.arena,
mem.uordblks,
mem.fordblks,
mem.mxordblk);
copysize = procfs_memcpy(priv->line, linesize, buffer, remaining, &offset);
totalsize += copysize;
/* Update the file offset */
if (totalsize > 0)
{
filep->f_pos += totalsize;
}
return totalsize;
}
/****************************************************************************
* Name: imm_dup
****************************************************************************/
static int imm_dup(FAR const struct file *oldp, FAR struct file *newp)
{
FAR struct imm_file_s *oldpriv;
FAR struct imm_file_s *newpriv;
finfo("Dup %p->%p\n", oldp, newp);
/* Recover our private data from the old struct file instance */
oldpriv = (FAR struct imm_file_s *)oldp->f_priv;
DEBUGASSERT(oldpriv);
/* Allocate a new container to hold the task and attribute selection */
newpriv = (FAR struct imm_file_s *)kmm_zalloc(sizeof(struct imm_file_s));
if (!newpriv)
{
ferr("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
/* The copy the file attributes from the old attributes to the new */
memcpy(newpriv, oldpriv, sizeof(struct imm_file_s));
/* Save the new attributes in the new file structure */
newp->f_priv = (FAR void *)newpriv;
return OK;
}
/****************************************************************************
* Name: imm_stat
****************************************************************************/
static int imm_stat(const char *relpath, struct stat *buf)
{
if (strcmp(relpath, "imm") != 0)
{
ferr("ERROR: relpath is '%s'\n", relpath);
return -ENOENT;
}
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
buf->st_size = 0;
buf->st_blksize = 0;
buf->st_blocks = 0;
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: imm_procfs_register
*
* Description:
* Register the internal heap procfs file system entry
*
****************************************************************************/
int imm_procfs_register(void)
{
return procfs_register(&g_procfs_imm);
}
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS &&
* CONFIG_FS_PROCFS_REGISTER && CONFIG_XTENSA_IMEM_PROCFS */

View File

@ -0,0 +1,64 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_procfs_imm.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32_IMM_H
#define __ARCH_XTENSA_SRC_ESP32_IMM_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: imm_procfs_register
*
* Description:
* Register the internal heap procfs file system entry
*
****************************************************************************/
#ifdef CONFIG_XTENSA_IMEM_PROCFS
int imm_procfs_register(void);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32_IMM_H */

View File

@ -56,6 +56,7 @@
#include <syslog.h>
#include <sys/errno.h>
#include "esp32_procfs_imm.h"
#include "esp32-core.h"
#include "esp32_wlan.h"
@ -135,6 +136,21 @@ int esp32_bringup(void)
{
int ret;
#ifdef CONFIG_XTENSA_IMEM_PROCFS
/* Register the internal memory procfs entry.
* This must be done before the procfs is mounted.
*/
ret = imm_procfs_register();
if (ret < 0)
{
syslog(LOG_ERR,
"ERROR: Failed to register internal memory to PROCFS: %d\n",
ret);
}
#endif
#ifdef CONFIG_FS_PROCFS
/* Mount the procfs file system */
@ -145,6 +161,7 @@ int esp32_bringup(void)
}
#endif
#ifdef CONFIG_MMCSD
ret = esp32_mmcsd_initialize(0);
if (ret < 0)