锁定物理内存-mlockall

7 篇文章 1 订阅
4 篇文章 0 订阅
/* mlockall -- lock in core all the pages in this process.  Hurd version.
   Copyright (C) 2001-2021 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <hurd.h>
#include <mach/mach_host.h>

/* Cause all currently mapped pages of the process to be memory resident
   until unlocked by a call to the `munlockall', until the process exits,
   or until the process calls `execve'.  */
   
#include <hurd.h>

#define __mach_task_self()	(__mach_task_self_ + 0)	

void _hurd_port_cleanup (void *cleanup_data, jmp_buf env, int val)
{
  __mach_port_deallocate (__mach_task_self (), (mach_port_t) cleanup_data);
}

_HURD_USERLINK_H_EXTERN_INLINE void _hurd_userlink_link (struct hurd_userlink **chainp,struct hurd_userlink *link)
{
  struct hurd_userlink **thread_chainp;

  link->resource.next = *chainp;
  if (link->resource.next)
    link->resource.next->resource.prevp = &link->resource.next;
  link->resource.prevp = chainp;
  *chainp = link;

  /* Also chain it on the current thread's list of active resources.  */
  thread_chainp = &_hurd_self_sigstate ()->active_resources;
  link->thread.next = *thread_chainp;
  if (link->thread.next)
    link->thread.next->thread.prevp = &link->thread.next;
  link->thread.prevp = thread_chainp;
  *thread_chainp = link;
}

_HURD_PORT_H_EXTERN_INLINE mach_port_t _hurd_port_locked_get (struct hurd_port *port,struct hurd_userlink *link)
{
  mach_port_t result;
  result = port->port;
  if (result != MACH_PORT_NULL)
    {
      link->cleanup = &_hurd_port_cleanup;
      link->cleanup_data = (void *) result;
      _hurd_userlink_link (&port->users, link);
    }
  __spin_unlock (&port->lock);
  return result;
}
   
_HURD_PORT_H_EXTERN_INLINE mach_port_t _hurd_port_get (struct hurd_port *port,struct hurd_userlink *link)
{
  mach_port_t result;
  HURD_CRITICAL_BEGIN;
  __spin_lock (&port->lock);
  result = _hurd_port_locked_get (port, link);
  HURD_CRITICAL_END;
  return result;
}

_HURD_PORT_H_EXTERN_INLINE void _hurd_port_free (struct hurd_port *port,struct hurd_userlink *link,mach_port_t used_port)
{
  int dealloc;
  if (used_port == MACH_PORT_NULL)
    return;
  HURD_CRITICAL_BEGIN;
  __spin_lock (&port->lock);
  dealloc = _hurd_userlink_unlink (link);
  __spin_unlock (&port->lock);
  HURD_CRITICAL_END;
  if (dealloc)
    __mach_port_deallocate (__mach_task_self (), used_port);
}

struct hurd_port *_hurd_ports;
#define	HURD_PORT_USE(expr) 
(
	{ 
	struct hurd_port *const __p = &_hurd_ports[INIT_PORT_PROC];	
	struct hurd_userlink __link;					      
	const mach_port_t port = _hurd_port_get (__p, &__link);
	__typeof(expr) __result = (expr);	
	_hurd_port_free (__p, &__link, port);	
	__result; 
	}
)

mach_port_t _hurd_host_priv, _hurd_device_master;

kern_return_t __get_privileged_ports (mach_port_t *host_priv_ptr,device_t *device_master_ptr)
{
  if ((host_priv_ptr && _hurd_host_priv == MACH_PORT_NULL) || (device_master_ptr && _hurd_device_master == MACH_PORT_NULL))
    {
      error_t err;

      if (_hurd_ports)
		err = HURD_PORT_USE (__proc_getprivports (port,&_hurd_host_priv,&_hurd_device_master));
      else
		return MACH_SEND_INVALID_DEST;

      if (err)
		return err;
    }

  if (host_priv_ptr)
    {
      error_t err = _hurd_host_priv == MACH_PORT_NULL ? 0 : __mach_port_mod_refs (__mach_task_self (),_hurd_host_priv, MACH_PORT_RIGHT_SEND, +1);
      if (err)
		return err;
	
      *host_priv_ptr = _hurd_host_priv;
    }

  if (device_master_ptr)
    {
      error_t err = _hurd_device_master == MACH_PORT_NULL ? 0 : __mach_port_mod_refs (__mach_task_self (),_hurd_device_master, MACH_PORT_RIGHT_SEND, +1);
      if (err)
		return err;
      *device_master_ptr = _hurd_device_master;
    }

  return KERN_SUCCESS;
}
weak_alias (__get_privileged_ports, get_privileged_ports)

int mlockall (int flags)
{
  mach_port_t host;
  error_t err;

  err = __get_privileged_ports (&host, NULL);
  if (err)
    return __hurd_fail (err);

  err = __vm_wire_all (host, __mach_task_self (), flags);
  __mach_port_deallocate (__mach_task_self (), host);
  return err ? __hurd_fail (err) : 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡致云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值